graph.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "graph.h"
00012 #include <qstring.h>
00013
00014 Graph::Graph(karamba* k, int x, int y, int w, int h, int nbrPts):
00015 Meter(k, x, y, w, h), lastValue(0)
00016 {
00017
00018 nbrPoints = (nbrPts==0)? 50:nbrPts ;
00019 ptPtr = 0;
00020 values = new int[nbrPoints];
00021 for(int i = 0; i < nbrPoints; i++)
00022 values[i] = 0;
00023 minValue = 0;
00024 maxValue = 100;
00025 }
00026
00027 Graph::~Graph()
00028 {
00029 delete[] values;
00030 }
00031
00032 void Graph::setValue( long v)
00033 {
00034 if( v > maxValue)
00035 {
00036
00037 v = maxValue;
00038 }
00039 if( v < minValue)
00040 {
00041
00042 v = minValue;
00043 }
00044 lastValue = v;
00045 values[ptPtr] = (int) (v / (maxValue + 0.0001) * getHeight());
00046 ptPtr = (ptPtr + 1) % nbrPoints;
00047 }
00048
00049 void Graph::setValue( QString v )
00050 {
00051 setValue((long)(v.toDouble() + 0.5));
00052 }
00053
00054 void Graph::mUpdate(QPainter *p)
00055 {
00056 if (hidden == 0)
00057 {
00058 double step = (getWidth() / (nbrPoints-1.001));
00059 double xPos = 0;
00060 double nextXPos = 0;
00061 p->setPen(color);
00062 for (int i = 0; i < nbrPoints - 1 ; i ++)
00063 {
00064 nextXPos = xPos + step;
00065 p->drawLine(getX() + (int)xPos, getY()+getHeight() -
00066 (int) values[(ptPtr+i) % nbrPoints] ,
00067 getX() + (int)nextXPos, getY()+getHeight() -
00068 (int) values[(ptPtr + i +1) % nbrPoints] );
00069 xPos = nextXPos;
00070 }
00071 }
00072 }
00073
00074 #include "graph.moc"
|