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 TimePrecisionClass
13 TimePrecisionClass(const int timePrecision)
15 _timePrecision = timePrecision;
18 virtual ~TimePrecisionClass()
22 virtual unsigned int GetTimePrecision() const
24 return _timePrecision;
27 virtual void SetTimePrecision(const unsigned int newPrecision)
29 _timePrecision = newPrecision;
32 unsigned int _timePrecision;
36 class TimeDomainDisplayZoomer: public QwtPlotZoomer, public TimePrecisionClass
39 TimeDomainDisplayZoomer(QwtPlotCanvas* canvas, const unsigned int timePrecision)
40 : QwtPlotZoomer(canvas),TimePrecisionClass(timePrecision)
42 setTrackerMode(QwtPicker::AlwaysOn);
45 virtual ~TimeDomainDisplayZoomer(){
49 virtual void updateTrackerText(){
53 void SetUnitType(const std::string &type)
59 virtual QwtText trackerText( const QwtDoublePoint& p ) const
61 QwtText t(QString("%1 %2, %3 V").arg(p.x(), 0, 'f', GetTimePrecision()).
62 arg(_unitType.c_str()).
63 arg(p.y(), 0, 'f', 4));
69 std::string _unitType;
72 TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent)
74 timespec_reset(&_lastReplot);
76 resize(parent->width(), parent->height());
79 _realDataPoints = new double[_numPoints];
80 _imagDataPoints = new double[_numPoints];
81 _xAxisPoints = new double[_numPoints];
83 _zoomer = new TimeDomainDisplayZoomer(canvas(), 0);
85 // Disable polygon clipping
86 QwtPainter::setDeviceClipping(false);
88 // We don't need the cache here
89 canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
90 canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
93 palette.setColor(canvas()->backgroundRole(), QColor("white"));
94 canvas()->setPalette(palette);
96 setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
97 set_xaxis(0, _numPoints);
98 setAxisTitle(QwtPlot::xBottom, "Time (sec)");
100 setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
101 set_yaxis(-2.0, 2.0);
102 setAxisTitle(QwtPlot::yLeft, "Normalized Voltage");
104 // Automatically deleted when parent is deleted
105 _real_plot_curve = new QwtPlotCurve("Real Data");
106 _real_plot_curve->attach(this);
107 _real_plot_curve->setPen(QPen(Qt::blue));
108 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
110 _imag_plot_curve = new QwtPlotCurve("Imaginary Data");
111 _imag_plot_curve->attach(this);
112 _imag_plot_curve->setPen(QPen(Qt::magenta));
113 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
114 // _imag_plot_curve->setVisible(false);
116 memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
117 memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
118 memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
125 #if QT_VERSION < 0x040000
126 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
127 Qt::RightButton, Qt::ControlModifier);
129 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
130 Qt::RightButton, Qt::ControlModifier);
132 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
135 _panner = new QwtPlotPanner(canvas());
136 _panner->setAxisEnabled(QwtPlot::yRight, false);
137 _panner->setMouseButton(Qt::MidButton);
139 // Avoid jumping when labels with more/less digits
140 // appear/disappear when scrolling vertically
142 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
143 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
144 sd->setMinimumExtent( fm.width("100.00") );
146 const QColor c(Qt::darkRed);
147 _zoomer->setRubberBandPen(c);
148 _zoomer->setTrackerPen(c);
150 QwtLegend* legendDisplay = new QwtLegend(this);
151 legendDisplay->setItemMode(QwtLegend::CheckableItem);
152 insertLegend(legendDisplay);
154 connect(this, SIGNAL( legendChecked(QwtPlotItem *, bool ) ),
155 this, SLOT( LegendEntryChecked(QwtPlotItem *, bool ) ));
158 TimeDomainDisplayPlot::~TimeDomainDisplayPlot(){
159 delete[] _realDataPoints;
160 delete[] _imagDataPoints;
161 delete[] _xAxisPoints;
163 // _fft_plot_curves deleted when parent deleted
164 // _zoomer and _panner deleted when parent deleted
168 TimeDomainDisplayPlot::set_yaxis(double min, double max)
170 setAxisScale(QwtPlot::yLeft, min, max);
171 _zoomer->setZoomBase();
175 TimeDomainDisplayPlot::set_xaxis(double min, double max)
177 setAxisScale(QwtPlot::xBottom, min, max);
178 _zoomer->setZoomBase();
182 void TimeDomainDisplayPlot::replot()
188 TimeDomainDisplayPlot::resizeSlot( QSize *s )
190 resize(s->width(), s->height());
193 void TimeDomainDisplayPlot::PlotNewData(const double* realDataPoints,
194 const double* imagDataPoints,
195 const int64_t numDataPoints,
196 const double timeInterval)
198 if((numDataPoints > 0) &&
199 (diff_timespec(get_highres_clock(), _lastReplot) > timeInterval)) {
201 if(numDataPoints != _numPoints){
202 _numPoints = numDataPoints;
204 delete[] _realDataPoints;
205 delete[] _imagDataPoints;
206 delete[] _xAxisPoints;
207 _realDataPoints = new double[_numPoints];
208 _imagDataPoints = new double[_numPoints];
209 _xAxisPoints = new double[_numPoints];
211 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
212 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
214 set_xaxis(0, numDataPoints);
219 memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
220 memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
224 _lastReplot = get_highres_clock();
228 void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag)
230 _imag_plot_curve->setVisible(visibleFlag);
233 void TimeDomainDisplayPlot::_resetXAxisPoints()
235 double delt = 1.0/_sampleRate;
236 for(long loc = 0; loc < _numPoints; loc++){
237 _xAxisPoints[loc] = loc*delt;
239 setAxisScale(QwtPlot::xBottom, 0, _numPoints*delt);
241 // Set up zoomer base for maximum unzoom x-axis
242 // and reset to maximum unzoom level
243 QwtDoubleRect zbase = _zoomer->zoomBase();
245 zbase.setRight(_numPoints*delt);
246 _zoomer->zoom(zbase);
247 _zoomer->setZoomBase(zbase);
251 void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on)
253 plotItem->setVisible(!on);
257 TimeDomainDisplayPlot::SetSampleRate(double sr, double units,
258 const std::string &strunits)
260 double newsr = sr/units;
261 if(newsr != _sampleRate) {
262 _sampleRate = sr/units;
265 // While we could change the displayed sigfigs based on the unit being
266 // displayed, I think it looks better by just setting it to 4 regardless.
267 //double display_units = ceil(log10(units)/2.0);
268 double display_units = 4;
269 setAxisTitle(QwtPlot::xBottom, QString("Time (%1)").arg(strunits.c_str()));
270 ((TimeDomainDisplayZoomer*)_zoomer)->SetTimePrecision(display_units);
271 ((TimeDomainDisplayZoomer*)_zoomer)->SetUnitType(strunits);
275 #endif /* TIME_DOMAIN_DISPLAY_PLOT_C */