1 #ifndef WATERFALL_DISPLAY_PLOT_C
2 #define WATERFALL_DISPLAY_PLOT_C
4 #include <WaterfallDisplayPlot.h>
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>
13 #include <qapplication.h>
15 class FreqOffsetAndPrecisionClass
18 FreqOffsetAndPrecisionClass(const int freqPrecision)
20 _frequencyPrecision = freqPrecision;
24 virtual ~FreqOffsetAndPrecisionClass()
28 virtual unsigned int GetFrequencyPrecision() const
30 return _frequencyPrecision;
33 virtual void SetFrequencyPrecision(const unsigned int newPrecision)
35 _frequencyPrecision = newPrecision;
38 virtual double GetCenterFrequency() const
40 return _centerFrequency;
43 virtual void SetCenterFrequency(const double newFreq)
45 _centerFrequency = newFreq;
49 unsigned int _frequencyPrecision;
50 double _centerFrequency;
56 class WaterfallFreqDisplayScaleDraw: public QwtScaleDraw, public FreqOffsetAndPrecisionClass{
58 WaterfallFreqDisplayScaleDraw(const unsigned int precision)
59 : QwtScaleDraw(), FreqOffsetAndPrecisionClass(precision)
63 virtual ~WaterfallFreqDisplayScaleDraw()
67 QwtText label(double value) const
69 return QString("%1").arg(value, 0, 'f', GetFrequencyPrecision());
72 virtual void initiateUpdate()
88 timespec_reset(&_zeroTime);
89 _secondsPerLine = 1.0;
92 virtual ~TimeScaleData()
96 virtual timespec GetZeroTime() const
101 virtual void SetZeroTime(const timespec newTime)
106 virtual void SetSecondsPerLine(const double newTime)
108 _secondsPerLine = newTime;
111 virtual double GetSecondsPerLine() const
113 return _secondsPerLine;
119 double _secondsPerLine;
125 class QwtTimeScaleDraw: public QwtScaleDraw, public TimeScaleData
128 QwtTimeScaleDraw():QwtScaleDraw(),TimeScaleData()
132 virtual ~QwtTimeScaleDraw()
136 virtual QwtText label(double value) const
138 QwtText returnLabel("");
140 timespec lineTime = timespec_add(GetZeroTime(), (-value) * GetSecondsPerLine());
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));
150 virtual void initiateUpdate()
152 // Do this in one call rather than when zeroTime and secondsPerLine
153 // updates is to prevent the display from being updated too often...
163 class WaterfallZoomer: public QwtPlotZoomer, public TimeScaleData,
164 public FreqOffsetAndPrecisionClass
167 WaterfallZoomer(QwtPlotCanvas* canvas, const unsigned int freqPrecision)
168 : QwtPlotZoomer(canvas), TimeScaleData(),
169 FreqOffsetAndPrecisionClass(freqPrecision)
171 setTrackerMode(QwtPicker::AlwaysOn);
174 virtual ~WaterfallZoomer()
178 virtual void updateTrackerText()
183 void SetUnitType(const std::string &type)
189 virtual QwtText trackerText( const QwtDoublePoint& p ) const
193 timespec lineTime = timespec_add(GetZeroTime(), (-p.y()) * GetSecondsPerLine());
195 gmtime_r(&lineTime.tv_sec, &timeTm);
196 yLabel = (QString("").sprintf("%04d/%02d/%02d %02d:%02d:%02d.%03ld",
197 timeTm.tm_year+1900, timeTm.tm_mon+1,
198 timeTm.tm_mday, timeTm.tm_hour, timeTm.tm_min,
199 timeTm.tm_sec, lineTime.tv_nsec/1000000));
201 QwtText t(QString("%1 %2, %3").
202 arg(p.x(), 0, 'f', GetFrequencyPrecision()).
203 arg(_unitType.c_str()).arg(yLabel));
208 std::string _unitType;
212 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
213 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_WHITE_HOT;
214 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_BLACK_HOT;
215 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_INCANDESCENT;
216 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_USER_DEFINED;
218 WaterfallDisplayPlot::WaterfallDisplayPlot(QWidget* parent)
223 _stopFrequency = 4000;
225 resize(parent->width(), parent->height());
228 _waterfallData = new WaterfallData(_startFrequency, _stopFrequency, _numPoints, 200);
231 palette.setColor(canvas()->backgroundRole(), QColor("white"));
232 canvas()->setPalette(palette);
234 setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
235 setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(0));
237 setAxisTitle(QwtPlot::yLeft, "Time");
238 setAxisScaleDraw(QwtPlot::yLeft, new QwtTimeScaleDraw());
240 timespec_reset(&_lastReplot);
242 d_spectrogram = new PlotWaterfall(_waterfallData, "Waterfall Display");
244 _intensityColorMapType = INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
246 QwtLinearColorMap colorMap(Qt::darkCyan, Qt::white);
247 colorMap.addColorStop(0.25, Qt::cyan);
248 colorMap.addColorStop(0.5, Qt::yellow);
249 colorMap.addColorStop(0.75, Qt::red);
251 d_spectrogram->setColorMap(colorMap);
253 d_spectrogram->attach(this);
255 // LeftButton for the zooming
256 // MidButton for the panning
257 // RightButton: zoom out by 1
258 // Ctrl+RighButton: zoom out to full size
260 _zoomer = new WaterfallZoomer(canvas(), 0);
261 #if QT_VERSION < 0x040000
262 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
263 Qt::RightButton, Qt::ControlModifier);
265 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
266 Qt::RightButton, Qt::ControlModifier);
268 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
271 _panner = new QwtPlotPanner(canvas());
272 _panner->setAxisEnabled(QwtPlot::yRight, false);
273 _panner->setMouseButton(Qt::MidButton);
275 // Avoid jumping when labels with more/less digits
276 // appear/disappear when scrolling vertically
278 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
279 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
280 sd->setMinimumExtent( fm.width("100.00") );
282 const QColor c(Qt::white);
283 _zoomer->setRubberBandPen(c);
284 _zoomer->setTrackerPen(c);
286 _UpdateIntensityRangeDisplay();
289 WaterfallDisplayPlot::~WaterfallDisplayPlot()
291 delete _waterfallData;
292 delete d_spectrogram;
296 WaterfallDisplayPlot::Reset()
298 _waterfallData->ResizeData(_startFrequency, _stopFrequency, _numPoints);
299 _waterfallData->Reset();
301 setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
303 // Load up the new base zoom settings
304 QwtDoubleRect newSize = _zoomer->zoomBase();
305 newSize.setLeft(_startFrequency);
306 newSize.setWidth(_stopFrequency-_startFrequency);
307 _zoomer->zoom(newSize);
308 _zoomer->setZoomBase(newSize);
313 WaterfallDisplayPlot::SetFrequencyRange(const double constStartFreq,
314 const double constStopFreq,
315 const double constCenterFreq,
316 const bool useCenterFrequencyFlag,
317 const double units, const std::string &strunits)
319 double startFreq = constStartFreq / units;
320 double stopFreq = constStopFreq / units;
321 double centerFreq = constCenterFreq / units;
323 _useCenterFrequencyFlag = useCenterFrequencyFlag;
325 if(_useCenterFrequencyFlag){
326 startFreq = (startFreq + centerFreq);
327 stopFreq = (stopFreq + centerFreq);
331 if((startFreq != _startFrequency) || (stopFreq != _stopFrequency))
334 if(stopFreq > startFreq) {
335 _startFrequency = startFreq;
336 _stopFrequency = stopFreq;
338 if((axisScaleDraw(QwtPlot::xBottom) != NULL) && (_zoomer != NULL)){
339 double display_units = ceil(log10(units)/2.0);
340 setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(display_units));
341 setAxisTitle(QwtPlot::xBottom, QString("Frequency (%1)").arg(strunits.c_str()));
347 ((WaterfallZoomer*)_zoomer)->SetFrequencyPrecision(display_units);
348 ((WaterfallZoomer*)_zoomer)->SetUnitType(strunits);
355 WaterfallDisplayPlot::GetStartFrequency() const
357 return _startFrequency;
361 WaterfallDisplayPlot::GetStopFrequency() const
363 return _stopFrequency;
367 WaterfallDisplayPlot::PlotNewData(const double* dataPoints,
368 const int64_t numDataPoints,
369 const double timePerFFT,
370 const timespec timestamp,
371 const int droppedFrames)
373 if(numDataPoints > 0){
374 if(numDataPoints != _numPoints){
375 _numPoints = numDataPoints;
379 d_spectrogram->invalidateCache();
380 d_spectrogram->itemChanged();
386 _lastReplot = get_highres_clock();
389 if(diff_timespec(get_highres_clock(), _lastReplot) > timePerFFT) {
390 //FIXME: We may want to average the data between these updates to smooth display
391 _waterfallData->addFFTData(dataPoints, numDataPoints, droppedFrames);
392 _waterfallData->IncrementNumLinesToUpdate();
394 QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
395 timeScale->SetSecondsPerLine(timePerFFT);
396 timeScale->SetZeroTime(timestamp);
398 ((WaterfallZoomer*)_zoomer)->SetSecondsPerLine(timePerFFT);
399 ((WaterfallZoomer*)_zoomer)->SetZeroTime(timestamp);
401 d_spectrogram->invalidateCache();
402 d_spectrogram->itemChanged();
406 _lastReplot = get_highres_clock();
412 WaterfallDisplayPlot::SetIntensityRange(const double minIntensity,
413 const double maxIntensity)
415 _waterfallData->setRange(QwtDoubleInterval(minIntensity, maxIntensity));
417 emit UpdatedLowerIntensityLevel(minIntensity);
418 emit UpdatedUpperIntensityLevel(maxIntensity);
420 _UpdateIntensityRangeDisplay();
424 WaterfallDisplayPlot::replot()
426 QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
427 timeScale->initiateUpdate();
429 WaterfallFreqDisplayScaleDraw* freqScale = (WaterfallFreqDisplayScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
430 freqScale->initiateUpdate();
432 // Update the time axis display
433 if(axisWidget(QwtPlot::yLeft) != NULL){
434 axisWidget(QwtPlot::yLeft)->update();
437 // Update the Frequency Offset Display
438 if(axisWidget(QwtPlot::xBottom) != NULL){
439 axisWidget(QwtPlot::xBottom)->update();
443 ((WaterfallZoomer*)_zoomer)->updateTrackerText();
450 WaterfallDisplayPlot::resizeSlot( QSize *s )
452 resize(s->width(), s->height());
456 WaterfallDisplayPlot::GetIntensityColorMapType() const
458 return _intensityColorMapType;
462 WaterfallDisplayPlot::SetIntensityColorMapType(const int newType,
463 const QColor lowColor,
464 const QColor highColor)
466 if((_intensityColorMapType != newType) ||
467 ((newType == INTENSITY_COLOR_MAP_TYPE_USER_DEFINED) &&
468 (lowColor.isValid() && highColor.isValid()))){
470 case INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR:{
471 _intensityColorMapType = newType;
472 QwtLinearColorMap colorMap(Qt::darkCyan, Qt::white);
473 colorMap.addColorStop(0.25, Qt::cyan);
474 colorMap.addColorStop(0.5, Qt::yellow);
475 colorMap.addColorStop(0.75, Qt::red);
476 d_spectrogram->setColorMap(colorMap);
479 case INTENSITY_COLOR_MAP_TYPE_WHITE_HOT:{
480 _intensityColorMapType = newType;
481 QwtLinearColorMap colorMap(Qt::black, Qt::white);
482 d_spectrogram->setColorMap(colorMap);
485 case INTENSITY_COLOR_MAP_TYPE_BLACK_HOT:{
486 _intensityColorMapType = newType;
487 QwtLinearColorMap colorMap(Qt::white, Qt::black);
488 d_spectrogram->setColorMap(colorMap);
491 case INTENSITY_COLOR_MAP_TYPE_INCANDESCENT:{
492 _intensityColorMapType = newType;
493 QwtLinearColorMap colorMap(Qt::black, Qt::white);
494 colorMap.addColorStop(0.5, Qt::darkRed);
495 d_spectrogram->setColorMap(colorMap);
498 case INTENSITY_COLOR_MAP_TYPE_USER_DEFINED:{
499 _userDefinedLowIntensityColor = lowColor;
500 _userDefinedHighIntensityColor = highColor;
501 _intensityColorMapType = newType;
502 QwtLinearColorMap colorMap(_userDefinedLowIntensityColor, _userDefinedHighIntensityColor);
503 d_spectrogram->setColorMap(colorMap);
509 _UpdateIntensityRangeDisplay();
514 WaterfallDisplayPlot::GetUserDefinedLowIntensityColor() const
516 return _userDefinedLowIntensityColor;
520 WaterfallDisplayPlot::GetUserDefinedHighIntensityColor() const
522 return _userDefinedHighIntensityColor;
526 WaterfallDisplayPlot::_UpdateIntensityRangeDisplay()
528 QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
529 rightAxis->setTitle("Intensity (dB)");
530 rightAxis->setColorBarEnabled(true);
531 rightAxis->setColorMap(d_spectrogram->data()->range(),
532 d_spectrogram->colorMap());
534 setAxisScale(QwtPlot::yRight,
535 d_spectrogram->data()->range().minValue(),
536 d_spectrogram->data()->range().maxValue() );
537 enableAxis(QwtPlot::yRight);
539 plotLayout()->setAlignCanvasToScales(true);
541 // Tell the display to redraw everything
542 d_spectrogram->invalidateCache();
543 d_spectrogram->itemChanged();
548 // Update the last replot timer
549 _lastReplot = get_highres_clock();
552 #endif /* WATERFALL_DISPLAY_PLOT_C */