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