refs #452 updated LED with timer based reset state

This commit is contained in:
Klaus Basan
2015-09-23 19:54:52 +02:00
committed by Mathew Sutcliffe
parent a39f454334
commit 933ffea201
2 changed files with 57 additions and 25 deletions

View File

@@ -24,26 +24,37 @@ namespace BlackGui
CLedWidget::CLedWidget(QWidget *parent) : QWidget(parent), m_renderer(new QSvgRenderer) CLedWidget::CLedWidget(QWidget *parent) : QWidget(parent), m_renderer(new QSvgRenderer)
{ {
this->setLed(); this->setLed();
this->init();
} }
CLedWidget::CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName, const QString &offName, int targetWidth, QWidget *parent) : 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_value(on ? On : Off), m_colorOn(onColor), m_colorOff(offColor),
m_shape(shape), m_widthTarget(targetWidth), m_tooltipOn(onName), m_tooltipOff(offName), 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->setLed();
this->init();
}
void CLedWidget::init()
{
m_resetTimer.setSingleShot(true);
m_resetTimer.setObjectName(this->objectName().isEmpty() ? "CLedWidget::ResetTimer" : this->objectName() + "::ResetTimer");
} }
CLedWidget::~CLedWidget() CLedWidget::~CLedWidget()
{ } {
m_resetTimer.stop();
}
void CLedWidget::setLed(LedColor ledColor) 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 // load image, init renderer
QString ledShapeAndColor; QString ledShapeAndColor(shapes().at(static_cast<int>(this->m_shape)));
ledShapeAndColor = shapes().at(static_cast<int>(this->m_shape));
if (ledColor == NoColor) if (ledColor == NoColor)
{ {
switch (m_value) switch (m_value)
@@ -84,7 +95,7 @@ namespace BlackGui
this->setToolTip(this->m_currentToolTip); // for widget this->setToolTip(this->m_currentToolTip); // for widget
// init renderer, load led. // init renderer, load led.
m_renderer->load(ledShapeAndColor); m_renderer->load(ledShapeAndColor); // load by filename
// original size // original size
QSize s = m_renderer->defaultSize(); QSize s = m_renderer->defaultSize();
@@ -117,8 +128,13 @@ namespace BlackGui
const QString &CLedWidget::colorString(CLedWidget::LedColor color) const QString &CLedWidget::colorString(CLedWidget::LedColor color)
{ {
static const QString empty; static const QString empty;
if (color == NoColor) return empty; if (color == NoColor) { return empty; }
return colors().at(static_cast<int>(color)); return colorFiles().at(static_cast<int>(color));
}
void CLedWidget::ps_resetState()
{
this->setOn(false);
} }
void CLedWidget::setToolTips(const QString &on, const QString &off, const QString &triState) void CLedWidget::setToolTips(const QString &on, const QString &off, const QString &triState)
@@ -199,23 +215,32 @@ namespace BlackGui
setLed(); setLed();
} }
QPixmap CLedWidget::asPixmap() const QPixmap CLedWidget::asPixmap() const
{ {
return this->renderToPixmap(); return this->renderToPixmap();
} }
void CLedWidget::setOn(bool on) void CLedWidget::setOn(bool on, int resetTimeMs)
{ {
State s = on ? On : Off; 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; m_value = s;
setLed(); 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; m_value = TriState;
setLed(); setLed();
} }
@@ -258,7 +283,7 @@ namespace BlackGui
return shapes; 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" }); static const QStringList colors( { "red.svg", "green.svg", "yellow.svg", "grey.svg", "orange.svg", "purple.svg", "blue.svg", "black.svg" });
return colors; return colors;
@@ -269,4 +294,4 @@ namespace BlackGui
static const QList<int> widths({ 16, 16, 16, 16}); static const QList<int> widths({ 16, 16, 16, 16});
return widths; return widths;
} }
} } // ns

View File

@@ -19,6 +19,7 @@
#include <QWidget> #include <QWidget>
#include <QSvgRenderer> #include <QSvgRenderer>
#include <QColor> #include <QColor>
#include <QTimer>
namespace BlackGui namespace BlackGui
{ {
@@ -31,9 +32,8 @@ namespace BlackGui
Q_ENUMS(LedShape) Q_ENUMS(LedShape)
public: public:
//! Colors //! 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}; enum LedColor { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue, Black, NoColor};
//! Shapes //! Shapes
@@ -55,10 +55,10 @@ namespace BlackGui
bool value() const { return m_value; } bool value() const { return m_value; }
//! Allows to set the led value {true, false} //! Allows to set the led value {true, false}
void setOn(bool on); void setOn(bool on, int resetTimeMs = -1);
//! Sets the 3rd state //! Sets the 3rd state
void setTriState(); void setTriState(int resetTimeMs = -1);
//! Toggle on / off //! Toggle on / off
void toggleValue(); void toggleValue();
@@ -139,6 +139,10 @@ namespace BlackGui
QString m_tooltipTriState = "tri-state"; //!< tooltip tri state QString m_tooltipTriState = "tri-state"; //!< tooltip tri state
QString m_currentToolTip = "off"; //!< currently used tooltip QString m_currentToolTip = "off"; //!< currently used tooltip
QScopedPointer<QSvgRenderer> m_renderer; //!< Renderer QScopedPointer<QSvgRenderer> m_renderer; //!< Renderer
QTimer m_resetTimer {this}; //!< reset state
//! Init
void init();
//! Paint event //! Paint event
virtual void paintEvent(QPaintEvent *event) override; virtual void paintEvent(QPaintEvent *event) override;
@@ -155,8 +159,8 @@ namespace BlackGui
//! All shapes //! All shapes
static const QStringList &shapes(); static const QStringList &shapes();
//! All colors //! All color files
static const QStringList &colors(); static const QStringList &colorFiles();
//! All target widths //! All target widths
static const QList<int> &widths(); static const QList<int> &widths();
@@ -164,6 +168,9 @@ namespace BlackGui
//! Color string //! Color string
static const QString &colorString(LedColor color); //!< Color string static const QString &colorString(LedColor color); //!< Color string
private slots:
//! Reset state
void ps_resetState();
}; };
} }
#endif #endif