summaryrefslogtreecommitdiffstats
path: root/plugins/stats/ChartDrawer.h
blob: 2345f143afe511758b6605d562652ca380e49b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/***************************************************************************
 *   Copyright © 2007 by Krzysztof Kundzicz                                *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef CHARTDRAWER_H_
#define CHARTDRAWER_H_

#include <stdint.h> 

#include <tqwidget.h>
#include <tqpainter.h>
#include <tqstring.h>
#include <tqtooltip.h>
#include <tqmime.h>
#include <tqimage.h>

#include <klocale.h>

#include <vector>
#include <cmath>
#include <algorithm> //fill

#include "ChartDrawerData.h"

namespace kt {

/**
\brief Widget for drawing line charts
\author Krzysztof Kundzicz <[email protected]>
*/
class ChartDrawer : public TQWidget
{
	Q_OBJECT
  TQ_OBJECT
	
	public:
		///Type used as widget size unit
		typedef uint32_t wgtsize_t;
		///Type used as unit in chart
		typedef int64_t wgtunit_t;
		/**
		\brief Type used for data storing
		\sa ChartDrawerData
		*/
		typedef std::vector<ChartDrawerData> val_t;
		///Determines max mode
		enum MaxMode { MaxModeTop, MaxModeExact };

	private:
		///Maximum X value
		wgtsize_t mXMax;
		///Maximum Y value
		wgtsize_t mYMax;
		///Auto maximum setting
		bool mAutoMax;
		///Chart data
		val_t mEls;
		///Name of the chart unit
		TQString mUnitName;
		///Mark max
		std::vector<bool> mMarkMax;
		///Max mode
		MaxMode mMMode;
	
		///Paint event handler
		void paintEvent ( TQPaintEvent * );
		/**
		\brief Draws chart's frame
		\param rPnt Painter on which things will be drawn
		*/
		void DrawFrame(TQPainter &rPnt);
		/**
		\brief Draws chart's scale
		\param rPnt Painter on which things will be drawn
		*/
		void DrawScale(TQPainter &rPnt);
		/**
		\brief Draws chart
		\param rPnt Painter on which things will be drawn
		*/
		void DrawChart(TQPainter &rPnt);
		
		/**
		\brief Gets distance between two values on OY
		\return Distance
		*/
		inline wgtsize_t GetYScale() const;
		
		/**
		\brief Translates widget Y coord to cartesian
		\param y Coord
		\return Coord
		*/
		inline wgtunit_t TrY(const wgtunit_t y) const;
		/**
		\brief Returns charts height
		\return Height
		
		Return only height of the chart's inside the frame — not the whole widget's
		*/
		inline wgtunit_t height() const;
		/**
		\brief Returns charts width
		\return Width
		
		Return only width of the chart's inside the frame — not the whole widget's
		*/
		inline wgtunit_t width() const;
		
		/**
		\brief Finds screen X coord on the widget
		\param x Coord
		\return Screen coord
		\warning Thera are rounding errors
		*/
		inline wgtunit_t FindXScreenCoords(const double x) const;
		/**
		\brief Finds screen Y coord on the widget
		\param y Coord
		\return Screen coord
		\warning Thera are rounding errors
		*/
		inline wgtunit_t FindYScreenCoords(const double y) const;
		
		///Sets tooltip with legend
		void MakeLegendTooltip();
		
	public:
		/**
		\brief Widget's constructor
		\param p Parent
		\param x_max Maximum X size
		\param y_max Maximum Y size
		\param autom Whether athomagically set the maximum Y size
		\param uname Unit name
		*/
		ChartDrawer(TQWidget *p = 0, wgtsize_t x_max = 2, wgtsize_t y_max = 1, bool autom = true, const TQString & uname = "KB/s");
		~ChartDrawer();
		
		/**
		\brief Gets maximum X
		\return Maximum X
		*/
		wgtsize_t GetXMax() const;
		/**
		\brief Gets maximum Y
		\return Maximum Y
		*/
		wgtsize_t GetYMax() const;
		
		/**
		\brief Sets the units name
		\param rN Name
		
		\note It'l be drawn on the chart
		*/
		void SetUnitName(const TQString & rN);
		
		/**
		\brief Gets unit name
		\return name
		*/
		TQString GetUnitName() const;
		/**
		\brief Doubleclick handler
		\param evt Mouse event
		*/
		void mouseDoubleClickEvent ( TQMouseEvent * evt );
		
		/**
		\brief Gets mode of OY axis maximum drawing
		\return mode
		*/
		MaxMode GetMaxMode() const;
		
	
	public slots:
		/**
		\brief Adds value to given dataset
		\param idx Dataset index
		\param val Value to add
		\param update Whether update widget after adding
		*/
		void AddValue(const size_t idx, const double val, bool update = true);
		/**
		\brief Adds dataset
		\param Cdd Dataset
		\param max Whether mark maximum of this dataset
		*/
		void AddValues(ChartDrawerData Cdd, const bool max = true);
		/**
		\brief Adds dataset
		\param Cdd Dataset
		\param idx Where
		\param max Whether mark maximum of this dataset
		*/
		void AddValues(ChartDrawerData Cdd, const size_t idx, const bool max = true);
		/**
		\brief Adds empty dataset
		\param rN Set's data name
		\param max Whether mark maximum of this dataset
		*/
		void AddValuesCnt(const TQString & rN, const bool max = true);
		/**
		\brief Adds empty dataset
		\param rP Pen that will be used to drawing
		\param rN Dataset name
		\param max Whether mark maximum of this dataset
		*/
		void AddValuesCnt(const TQPen & rP, const TQString & rN, const bool max = true );
		
		/**
		\brief Removes dataset
		\param idx Dataset index
		*/
		void RemoveValuesCnt(const size_t idx);
		/**
		\brief Zeroes values
		\param idx Dataset index
		*/
		void Zero(const size_t idx);
		
		///Finds and sets maximum
		void FindSetMax();
		
		/**
		\brief Toggles marking of the maximum Y value on given dataset
		\param at dataset
		\param e Toggle?
		*/
		void EnableMaxDrawAt(const size_t, const bool);
		/**
		\brief Toggles automatic max Y scale settin
		\param a Toggle?
		*/
		void EnableAutoMax(bool a);
		
		/**
		\brief Sets maximum X
		\param x X size
		*/
		void SetXMax(const wgtsize_t x);
		/**
		\brief Sets maximum Y
		\param y Y size
		*/
		void SetYMax(const wgtsize_t x);
		
		/**
		\brief Sets mode of max of OY axis
		\param mm Mode
		*/
		void SetMaxMode(const MaxMode mm);
		
		
	signals:
		/**
		\brief Emited when widget is doubleclicked
		\param evt Mouse event
		*/
		void DoubleClicked(TQMouseEvent * evt);
	
};

}

#endif