From dd652a755a3c9622274abe171159e768465843d5 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 13 Oct 2017 19:49:56 +0200 Subject: [PATCH] Ref T171, fixed wrong reset state in LED (always off) - distinguish between value and state - protected -> private (no need for protected) --- src/blackgui/led.cpp | 41 ++++++++++++++++++++++++++--------------- src/blackgui/led.h | 14 +++++++------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/blackgui/led.cpp b/src/blackgui/led.cpp index a91c7858a..2aec0095b 100644 --- a/src/blackgui/led.cpp +++ b/src/blackgui/led.cpp @@ -33,7 +33,7 @@ namespace BlackGui 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_state(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(this)) { @@ -61,7 +61,7 @@ namespace BlackGui QString ledShapeAndColor(shapes().at(static_cast(this->m_shape))); if (ledColor == NoColor) { - switch (m_value) + switch (m_state) { case On: this->m_currentToolTip = this->m_tooltipOn; @@ -102,12 +102,12 @@ namespace BlackGui m_renderer->load(ledShapeAndColor); // load by filename // original size - QSize s = m_renderer->defaultSize(); + const QSize s = m_renderer->defaultSize(); this->m_whRatio = s.width() / s.height(); // size if (this->m_widthTarget < 0) { this->m_widthTarget = widths().at(static_cast(m_shape)); } - double h = this->m_widthTarget / this->m_whRatio; + const double h = this->m_widthTarget / this->m_whRatio; this->m_heightCalculated = qRound(h); this->setFixedHeight(this->m_heightCalculated); @@ -136,9 +136,11 @@ namespace BlackGui return colorFiles().at(static_cast(color)); } - void CLedWidget::ps_resetState() + void CLedWidget::resetState() { - this->setOn(false); + if (m_value == m_state) { return; } + m_state = m_value; + this->setLed(); } void CLedWidget::setToolTips(const QString &on, const QString &off, const QString &triState) @@ -227,36 +229,45 @@ namespace BlackGui void CLedWidget::setOn(bool on, int resetTimeMs) { State s = on ? On : Off; - if (resetTimeMs < 0 && m_resetTimer.isActive()) { m_resetTimer.stop();} if (resetTimeMs > 0) { - m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::ps_resetState); + m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::resetState); } - if (m_value == s) { return; } - m_value = s; + else + { + m_resetTimer.stop(); + m_value = s; + } + if (m_state == s) { return; } + m_state = s; setLed(); } void CLedWidget::blink(int resetTimeMs) { + m_value = Off; this->setOn(true, resetTimeMs); } void CLedWidget::setTriState(int resetTimeMs) { - if (resetTimeMs < 0 && m_resetTimer.isActive()) { m_resetTimer.stop();} if (resetTimeMs > 0) { - m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::ps_resetState); + m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::resetState); } - if (m_value == TriState) { return; } - m_value = TriState; + else + { + m_resetTimer.stop(); + m_value = TriState; + } + if (m_state == TriState) { return; } + m_state = TriState; setLed(); } void CLedWidget::toggleValue() { - m_value = (m_value == Off) ? On : Off; + m_state = (m_state == Off) ? On : Off; setLed(); } diff --git a/src/blackgui/led.h b/src/blackgui/led.h index 429ea00cc..0af6195aa 100644 --- a/src/blackgui/led.h +++ b/src/blackgui/led.h @@ -60,7 +60,7 @@ namespace BlackGui virtual ~CLedWidget(); //! Value - bool value() const { return m_value; } + bool value() const { return m_state; } //! Allows to set the led value {true, false} void setOn(bool on, int resetTimeMs = -1); @@ -135,8 +135,9 @@ namespace BlackGui //! LED clicked void clicked(); - protected: - State m_value = Off; //!< current value + private: + State m_state = Off; //!< current state, can be different from value when blinking + State m_value = Off; //!< explicit value LedColor m_colorOn = Yellow; //!< On color LedColor m_colorOff = Black; //!< Off color LedColor m_colorTriState = Blue; //!< tri-state color @@ -155,6 +156,9 @@ namespace BlackGui //! Init void init(); + //! Reset the state + void resetState(); + //! Paint event virtual void paintEvent(QPaintEvent *event) override; @@ -178,10 +182,6 @@ namespace BlackGui //! Color string static const QString &colorString(LedColor color); //!< Color string - - private slots: - //! Reset state - void ps_resetState(); }; } #endif