{
setTitle( "Sensor Demo" );
setCanvasBackground( Qt::white );
setAxisScale
( QwtPlot::yLeft,
0.0,
34816.0 );
grid->attach( this );
qsrand
(QDateTime::currentDateTime().
currentMSecsSinceEpoch());
resize( 600, 400 );
for(int j=0;j<8;++j)
{
qDebug() << "Making curve " << j ;
QList<double> list;
for(int i=0;i<MAX_POINTS;++i)
{
list.append(qrand()%2048);
}
masterList.append(list);
}
qDebug() << "Lists added....";
for(int j=0;j<8;++j)
{
qDebug() << " Setting curve: " << j;
curve
->setTitle
( QString("Sensor %1").
arg(j
+1) );
curve->setPen( Qt::blue, 1 ),
curve
->setRenderHint
( QwtPlotItem::RenderAntialiased,
true );
int newFact = 10000;
curve->setSamples( makeNewList(0,MAX_POINTS, j, newFact) );
qDebug() << " Added sample points";
if(newFact < 100)
{
}
qDebug() << "Added curve fitter";
curve->attach( this );
myCurves.append(curve);
qDebug() << "Attached curve.";
}
MyZoomer *zoom = new MyZoomer(canvas());
zoom
->setRubberBandPen
(QPen(Qt
::black,
2, Qt
::DotLine));
zoom
->setTrackerPen
(QPen(Qt
::black));
connect(this
->axisWidget
(QwtPlot::xBottom),
SIGNAL(scaleDivChanged
()),
this,
SLOT(handleScaleDivChanged
()));
//connect(this->axisWidget(1), SIGNAL(scaleDivChanged()), this, SLOT(handleScaleDivChanged()));
show();
zoom->setZoomBase();
// Uncomment this to preselect from 0 to 10000;
//QwtScaleDiv sc(0, 10000);
//this->setAxisScaleDiv(QwtPlot::xBottom, sc);
}
MyPlot::~MyPlot()
{
qDeleteAll(myCurves);
}
QPolygonF MyPlot
::makeNewList(double min,
double max,
int curve, int
& newFact
) {
double factor = 1;
double limit1 = (max-min) / 10.0;
double limit2 = limit1 * 10;
double limit3 = limit2 * 10;
double limit4 = limit3 * 10;
if( (max-min) > limit1) factor = 10;
if( (max-min) > limit2) factor = 100;
if( (max-min) > limit3) factor = 1000;
if( (max-min) > limit4) factor = 10000;
//qDebug() << "Factor: " << factor;
for(int i = min; i<=max;i=i+factor)
{
//qDebug() << "point: " << i;
QPointF p
(i,
2048+4096*curve
+getPeak
(i, i
+factor
-1, curve
));
points.append(p);
}
newFact = factor;
return points;
}
double MyPlot::getPeak(int from, int to, int curve)
{
double res = 0;
from = from < masterList.at(curve).size() ? from : masterList.at(curve).size()-1;
to = to < masterList.at(curve).size() ? to : masterList.at(curve).size()-1;
//qDebug() << "From: " << from << " To: " << to;
res = masterList.at(curve)[from];
for(int i=from; i<= to; ++i)
{
if(masterList.at(curve)[i]> res)
{
res = masterList.at(curve)[i];
}
}
return res;
}
void MyPlot::handleScaleDivChanged()
{
//qDebug() << "ScaleDivChanged";
//qDebug() << "Interval: " << div.interval().minValue() << " - " << div.interval().maxValue();
double min = div.interval().minValue();
double max = div.interval().maxValue();
int newFact = 10000;
for(int k=0;k<myCurves.size();++k)
{
//qDebug() << "New Points curve: " << k;
myCurves.at(k)->setSamples( makeNewList(min,max, k, newFact) );
if(newFact < 100)
{
myCurves.
at(k
)->setCurveAttribute
(QwtPlotCurve::Fitted,
true);
}
else
{
myCurves.
at(k
)->setCurveAttribute
(QwtPlotCurve::Fitted,
false);
}
}
}
MyPlot::MyPlot(QWidget *parent) :
QwtPlot(parent)
{
setTitle( "Sensor Demo" );
setCanvasBackground( Qt::white );
setAxisScale( QwtPlot::yLeft, 0.0, 34816.0 );
insertLegend( new QwtLegend() );
QwtPlotGrid *grid = new QwtPlotGrid();
grid->attach( this );
QwtPlotCurve *curve;
qsrand(QDateTime::currentDateTime().currentMSecsSinceEpoch());
resize( 600, 400 );
for(int j=0;j<8;++j)
{
qDebug() << "Making curve " << j ;
QList<double> list;
for(int i=0;i<MAX_POINTS;++i)
{
list.append(qrand()%2048);
}
masterList.append(list);
}
qDebug() << "Lists added....";
for(int j=0;j<8;++j)
{
qDebug() << " Setting curve: " << j;
curve = new QwtPlotCurve();
curve->setTitle( QString("Sensor %1").arg(j+1) );
curve->setPen( Qt::blue, 1 ),
curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
int newFact = 10000;
curve->setSamples( makeNewList(0,MAX_POINTS, j, newFact) );
qDebug() << " Added sample points";
if(newFact < 100)
{
curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
}
curve->setPaintAttribute(QwtPlotCurve::ClipPolygons, true);
qDebug() << "Added curve fitter";
curve->attach( this );
myCurves.append(curve);
qDebug() << "Attached curve.";
}
MyZoomer *zoom = new MyZoomer(canvas());
zoom->setRubberBandPen(QPen(Qt::black, 2, Qt::DotLine));
zoom->setTrackerPen(QPen(Qt::black));
connect(this->axisWidget(QwtPlot::xBottom), SIGNAL(scaleDivChanged()), this, SLOT(handleScaleDivChanged()));
//connect(this->axisWidget(1), SIGNAL(scaleDivChanged()), this, SLOT(handleScaleDivChanged()));
show();
zoom->setZoomBase();
// Uncomment this to preselect from 0 to 10000;
//QwtScaleDiv sc(0, 10000);
//this->setAxisScaleDiv(QwtPlot::xBottom, sc);
}
MyPlot::~MyPlot()
{
qDeleteAll(myCurves);
}
QPolygonF MyPlot::makeNewList(double min, double max, int curve, int& newFact)
{
QPolygonF points;
double factor = 1;
double limit1 = (max-min) / 10.0;
double limit2 = limit1 * 10;
double limit3 = limit2 * 10;
double limit4 = limit3 * 10;
if( (max-min) > limit1) factor = 10;
if( (max-min) > limit2) factor = 100;
if( (max-min) > limit3) factor = 1000;
if( (max-min) > limit4) factor = 10000;
//qDebug() << "Factor: " << factor;
for(int i = min; i<=max;i=i+factor)
{
//qDebug() << "point: " << i;
QPointF p(i, 2048+4096*curve+getPeak(i, i+factor-1, curve));
points.append(p);
}
newFact = factor;
return points;
}
double MyPlot::getPeak(int from, int to, int curve)
{
double res = 0;
from = from < masterList.at(curve).size() ? from : masterList.at(curve).size()-1;
to = to < masterList.at(curve).size() ? to : masterList.at(curve).size()-1;
//qDebug() << "From: " << from << " To: " << to;
res = masterList.at(curve)[from];
for(int i=from; i<= to; ++i)
{
if(masterList.at(curve)[i]> res)
{
res = masterList.at(curve)[i];
}
}
return res;
}
void MyPlot::handleScaleDivChanged()
{
//qDebug() << "ScaleDivChanged";
const QwtScaleDiv div = axisScaleDiv(QwtPlot::xBottom);
//qDebug() << "Interval: " << div.interval().minValue() << " - " << div.interval().maxValue();
double min = div.interval().minValue();
double max = div.interval().maxValue();
int newFact = 10000;
for(int k=0;k<myCurves.size();++k)
{
//qDebug() << "New Points curve: " << k;
myCurves.at(k)->setSamples( makeNewList(min,max, k, newFact) );
if(newFact < 100)
{
myCurves.at(k)->setCurveAttribute(QwtPlotCurve::Fitted, true);
}
else
{
myCurves.at(k)->setCurveAttribute(QwtPlotCurve::Fitted, false);
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks