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 BBitmap; 21 class BMessageRunner; 22 class Scale; 23 class SystemInfoHandler; 24 class ViewHistory; 25 struct data_item; 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((int64*)&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 MouseDown(BPoint where); 90 virtual void MouseUp(BPoint where); 91 virtual void MouseMoved(BPoint where, uint32 transit, 92 const BMessage* dragMessage); 93 94 virtual void MessageReceived(BMessage* message); 95 96 virtual void Draw(BRect updateRect); 97 98 private: 99 void _Init(const BMessage* settings); 100 ::Scale* _ScaleFor(scale_type type); 101 void _Refresh(); 102 static status_t _RefreshThread(void* self); 103 void _UpdateFrame(); 104 BRect _HistoryFrame() const; 105 float _LegendHeight() const; 106 BRect _LegendFrame() const; 107 BRect _LegendFrameAt(BRect frame, int32 index) const; 108 BRect _LegendColorFrameAt(BRect frame, int32 index) const; 109 float _PositionForValue(DataSource* source, 110 DataHistory* values, int64 value); 111 void _DrawHistory(); 112 void _UpdateResolution(int32 resolution, 113 bool broadcast = true); 114 115 private: 116 class HistoryLayoutItem; 117 class LegendLayoutItem; 118 119 friend class HistoryLayoutItem; 120 friend class LegendLayoutItem; 121 122 rgb_color fHistoryBackgroundColor; 123 rgb_color fLegendBackgroundColor; 124 #ifdef __HAIKU__ 125 BLayoutItem* fHistoryLayoutItem; 126 BLayoutItem* fLegendLayoutItem; 127 #endif 128 129 mutable BLocker fSourcesLock; 130 BObjectList<DataSource> fSources; 131 BObjectList<DataHistory> fValues; 132 BObjectList<ViewHistory> fViewValues; 133 thread_id fRefreshThread; 134 sem_id fRefreshSem; 135 bigtime_t fRefreshInterval; 136 bigtime_t fLastRefresh; 137 int32 fDrawResolution; 138 bool fShowLegend; 139 bool fZooming; 140 BPoint fZoomPoint; 141 int32 fOriginalResolution; 142 SystemInfoHandler* fSystemInfoHandler; 143 std::map<scale_type, ::Scale*> fScales; 144 }; 145 146 #endif // ACTIVITY_VIEW_H 147