From 933ffea20158b4a31ac9b6979e696a084e9e9303 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 23 Sep 2015 19:54:52 +0200 Subject: [PATCH] refs #452 updated LED with timer based reset state --- src/blackgui/led.cpp | 55 ++++++++++++++++++++++++++++++++------------ src/blackgui/led.h | 27 ++++++++++++++-------- 2 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/blackgui/led.cpp b/src/blackgui/led.cpp index fea76dee3..274bc4264 100644 --- a/src/blackgui/led.cpp +++ b/src/blackgui/led.cpp @@ -24,26 +24,37 @@ namespace BlackGui CLedWidget::CLedWidget(QWidget *parent) : QWidget(parent), m_renderer(new QSvgRenderer) { this->setLed(); + this->init(); } CLedWidget::CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName, const QString &offName, int targetWidth, QWidget *parent) : + QWidget(parent), m_value(on ? On : Off), m_colorOn(onColor), m_colorOff(offColor), m_shape(shape), m_widthTarget(targetWidth), m_tooltipOn(onName), m_tooltipOff(offName), - m_renderer(new QSvgRenderer(parent)) + m_renderer(new QSvgRenderer(this)) { this->setLed(); + this->init(); + } + + void CLedWidget::init() + { + m_resetTimer.setSingleShot(true); + m_resetTimer.setObjectName(this->objectName().isEmpty() ? "CLedWidget::ResetTimer" : this->objectName() + "::ResetTimer"); } CLedWidget::~CLedWidget() - { } + { + m_resetTimer.stop(); + } void CLedWidget::setLed(LedColor ledColor) { - Q_ASSERT(!this->m_renderer.isNull()); + Q_ASSERT_X(!this->m_renderer.isNull(), Q_FUNC_INFO, "no renderer"); + if (!this->m_renderer) { return; } // load image, init renderer - QString ledShapeAndColor; - ledShapeAndColor = shapes().at(static_cast(this->m_shape)); + QString ledShapeAndColor(shapes().at(static_cast(this->m_shape))); if (ledColor == NoColor) { switch (m_value) @@ -84,7 +95,7 @@ namespace BlackGui this->setToolTip(this->m_currentToolTip); // for widget // init renderer, load led. - m_renderer->load(ledShapeAndColor); + m_renderer->load(ledShapeAndColor); // load by filename // original size QSize s = m_renderer->defaultSize(); @@ -117,8 +128,13 @@ namespace BlackGui const QString &CLedWidget::colorString(CLedWidget::LedColor color) { static const QString empty; - if (color == NoColor) return empty; - return colors().at(static_cast(color)); + if (color == NoColor) { return empty; } + return colorFiles().at(static_cast(color)); + } + + void CLedWidget::ps_resetState() + { + this->setOn(false); } void CLedWidget::setToolTips(const QString &on, const QString &off, const QString &triState) @@ -199,23 +215,32 @@ namespace BlackGui setLed(); } - QPixmap CLedWidget::asPixmap() const { return this->renderToPixmap(); } - void CLedWidget::setOn(bool on) + void CLedWidget::setOn(bool on, int resetTimeMs) { State s = on ? On : Off; - if (m_value == s) return; + if (resetTimeMs < 0 && m_resetTimer.isActive()) { m_resetTimer.stop();} + if (resetTimeMs > 0) + { + m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::ps_resetState); + } + if (m_value == s) { return; } m_value = s; setLed(); } - void CLedWidget::setTriState() + void CLedWidget::setTriState(int resetTimeMs) { - if (m_value == TriState) return; + if (resetTimeMs < 0 && m_resetTimer.isActive()) { m_resetTimer.stop();} + if (resetTimeMs > 0) + { + m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::ps_resetState); + } + if (m_value == TriState) { return; } m_value = TriState; setLed(); } @@ -258,7 +283,7 @@ namespace BlackGui return shapes; } - const QStringList &CLedWidget::colors() + const QStringList &CLedWidget::colorFiles() { static const QStringList colors( { "red.svg", "green.svg", "yellow.svg", "grey.svg", "orange.svg", "purple.svg", "blue.svg", "black.svg" }); return colors; @@ -269,4 +294,4 @@ namespace BlackGui static const QList widths({ 16, 16, 16, 16}); return widths; } -} +} // ns diff --git a/src/blackgui/led.h b/src/blackgui/led.h index 73b7131da..7ea914fee 100644 --- a/src/blackgui/led.h +++ b/src/blackgui/led.h @@ -19,6 +19,7 @@ #include #include #include +#include namespace BlackGui { @@ -31,9 +32,8 @@ namespace BlackGui Q_ENUMS(LedShape) public: - //! Colors - //! \remarks None has to be last entry + //! \remarks NoColor has to be last entry enum LedColor { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue, Black, NoColor}; //! Shapes @@ -55,10 +55,10 @@ namespace BlackGui bool value() const { return m_value; } //! Allows to set the led value {true, false} - void setOn(bool on); + void setOn(bool on, int resetTimeMs = -1); //! Sets the 3rd state - void setTriState(); + void setTriState(int resetTimeMs = -1); //! Toggle on / off void toggleValue(); @@ -134,11 +134,15 @@ namespace BlackGui int m_widthTarget = -1; //!< desired width int m_heightCalculated = 1; //!< calculated height - QString m_tooltipOn = "on"; //!< tooltip when on - QString m_tooltipOff = "off"; //!< tooltip when off + QString m_tooltipOn = "on"; //!< tooltip when on + QString m_tooltipOff = "off"; //!< tooltip when off QString m_tooltipTriState = "tri-state"; //!< tooltip tri state - QString m_currentToolTip = "off"; //!< currently used tooltip + QString m_currentToolTip = "off"; //!< currently used tooltip QScopedPointer m_renderer; //!< Renderer + QTimer m_resetTimer {this}; //!< reset state + + //! Init + void init(); //! Paint event virtual void paintEvent(QPaintEvent *event) override; @@ -155,15 +159,18 @@ namespace BlackGui //! All shapes static const QStringList &shapes(); - //! All colors - static const QStringList &colors(); + //! All color files + static const QStringList &colorFiles(); //! All target widths static const QList &widths(); //! Color string - static const QString &colorString(LedColor color); //!