Fixes the replotting update. It's now based on a QTimer so it's in the event buffer...
[debian/gnuradio] / gr-qtgui / src / lib / TimeDomainDisplayPlot.cc
1 #ifndef TIME_DOMAIN_DISPLAY_PLOT_C
2 #define TIME_DOMAIN_DISPLAY_PLOT_C
3
4 #include <TimeDomainDisplayPlot.h>
5
6 #include <qwt_scale_draw.h>
7 #include <qwt_legend.h>
8
9
10 class TimeDomainDisplayZoomer: public QwtPlotZoomer
11 {
12 public:
13   TimeDomainDisplayZoomer(QwtPlotCanvas* canvas):QwtPlotZoomer(canvas)
14   {
15     setTrackerMode(QwtPicker::AlwaysOn);
16   }
17
18   virtual ~TimeDomainDisplayZoomer(){
19
20   }
21   
22   virtual void updateTrackerText(){
23     updateDisplay();
24   }
25
26 protected:
27   virtual QwtText trackerText( const QwtDoublePoint& p ) const 
28   {
29     QwtText t(QString("Sample %1, %2 V").arg(p.x(), 0, 'f', 0).arg(p.y(), 0, 'f', 4));
30
31     return t;
32   }
33 };
34
35 TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent)
36 {
37   timespec_reset(&_lastReplot);
38
39   resize(parent->width(), parent->height());
40
41   _numPoints = 1024;
42   _realDataPoints = new double[_numPoints];
43   _imagDataPoints = new double[_numPoints];
44   _xAxisPoints = new double[_numPoints];
45
46   _zoomer = new TimeDomainDisplayZoomer(canvas());
47
48   // Disable polygon clipping
49   QwtPainter::setDeviceClipping(false);
50   
51   // We don't need the cache here
52   canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
53   canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
54
55   QPalette palette;
56   palette.setColor(canvas()->backgroundRole(), QColor("white"));
57   canvas()->setPalette(palette);  
58
59   setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
60   set_xaxis(0, _numPoints);
61   setAxisTitle(QwtPlot::xBottom, "Sample Number");
62
63   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
64   set_yaxis(-2.0, 2.0);
65   setAxisTitle(QwtPlot::yLeft, "Normalized Voltage");
66
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);
72
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);
78
79   memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
80   memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
81   memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
82
83   _resetXAxisPoints();
84
85   replot();
86
87 #if QT_VERSION < 0x040000
88   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
89                           Qt::RightButton, Qt::ControlModifier);
90 #else
91   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
92                           Qt::RightButton, Qt::ControlModifier);
93 #endif
94   _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
95                           Qt::RightButton);
96
97   _panner = new QwtPlotPanner(canvas());
98   _panner->setAxisEnabled(QwtPlot::yRight, false);
99   _panner->setMouseButton(Qt::MidButton);
100
101   // Avoid jumping when labels with more/less digits
102   // appear/disappear when scrolling vertically
103
104   const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
105   QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
106   sd->setMinimumExtent( fm.width("100.00") );
107
108   const QColor c(Qt::darkRed);
109   _zoomer->setRubberBandPen(c);
110   _zoomer->setTrackerPen(c);
111
112   QwtLegend* legendDisplay = new QwtLegend(this);
113   legendDisplay->setItemMode(QwtLegend::CheckableItem);
114   insertLegend(legendDisplay);
115
116   connect(this, SIGNAL( legendChecked(QwtPlotItem *, bool ) ), 
117           this, SLOT( LegendEntryChecked(QwtPlotItem *, bool ) ));
118 }
119
120 TimeDomainDisplayPlot::~TimeDomainDisplayPlot(){
121   delete[] _realDataPoints;
122   delete[] _imagDataPoints;
123   delete[] _xAxisPoints;
124
125   // _fft_plot_curves deleted when parent deleted
126   // _zoomer and _panner deleted when parent deleted
127 }
128
129 void
130 TimeDomainDisplayPlot::set_yaxis(double min, double max)
131 {
132   setAxisScale(QwtPlot::yLeft, min, max);
133   _zoomer->setZoomBase();
134 }
135
136 void
137 TimeDomainDisplayPlot::set_xaxis(double min, double max)
138 {
139   setAxisScale(QwtPlot::xBottom, min, max);
140   _zoomer->setZoomBase();
141 }
142
143
144 void TimeDomainDisplayPlot::replot()
145 {
146   QwtPlot::replot();
147 }
148
149 void
150 TimeDomainDisplayPlot::resizeSlot( QSize *s )
151 {
152   resize(s->width(), s->height());
153 }
154
155 void TimeDomainDisplayPlot::PlotNewData(const double* realDataPoints,
156                                         const double* imagDataPoints,
157                                         const int64_t numDataPoints,
158                                         const double timeInterval)
159 {
160   if((numDataPoints > 0) && 
161      (diff_timespec(get_highres_clock(), _lastReplot) > timeInterval)) {
162   
163     if(numDataPoints != _numPoints){
164       _numPoints = numDataPoints;
165
166       delete[] _realDataPoints;
167       delete[] _imagDataPoints;
168       delete[] _xAxisPoints;
169       _realDataPoints = new double[_numPoints];
170       _imagDataPoints = new double[_numPoints];
171       _xAxisPoints = new double[_numPoints];
172       
173       _real_plot_curve->setRawData(_xAxisPoints, _realDataPoints, _numPoints);
174       _imag_plot_curve->setRawData(_xAxisPoints, _imagDataPoints, _numPoints);
175
176       set_xaxis(0, numDataPoints);
177
178       _resetXAxisPoints();
179     }
180
181     memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
182     memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
183
184     replot();
185
186     _lastReplot = get_highres_clock();
187   }
188 }
189
190 void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag){
191   _imag_plot_curve->setVisible(visibleFlag);
192 }
193
194 void TimeDomainDisplayPlot::_resetXAxisPoints(){
195   for(long loc = 0; loc < _numPoints; loc++){
196     _xAxisPoints[loc] = loc;
197   }
198   setAxisScale(QwtPlot::xBottom, 0, _numPoints);
199 }
200
201 void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on){
202   plotItem->setVisible(!on);
203 }
204
205 #endif /* TIME_DOMAIN_DISPLAY_PLOT_C */