Fixes a lot of warnings by cleaning up namespace issues.
[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   using QwtPlotZoomer::trackerText;
28   virtual QwtText trackerText( const QwtDoublePoint& p ) const 
29   {
30     QwtText t(QString("(%1, %2)").arg(p.x(), 0, 'f', 4).
31               arg(p.y(), 0, 'f', 4));
32     return t;
33   }
34 };
35
36 ConstellationDisplayPlot::ConstellationDisplayPlot(QWidget* parent)
37   : QwtPlot(parent)
38 {
39   timespec_reset(&_lastReplot);
40
41   resize(parent->width(), parent->height());
42
43   _numPoints = 1024;
44   _penSize = 5;
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   set_xaxis(-2.0, 2.0);
61   setAxisTitle(QwtPlot::xBottom, "In-phase");
62
63   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
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, _penSize, 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 {
111   delete[] _realDataPoints;
112   delete[] _imagDataPoints;
113
114   // _fft_plot_curves deleted when parent deleted
115   // _zoomer and _panner deleted when parent deleted
116 }
117
118 void 
119 ConstellationDisplayPlot::set_pen_size(int size)
120 {
121   if(size > 0 && size < 30){
122     _penSize = size;
123     _plot_curve->setPen(QPen(Qt::blue, _penSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
124   }
125 }
126
127
128 void
129 ConstellationDisplayPlot::set_xaxis(double min, double max)
130 {
131   setAxisScale(QwtPlot::xBottom, min, max);
132 }
133
134 void
135 ConstellationDisplayPlot::set_yaxis(double min, double max)
136 {
137   setAxisScale(QwtPlot::yLeft, min, max);
138 }
139
140 void
141 ConstellationDisplayPlot::set_axis(double xmin, double xmax,
142                                    double ymin, double ymax)
143 {
144   set_xaxis(xmin, xmax);
145   set_yaxis(ymin, ymax);
146 }
147
148 void ConstellationDisplayPlot::replot()
149 {
150   QwtPlot::replot();
151 }
152
153 void
154 ConstellationDisplayPlot::resizeSlot( QSize *s )
155 {
156   resize(s->width(), s->height());
157 }
158
159 void ConstellationDisplayPlot::PlotNewData(const double* realDataPoints,
160                                            const double* imagDataPoints,
161                                            const int64_t numDataPoints,
162                                            const double timeInterval)
163 {
164   if((numDataPoints > 0) && 
165      (diff_timespec(get_highres_clock(), _lastReplot) > timeInterval)) {
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
178     memcpy(_realDataPoints, realDataPoints, numDataPoints*sizeof(double));
179     memcpy(_imagDataPoints, imagDataPoints, numDataPoints*sizeof(double));
180
181     replot();
182     
183     _lastReplot = get_highres_clock();
184   }
185 }
186
187 void
188 ConstellationDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on)
189 {
190   plotItem->setVisible(!on);
191 }
192
193 #endif /* CONSTELLATION_DISPLAY_PLOT_C */