Fixes units of pen in frequency plot.
[debian/gnuradio] / gr-qtgui / src / lib / FrequencyDisplayPlot.cc
index 4f9bfdd952167a49d670cab975409e3ec13bdedc..641b2272bc2581124b54718fccaa84b2e6f03407 100644 (file)
@@ -73,13 +73,22 @@ public:
     updateDisplay();
   }
 
+  void SetUnitType(const std::string &type)
+  {
+    _unitType = type;
+  }
+
 protected:
   virtual QwtText trackerText( const QwtDoublePoint& p ) const 
   {
-    QwtText t(QString("%1 %2, %3 dB").arg(p.x(), 0, 'f', GetFrequencyPrecision()).arg( (GetFrequencyPrecision() == 0) ? "Hz" : "kHz").arg(p.y(), 0, 'f', 2));
+    QwtText t(QString("%1 %2, %3 dB").arg(p.x(), 0, 'f', 
+                                         GetFrequencyPrecision()).arg(_unitType.c_str()).arg(p.y(), 0, 'f', 2));
 
     return t;
   }
+
+private:
+  std::string _unitType;
 };
 
 FrequencyDisplayPlot::FrequencyDisplayPlot(QWidget* parent)
@@ -91,7 +100,7 @@ FrequencyDisplayPlot::FrequencyDisplayPlot(QWidget* parent)
   timespec_reset(&_lastReplot);
 
   resize(parent->width(), parent->height());
-
+  
   _displayIntervalTime = (1.0/10.0); // 1/10 of a second between updates
 
   _useCenterFrequencyFlag = false;
@@ -117,8 +126,10 @@ FrequencyDisplayPlot::FrequencyDisplayPlot(QWidget* parent)
   setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
   setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
 
+  _minYAxis = -120;
+  _maxYAxis = 10;
   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
-  setAxisScale(QwtPlot::yLeft, -210, 5);
+  setAxisScale(QwtPlot::yLeft, _minYAxis, _maxYAxis);
   setAxisTitle(QwtPlot::yLeft, "Power (dB)");
 
   // Automatically deleted when parent is deleted
@@ -222,6 +233,20 @@ FrequencyDisplayPlot::~FrequencyDisplayPlot()
   // _zoomer and _panner deleted when parent deleted
 }
 
+void
+FrequencyDisplayPlot::set_yaxis(double min, double max)
+{
+  // Get the new max/min values for the plot
+  _minYAxis = min;
+  _maxYAxis = max;
+
+  // Set the axis max/min to the new values
+  setAxisScale(QwtPlot::yLeft, _minYAxis, _maxYAxis);
+
+  // Reset the base zoom level to the new axis scale set here
+  _zoomer->setZoomBase();
+}
+
 void
 FrequencyDisplayPlot::SetFrequencyRange(const double constStartFreq,
                                        const double constStopFreq,
@@ -243,17 +268,16 @@ FrequencyDisplayPlot::SetFrequencyRange(const double constStartFreq,
   _startFrequency = startFreq;
   _stopFrequency = stopFreq;
   _resetXAxisPoints();
-  
+
+  double display_units = ceil(log10(units)/2.0);
   setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
-  setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(2));
+  setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(display_units));
   setAxisTitle(QwtPlot::xBottom, QString("Frequency (%1)").arg(strunits.c_str()));
-  ((FreqDisplayZoomer*)_zoomer)->SetFrequencyPrecision(2);
+  ((FreqDisplayZoomer*)_zoomer)->SetFrequencyPrecision(display_units);
+  ((FreqDisplayZoomer*)_zoomer)->SetUnitType(strunits);
 
   // Load up the new base zoom settings
-  QwtDoubleRect newSize = _zoomer->zoomBase();
-  newSize.setLeft(_startFrequency);
-  newSize.setWidth(_stopFrequency-_startFrequency);
-  _zoomer->setZoomBase(newSize);
+  _zoomer->setZoomBase();
   
   // Zooms back to the base and clears any other zoom levels
   _zoomer->zoom(0);
@@ -298,6 +322,12 @@ FrequencyDisplayPlot::replot()
     _displayIntervalTime = differenceTime;
   }
 }
+void
+FrequencyDisplayPlot::resizeSlot( QSize *s )
+{
+  resize(s->width(), s->height());
+}
 
 void
 FrequencyDisplayPlot::PlotNewData(const double* dataPoints, const int64_t numDataPoints,
@@ -326,6 +356,7 @@ FrequencyDisplayPlot::PlotNewData(const double* dataPoints, const int64_t numDat
       ClearMaxData();
       ClearMinData();
     }
+
     memcpy(_dataPoints, dataPoints, numDataPoints*sizeof(double));
     for(int64_t point = 0; point < numDataPoints; point++){
       if(dataPoints[point] < _minFFTPoints[point]){
@@ -356,7 +387,7 @@ void
 FrequencyDisplayPlot::ClearMaxData()
 {
   for(int64_t number = 0; number < _numPoints; number++){
-    _maxFFTPoints[number] = -280.0;
+    _maxFFTPoints[number] = _minYAxis;
   }
 }
 
@@ -364,7 +395,7 @@ void
 FrequencyDisplayPlot::ClearMinData()
 {
   for(int64_t number = 0; number < _numPoints; number++){
-    _minFFTPoints[number] = 200.0;
+    _minFFTPoints[number] = _maxYAxis;
   }
 }