1 #ifndef TIME_DOMAIN_DISPLAY_PLOT_C
2 #define TIME_DOMAIN_DISPLAY_PLOT_C
4 #include <TimeDomainDisplayPlot.h>
6 #include <qwt_scale_draw.h>
7 #include <qwt_legend.h>
10 class TimeDomainDisplayZoomer: public QwtPlotZoomer
13 TimeDomainDisplayZoomer(QwtPlotCanvas* canvas):QwtPlotZoomer(canvas)
15 setTrackerMode(QwtPicker::AlwaysOn);
18 virtual ~TimeDomainDisplayZoomer(){
22 virtual void updateTrackerText(){
27 virtual QwtText trackerText( const QwtDoublePoint& p ) const
29 QwtText t(QString("Sample %1, %2 V").arg(p.x(), 0, 'f', 0).arg(p.y(), 0, 'f', 4));
35 TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent){
36 timespec_reset(&_lastReplot);
38 resize(parent->width(), parent->height());
40 _displayIntervalTime = (1.0/10.0); // 1/10 of a second between updates
43 _realDataPoints = new double[_numPoints];
44 _imagDataPoints = new double[_numPoints];
45 _xAxisPoints = new double[_numPoints];
47 // Disable polygon clipping
48 QwtPainter::setDeviceClipping(false);
50 // We don't need the cache here
51 canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
52 canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
55 palette.setColor(canvas()->backgroundRole(), QColor("white"));
56 canvas()->setPalette(palette);
58 setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
59 setAxisScale(QwtPlot::xBottom, 0, _numPoints);
60 setAxisTitle(QwtPlot::xBottom, "Sample Number");
62 setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
64 setAxisTitle(QwtPlot::yLeft, "Normalized Voltage");
66 // Automatically deleted when parent is deleted
67 _real_plot_curve = new QwtPlotCurve("Real Data");
68 _real_plot_curve->attach(this);
69 _real_plot_curve->setPen(QPen(Qt::blue));
70 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
72 _imag_plot_curve = new QwtPlotCurve("Imaginary Data");
73 _imag_plot_curve->attach(this);
74 _imag_plot_curve->setPen(QPen(Qt::magenta));
75 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
76 // _imag_plot_curve->setVisible(false);
78 memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
79 memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
80 memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
86 _zoomer = new TimeDomainDisplayZoomer(canvas());
87 #if QT_VERSION < 0x040000
88 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
89 Qt::RightButton, Qt::ControlModifier);
91 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
92 Qt::RightButton, Qt::ControlModifier);
94 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
97 _panner = new QwtPlotPanner(canvas());
98 _panner->setAxisEnabled(QwtPlot::yRight, false);
99 _panner->setMouseButton(Qt::MidButton);
101 // Avoid jumping when labels with more/less digits
102 // appear/disappear when scrolling vertically
104 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
105 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
106 sd->setMinimumExtent( fm.width("100.00") );
108 const QColor c(Qt::darkRed);
109 _zoomer->setRubberBandPen(c);
110 _zoomer->setTrackerPen(c);
112 QwtLegend* legendDisplay = new QwtLegend(this);
113 legendDisplay->setItemMode(QwtLegend::CheckableItem);
114 insertLegend(legendDisplay);
116 connect(this, SIGNAL( legendChecked(QwtPlotItem *, bool ) ), this, SLOT( LegendEntryChecked(QwtPlotItem *, bool ) ));
119 TimeDomainDisplayPlot::~TimeDomainDisplayPlot(){
120 delete[] _realDataPoints;
121 delete[] _imagDataPoints;
122 delete[] _xAxisPoints;
124 // _fft_plot_curves deleted when parent deleted
125 // _zoomer and _panner deleted when parent deleted
129 TimeDomainDisplayPlot::set_yaxis(double min, double max)
131 setAxisScale(QwtPlot::yLeft, min, max);
134 void TimeDomainDisplayPlot::replot(){
136 const timespec startTime = get_highres_clock();
140 double differenceTime = (diff_timespec(get_highres_clock(), startTime));
142 differenceTime *= 99.0;
143 // Require at least a 10% duty cycle
144 if(differenceTime > (1.0/10.0)){
145 _displayIntervalTime = differenceTime;
149 void TimeDomainDisplayPlot::PlotNewData(const double* realDataPoints, const double* imagDataPoints, const int64_t numDataPoints){
150 if(numDataPoints > 0){
152 if(numDataPoints != _numPoints){
153 _numPoints = numDataPoints;
155 delete[] _realDataPoints;
156 delete[] _imagDataPoints;
157 delete[] _xAxisPoints;
158 _realDataPoints = new double[_numPoints];
159 _imagDataPoints = new double[_numPoints];
160 _xAxisPoints = new double[_numPoints];
162 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
163 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
167 memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
168 memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
172 // Allow at least a 50% duty cycle
173 if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
174 // Only replot the screen if it is visible
178 _lastReplot = get_highres_clock();
182 void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag){
183 _imag_plot_curve->setVisible(visibleFlag);
186 void TimeDomainDisplayPlot::_resetXAxisPoints(){
187 for(long loc = 0; loc < _numPoints; loc++){
188 _xAxisPoints[loc] = loc;
190 setAxisScale(QwtPlot::xBottom, 0, _numPoints);
193 void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on){
194 plotItem->setVisible(!on);
197 #endif /* TIME_DOMAIN_DISPLAY_PLOT_C */