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 _zoomer = new TimeDomainDisplayZoomer(canvas());
49 // Disable polygon clipping
50 QwtPainter::setDeviceClipping(false);
52 // We don't need the cache here
53 canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
54 canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
57 palette.setColor(canvas()->backgroundRole(), QColor("white"));
58 canvas()->setPalette(palette);
60 setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
61 set_xaxis(0, _numPoints);
62 setAxisTitle(QwtPlot::xBottom, "Sample Number");
64 setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
66 setAxisTitle(QwtPlot::yLeft, "Normalized Voltage");
68 // Automatically deleted when parent is deleted
69 _real_plot_curve = new QwtPlotCurve("Real Data");
70 _real_plot_curve->attach(this);
71 _real_plot_curve->setPen(QPen(Qt::blue));
72 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
74 _imag_plot_curve = new QwtPlotCurve("Imaginary Data");
75 _imag_plot_curve->attach(this);
76 _imag_plot_curve->setPen(QPen(Qt::magenta));
77 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
78 // _imag_plot_curve->setVisible(false);
80 memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
81 memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
82 memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
88 #if QT_VERSION < 0x040000
89 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
90 Qt::RightButton, Qt::ControlModifier);
92 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
93 Qt::RightButton, Qt::ControlModifier);
95 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
98 _panner = new QwtPlotPanner(canvas());
99 _panner->setAxisEnabled(QwtPlot::yRight, false);
100 _panner->setMouseButton(Qt::MidButton);
102 // Avoid jumping when labels with more/less digits
103 // appear/disappear when scrolling vertically
105 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
106 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
107 sd->setMinimumExtent( fm.width("100.00") );
109 const QColor c(Qt::darkRed);
110 _zoomer->setRubberBandPen(c);
111 _zoomer->setTrackerPen(c);
113 QwtLegend* legendDisplay = new QwtLegend(this);
114 legendDisplay->setItemMode(QwtLegend::CheckableItem);
115 insertLegend(legendDisplay);
117 connect(this, SIGNAL( legendChecked(QwtPlotItem *, bool ) ), this, SLOT( LegendEntryChecked(QwtPlotItem *, bool ) ));
120 TimeDomainDisplayPlot::~TimeDomainDisplayPlot(){
121 delete[] _realDataPoints;
122 delete[] _imagDataPoints;
123 delete[] _xAxisPoints;
125 // _fft_plot_curves deleted when parent deleted
126 // _zoomer and _panner deleted when parent deleted
130 TimeDomainDisplayPlot::set_yaxis(double min, double max)
132 setAxisScale(QwtPlot::yLeft, min, max);
133 _zoomer->setZoomBase();
137 TimeDomainDisplayPlot::set_xaxis(double min, double max)
139 setAxisScale(QwtPlot::xBottom, min, max);
140 _zoomer->setZoomBase();
144 void TimeDomainDisplayPlot::replot()
146 const timespec startTime = get_highres_clock();
150 double differenceTime = (diff_timespec(get_highres_clock(), startTime));
152 differenceTime *= 99.0;
153 // Require at least a 10% duty cycle
154 if(differenceTime > (1.0/10.0)){
155 _displayIntervalTime = differenceTime;
160 TimeDomainDisplayPlot::resizeSlot( QSize *s )
162 resize(s->width(), s->height());
165 void TimeDomainDisplayPlot::PlotNewData(const double* realDataPoints,
166 const double* imagDataPoints,
167 const int64_t numDataPoints)
169 if(numDataPoints > 0){
171 if(numDataPoints != _numPoints){
172 _numPoints = numDataPoints;
174 delete[] _realDataPoints;
175 delete[] _imagDataPoints;
176 delete[] _xAxisPoints;
177 _realDataPoints = new double[_numPoints];
178 _imagDataPoints = new double[_numPoints];
179 _xAxisPoints = new double[_numPoints];
181 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
182 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
184 set_xaxis(0, numDataPoints);
188 memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
189 memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
193 // Allow at least a 50% duty cycle
194 if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
195 // Only replot the screen if it is visible
199 _lastReplot = get_highres_clock();
203 void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag){
204 _imag_plot_curve->setVisible(visibleFlag);
207 void TimeDomainDisplayPlot::_resetXAxisPoints(){
208 for(long loc = 0; loc < _numPoints; loc++){
209 _xAxisPoints[loc] = loc;
211 setAxisScale(QwtPlot::xBottom, 0, _numPoints);
214 void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on){
215 plotItem->setVisible(!on);
218 #endif /* TIME_DOMAIN_DISPLAY_PLOT_C */