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 using QwtPlotZoomer::trackerText;
190 virtual QwtText trackerText( const QwtDoublePoint& p ) const
194 timespec lineTime = timespec_add(GetZeroTime(), (-p.y()) * GetSecondsPerLine());
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));
202 QwtText t(QString("%1 %2, %3").
203 arg(p.x(), 0, 'f', GetFrequencyPrecision()).
204 arg(_unitType.c_str()).arg(yLabel));
209 std::string _unitType;
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;
219 WaterfallDisplayPlot::WaterfallDisplayPlot(QWidget* parent)
224 _stopFrequency = 4000;
226 resize(parent->width(), parent->height());
229 _waterfallData = new WaterfallData(_startFrequency, _stopFrequency, _numPoints, 200);
232 palette.setColor(canvas()->backgroundRole(), QColor("white"));
233 canvas()->setPalette(palette);
235 setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
236 setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(0));
238 setAxisTitle(QwtPlot::yLeft, "Time");
239 setAxisScaleDraw(QwtPlot::yLeft, new QwtTimeScaleDraw());
241 timespec_reset(&_lastReplot);
243 d_spectrogram = new PlotWaterfall(_waterfallData, "Waterfall Display");
245 _intensityColorMapType = INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
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);
252 d_spectrogram->setColorMap(colorMap);
254 d_spectrogram->attach(this);
256 // LeftButton for the zooming
257 // MidButton for the panning
258 // RightButton: zoom out by 1
259 // Ctrl+RighButton: zoom out to full size
261 _zoomer = new WaterfallZoomer(canvas(), 0);
262 #if QT_VERSION < 0x040000
263 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
264 Qt::RightButton, Qt::ControlModifier);
266 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
267 Qt::RightButton, Qt::ControlModifier);
269 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
272 _panner = new QwtPlotPanner(canvas());
273 _panner->setAxisEnabled(QwtPlot::yRight, false);
274 _panner->setMouseButton(Qt::MidButton);
276 // Avoid jumping when labels with more/less digits
277 // appear/disappear when scrolling vertically
279 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
280 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
281 sd->setMinimumExtent( fm.width("100.00") );
283 const QColor c(Qt::white);
284 _zoomer->setRubberBandPen(c);
285 _zoomer->setTrackerPen(c);
287 _UpdateIntensityRangeDisplay();
290 WaterfallDisplayPlot::~WaterfallDisplayPlot()
292 delete _waterfallData;
293 delete d_spectrogram;
297 WaterfallDisplayPlot::Reset()
299 _waterfallData->ResizeData(_startFrequency, _stopFrequency, _numPoints);
300 _waterfallData->Reset();
302 setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
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);
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)
320 double startFreq = constStartFreq / units;
321 double stopFreq = constStopFreq / units;
322 double centerFreq = constCenterFreq / units;
324 _useCenterFrequencyFlag = useCenterFrequencyFlag;
326 if(_useCenterFrequencyFlag){
327 startFreq = (startFreq + centerFreq);
328 stopFreq = (stopFreq + centerFreq);
332 if((startFreq != _startFrequency) || (stopFreq != _stopFrequency))
335 if(stopFreq > startFreq) {
336 _startFrequency = startFreq;
337 _stopFrequency = stopFreq;
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()));
348 ((WaterfallZoomer*)_zoomer)->SetFrequencyPrecision(display_units);
349 ((WaterfallZoomer*)_zoomer)->SetUnitType(strunits);
356 WaterfallDisplayPlot::GetStartFrequency() const
358 return _startFrequency;
362 WaterfallDisplayPlot::GetStopFrequency() const
364 return _stopFrequency;
368 WaterfallDisplayPlot::PlotNewData(const double* dataPoints,
369 const int64_t numDataPoints,
370 const double timePerFFT,
371 const timespec timestamp,
372 const int droppedFrames)
374 if(numDataPoints > 0){
375 if(numDataPoints != _numPoints){
376 _numPoints = numDataPoints;
380 d_spectrogram->invalidateCache();
381 d_spectrogram->itemChanged();
387 _lastReplot = get_highres_clock();
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();
395 QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
396 timeScale->SetSecondsPerLine(timePerFFT);
397 timeScale->SetZeroTime(timestamp);
399 ((WaterfallZoomer*)_zoomer)->SetSecondsPerLine(timePerFFT);
400 ((WaterfallZoomer*)_zoomer)->SetZeroTime(timestamp);
402 d_spectrogram->invalidateCache();
403 d_spectrogram->itemChanged();
407 _lastReplot = get_highres_clock();
413 WaterfallDisplayPlot::SetIntensityRange(const double minIntensity,
414 const double maxIntensity)
416 _waterfallData->setRange(QwtDoubleInterval(minIntensity, maxIntensity));
418 emit UpdatedLowerIntensityLevel(minIntensity);
419 emit UpdatedUpperIntensityLevel(maxIntensity);
421 _UpdateIntensityRangeDisplay();
425 WaterfallDisplayPlot::replot()
427 QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
428 timeScale->initiateUpdate();
430 WaterfallFreqDisplayScaleDraw* freqScale = (WaterfallFreqDisplayScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
431 freqScale->initiateUpdate();
433 // Update the time axis display
434 if(axisWidget(QwtPlot::yLeft) != NULL){
435 axisWidget(QwtPlot::yLeft)->update();
438 // Update the Frequency Offset Display
439 if(axisWidget(QwtPlot::xBottom) != NULL){
440 axisWidget(QwtPlot::xBottom)->update();
444 ((WaterfallZoomer*)_zoomer)->updateTrackerText();
451 WaterfallDisplayPlot::resizeSlot( QSize *s )
453 resize(s->width(), s->height());
457 WaterfallDisplayPlot::GetIntensityColorMapType() const
459 return _intensityColorMapType;
463 WaterfallDisplayPlot::SetIntensityColorMapType(const int newType,
464 const QColor lowColor,
465 const QColor highColor)
467 if((_intensityColorMapType != newType) ||
468 ((newType == INTENSITY_COLOR_MAP_TYPE_USER_DEFINED) &&
469 (lowColor.isValid() && highColor.isValid()))){
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);
480 case INTENSITY_COLOR_MAP_TYPE_WHITE_HOT:{
481 _intensityColorMapType = newType;
482 QwtLinearColorMap colorMap(Qt::black, Qt::white);
483 d_spectrogram->setColorMap(colorMap);
486 case INTENSITY_COLOR_MAP_TYPE_BLACK_HOT:{
487 _intensityColorMapType = newType;
488 QwtLinearColorMap colorMap(Qt::white, Qt::black);
489 d_spectrogram->setColorMap(colorMap);
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);
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);
510 _UpdateIntensityRangeDisplay();
515 WaterfallDisplayPlot::GetUserDefinedLowIntensityColor() const
517 return _userDefinedLowIntensityColor;
521 WaterfallDisplayPlot::GetUserDefinedHighIntensityColor() const
523 return _userDefinedHighIntensityColor;
527 WaterfallDisplayPlot::_UpdateIntensityRangeDisplay()
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());
535 setAxisScale(QwtPlot::yRight,
536 d_spectrogram->data()->range().minValue(),
537 d_spectrogram->data()->range().maxValue() );
538 enableAxis(QwtPlot::yRight);
540 plotLayout()->setAlignCanvasToScales(true);
542 // Tell the display to redraw everything
543 d_spectrogram->invalidateCache();
544 d_spectrogram->itemChanged();
549 // Update the last replot timer
550 _lastReplot = get_highres_clock();
553 #endif /* WATERFALL_DISPLAY_PLOT_C */