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

graph.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Hans Karlsson                                   *
00003  *   karlsson.h@home.se                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 #include "graph.h"
00011 #include <qstring.h>
00012 Graph::Graph( int  x  , int y, int w, int h, int nbrPts) : Meter(x, y, w, h)
00013 {
00014 
00015     nbrPoints = (nbrPts==0)? 50:nbrPts ;
00016     ptPtr = 0;
00017     values = new int[nbrPoints];
00018     for(int i = 0; i < nbrPoints; i++)
00019         values[i] = 0;
00020     minValue = 0;
00021     maxValue = 100;
00022 
00023 }
00024 
00025 
00026 Graph::~Graph()
00027 {
00028     delete values;
00029 }
00030 
00031 void Graph::setValue( int v)
00032 {
00033     if( v > maxValue)
00034     {
00035         // maxValue = v;
00036         v = maxValue;
00037     }
00038     if( v < minValue)
00039     {
00040         //minValue = v;
00041         v = minValue;
00042     }
00043     values[ptPtr] = (int) (v/ (maxValue+0.0001)*height);
00044     ptPtr = (ptPtr + 1) % nbrPoints;
00045 }
00046 
00047 void Graph::setValue( QString v )
00048 {
00049     setValue( (int) (v.toDouble()  + 0.5) );
00050 }
00051 
00052 void Graph::setMin( int mv )
00053 {
00054     minValue = mv;
00055 }
00056 
00057 void Graph::setMax( int mv )
00058 {
00059     maxValue = mv;
00060 }
00061 
00062 
00063 void Graph::setColor(int r, int g, int b)
00064 {
00065     color = QColor(r,g,b);
00066 }
00067 
00068 
00069 void Graph::mUpdate(QPainter *p)
00070 {
00071     double step = (width / (nbrPoints-1.001));
00072     double xPos = 0;
00073     double nextXPos = 0;
00074     p->setPen(color);
00075     for (int i = 0; i < nbrPoints - 1 ; i ++)
00076     {
00077         nextXPos = xPos + step;
00078         p->drawLine(x + (int)xPos, y+height - (int) values[(ptPtr+i) % nbrPoints] ,
00079                     x + (int)nextXPos, y+height -  (int) values[(ptPtr + i +1) % nbrPoints] );
00080         xPos = nextXPos;
00081     }
00082 }

Generated on Mon May 16 13:59:20 2005 for karamba by  doxygen 1.3.9.1