Add initial gauge widget

This commit is contained in:
Alex Spataru 2021-09-26 20:03:03 -05:00
parent c747f6e53e
commit fa5bbdad76
6 changed files with 159 additions and 4 deletions

View File

@ -92,6 +92,14 @@ JSON::Dataset *Dashboard::getBar(const int index)
return nullptr; return nullptr;
} }
JSON::Dataset *Dashboard::getGauge(const int index)
{
if (index < m_gaugeWidgets.count())
return m_gaugeWidgets.at(index);
return nullptr;
}
JSON::Dataset *Dashboard::getCompass(const int index) JSON::Dataset *Dashboard::getCompass(const int index)
{ {
if (index < m_compassWidgets.count()) if (index < m_compassWidgets.count())

View File

@ -127,6 +127,7 @@ public:
QFont monoFont() const; QFont monoFont() const;
JSON::Group *getGroup(const int index); JSON::Group *getGroup(const int index);
JSON::Dataset *getBar(const int index); JSON::Dataset *getBar(const int index);
JSON::Dataset *getGauge(const int index);
JSON::Dataset *getCompass(const int index); JSON::Dataset *getCompass(const int index);
QString title(); QString title();

View File

@ -51,9 +51,11 @@ Compass::Compass(const int index)
m_compass.setNeedle( m_compass.setNeedle(
new QwtCompassMagnetNeedle(QwtCompassMagnetNeedle::TriangleStyle)); new QwtCompassMagnetNeedle(QwtCompassMagnetNeedle::TriangleStyle));
// Set stylesheet // Set window palette
auto qss = QSS("background-color:%1", theme->datasetWindowBackground()); QPalette windowPalette;
setStyleSheet(qss); windowPalette.setColor(QPalette::Base, theme->datasetWindowBackground());
windowPalette.setColor(QPalette::Window, theme->datasetWindowBackground());
setPalette(windowPalette);
// Add compass to layout // Add compass to layout
m_layout.addWidget(&m_compass); m_layout.addWidget(&m_compass);

View File

@ -21,3 +21,118 @@
*/ */
#include "Gauge.h" #include "Gauge.h"
#include "UI/Dashboard.h"
#include "Misc/ThemeManager.h"
#include <QPainter>
#include <QwtDialNeedle>
#include <QwtRoundScaleDraw>
#include <qwt_dial_needle.h>
using namespace Widgets;
//--------------------------------------------------------------------------------------------------
// Gauge/Speedometer implementation (based on Qwt examples)
//--------------------------------------------------------------------------------------------------
GaugeObject::GaugeObject(QWidget *parent)
: QwtDial(parent)
{
setReadOnly(true);
setWrapping(false);
// Set gauge origin & min/max angles
setOrigin(135);
setScaleArc(0, 270);
QwtDialSimpleNeedle *needle = new QwtDialSimpleNeedle(
QwtDialSimpleNeedle::Arrow, true, Qt::red, QColor(Qt::gray).lighter(130));
setNeedle(needle);
}
QString GaugeObject::label() const
{
return m_label;
}
void GaugeObject::setLabel(const QString &label)
{
m_label = label;
update();
}
void GaugeObject::drawScaleContents(QPainter *painter, const QPointF &center,
double radius) const
{
QRectF rect(0.0, 0.0, 2.0 * radius, 2.0 * radius - 10.0);
rect.moveCenter(center);
const QColor color = palette().color(QPalette::Text);
painter->setPen(color);
const int flags = Qt::AlignBottom | Qt::AlignHCenter;
painter->drawText(rect, flags, m_label);
}
//--------------------------------------------------------------------------------------------------
// Gauge widget implementation
//--------------------------------------------------------------------------------------------------
Gauge::Gauge(const int index)
: m_index(index)
{
// Get pointers to Serial Studio modules
auto dash = UI::Dashboard::getInstance();
auto theme = Misc::ThemeManager::getInstance();
// Invalid index, abort initialization
if (m_index < 0 || m_index >= dash->gaugeCount())
return;
// Configure gauge
m_gauge.setFont(dash->monoFont());
// Set gauge scale (lazy widgets only)
#ifdef LAZY_WIDGETS
auto dataset = dash->getGauge(m_index);
if (dataset)
{
m_gauge.setScale(dataset->min(), dataset->max());
if (!dataset->units().isEmpty())
m_gauge.setLabel(dataset->units());
}
#endif
// Set window palette
QPalette windowPalette;
windowPalette.setColor(QPalette::Base, theme->datasetWindowBackground());
windowPalette.setColor(QPalette::Window, theme->datasetWindowBackground());
setPalette(windowPalette);
// Add gauge to layout
m_layout.addWidget(&m_gauge);
m_layout.setContentsMargins(8, 8, 8, 8);
setLayout(&m_layout);
// React to dashboard events
connect(dash, SIGNAL(updated()), this, SLOT(update()));
}
void Gauge::update()
{
// Widget not enabled, do nothing
if (!isEnabled())
return;
// Update compass heading
auto dataset = UI::Dashboard::getInstance()->getGauge(m_index);
if (dataset)
{
#ifndef LAZY_WIDGETS
m_gauge.setScale(dataset->min(), dataset->max());
if (!dataset->units().isEmpty())
m_gauge.setLabel(dataset->units());
#endif
m_gauge.setValue(dataset->value().toDouble());
}
}

View File

@ -26,12 +26,40 @@
#include <QWidget> #include <QWidget>
#include <QwtDial> #include <QwtDial>
#include <QwtDialNeedle> #include <QwtDialNeedle>
#include <QVBoxLayout>
namespace Widgets namespace Widgets
{ {
class GaugeObject : public QwtDial
{
public:
GaugeObject(QWidget *parent = nullptr);
QString label() const;
void setLabel(const QString &label);
protected:
virtual void drawScaleContents(QPainter *painter, const QPointF &center,
double radius) const QWT_OVERRIDE;
private:
QString m_label;
};
class Gauge : public QWidget class Gauge : public QWidget
{ {
Q_OBJECT Q_OBJECT
public:
Gauge(const int index = -1);
private slots:
void update();
private:
int m_index;
GaugeObject m_gauge;
QVBoxLayout m_layout;
}; };
} }

View File

@ -249,7 +249,8 @@ void WidgetLoader::setWidgetIndex(const int index)
m_window.setCentralWidget(new Bar(relativeIndex())); m_window.setCentralWidget(new Bar(relativeIndex()));
break; break;
case UI::Dashboard::WidgetType::Gauge: case UI::Dashboard::WidgetType::Gauge:
m_widget = new QPushButton("Gauge"); m_widget = new Gauge(relativeIndex());
m_window.setCentralWidget(new Gauge(relativeIndex()));
break; break;
case UI::Dashboard::WidgetType::Thermometer: case UI::Dashboard::WidgetType::Thermometer:
m_widget = new QPushButton("Thermometer"); m_widget = new QPushButton("Thermometer");