summaryrefslogtreecommitdiffstats
path: root/src/statgraph.h
blob: 2c526f536c6691a9f3354edece3d89868a8ce387 (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
/***************************************************************************
 * $Id: statgraph.h,v 1.4 2008/07/31 19:56:26 hoganrobert Exp $
 *   Copyright (C) 2006 - 2008 Robert Hogan                                *
 *   robert@roberthogan.net                                                *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   KCPULoad and KNetLoad are copyright (c) 1999-2000, Markus Gustavsson  *
 *                                       (c) 2002, Ben Burton              *
 *                                       (c) 2004-2005, Diego Pettenò      *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef STATGRAPH_H
#define STATGRAPH_H

#include <ntqobject.h>
#include <ntqstring.h>
#include <ntqcolor.h>
#include <ntqlabel.h>

/**
 * @author Diego 'Flameeyes' Pettenò
 * 
 * @brief Abstraction of the dock's graph class for KNetLoad and KCPULoad.
 *
 * This classes abstracts the access to the graphs for KNetLoad and KCPULoad
 * (and eventually other apps, too), making possible to use them without
 * system trays and so on.
 *
 */
class StatGraph : public TQLabel {
Q_OBJECT
public:
//@{
/**
 * @name defaultcolors Default graphs' colors
 * @brief These colors will be used as defaults color for graphs.
 */
	static const TQColor defaultBgColor;
	static const TQColor defaultReadingColor;
	static const TQColor defaultLabelColor;
	static const TQColor defaultGridColor;
	static const TQColor invalidColor;	///< Color used to pass transparent colors
//@}
	
	enum Style { Lines, Bars, Shades };
protected:
	Style m_style;		///< Style of the graph
	TQString m_label;	///< Label to show on the graph (TQString::null to not show label)
	
	TQColor m_bgColor;	///< Background color for the graph @see defaultBgColor
	TQColor m_readingColor;	///< Color for the readings graph
	TQColor m_labelColor;	///< Color for the graph's label (if any)
	TQColor m_gridColor;	///< Color for the graph's grid (if any)
	
	ushort m_gridPace;	///< Pace at which a the grid is drawn. If 0, the grid is not drawn.
	ushort m_pace;		///< Horizontal pace for readings
	uint m_numReadings;	///< Maximum number of readings (label's width / graph's pace)
	uint m_currReading;	///< Current index in the readings arrays.
	uint *m_readings;	///< Array for readings.
	
	TQt::ButtonState m_button;	///< Button clicked in pressed/released events
public:
	/**
	 * @brief Constructor for a StatGraph instance
	 * @param parent Label where to draw the graph
	 * @param pace X-distance between two reads. This will also influence the number
	 *        of readings which can be added (label's width / pace)
	 * @param gridPace Y-distance between two grid lines. If 0, the grid is not drawn.
	 * @param label Label to show on the graph (TQString::null to not show label)
	 * @param bgColor Background color to apply to the graph
	 * @param name Name of the statgraph instance (passed to TQObject)
	 *
	 * This constructor initialize the TQObject parent class and the
	 * label which will be used to draw the graph on.
	 * Note: it will @b not change the background of the label, nor it will draw
	 * the label.
	 */
	StatGraph(TQWidget *parent, ushort pace = 1, ushort gridPace = 0, const TQString &label = TQString::null, const TQColor &bgColor = defaultBgColor, const char *name = "");
	
	virtual ~StatGraph();

	/**
	 * @brief Sets the graph's style
	 */
	void setStyle(Style style)
	{ m_style = style; }
	
protected:
	virtual void resizeEvent(TQResizeEvent*);
	virtual void paintEvent(TQPaintEvent*);
	virtual void mousePressEvent(TQMouseEvent *);
	virtual void mouseReleaseEvent(TQMouseEvent *);
	
	/**
	 * @brief Paints the grid on the graph
	 */
	void paintGrid(TQPainter &p);
	
	/**
	 * @brief Paints the graph using Shades style
	 */
	void paintShades(TQPainter &p);
	
	/**
	 * @brief Paints the graph using Bars style
	 */
	void paintBars(TQPainter &p);
	
	/**
	 * @brief Paints the graph using Lines style
	 */
	void paintLines(TQPainter &p);
	
	/**
	 * @brief Paints the label on the graph
	 */
	void paintLabel(TQPainter &p);
	
	/**
	 * @brief Apply softening algorithm to readings
	 *
	 * This function is called to 'soften' the readings when addPercentReadings() is
	 * called with @c soft parameter true.
	 */
	void softenReadings(uint &reading);
	
public slots:
	/**
	 * @brief REquest a clear of the graph
	 *
	 * This functions resets the readings arrays and redraw completely the graph.
	 */
	void clear();
	
	/**
	 * @brief Sets the label to show on the graph
	 *
	 * This function sets the string to show on the graph.
	 * To disable showLabel, simply pass TQString::null as label to show.
	 */
	inline void setLabel(const TQString &label)
	{
		m_label = label;
	}
	
	/**
	 * @brief Sets the background color for the graph.
	 * 
	 * This function sets the background color for the graph instance
	 * To set the background to transparent, simply pass invalidColor value.
	 */
	void setBgColor(const TQColor &color);
	
	/**
	 * @brief Sets the reading color for the graph.
	 * 
	 * This function sets the reading color for the graph instance.
	 * @note Passing invalidColor to this, made it move to the default color
	 *       as soon as a redraw is requested.
	 */
	inline void setReadingColor(const TQColor &color)
	{ m_readingColor = color; }

	/**
	 * @brief Sets the label color for the graph.
	 * 
	 * This function sets the label color for the graph instance.
	 * @note Passing invalidColor to this, made it move to the default color
	 *       as soon as a redraw is requested.
	 */
	inline void setLabelColor(const TQColor &color)
	{ m_labelColor = color; }
	
	/**
	 * @brief Sets the grid color for the graph.
	 * 
	 * This function sets the grid color for the graph instance.
	 * @note Passing invalidColor to this, made it move to the default color
	 *       as soon as a redraw is requested.
	 */
	inline void setGridColor(const TQColor &color)
	{ m_gridColor = color; }
	
	/**
	 * @brief Sets the grid pace for the graph.
	 * 
	 * This function sets the grid pace for the graph instance.
	 * @note Pass 0 to disable grid painting.
	 */
	inline void setGridPace(const ushort gridpace)
	{ m_gridPace = gridpace; }
	
	/**
	* @brief Add the given reading as the most recent in our list.
	* @param reading The reading
	* @param soft If true, the reading will be aproximated to have a soft curve.
	*
	* The diagram will be updated accordingly.
	*/
	void addPercentReading(uchar reading, bool soft);

signals:
	void clickedLeft();
	void clickedRight();
};

#endif