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)
37 timespec_reset(&_lastReplot);
39 resize(parent->width(), parent->height());
42 _realDataPoints = new double[_numPoints];
43 _imagDataPoints = new double[_numPoints];
44 _xAxisPoints = new double[_numPoints];
46 _zoomer = new TimeDomainDisplayZoomer(canvas());
48 // Disable polygon clipping
49 QwtPainter::setDeviceClipping(false);
51 // We don't need the cache here
52 canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
53 canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
56 palette.setColor(canvas()->backgroundRole(), QColor("white"));
57 canvas()->setPalette(palette);
59 setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
60 set_xaxis(0, _numPoints);
61 setAxisTitle(QwtPlot::xBottom, "Sample Number");
63 setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
65 setAxisTitle(QwtPlot::yLeft, "Normalized Voltage");
67 // Automatically deleted when parent is deleted
68 _real_plot_curve = new QwtPlotCurve("Real Data");
69 _real_plot_curve->attach(this);
70 _real_plot_curve->setPen(QPen(Qt::blue));
71 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
73 _imag_plot_curve = new QwtPlotCurve("Imaginary Data");
74 _imag_plot_curve->attach(this);
75 _imag_plot_curve->setPen(QPen(Qt::magenta));
76 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
77 // _imag_plot_curve->setVisible(false);
79 memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
80 memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
81 memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
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 ) ),
117 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()
150 TimeDomainDisplayPlot::resizeSlot( QSize *s )
152 resize(s->width(), s->height());
155 void TimeDomainDisplayPlot::PlotNewData(const double* realDataPoints,
156 const double* imagDataPoints,
157 const int64_t numDataPoints,
158 const double timeInterval)
160 if((numDataPoints > 0) &&
161 (diff_timespec(get_highres_clock(), _lastReplot) > timeInterval)) {
163 if(numDataPoints != _numPoints){
164 _numPoints = numDataPoints;
166 delete[] _realDataPoints;
167 delete[] _imagDataPoints;
168 delete[] _xAxisPoints;
169 _realDataPoints = new double[_numPoints];
170 _imagDataPoints = new double[_numPoints];
171 _xAxisPoints = new double[_numPoints];
173 _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
174 _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
176 set_xaxis(0, numDataPoints);
181 memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
182 memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
186 _lastReplot = get_highres_clock();
190 void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag){
191 _imag_plot_curve->setVisible(visibleFlag);
194 void TimeDomainDisplayPlot::_resetXAxisPoints(){
195 for(long loc = 0; loc < _numPoints; loc++){
196 _xAxisPoints[loc] = loc;
198 setAxisScale(QwtPlot::xBottom, 0, _numPoints);
201 void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on){
202 plotItem->setVisible(!on);
205 #endif /* TIME_DOMAIN_DISPLAY_PLOT_C */