00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qpainter.h>
00022 #include <qpixmap.h>
00023 #include "graph.h"
00024
00025 void GraphData::clear()
00026 {
00027 names.clear();
00028 values.resize(0);
00029 }
00030
00031 void GraphData::addItem( const QString &name, int value )
00032 {
00033 names.append( name );
00034 values.resize( values.size() + 1 );
00035 values[values.size()-1] = value;
00036 }
00037
00038 Graph::Graph(QWidget *parent, const char *name, WFlags f )
00039 : QFrame( parent, name, f )
00040 {
00041 }
00042
00043 PieGraph::PieGraph(QWidget *parent, const char *name, WFlags f )
00044 : Graph( parent, name, f )
00045 {
00046 }
00047
00048 void PieGraph::drawContents( QPainter *p )
00049 {
00050 int size = QMIN( contentsRect().width(), contentsRect().height() ) - 1;
00051
00052 int total = 0;
00053 for ( unsigned i = 0; i < data->count(); i++ )
00054 total += data->value(i);
00055
00056 int angle = 0;
00057 for ( unsigned i = 0; i < data->count(); i++ ) {
00058 int len;
00059 if ( i == data->count() - 1 || !total )
00060 len = 5760 - angle;
00061 else
00062 len = data->value(i) * 5760 / total;
00063 QColor col;
00064 col.setHsv( i * 360 / data->count(), 255, 255 );
00065 p->setBrush( col );
00066 p->drawPie ( contentsRect().x(), contentsRect().y(),
00067 size, size, angle, len+32 );
00068 angle += len;
00069 }
00070 }
00071
00072 BarGraph::BarGraph(QWidget *parent, const char *name, WFlags f )
00073 : Graph( parent, name, f )
00074 {
00075 setMinimumHeight( 10 );
00076 setMaximumHeight( 45 );
00077 }
00078
00079 void BarGraph::drawContents( QPainter *p )
00080 {
00081 int h = contentsRect().height();
00082 int y = contentsRect().top();
00083
00084 int total = 0;
00085 for ( unsigned i = 0; i < data->count(); i++ )
00086 total += data->value(i);
00087
00088 int pos = 0;
00089 for ( unsigned i = 0; i < data->count(); i++ ) {
00090 int len;
00091 if ( i == data->count() - 1 || !total )
00092 len = contentsRect().width() - pos;
00093 else
00094 len = data->value(i) * contentsRect().width() / total;
00095 QColor col;
00096 col.setHsv( i * 360 / data->count(), 255, 255 );
00097 drawSegment( p, QRect(contentsRect().x() + pos, y, len, h), col );
00098 pos += len;
00099 }
00100 }
00101
00102 void BarGraph::drawSegment( QPainter *p, const QRect &r, const QColor &c )
00103 {
00104 if ( QPixmap::defaultDepth() > 8 ) {
00105 QColor topgrad = c.light(170);
00106 QColor botgrad = c.dark();
00107
00108 int h1, h2, s1, s2, v1, v2;
00109 topgrad.hsv( &h1, &s1, &v1 );
00110 botgrad.hsv( &h2, &s2, &v2 );
00111 int ng = r.height();
00112 for ( int j =0; j < ng; j++ ) {
00113 p->setPen( QColor( h1 + ((h2-h1)*j)/(ng-1),
00114 s1 + ((s2-s1)*j)/(ng-1),
00115 v1 + ((v2-v1)*j)/(ng-1), QColor::Hsv ) );
00116 p->drawLine( r.x(), r.top()+j, r.x()+r.width(), r.top()+j );
00117 }
00118 } else {
00119 p->fillRect( r.x(), r.top(), r.width(), r.height(), c );
00120 }
00121 }
00122
00123
00124 GraphLegend::GraphLegend( QWidget *parent, const char *name, WFlags f )
00125 : QFrame( parent, name, f )
00126 {
00127 horz = FALSE;
00128 }
00129
00130 void GraphLegend::setOrientation(Orientation o)
00131 {
00132 horz = o == Horizontal;
00133 }
00134
00135 void GraphLegend::drawContents( QPainter *p )
00136 {
00137 int total = 0;
00138 for ( unsigned i = 0; i < data->count(); i++ )
00139 total += data->value(i);
00140
00141 int tw = width()/data->count()-1;
00142 int th = height()/(horz ? 1 : data->count());
00143 if ( th > p->fontMetrics().height() )
00144 th = p->fontMetrics().height();
00145 int x = 0;
00146 int y = 0;
00147 for ( unsigned i = 0; i < data->count(); i++ ) {
00148 QColor col;
00149 col.setHsv( i * 360 / data->count(), 255, 255 );
00150 p->setBrush( col );
00151 p->drawRect( x+1, y+1, th - 2, th - 2 );
00152 p->drawText( x+th + 1, y + p->fontMetrics().ascent()+1, data->name(i) );
00153 if ( horz ) {
00154 x += tw;
00155 } else {
00156 y += th;
00157 }
00158 }
00159 }
00160
00161 QSize GraphLegend::sizeHint() const
00162 {
00163 int th = fontMetrics().height() + 2;
00164 int maxw = 0;
00165 for ( unsigned i = 0; i < data->count(); i++ ) {
00166 int w = fontMetrics().width( data->name(i) );
00167 if ( w > maxw )
00168 maxw = w;
00169 }
00170 if ( 0 && horz ) {
00171 return QSize( maxw * data->count(), th );
00172 } else {
00173 return QSize( maxw, th * data->count() );
00174 }
00175 }
00176
00177 void GraphLegend::setData( const GraphData *p )
00178 {
00179 data = p;
00180 int th = fontMetrics().height();
00181 setMinimumHeight( th * ( horz ? 1 : data->count() ) );
00182 updateGeometry();
00183 }