diff --git a/src/UI/Dashboard.cpp b/src/UI/Dashboard.cpp index 6f69f815..b935b5a0 100644 --- a/src/UI/Dashboard.cpp +++ b/src/UI/Dashboard.cpp @@ -92,6 +92,14 @@ JSON::Dataset *Dashboard::getBar(const int index) 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) { if (index < m_compassWidgets.count()) diff --git a/src/UI/Dashboard.h b/src/UI/Dashboard.h index d01fe543..629d2a7f 100644 --- a/src/UI/Dashboard.h +++ b/src/UI/Dashboard.h @@ -127,6 +127,7 @@ public: QFont monoFont() const; JSON::Group *getGroup(const int index); JSON::Dataset *getBar(const int index); + JSON::Dataset *getGauge(const int index); JSON::Dataset *getCompass(const int index); QString title(); diff --git a/src/Widgets/Compass.cpp b/src/Widgets/Compass.cpp index 88de9e63..0f530c65 100644 --- a/src/Widgets/Compass.cpp +++ b/src/Widgets/Compass.cpp @@ -51,9 +51,11 @@ Compass::Compass(const int index) m_compass.setNeedle( new QwtCompassMagnetNeedle(QwtCompassMagnetNeedle::TriangleStyle)); - // Set stylesheet - auto qss = QSS("background-color:%1", theme->datasetWindowBackground()); - setStyleSheet(qss); + // Set window palette + QPalette windowPalette; + windowPalette.setColor(QPalette::Base, theme->datasetWindowBackground()); + windowPalette.setColor(QPalette::Window, theme->datasetWindowBackground()); + setPalette(windowPalette); // Add compass to layout m_layout.addWidget(&m_compass); diff --git a/src/Widgets/Gauge.cpp b/src/Widgets/Gauge.cpp index 42b0ff52..34af3bc0 100644 --- a/src/Widgets/Gauge.cpp +++ b/src/Widgets/Gauge.cpp @@ -21,3 +21,118 @@ */ #include "Gauge.h" +#include "UI/Dashboard.h" +#include "Misc/ThemeManager.h" + +#include +#include +#include +#include + +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 ¢er, + 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()); + } +} diff --git a/src/Widgets/Gauge.h b/src/Widgets/Gauge.h index 408dd17e..6cbe6d96 100644 --- a/src/Widgets/Gauge.h +++ b/src/Widgets/Gauge.h @@ -26,12 +26,40 @@ #include #include #include +#include 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 ¢er, + double radius) const QWT_OVERRIDE; + +private: + QString m_label; +}; + class Gauge : public QWidget { Q_OBJECT + +public: + Gauge(const int index = -1); + +private slots: + void update(); + +private: + int m_index; + GaugeObject m_gauge; + QVBoxLayout m_layout; }; } diff --git a/src/Widgets/WidgetLoader.cpp b/src/Widgets/WidgetLoader.cpp index 25f11a61..1b42fbde 100644 --- a/src/Widgets/WidgetLoader.cpp +++ b/src/Widgets/WidgetLoader.cpp @@ -249,7 +249,8 @@ void WidgetLoader::setWidgetIndex(const int index) m_window.setCentralWidget(new Bar(relativeIndex())); break; case UI::Dashboard::WidgetType::Gauge: - m_widget = new QPushButton("Gauge"); + m_widget = new Gauge(relativeIndex()); + m_window.setCentralWidget(new Gauge(relativeIndex())); break; case UI::Dashboard::WidgetType::Thermometer: m_widget = new QPushButton("Thermometer");