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