summaryrefslogtreecommitdiff
path: root/noncore/applets/memoryapplet/graph.cpp
blob: 0b02bf79fe1f2051e50c87a1fd4f2b751ea0608d (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include <qpainter.h>
#include <qpixmap.h>
#include "graph.h"

void GraphData::clear()
{
    names.clear();
    values.resize(0);
}

void GraphData::addItem( const QString &name, int value )
{
    names.append( name );
    values.resize( values.size() + 1 );
    values[values.size()-1] = value;
}

Graph::Graph(QWidget *parent, const char *name, WFlags f )
    : QFrame( parent, name, f )
{
}

PieGraph::PieGraph(QWidget *parent, const char *name, WFlags f )
    : Graph( parent, name, f )
{
}

void PieGraph::drawContents( QPainter *p )
{
    int size = QMIN( contentsRect().width(), contentsRect().height() ) - 1;

    int total = 0;
    for ( unsigned i = 0; i < data->count(); i++ )
	total += data->value(i);

    int angle = 0;
    for ( unsigned i = 0; i < data->count(); i++ ) {
	int len;
	if ( i == data->count() - 1 || !total )
	    len = 5760 - angle;
	else
	    len = data->value(i) * 5760 / total;
	QColor col;
	col.setHsv( i * 360 / data->count(), 255, 255 );
	p->setBrush( col );
	p->drawPie ( contentsRect().x(), contentsRect().y(),
		     size, size, angle, len+32 );
	angle += len;
    }
}

BarGraph::BarGraph(QWidget *parent, const char *name, WFlags f )
    : Graph( parent, name, f )
{
    setMinimumHeight( 10 );
    setMaximumHeight( 45 );
}

void BarGraph::drawContents( QPainter *p )
{
    int h = contentsRect().height();
    int y = contentsRect().top();

    int total = 0;
    for ( unsigned i = 0; i < data->count(); i++ )
	total += data->value(i);

    int pos = 0;
    for ( unsigned i = 0; i < data->count(); i++ ) {
	int len;
	if ( i == data->count() - 1 || !total )
	    len = contentsRect().width() - pos;
	else
	    len = data->value(i) * contentsRect().width() / total;
	QColor col;
	col.setHsv( i * 360 / data->count(), 255, 255 );
	drawSegment( p, QRect(contentsRect().x() + pos, y, len, h), col );
	pos += len;
    }
}

void BarGraph::drawSegment( QPainter *p, const QRect &r, const QColor &c )
{
    if ( QPixmap::defaultDepth() > 8 ) {
	QColor topgrad = c.light(170);
	QColor botgrad = c.dark();

	int h1, h2, s1, s2, v1, v2;
	topgrad.hsv( &h1, &s1, &v1 );
	botgrad.hsv( &h2, &s2, &v2 );
	int ng = r.height();
	for ( int j =0; j < ng; j++ ) {
	    p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1),
			s1 + ((s2-s1)*j)/(ng-1),
			v1 + ((v2-v1)*j)/(ng-1),  QColor::Hsv ) );
	    p->drawLine( r.x(), r.top()+j, r.x()+r.width(), r.top()+j );
	}
    } else {
	p->fillRect( r.x(), r.top(), r.width(), r.height(), c );
    }
}


GraphLegend::GraphLegend( QWidget *parent, const char *name, WFlags f )
    : QFrame( parent, name, f )
{
    horz = FALSE;
}

void GraphLegend::setOrientation(Orientation o)
{
    horz = o == Horizontal;
}

void GraphLegend::drawContents( QPainter *p )
{
    int total = 0;
    for ( unsigned i = 0; i < data->count(); i++ )
	total += data->value(i);

    int tw = width()/data->count()-1;
    int th = height()/(horz ? 1 : data->count());
    if ( th > p->fontMetrics().height() )
	th = p->fontMetrics().height();
    int x = 0;
    int y = 0;
    for ( unsigned i = 0; i < data->count(); i++ ) {
	QColor col;
	col.setHsv( i * 360 / data->count(), 255, 255 );
	p->setBrush( col );
	p->drawRect( x+1, y+1, th - 2, th - 2 );
	p->drawText( x+th + 1, y + p->fontMetrics().ascent()+1, data->name(i) );
	if ( horz ) {
	    x += tw;
	} else {
	    y += th;
	}
    }
}

QSize GraphLegend::sizeHint() const
{
    int th = fontMetrics().height() + 2;
    int maxw = 0;
    for ( unsigned i = 0; i < data->count(); i++ ) {
	int w = fontMetrics().width( data->name(i) );
	if ( w > maxw )
	    maxw = w;
    }
    if ( 0 && horz ) {
	return QSize( maxw * data->count(), th );
    } else {
	return QSize( maxw, th * data->count() );
    }
}

void GraphLegend::setData( const GraphData *p )
{
    data = p;
    int th = fontMetrics().height();
    setMinimumHeight( th * ( horz ? 1 : data->count() ) );
    updateGeometry();
}