WSJT-X/signalmeter.cpp
Bill Somerville 080f7ef1a2 Make mutable static variables instance variables where necessary
Static storage variables that should  have been class members are made
so. This  ensures that  if they  are used  as initialization  one time
switches   then  they   will  operate   correctly  when   their  class
instantiated more than once. This now happoens for most classes due to
the configurations  switching facility which destroys  all windows and
re-instantiates them.

git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6661 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
2016-05-01 00:40:51 +00:00

103 lines
2.8 KiB
C++

// Simple bargraph dB meter
// Implemented by Edson Pereira PY2SDR
//
#include "signalmeter.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QFontMetrics>
#include <QDebug>
#include <meterwidget.h>
#include "moc_signalmeter.cpp"
class Scale final
: public QWidget
{
public:
explicit Scale (QWidget * parent = 0)
: QWidget {parent}
{
setSizePolicy (QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
}
QSize sizeHint () const override
{
return minimumSizeHint ();
}
QSize minimumSizeHint () const override
{
QFontMetrics font_metrics {font (), nullptr};
return {tick_length + text_indent + font_metrics.width ("00+"), (font_metrics.height () + line_spacing) * range};
}
protected:
void paintEvent (QPaintEvent * event) override
{
QWidget::paintEvent (event);
QPainter p {this};
auto const& target = contentsRect ();
QFontMetrics font_metrics {p.font (), this};
auto font_offset = font_metrics.ascent () / 2;
p.drawLine (target.left (), target.top () + font_offset, target.left (), target.bottom () - font_offset - font_metrics.descent ());
for (int i = 0; i <= range; ++i)
{
p.save ();
p.translate (target.left ()
, target.top () + font_offset + i * (target.height () - font_metrics.ascent () - font_metrics.descent ()) / range);
p.drawLine (0, 0, tick_length, 0);
auto text = i ? QString::number ((range - i) * scale) : QString {"%1%2"}.arg ((range - i) * scale).arg ('+');
p.drawText (tick_length + text_indent, font_offset, text);
p.restore ();
}
}
private:
static int constexpr tick_length {4};
static int constexpr text_indent {2};
static int constexpr line_spacing {0};
static int constexpr range {6};
static int constexpr scale {10};
};
SignalMeter::SignalMeter (QWidget * parent)
: QFrame {parent}
{
auto outer_layout = new QVBoxLayout;
outer_layout->setSpacing (0);
auto inner_layout = new QHBoxLayout;
inner_layout->setContentsMargins (9, 0, 9, 0);
inner_layout->setSpacing (0);
m_meter = new MeterWidget;
m_meter->setSizePolicy (QSizePolicy::Minimum, QSizePolicy::Minimum);
inner_layout->addWidget (m_meter);
m_scale = new Scale;
inner_layout->addWidget (m_scale);
m_reading = new QLabel(this);
outer_layout->addLayout (inner_layout);
outer_layout->addWidget (m_reading);
setLayout (outer_layout);
}
void SignalMeter::setValue(float value)
{
if(value<0) value=0;
QFontMetrics font_metrics {m_scale->font (), nullptr};
m_meter->setContentsMargins (0, font_metrics.ascent () / 2, 0, font_metrics.ascent () / 2 + font_metrics.descent ());
m_meter->setValue(int(value));
QString t;
t.sprintf("%4.1f dB",value);
m_reading->setText(t);
}