Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mgraph.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** MGraph
00003 **
00004 ** A reusable graph widget.
00005 **
00006 ** Copyright (C) 2002, Michael Lauer
00007 **                    mickey@tm.informatik.uni-frankfurt.de
00008 **                    http://www.Vanille.de
00009 **
00010 ** This file may be distributed and/or modified under the terms of the
00011 ** GNU General Public License version 2 as published by the Free Software
00012 ** Foundation and appearing in the file LICENSE.GPL included in the
00013 ** packaging of this file.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 **********************************************************************/
00019 
00020 #include "mgraph.h"
00021 #include "graphbackground.xpm"
00022 #include <qpainter.h>
00023 #include <qpixmap.h>
00024 
00025 //---------------------------------------------------------------------------
00026 
00027 MGraph::MGraph( QWidget *parent, const char *name, WFlags f )
00028     : QFrame( parent, name, f ), min( 0 ), max( 0), values( 0 )
00029 {
00030     background = new QPixmap( (const char** ) graphbackground_xpm );
00031     values = new ValueList();
00032 }
00033 
00034 void MGraph::setFrameStyle( int style )
00035 {
00036     QFrame::setFrameStyle( style );
00037     setFixedSize( background->width() + frameWidth()*2, background->height() + frameWidth()*2 );
00038 }
00039 
00040 void MGraph::addValue( int value, bool followMax )
00041 {
00042     values->append( value );
00043     if ( followMax && (value > max) )
00044         setMax( value );
00045     if ( values->count() == background->width()-2 ) // compensate for graph display element border
00046         values->remove( values->begin() );
00047     repaint( false );
00048 }
00049 
00050 void MGraph::drawContents( QPainter* p )
00051 {
00052     p->drawPixmap( frameWidth(), frameWidth(), (const QPixmap&) *background );
00053     p->setPen( QColor( 40, 235, 40 ) );
00054 
00055     int x = frameWidth() + 2; // compensate for graph display element border
00056     int y = 0;
00057 
00058     ValueList::ConstIterator it;
00059     for ( it = values->begin(); it != values->end(); ++it )
00060     {
00061         y = frameWidth() + background->height() -3 - ( ( *(it)*(background->height()-4 ) /max ) );
00062         p->drawPoint( x++, y );
00063     }
00064 }
00065 
00066 MGraph::~MGraph()
00067 {
00068     delete background;
00069     delete values;
00070 }
00071 

Generated on Sat Nov 5 16:16:45 2005 for OPIE by  doxygen 1.4.2