author | mickeyl <mickeyl> | 2005-01-29 09:35:52 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2005-01-29 09:35:52 (UTC) |
commit | d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a (patch) (unidiff) | |
tree | c17be6e75e277c3fa0378f352f45766f75031cdb /noncore/applets/memoryapplet/graph.cpp | |
parent | f85af28663814f3262f5ecfcd20a4b4f67c23067 (diff) | |
download | opie-d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a.zip opie-d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a.tar.gz opie-d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a.tar.bz2 |
fix 1505 and suck sysinfo files into the tree instead of cross referencing
eventually we may find that graph and load are of universal usage, then
they should appear in some kind of library
Diffstat (limited to 'noncore/applets/memoryapplet/graph.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/applets/memoryapplet/graph.cpp | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/noncore/applets/memoryapplet/graph.cpp b/noncore/applets/memoryapplet/graph.cpp new file mode 100644 index 0000000..0b02bf7 --- a/dev/null +++ b/noncore/applets/memoryapplet/graph.cpp | |||
@@ -0,0 +1,183 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include <qpainter.h> | ||
22 | #include <qpixmap.h> | ||
23 | #include "graph.h" | ||
24 | |||
25 | void GraphData::clear() | ||
26 | { | ||
27 | names.clear(); | ||
28 | values.resize(0); | ||
29 | } | ||
30 | |||
31 | void GraphData::addItem( const QString &name, int value ) | ||
32 | { | ||
33 | names.append( name ); | ||
34 | values.resize( values.size() + 1 ); | ||
35 | values[values.size()-1] = value; | ||
36 | } | ||
37 | |||
38 | Graph::Graph(QWidget *parent, const char *name, WFlags f ) | ||
39 | : QFrame( parent, name, f ) | ||
40 | { | ||
41 | } | ||
42 | |||
43 | PieGraph::PieGraph(QWidget *parent, const char *name, WFlags f ) | ||
44 | : Graph( parent, name, f ) | ||
45 | { | ||
46 | } | ||
47 | |||
48 | void PieGraph::drawContents( QPainter *p ) | ||
49 | { | ||
50 | int size = QMIN( contentsRect().width(), contentsRect().height() ) - 1; | ||
51 | |||
52 | int total = 0; | ||
53 | for ( unsigned i = 0; i < data->count(); i++ ) | ||
54 | total += data->value(i); | ||
55 | |||
56 | int angle = 0; | ||
57 | for ( unsigned i = 0; i < data->count(); i++ ) { | ||
58 | int len; | ||
59 | if ( i == data->count() - 1 || !total ) | ||
60 | len = 5760 - angle; | ||
61 | else | ||
62 | len = data->value(i) * 5760 / total; | ||
63 | QColor col; | ||
64 | col.setHsv( i * 360 / data->count(), 255, 255 ); | ||
65 | p->setBrush( col ); | ||
66 | p->drawPie ( contentsRect().x(), contentsRect().y(), | ||
67 | size, size, angle, len+32 ); | ||
68 | angle += len; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | BarGraph::BarGraph(QWidget *parent, const char *name, WFlags f ) | ||
73 | : Graph( parent, name, f ) | ||
74 | { | ||
75 | setMinimumHeight( 10 ); | ||
76 | setMaximumHeight( 45 ); | ||
77 | } | ||
78 | |||
79 | void BarGraph::drawContents( QPainter *p ) | ||
80 | { | ||
81 | int h = contentsRect().height(); | ||
82 | int y = contentsRect().top(); | ||
83 | |||
84 | int total = 0; | ||
85 | for ( unsigned i = 0; i < data->count(); i++ ) | ||
86 | total += data->value(i); | ||
87 | |||
88 | int pos = 0; | ||
89 | for ( unsigned i = 0; i < data->count(); i++ ) { | ||
90 | int len; | ||
91 | if ( i == data->count() - 1 || !total ) | ||
92 | len = contentsRect().width() - pos; | ||
93 | else | ||
94 | len = data->value(i) * contentsRect().width() / total; | ||
95 | QColor col; | ||
96 | col.setHsv( i * 360 / data->count(), 255, 255 ); | ||
97 | drawSegment( p, QRect(contentsRect().x() + pos, y, len, h), col ); | ||
98 | pos += len; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | void BarGraph::drawSegment( QPainter *p, const QRect &r, const QColor &c ) | ||
103 | { | ||
104 | if ( QPixmap::defaultDepth() > 8 ) { | ||
105 | QColor topgrad = c.light(170); | ||
106 | QColor botgrad = c.dark(); | ||
107 | |||
108 | int h1, h2, s1, s2, v1, v2; | ||
109 | topgrad.hsv( &h1, &s1, &v1 ); | ||
110 | botgrad.hsv( &h2, &s2, &v2 ); | ||
111 | int ng = r.height(); | ||
112 | for ( int j =0; j < ng; j++ ) { | ||
113 | p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1), | ||
114 | s1 + ((s2-s1)*j)/(ng-1), | ||
115 | v1 + ((v2-v1)*j)/(ng-1), QColor::Hsv ) ); | ||
116 | p->drawLine( r.x(), r.top()+j, r.x()+r.width(), r.top()+j ); | ||
117 | } | ||
118 | } else { | ||
119 | p->fillRect( r.x(), r.top(), r.width(), r.height(), c ); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | |||
124 | GraphLegend::GraphLegend( QWidget *parent, const char *name, WFlags f ) | ||
125 | : QFrame( parent, name, f ) | ||
126 | { | ||
127 | horz = FALSE; | ||
128 | } | ||
129 | |||
130 | void GraphLegend::setOrientation(Orientation o) | ||
131 | { | ||
132 | horz = o == Horizontal; | ||
133 | } | ||
134 | |||
135 | void GraphLegend::drawContents( QPainter *p ) | ||
136 | { | ||
137 | int total = 0; | ||
138 | for ( unsigned i = 0; i < data->count(); i++ ) | ||
139 | total += data->value(i); | ||
140 | |||
141 | int tw = width()/data->count()-1; | ||
142 | int th = height()/(horz ? 1 : data->count()); | ||
143 | if ( th > p->fontMetrics().height() ) | ||
144 | th = p->fontMetrics().height(); | ||
145 | int x = 0; | ||
146 | int y = 0; | ||
147 | for ( unsigned i = 0; i < data->count(); i++ ) { | ||
148 | QColor col; | ||
149 | col.setHsv( i * 360 / data->count(), 255, 255 ); | ||
150 | p->setBrush( col ); | ||
151 | p->drawRect( x+1, y+1, th - 2, th - 2 ); | ||
152 | p->drawText( x+th + 1, y + p->fontMetrics().ascent()+1, data->name(i) ); | ||
153 | if ( horz ) { | ||
154 | x += tw; | ||
155 | } else { | ||
156 | y += th; | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | |||
161 | QSize GraphLegend::sizeHint() const | ||
162 | { | ||
163 | int th = fontMetrics().height() + 2; | ||
164 | int maxw = 0; | ||
165 | for ( unsigned i = 0; i < data->count(); i++ ) { | ||
166 | int w = fontMetrics().width( data->name(i) ); | ||
167 | if ( w > maxw ) | ||
168 | maxw = w; | ||
169 | } | ||
170 | if ( 0 && horz ) { | ||
171 | return QSize( maxw * data->count(), th ); | ||
172 | } else { | ||
173 | return QSize( maxw, th * data->count() ); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | void GraphLegend::setData( const GraphData *p ) | ||
178 | { | ||
179 | data = p; | ||
180 | int th = fontMetrics().height(); | ||
181 | setMinimumHeight( th * ( horz ? 1 : data->count() ) ); | ||
182 | updateGeometry(); | ||
183 | } | ||