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