mirror of
https://github.com/Serial-Studio/Serial-Studio.git
synced 2025-01-15 05:22:53 +08:00
Add initial gauge widget
This commit is contained in:
parent
c747f6e53e
commit
fa5bbdad76
@ -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())
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -21,3 +21,118 @@
|
||||
*/
|
||||
|
||||
#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 ¢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());
|
||||
}
|
||||
}
|
||||
|
@ -26,12 +26,40 @@
|
||||
#include <QWidget>
|
||||
#include <QwtDial>
|
||||
#include <QwtDialNeedle>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user