Fixes a lot of warnings by cleaning up namespace issues.
[debian/gnuradio] / gr-qtgui / src / lib / WaterfallDisplayPlot.cc
1 #ifndef WATERFALL_DISPLAY_PLOT_C
2 #define WATERFALL_DISPLAY_PLOT_C
3
4 #include <WaterfallDisplayPlot.h>
5
6 #include <qwt_color_map.h>
7 #include <qwt_scale_widget.h>
8 #include <qwt_scale_draw.h>
9 #include <qwt_plot_zoomer.h>
10 #include <qwt_plot_panner.h>
11 #include <qwt_plot_layout.h>
12
13 #include <qapplication.h>
14
15 class FreqOffsetAndPrecisionClass
16 {
17 public:
18   FreqOffsetAndPrecisionClass(const int freqPrecision)
19   {
20     _frequencyPrecision = freqPrecision;
21     _centerFrequency = 0;
22   }
23
24   virtual ~FreqOffsetAndPrecisionClass()
25   {
26   }
27
28   virtual unsigned int GetFrequencyPrecision() const
29   {
30     return _frequencyPrecision;
31   }
32
33   virtual void SetFrequencyPrecision(const unsigned int newPrecision)
34   {
35     _frequencyPrecision = newPrecision;
36   }
37
38   virtual double GetCenterFrequency() const
39   {
40     return _centerFrequency;
41   }
42
43   virtual void SetCenterFrequency(const double newFreq)
44   {
45     _centerFrequency = newFreq;
46   }
47
48 protected:
49   unsigned int _frequencyPrecision;
50   double _centerFrequency;
51
52 private:
53
54 };
55
56 class WaterfallFreqDisplayScaleDraw: public QwtScaleDraw, public FreqOffsetAndPrecisionClass{
57 public:
58   WaterfallFreqDisplayScaleDraw(const unsigned int precision)
59     : QwtScaleDraw(), FreqOffsetAndPrecisionClass(precision)
60   {
61   }
62
63   virtual ~WaterfallFreqDisplayScaleDraw()
64   {
65   }
66
67   QwtText label(double value) const
68   {
69     return QString("%1").arg(value, 0, 'f', GetFrequencyPrecision());
70   }
71
72   virtual void initiateUpdate()
73   {
74     invalidateCache();
75   }
76
77 protected:
78
79 private:
80
81 };
82
83 class TimeScaleData
84 {
85 public:
86   TimeScaleData()
87   {
88     timespec_reset(&_zeroTime);
89     _secondsPerLine = 1.0;
90   }
91   
92   virtual ~TimeScaleData()
93   {    
94   }
95
96   virtual timespec GetZeroTime() const
97   {
98     return _zeroTime;
99   }
100   
101   virtual void SetZeroTime(const timespec newTime)
102   {
103     _zeroTime = newTime;
104   }
105
106   virtual void SetSecondsPerLine(const double newTime)
107   {
108     _secondsPerLine = newTime;
109   }
110
111   virtual double GetSecondsPerLine() const
112   {
113     return _secondsPerLine;
114   }
115
116   
117 protected:
118   timespec _zeroTime;
119   double _secondsPerLine;
120   
121 private:
122   
123 };
124
125 class QwtTimeScaleDraw: public QwtScaleDraw, public TimeScaleData
126 {
127 public:
128   QwtTimeScaleDraw():QwtScaleDraw(),TimeScaleData()
129   {    
130   }
131
132   virtual ~QwtTimeScaleDraw()
133   {    
134   }
135
136   virtual QwtText label(double value) const
137   {
138     QwtText returnLabel("");
139
140     timespec lineTime = timespec_add(GetZeroTime(), (-value) * GetSecondsPerLine());
141     struct tm timeTm;
142     gmtime_r(&lineTime.tv_sec, &timeTm);
143     returnLabel = (QString("").sprintf("%04d/%02d/%02d\n%02d:%02d:%02d.%03ld",
144                                        timeTm.tm_year+1900, timeTm.tm_mon+1,
145                                        timeTm.tm_mday, timeTm.tm_hour, timeTm.tm_min,
146                                        timeTm.tm_sec, lineTime.tv_nsec/1000000));
147     return returnLabel;
148   }
149
150   virtual void initiateUpdate()
151   {
152     // Do this in one call rather than when zeroTime and secondsPerLine
153     // updates is to prevent the display from being updated too often...
154     invalidateCache();
155   }
156   
157 protected:
158
159 private:
160
161 };
162
163 class WaterfallZoomer: public QwtPlotZoomer, public TimeScaleData, 
164                        public FreqOffsetAndPrecisionClass
165 {
166 public:
167   WaterfallZoomer(QwtPlotCanvas* canvas, const unsigned int freqPrecision)
168     : QwtPlotZoomer(canvas), TimeScaleData(), 
169       FreqOffsetAndPrecisionClass(freqPrecision)
170   {
171     setTrackerMode(QwtPicker::AlwaysOn);
172   }
173
174   virtual ~WaterfallZoomer()
175   {
176   }
177   
178   virtual void updateTrackerText()
179   {
180     updateDisplay();
181   }
182
183   void SetUnitType(const std::string &type)
184   {
185     _unitType = type;
186   }
187
188 protected:
189   using QwtPlotZoomer::trackerText;
190   virtual QwtText trackerText( const QwtDoublePoint& p ) const 
191   {
192     QString yLabel("");
193
194     timespec lineTime = timespec_add(GetZeroTime(), (-p.y()) * GetSecondsPerLine());
195     struct tm timeTm;
196     gmtime_r(&lineTime.tv_sec, &timeTm);
197     yLabel = (QString("").sprintf("%04d/%02d/%02d %02d:%02d:%02d.%03ld",
198                                   timeTm.tm_year+1900, timeTm.tm_mon+1,
199                                   timeTm.tm_mday, timeTm.tm_hour, timeTm.tm_min,
200                                   timeTm.tm_sec, lineTime.tv_nsec/1000000));
201
202     QwtText t(QString("%1 %2, %3").
203               arg(p.x(), 0, 'f', GetFrequencyPrecision()).
204               arg(_unitType.c_str()).arg(yLabel));
205     return t;
206   }
207
208 private:
209   std::string _unitType;
210 };
211
212
213 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
214 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_WHITE_HOT;
215 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_BLACK_HOT;
216 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_INCANDESCENT;
217 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_USER_DEFINED;
218
219 WaterfallDisplayPlot::WaterfallDisplayPlot(QWidget* parent)
220   : QwtPlot(parent)
221 {
222   _zoomer = NULL;
223   _startFrequency = 0;
224   _stopFrequency = 4000;
225   
226   resize(parent->width(), parent->height());
227   _numPoints = 1024;
228
229   _waterfallData = new WaterfallData(_startFrequency, _stopFrequency, _numPoints, 200);
230
231   QPalette palette;
232   palette.setColor(canvas()->backgroundRole(), QColor("white"));
233   canvas()->setPalette(palette);   
234
235   setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
236   setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(0));
237
238   setAxisTitle(QwtPlot::yLeft, "Time");
239   setAxisScaleDraw(QwtPlot::yLeft, new QwtTimeScaleDraw());
240
241   timespec_reset(&_lastReplot);
242
243   d_spectrogram = new PlotWaterfall(_waterfallData, "Waterfall Display");
244
245   _intensityColorMapType = INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
246
247   QwtLinearColorMap colorMap(Qt::darkCyan, Qt::white);
248   colorMap.addColorStop(0.25, Qt::cyan);
249   colorMap.addColorStop(0.5, Qt::yellow);
250   colorMap.addColorStop(0.75, Qt::red);
251
252   d_spectrogram->setColorMap(colorMap);
253   
254   d_spectrogram->attach(this);
255   
256   // LeftButton for the zooming
257   // MidButton for the panning
258   // RightButton: zoom out by 1
259   // Ctrl+RighButton: zoom out to full size
260   
261   _zoomer = new WaterfallZoomer(canvas(), 0);
262 #if QT_VERSION < 0x040000
263   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
264                            Qt::RightButton, Qt::ControlModifier);
265 #else
266   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
267                            Qt::RightButton, Qt::ControlModifier);
268 #endif
269   _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
270                            Qt::RightButton);
271   
272   _panner = new QwtPlotPanner(canvas());
273   _panner->setAxisEnabled(QwtPlot::yRight, false);
274   _panner->setMouseButton(Qt::MidButton);
275   
276   // Avoid jumping when labels with more/less digits
277   // appear/disappear when scrolling vertically
278   
279   const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
280   QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
281   sd->setMinimumExtent( fm.width("100.00") );
282   
283   const QColor c(Qt::white);
284   _zoomer->setRubberBandPen(c);
285   _zoomer->setTrackerPen(c);
286
287   _UpdateIntensityRangeDisplay();
288 }
289
290 WaterfallDisplayPlot::~WaterfallDisplayPlot()
291 {
292   delete _waterfallData;
293   delete d_spectrogram;
294 }
295
296 void 
297 WaterfallDisplayPlot::Reset()
298 {
299   _waterfallData->ResizeData(_startFrequency, _stopFrequency, _numPoints);
300   _waterfallData->Reset();
301
302   setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
303
304   // Load up the new base zoom settings
305   QwtDoubleRect newSize = _zoomer->zoomBase();
306   newSize.setLeft(_startFrequency);
307   newSize.setWidth(_stopFrequency-_startFrequency);
308   _zoomer->zoom(newSize);
309   _zoomer->setZoomBase(newSize);
310   _zoomer->zoom(0);
311 }
312
313 void
314 WaterfallDisplayPlot::SetFrequencyRange(const double constStartFreq,
315                                         const double constStopFreq,
316                                         const double constCenterFreq,
317                                         const bool useCenterFrequencyFlag,
318                                         const double units, const std::string &strunits)
319 {
320   double startFreq = constStartFreq / units;
321   double stopFreq = constStopFreq / units;
322   double centerFreq = constCenterFreq / units;
323
324   _useCenterFrequencyFlag = useCenterFrequencyFlag;
325
326   if(_useCenterFrequencyFlag){
327     startFreq = (startFreq + centerFreq);
328     stopFreq = (stopFreq + centerFreq);
329   }
330
331   bool reset = false;
332   if((startFreq != _startFrequency) || (stopFreq != _stopFrequency))
333     reset = true;
334
335   if(stopFreq > startFreq) {
336     _startFrequency = startFreq;
337     _stopFrequency = stopFreq;
338  
339     if((axisScaleDraw(QwtPlot::xBottom) != NULL) && (_zoomer != NULL)){
340       double display_units = ceil(log10(units)/2.0);
341       setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(display_units));
342       setAxisTitle(QwtPlot::xBottom, QString("Frequency (%1)").arg(strunits.c_str()));
343
344       if(reset) {
345         Reset();
346       }
347
348       ((WaterfallZoomer*)_zoomer)->SetFrequencyPrecision(display_units);
349       ((WaterfallZoomer*)_zoomer)->SetUnitType(strunits);
350     }
351   }
352 }
353
354
355 double
356 WaterfallDisplayPlot::GetStartFrequency() const
357 {
358   return _startFrequency;
359 }
360
361 double
362 WaterfallDisplayPlot::GetStopFrequency() const
363 {
364   return _stopFrequency;
365 }
366
367 void
368 WaterfallDisplayPlot::PlotNewData(const double* dataPoints, 
369                                   const int64_t numDataPoints,
370                                   const double timePerFFT,
371                                   const timespec timestamp,
372                                   const int droppedFrames)
373 {
374   if(numDataPoints > 0){
375     if(numDataPoints != _numPoints){
376       _numPoints = numDataPoints;
377       
378       Reset();
379       
380       d_spectrogram->invalidateCache();
381       d_spectrogram->itemChanged();
382       
383       if(isVisible()){
384         replot();
385       }
386       
387       _lastReplot = get_highres_clock();
388     }
389
390     if(diff_timespec(get_highres_clock(), _lastReplot) > timePerFFT) {
391       //FIXME: We may want to average the data between these updates to smooth display
392       _waterfallData->addFFTData(dataPoints, numDataPoints, droppedFrames);
393       _waterfallData->IncrementNumLinesToUpdate();
394       
395       QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
396       timeScale->SetSecondsPerLine(timePerFFT);
397       timeScale->SetZeroTime(timestamp);
398       
399       ((WaterfallZoomer*)_zoomer)->SetSecondsPerLine(timePerFFT);
400       ((WaterfallZoomer*)_zoomer)->SetZeroTime(timestamp);
401       
402       d_spectrogram->invalidateCache();
403       d_spectrogram->itemChanged();
404       
405       replot();
406
407       _lastReplot = get_highres_clock();
408     }
409   }
410 }
411
412 void
413 WaterfallDisplayPlot::SetIntensityRange(const double minIntensity, 
414                                              const double maxIntensity)
415 {
416   _waterfallData->setRange(QwtDoubleInterval(minIntensity, maxIntensity));
417
418   emit UpdatedLowerIntensityLevel(minIntensity);
419   emit UpdatedUpperIntensityLevel(maxIntensity);
420
421   _UpdateIntensityRangeDisplay();
422 }
423
424 void
425 WaterfallDisplayPlot::replot()
426 {
427   QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
428   timeScale->initiateUpdate();
429
430   WaterfallFreqDisplayScaleDraw* freqScale = (WaterfallFreqDisplayScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
431   freqScale->initiateUpdate();
432
433   // Update the time axis display
434   if(axisWidget(QwtPlot::yLeft) != NULL){
435     axisWidget(QwtPlot::yLeft)->update();
436   }
437
438   // Update the Frequency Offset Display
439   if(axisWidget(QwtPlot::xBottom) != NULL){
440     axisWidget(QwtPlot::xBottom)->update();
441   }
442
443   if(_zoomer != NULL){
444     ((WaterfallZoomer*)_zoomer)->updateTrackerText();
445   }
446
447   QwtPlot::replot();
448 }
449
450 void
451 WaterfallDisplayPlot::resizeSlot( QSize *s )
452 {
453   resize(s->width(), s->height());
454 }
455
456 int
457 WaterfallDisplayPlot::GetIntensityColorMapType() const
458 {
459   return _intensityColorMapType;
460 }
461
462 void
463 WaterfallDisplayPlot::SetIntensityColorMapType(const int newType, 
464                                                const QColor lowColor, 
465                                                const QColor highColor)
466 {
467   if((_intensityColorMapType != newType) || 
468      ((newType == INTENSITY_COLOR_MAP_TYPE_USER_DEFINED) &&
469       (lowColor.isValid() && highColor.isValid()))){
470     switch(newType){
471     case INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR:{
472       _intensityColorMapType = newType;
473       QwtLinearColorMap colorMap(Qt::darkCyan, Qt::white);
474       colorMap.addColorStop(0.25, Qt::cyan);
475       colorMap.addColorStop(0.5, Qt::yellow);
476       colorMap.addColorStop(0.75, Qt::red);
477       d_spectrogram->setColorMap(colorMap);
478       break;
479     }
480     case INTENSITY_COLOR_MAP_TYPE_WHITE_HOT:{
481       _intensityColorMapType = newType;
482       QwtLinearColorMap colorMap(Qt::black, Qt::white);
483       d_spectrogram->setColorMap(colorMap);
484       break;
485     }
486     case INTENSITY_COLOR_MAP_TYPE_BLACK_HOT:{
487       _intensityColorMapType = newType;
488       QwtLinearColorMap colorMap(Qt::white, Qt::black);
489       d_spectrogram->setColorMap(colorMap);
490       break;
491     }
492     case INTENSITY_COLOR_MAP_TYPE_INCANDESCENT:{
493       _intensityColorMapType = newType;
494       QwtLinearColorMap colorMap(Qt::black, Qt::white);
495       colorMap.addColorStop(0.5, Qt::darkRed);
496       d_spectrogram->setColorMap(colorMap);
497       break;
498     }
499     case INTENSITY_COLOR_MAP_TYPE_USER_DEFINED:{
500       _userDefinedLowIntensityColor = lowColor;
501       _userDefinedHighIntensityColor = highColor;
502       _intensityColorMapType = newType;
503       QwtLinearColorMap colorMap(_userDefinedLowIntensityColor, _userDefinedHighIntensityColor);
504       d_spectrogram->setColorMap(colorMap);
505       break;
506     }
507     default: break;
508     }
509     
510     _UpdateIntensityRangeDisplay();
511   }
512 }
513
514 const QColor
515 WaterfallDisplayPlot::GetUserDefinedLowIntensityColor() const
516 {
517   return _userDefinedLowIntensityColor;
518 }
519
520 const QColor
521 WaterfallDisplayPlot::GetUserDefinedHighIntensityColor() const
522 {
523   return _userDefinedHighIntensityColor;
524 }
525
526 void
527 WaterfallDisplayPlot::_UpdateIntensityRangeDisplay()
528 {
529   QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
530   rightAxis->setTitle("Intensity (dB)");
531   rightAxis->setColorBarEnabled(true);
532   rightAxis->setColorMap(d_spectrogram->data()->range(),
533                          d_spectrogram->colorMap());
534
535   setAxisScale(QwtPlot::yRight, 
536                d_spectrogram->data()->range().minValue(),
537                d_spectrogram->data()->range().maxValue() );
538   enableAxis(QwtPlot::yRight);
539   
540   plotLayout()->setAlignCanvasToScales(true);
541
542   // Tell the display to redraw everything
543   d_spectrogram->invalidateCache();
544   d_spectrogram->itemChanged();
545
546   // Draw again
547   replot();
548
549   // Update the last replot timer
550   _lastReplot = get_highres_clock();
551 }
552
553 #endif /* WATERFALL_DISPLAY_PLOT_C */