Imported Upstream version 3.2.2
[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):QwtPlot(parent){
36   timespec_reset(&_lastReplot);
37
38   resize(parent->width(), parent->height());
39
40   _displayIntervalTime = (1.0/10.0); // 1/10 of a second between updates
41
42   _numPoints = 1024;
43   _realDataPoints = new double[_numPoints];
44   _imagDataPoints = new double[_numPoints];
45
46   // Disable polygon clipping
47   QwtPainter::setDeviceClipping(false);
48   
49   // We don't need the cache here
50   canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
51   canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
52
53   QPalette palette;
54   palette.setColor(canvas()->backgroundRole(), QColor("white"));
55   canvas()->setPalette(palette);  
56
57   setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
58   //setAxisScale(QwtPlot::xBottom, -1.0, 1.0);
59   set_xaxis(-2.0, 2.0);
60   setAxisTitle(QwtPlot::xBottom, "In-phase");
61
62   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
63   //setAxisScale(QwtPlot::yLeft, -1.0, 1.0);
64   set_yaxis(-2.0, 2.0);
65   setAxisTitle(QwtPlot::yLeft, "Quadrature");
66
67   // Automatically deleted when parent is deleted
68   _plot_curve = new QwtPlotCurve("Constellation Points");
69   _plot_curve->attach(this);
70   _plot_curve->setPen(QPen(Qt::blue, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
71   _plot_curve->setStyle(QwtPlotCurve::Dots);
72   _plot_curve->setRawData(_realDataPoints, _imagDataPoints, _numPoints);
73
74   memset(_realDataPoints, 0x0, _numPoints*sizeof(double));
75   memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
76
77   replot();
78
79   _zoomer = new ConstellationDisplayZoomer(canvas());
80 #if QT_VERSION < 0x040000
81   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
82                           Qt::RightButton, Qt::ControlModifier);
83 #else
84   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
85                           Qt::RightButton, Qt::ControlModifier);
86 #endif
87   _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
88                           Qt::RightButton);
89
90   _panner = new QwtPlotPanner(canvas());
91   _panner->setAxisEnabled(QwtPlot::yRight, false);
92   _panner->setMouseButton(Qt::MidButton);
93
94   // Avoid jumping when labels with more/less digits
95   // appear/disappear when scrolling vertically
96
97   const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
98   QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
99   sd->setMinimumExtent( fm.width("100.00") );
100
101   const QColor c(Qt::darkRed);
102   _zoomer->setRubberBandPen(c);
103   _zoomer->setTrackerPen(c);
104
105   connect(this, SIGNAL( legendChecked(QwtPlotItem *, bool ) ), 
106           this, SLOT( LegendEntryChecked(QwtPlotItem *, bool ) ));
107 }
108
109 ConstellationDisplayPlot::~ConstellationDisplayPlot(){
110   delete[] _realDataPoints;
111   delete[] _imagDataPoints;
112
113   // _fft_plot_curves deleted when parent deleted
114   // _zoomer and _panner deleted when parent deleted
115 }
116
117 void
118 ConstellationDisplayPlot::set_xaxis(double min, double max)
119 {
120   setAxisScale(QwtPlot::xBottom, min, max);
121 }
122
123 void
124 ConstellationDisplayPlot::set_yaxis(double min, double max)
125 {
126   setAxisScale(QwtPlot::yLeft, min, max);
127 }
128
129 void
130 ConstellationDisplayPlot::set_axis(double xmin, double xmax,
131                                    double ymin, double ymax)
132 {
133   set_xaxis(xmin, xmax);
134   set_yaxis(ymin, ymax);
135 }
136
137 void ConstellationDisplayPlot::replot(){
138
139   const timespec startTime = get_highres_clock();
140   
141   QwtPlot::replot();
142
143   double differenceTime = (diff_timespec(get_highres_clock(), startTime));
144
145   differenceTime *= 99.0;
146   // Require at least a 10% duty cycle
147   if(differenceTime > (1.0/10.0)){
148     _displayIntervalTime = differenceTime;
149   }
150 }
151
152 void ConstellationDisplayPlot::PlotNewData(const double* realDataPoints, const double* imagDataPoints, const int64_t numDataPoints){
153   if(numDataPoints > 0){
154
155     if(numDataPoints != _numPoints){
156       _numPoints = numDataPoints;
157
158       delete[] _realDataPoints;
159       delete[] _imagDataPoints;
160       _realDataPoints = new double[_numPoints];
161       _imagDataPoints = new double[_numPoints];
162       
163       _plot_curve->setRawData(_realDataPoints, _imagDataPoints, _numPoints);
164     }
165     memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
166     memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
167
168   }
169
170   // Allow at least a 50% duty cycle
171   if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
172     // Only replot the screen if it is visible
173     if(isVisible()){
174       replot();
175     }
176     _lastReplot = get_highres_clock();
177   }
178 }
179
180 void ConstellationDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on){
181   plotItem->setVisible(!on);
182 }
183
184 #endif /* CONSTELLATION_DISPLAY_PLOT_C */