1 /* 2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef ACTIVITY_VIEW_H 6 #define ACTIVITY_VIEW_H 7 8 9 #include <map> 10 11 #include <Locker.h> 12 #include <ObjectList.h> 13 #include <View.h> 14 15 #include "CircularBuffer.h" 16 #include "DataSource.h" 17 18 class BBitmap; 19 class BMessageRunner; 20 class DataSource; 21 class Scale; 22 class SystemInfoHandler; 23 struct data_item; 24 25 26 class DataHistory { 27 public: 28 DataHistory(bigtime_t memorize, bigtime_t interval); 29 ~DataHistory(); 30 31 void AddValue(bigtime_t time, int64 value); 32 33 int64 ValueAt(bigtime_t time); 34 int64 MaximumValue() const; 35 int64 MinimumValue() const; 36 bigtime_t Start() const; 37 bigtime_t End() const; 38 39 void SetRefreshInterval(bigtime_t interval); 40 void SetScale(Scale* scale); 41 42 private: 43 CircularBuffer<data_item> fBuffer; 44 int64 fMinimumValue; 45 int64 fMaximumValue; 46 bigtime_t fRefreshInterval; 47 int32 fLastIndex; 48 Scale* fScale; 49 }; 50 51 52 class ActivityView : public BView { 53 public: 54 ActivityView(BRect frame, const char* name, 55 const BMessage* settings, uint32 resizingMode); 56 ActivityView(const char* name, 57 const BMessage* settings); 58 ActivityView(BMessage* archive); 59 virtual ~ActivityView(); 60 61 virtual status_t Archive(BMessage* into, bool deep = true) const; 62 static BArchivable* Instantiate(BMessage* archive); 63 64 status_t SaveState(BMessage& state) const; 65 66 #ifdef __HAIKU__ 67 BLayoutItem* CreateHistoryLayoutItem(); 68 BLayoutItem* CreateLegendLayoutItem(); 69 #endif 70 71 DataSource* FindDataSource(const DataSource* source); 72 status_t AddDataSource(const DataSource* source, 73 const BMessage* state = NULL); 74 status_t RemoveDataSource(const DataSource* source); 75 void RemoveAllDataSources(); 76 77 protected: 78 virtual void AttachedToWindow(); 79 virtual void DetachedFromWindow(); 80 81 #ifdef __HAIKU__ 82 virtual BSize MinSize(); 83 #endif 84 85 virtual void FrameResized(float width, float height); 86 virtual void MouseDown(BPoint where); 87 virtual void MouseUp(BPoint where); 88 virtual void MouseMoved(BPoint where, uint32 transit, 89 const BMessage* dragMessage); 90 91 virtual void MessageReceived(BMessage* message); 92 93 virtual void Draw(BRect updateRect); 94 95 private: 96 void _Init(const BMessage* settings); 97 ::Scale* _ScaleFor(scale_type type); 98 void _Refresh(); 99 static status_t _RefreshThread(void* self); 100 void _UpdateOffscreenBitmap(); 101 BView* _OffscreenView(); 102 void _UpdateFrame(); 103 BRect _HistoryFrame() const; 104 float _LegendHeight() const; 105 BRect _LegendFrame() const; 106 BRect _LegendFrameAt(BRect frame, int32 index) const; 107 BRect _LegendColorFrameAt(BRect frame, int32 index) const; 108 float _PositionForValue(DataSource* source, 109 DataHistory* values, int64 value); 110 void _DrawHistory(); 111 112 private: 113 class HistoryLayoutItem; 114 class LegendLayoutItem; 115 116 friend class HistoryLayoutItem; 117 friend class LegendLayoutItem; 118 119 rgb_color fHistoryBackgroundColor; 120 rgb_color fLegendBackgroundColor; 121 BBitmap* fOffscreen; 122 #ifdef __HAIKU__ 123 BLayoutItem* fHistoryLayoutItem; 124 BLayoutItem* fLegendLayoutItem; 125 #endif 126 mutable BLocker fSourcesLock; 127 BObjectList<DataSource> fSources; 128 BObjectList<DataHistory> fValues; 129 thread_id fRefreshThread; 130 sem_id fRefreshSem; 131 bigtime_t fRefreshInterval; 132 bigtime_t fLastRefresh; 133 bigtime_t fDrawInterval; 134 int32 fDrawResolution; 135 bool fShowLegend; 136 bool fZooming; 137 BPoint fZoomPoint; 138 int32 fOriginalResolution; 139 SystemInfoHandler* fSystemInfoHandler; 140 std::map<scale_type, ::Scale*> fScales; 141 }; 142 143 #endif // ACTIVITY_VIEW_H 144