Ok here is the code concerning the creation and modifications of the curves, I tried to extract the most important parts.
Definition of the plot:
class DataPlot
{
Q_OBJECT
public:
void updateCurves (); //update all curves (called by a thread)
private:
std::vector <Curve*> curves; //vector of curves
};
class DataPlot
: public QwtPlot
{
Q_OBJECT
public:
void newCurve (QString, QString); //create a new curve
void updateCurves (); //update all curves (called by a thread)
private:
std::vector <Curve*> curves; //vector of curves
};
To copy to clipboard, switch view to plain text mode
Definition of a curve:
class Curve
{
public:
void updaterX (Message&); //update X values
void updaterY (Message&); //update Y values
void updateNPoints (int); //update nb of points
void reattach (); //reattach with the correct nb of points
private:
int nPoints; //nb of points asked
int nCurPointsX; //current nb of values on X called in a thread
int nCurPointsY; //current nb of values on Y called in a thread
double d_x[MAX_PLOT_SIZE]; //values on X
double d_y[MAX_PLOT_SIZE]; //values on Y
};
class Curve
: public QwtPlotCurve, QWidget
{
public:
void updaterX (Message&); //update X values
void updaterY (Message&); //update Y values
void updateNPoints (int); //update nb of points
void reattach (); //reattach with the correct nb of points
private:
int nPoints; //nb of points asked
int nCurPointsX; //current nb of values on X called in a thread
int nCurPointsY; //current nb of values on Y called in a thread
double d_x[MAX_PLOT_SIZE]; //values on X
double d_y[MAX_PLOT_SIZE]; //values on Y
QColor color; //curve color
QwtSymbol symbol; //curve symbol
};
To copy to clipboard, switch view to plain text mode
Interesting code in DataPlot:
//////////////////////////////////
// create a new curve
//////////////////////////////////
void
{
[...]
//create a new curve
curves.push_back (new Curve (_name, robot,
nbCurves++, col,
nbPlots, this));
//attach the new curve to the plot
curves.back ()->attach (this);
curves.back ()->setPen (col);
[...]
}
//////////////////////////////////
// update the curves
//////////////////////////////////
void
DataPlot::updateCurves()
{
[...]
//for each curve
for (unsigned int i=0; i<curves.size (); i++)
{
[...]
//reattach the curve with an updated number of points
curves[i]->reattach (this);
}
// update the display
replot ();
}
//////////////////////////////////
// create a new curve
//////////////////////////////////
void
DataPlot::newCurve (QString _varX,
QString _varY)
{
[...]
//create a new curve
curves.push_back (new Curve (_name, robot,
nbCurves++, col,
nbPlots, this));
//attach the new curve to the plot
curves.back ()->attach (this);
curves.back ()->setPen (col);
[...]
}
//////////////////////////////////
// update the curves
//////////////////////////////////
void
DataPlot::updateCurves()
{
[...]
//for each curve
for (unsigned int i=0; i<curves.size (); i++)
{
[...]
//reattach the curve with an updated number of points
curves[i]->reattach (this);
}
// update the display
replot ();
}
To copy to clipboard, switch view to plain text mode
Interesting code in Curve:
////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////
Curve::Curve (const QString& s,
int _nPoints,
s_error (false),
id (_id),
nPoints (_nPoints),
nCurPointsX (0),
nCurPointsY (0),
color (_color),
{
//initiate values to 0
for (int i = 0; i< MAX_PLOT_SIZE; i++)
{
d_x[i] = 0;
d_y[i] = 0;
}
setSymbol (symbol);
}
////////////////////////////////////////////////////////////
// updates Y values
////////////////////////////////////////////////////////////
void
Curve::updaterY (Message &msg)
{
//when we receive a correct message
if (msg.value->type == DATA_DOUBLE)
{
//the following code allows a fluent printing of the
//points, increasing 1 by 1 when more points are asked
//or decreasing and keeping the last points when less
//points are requiered
//if we have to decrease the size of the curve
if (nCurPointsY > nPoints)
{
for (int i = (nCurPointsY - nPoints); i <= nCurPointsY; i++)
d_y[i-(nCurPointsY - nPoints)] = d_y[i];
nCurPointsY = nPoints ;
}
//if we have to increase it
if (nCurPointsY < nPoints)
{
nCurPointsY++;
}
//move the array to left
else
{
for (int j = 1; j <= nCurPointsY; j++)
d_y[j-1] = d_y[j];
}
//add the new value in the end
if (nCurPointsY >= 0)
d_y[nCurPointsY-1] = msg.value->val;
}
////////////////////////////////////////////////////////////
// updates X values
////////////////////////////////////////////////////////////
void
Curve::updaterX (Message &msg)
{
//same with X instead of Y....
[...]
}
////////////////////////////////////////////////////////////
// reattach the curve with the correct nb of points
////////////////////////////////////////////////////////////
void
Curve::reattach ()
{
setRawData (d_x, d_y, nCurPointsY);
}
////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////
Curve::Curve (const QString& s,
QColor _color,
int _nPoints,
QWidget* _parent) :
QwtPlotCurve (s),
QWidget (_parent),
s_error (false),
id (_id),
nPoints (_nPoints),
nCurPointsX (0),
nCurPointsY (0),
color (_color),
symbol (QwtSymbol::Cross,
QBrush (),
QPen (_color),
QSize (5,5))
{
//initiate values to 0
for (int i = 0; i< MAX_PLOT_SIZE; i++)
{
d_x[i] = 0;
d_y[i] = 0;
}
setSymbol (symbol);
}
////////////////////////////////////////////////////////////
// updates Y values
////////////////////////////////////////////////////////////
void
Curve::updaterY (Message &msg)
{
//when we receive a correct message
if (msg.value->type == DATA_DOUBLE)
{
//the following code allows a fluent printing of the
//points, increasing 1 by 1 when more points are asked
//or decreasing and keeping the last points when less
//points are requiered
//if we have to decrease the size of the curve
if (nCurPointsY > nPoints)
{
for (int i = (nCurPointsY - nPoints); i <= nCurPointsY; i++)
d_y[i-(nCurPointsY - nPoints)] = d_y[i];
nCurPointsY = nPoints ;
}
//if we have to increase it
if (nCurPointsY < nPoints)
{
nCurPointsY++;
}
//move the array to left
else
{
for (int j = 1; j <= nCurPointsY; j++)
d_y[j-1] = d_y[j];
}
//add the new value in the end
if (nCurPointsY >= 0)
d_y[nCurPointsY-1] = msg.value->val;
}
////////////////////////////////////////////////////////////
// updates X values
////////////////////////////////////////////////////////////
void
Curve::updaterX (Message &msg)
{
//same with X instead of Y....
[...]
}
////////////////////////////////////////////////////////////
// reattach the curve with the correct nb of points
////////////////////////////////////////////////////////////
void
Curve::reattach ()
{
setRawData (d_x, d_y, nCurPointsY);
}
To copy to clipboard, switch view to plain text mode
I hope this can help... thanks!
Bookmarks