Cleaning up Qt code. This mostly reformats the code to be more consistent with our...
[debian/gnuradio] / gr-qtgui / src / lib / ConstellationDisplayPlot.cc
1 #ifndef CONSTELLATION_DISPLAY_PLOT_C
2 #define CONSTELLATION_DISPLAY_PLOT_C
3
4 #include <ConstellationDisplayPlot.h>
5
6 #include <qwt_scale_draw.h>
7 #include <qwt_legend.h>
8
9
10 class ConstellationDisplayZoomer: public QwtPlotZoomer
11 {
12 public:
13   ConstellationDisplayZoomer(QwtPlotCanvas* canvas):QwtPlotZoomer(canvas)
14   {
15     setTrackerMode(QwtPicker::AlwaysOn);
16   }
17
18   virtual ~ConstellationDisplayZoomer(){
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 ConstellationDisplayPlot::ConstellationDisplayPlot(QWidget* parent)
36   : QwtPlot(parent)
37 {
38   timespec_reset(&_lastReplot);
39
40   resize(parent->width(), parent->height());
41
42   _displayIntervalTime = (1.0/10.0); // 1/10 of a second between updates
43
44   _numPoints = 1024;
45   _realDataPoints = new double[_numPoints];
46   _imagDataPoints = new double[_numPoints];
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   //setAxisScale(QwtPlot::xBottom, -1.0, 1.0);
61   set_xaxis(-2.0, 2.0);
62   setAxisTitle(QwtPlot::xBottom, "In-phase");
63
64   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
65   //setAxisScale(QwtPlot::yLeft, -1.0, 1.0);
66   set_yaxis(-2.0, 2.0);
67   setAxisTitle(QwtPlot::yLeft, "Quadrature");
68
69   // Automatically deleted when parent is deleted
70   _plot_curve = new QwtPlotCurve("Constellation Points");
71   _plot_curve->attach(this);
72   _plot_curve->setPen(QPen(Qt::blue, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
73   _plot_curve->setStyle(QwtPlotCurve::Dots);
74   _plot_curve->setRawData(_realDataPoints, _imagDataPoints, _numPoints);
75
76   memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
77   memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
78
79   replot();
80
81   _zoomer = new ConstellationDisplayZoomer(canvas());
82 #if QT_VERSION < 0x040000
83   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
84                           Qt::RightButton, Qt::ControlModifier);
85 #else
86   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
87                           Qt::RightButton, Qt::ControlModifier);
88 #endif
89   _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
90                           Qt::RightButton);
91
92   _panner = new QwtPlotPanner(canvas());
93   _panner->setAxisEnabled(QwtPlot::yRight, false);
94   _panner->setMouseButton(Qt::MidButton);
95
96   // Avoid jumping when labels with more/less digits
97   // appear/disappear when scrolling vertically
98
99   const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
100   QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
101   sd->setMinimumExtent( fm.width("100.00") );
102
103   const QColor c(Qt::darkRed);
104   _zoomer->setRubberBandPen(c);
105   _zoomer->setTrackerPen(c);
106
107   connect(this, SIGNAL( legendChecked(QwtPlotItem *, bool ) ), 
108           this, SLOT( LegendEntryChecked(QwtPlotItem *, bool ) ));
109 }
110
111 ConstellationDisplayPlot::~ConstellationDisplayPlot()
112 {
113   delete[] _realDataPoints;
114   delete[] _imagDataPoints;
115
116   // _fft_plot_curves deleted when parent deleted
117   // _zoomer and _panner deleted when parent deleted
118 }
119
120 void
121 ConstellationDisplayPlot::set_xaxis(double min, double max)
122 {
123   setAxisScale(QwtPlot::xBottom, min, max);
124 }
125
126 void
127 ConstellationDisplayPlot::set_yaxis(double min, double max)
128 {
129   setAxisScale(QwtPlot::yLeft, min, max);
130 }
131
132 void
133 ConstellationDisplayPlot::set_axis(double xmin, double xmax,
134                                    double ymin, double ymax)
135 {
136   set_xaxis(xmin, xmax);
137   set_yaxis(ymin, ymax);
138 }
139
140 void ConstellationDisplayPlot::replot(){
141
142   const timespec startTime = get_highres_clock();
143   
144   QwtPlot::replot();
145
146   double differenceTime = (diff_timespec(get_highres_clock(), startTime));
147
148   differenceTime *= 99.0;
149   // Require at least a 10% duty cycle
150   if(differenceTime > (1.0/10.0)){
151     _displayIntervalTime = differenceTime;
152   }
153 }
154
155 void
156 ConstellationDisplayPlot::resizeSlot( QSize *s )
157 {
158   resize(s->width(), s->height());
159 }
160
161 void ConstellationDisplayPlot::PlotNewData(const double* realDataPoints,
162                                            const double* imagDataPoints,
163                                            const int64_t numDataPoints)
164 {
165   if(numDataPoints > 0){
166
167     if(numDataPoints != _numPoints){
168       _numPoints = numDataPoints;
169
170       delete[] _realDataPoints;
171       delete[] _imagDataPoints;
172       _realDataPoints = new double[_numPoints];
173       _imagDataPoints = new double[_numPoints];
174       
175       _plot_curve->setRawData(_realDataPoints, _imagDataPoints, _numPoints);
176     }
177     memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
178     memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
179
180   }
181
182   // Allow at least a 50% duty cycle
183   if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
184     // Only replot the screen if it is visible
185     if(isVisible()){
186       replot();
187     }
188     _lastReplot = get_highres_clock();
189   }
190 }
191
192 void
193 ConstellationDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on)
194 {
195   plotItem->setVisible(!on);
196 }
197
198 #endif /* CONSTELLATION_DISPLAY_PLOT_C */