From 674a0b56652a26a5142ed3be7358c56804d367bd Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 31 Aug 2014 14:56:27 +0200 Subject: [PATCH] During refs #319, refactoring of QLed (as it is used with a LED formatter) * renamed to CLedWidget * added missing SVG * rendering to pixmap possible * QScopedPointer * Led, copyright --- .../components/infobarstatuscomponent.cpp | 16 +- .../components/infobarstatuscomponent.ui | 12 +- src/blackgui/led.cpp | 161 ++++++----- src/blackgui/led.h | 103 +++---- src/blackmisc/blackmisc.qrc | 2 + src/blackmisc/icons/qled/round_black.svg | 254 ++++++++++++++++++ 6 files changed, 405 insertions(+), 143 deletions(-) create mode 100644 src/blackmisc/icons/qled/round_black.svg diff --git a/src/blackgui/components/infobarstatuscomponent.cpp b/src/blackgui/components/infobarstatuscomponent.cpp index 5f0a2c117..97e781923 100644 --- a/src/blackgui/components/infobarstatuscomponent.cpp +++ b/src/blackgui/components/infobarstatuscomponent.cpp @@ -40,14 +40,14 @@ namespace BlackGui void CInfoBarStatusComponent::initLeds() { - CLed::LedShape shape = CLed::Circle; - this->ui->led_DBus->setValues(CLed::Yellow, CLed::Black, shape, "DBus connected", "DBus disconnected", 14); - this->ui->led_Network->setValues(CLed::Yellow, CLed::Black, shape, "Network connected", "Network disconnected", 14); - this->ui->led_Simulator->setValues(CLed::Yellow, CLed::Black, shape, "Simulator connected", "Simulator disconnected", 14); + CLedWidget::LedShape shape = CLedWidget::Circle; + this->ui->led_DBus->setValues(CLedWidget::Yellow, CLedWidget::Black, shape, "DBus connected", "DBus disconnected", 14); + this->ui->led_Network->setValues(CLedWidget::Yellow, CLedWidget::Black, shape, "Network connected", "Network disconnected", 14); + this->ui->led_Simulator->setValues(CLedWidget::Yellow, CLedWidget::Black, shape, "Simulator connected", "Simulator disconnected", 14); - shape = CLed::Rounded; - this->ui->led_Ptt->setValues(CLed::Yellow, CLed::Red, shape, "Ptt", "Silence", 18); - this->ui->led_Audio->setValues(CLed::Yellow, CLed::Red, shape, "On", "Muted", 18); + shape = CLedWidget::Rounded; + this->ui->led_Ptt->setValues(CLedWidget::Yellow, CLedWidget::Red, shape, "Ptt", "Silence", 18); + this->ui->led_Audio->setValues(CLedWidget::Yellow, CLedWidget::Red, shape, "On", "Muted", 18); } void CInfoBarStatusComponent::setDBusStatus(bool dbus) @@ -125,7 +125,7 @@ namespace BlackGui this->ui->led_Network->setOn(true); break; case INetwork::Connecting: - this->ui->led_Network->setTemporaryColor(CLed::Yellow); + this->ui->led_Network->setTemporaryColor(CLedWidget::Yellow); break; default: this->ui->led_Network->setOn(false); diff --git a/src/blackgui/components/infobarstatuscomponent.ui b/src/blackgui/components/infobarstatuscomponent.ui index 38e13dac4..020ce3594 100644 --- a/src/blackgui/components/infobarstatuscomponent.ui +++ b/src/blackgui/components/infobarstatuscomponent.ui @@ -56,7 +56,7 @@ - + @@ -75,7 +75,7 @@ - + 0 @@ -98,7 +98,7 @@ - + 0 @@ -124,7 +124,7 @@ - + @@ -143,7 +143,7 @@ - + @@ -162,7 +162,7 @@ - BlackGui::CLed + BlackGui::CLedWidget QWidget
blackgui/led.h
1 diff --git a/src/blackgui/led.cpp b/src/blackgui/led.cpp index d6a43069e..b74f07015 100644 --- a/src/blackgui/led.cpp +++ b/src/blackgui/led.cpp @@ -6,12 +6,7 @@ * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. * - * Copyright (C) 2010 by P. Sereno - * http://www.sereno-online.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation + * Class based on qLed: Copyright (C) 2010 by P. Sereno, http://www.sereno-online.com */ #include "led.h" @@ -22,22 +17,29 @@ #include #include #include +#include namespace BlackGui { - CLed::CLed(QWidget *parent) : QWidget(parent) + CLedWidget::CLedWidget(QWidget *parent) : QWidget(parent), m_renderer(new QSvgRenderer) { this->setLed(); } - CLed::CLed(bool on, CLed::LedColor onColor, CLed::LedColor offColor, CLed::LedShape shape, QWidget *parent) : - QWidget(parent), m_value(on ? On : Off), m_onColor(onColor), m_offColor(offColor), m_shape(shape) + CLedWidget::CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName, const QString &offName, QWidget *parent) : + m_value(on ? On : Off), m_colorOn(onColor), m_colorOff(offColor), + m_shape(shape), m_tooltipOn(onName), m_tooltipOff(offName), m_renderer(new QSvgRenderer(parent)) { this->setLed(); } - void CLed::setLed(LedColor ledColor) + CLedWidget::~CLedWidget() + { } + + void CLedWidget::setLed(LedColor ledColor) { + Q_ASSERT(!this->m_renderer.isNull()); + // load image, init renderer QString ledShapeAndColor; ledShapeAndColor = shapes().at(static_cast(this->m_shape)); @@ -45,29 +47,23 @@ namespace BlackGui { if (m_value == On) { - this->setToolTip(this->m_tooltipOn); - ledShapeAndColor.append(CLed::colorString(this->m_onColor)); + this->m_currentToolTip = this->m_tooltipOn; + ledShapeAndColor.append(CLedWidget::colorString(this->m_colorOn)); } else { - this->setToolTip(this->m_tooltipOff); - ledShapeAndColor.append(CLed::colorString(this->m_offColor)); + this->m_currentToolTip = this->m_tooltipOff; + ledShapeAndColor.append(CLedWidget::colorString(this->m_colorOff)); } } else { - this->setToolTip("transition"); - ledShapeAndColor.append(CLed::colorString(ledColor)); - } - - // init renderer, allow re-init - bool firstTime = false; - if (!m_renderer) - { - firstTime = true; - m_renderer = new QSvgRenderer(this); + this->m_currentToolTip = "transition"; + ledShapeAndColor.append(CLedWidget::colorString(ledColor)); } + this->setToolTip(this->m_currentToolTip); // for widget + // init renderer, load led. m_renderer->load(ledShapeAndColor); // original size @@ -75,105 +71,95 @@ namespace BlackGui this->m_whRatio = s.width() / s.height(); // size - if (this->m_targetWidth < 0) + if (this->m_widthTarget < 0) { - this->m_targetWidth = widths().at(static_cast(m_shape)); + this->m_widthTarget = widths().at(static_cast(m_shape)); } - double w = this->m_targetWidth; - double h = w / this->m_whRatio; - this->setFixedHeight(qRound(h)); - this->setFixedWidth(qRound(w)); + double h = this->m_widthTarget / this->m_whRatio; + this->m_heightCalculated = qRound(h); - if (!firstTime) - { - // re-init widget (adjust size etc.) - this->update(); - } + this->setFixedHeight(this->m_heightCalculated); + this->setFixedWidth(this->m_widthTarget); + this->update(); } - const QString &CLed::colorString(CLed::LedColor color) + QPixmap CLedWidget::renderToPixmap() const + { + Q_ASSERT(!this->m_renderer.isNull()); + + // Prepare a QImage with desired characteritiscs + QImage image(QSize(this->m_widthTarget, this->m_heightCalculated), QImage::Format_ARGB32); + image.fill(qRgba(0, 0, 0, 0)); // transparent background + + // Get QPainter that paints to the image + QPainter painter(&image); + this->m_renderer->render(&painter); + return QPixmap::fromImage(image); + } + + const QString &CLedWidget::colorString(CLedWidget::LedColor color) { static const QString empty; if (color == NoColor) return empty; return colors().at(static_cast(color)); } - CLed::~CLed() - { - delete m_renderer; - } - - void CLed::setToolTips(const QString &on, const QString &off) + void CLedWidget::setToolTips(const QString &on, const QString &off) { this->m_tooltipOn = on; this->m_tooltipOff = off; - if (this->m_value) - { - this->setToolTip(on); - } - else - { - this->setToolTip(off); - } + this->setLed(); } - void CLed::setOnToolTip(const QString &on) + void CLedWidget::setOnToolTip(const QString &on) { this->setToolTips(on, this->m_tooltipOff); } - void CLed::paintEvent(QPaintEvent *) + void CLedWidget::setOnColor(LedColor color) { - // init style sheets with this widget - QStyleOption opt; - opt.init(this); - - // paint - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing, true); - painter.setBackgroundMode(Qt::TransparentMode); - m_renderer->render(&painter); - } - - void CLed::setOnColor(LedColor color) - { - if (color == this->m_onColor) return; - m_onColor = color; + if (color == this->m_colorOn) return; + m_colorOn = color; setLed(); } - void CLed::setOffColor(LedColor color) + void CLedWidget::setOffColor(LedColor color) { - if (color == this->m_offColor) return; - m_offColor = color; + if (color == this->m_colorOff) return; + m_colorOff = color; setLed(); } - void CLed::setTemporaryColor(CLed::LedColor color) + void CLedWidget::setTemporaryColor(CLedWidget::LedColor color) { m_value = Temporary; setLed(color); } - void CLed::setShape(LedShape newShape) + void CLedWidget::setShape(LedShape newShape) { if (newShape == this->m_shape) return; m_shape = newShape; setLed(); } - void CLed::setValues(CLed::LedColor onColor, CLed::LedColor offColor, CLed::LedShape shape, const QString &toolTipOn, const QString &toolTipOff, int width) + void CLedWidget::setValues(LedColor onColor, LedColor offColor, LedShape shape, const QString &toolTipOn, const QString &toolTipOff, int width) { - m_onColor = onColor; - m_offColor = offColor; + m_colorOn = onColor; + m_colorOff = offColor; m_shape = shape; m_tooltipOn = toolTipOn; m_tooltipOff = toolTipOff; - m_targetWidth = width; + m_widthTarget = width; setLed(); } - void CLed::setOn(bool on) + QPixmap CLedWidget::asPixmap() const + { + return this->renderToPixmap(); + } + + void CLedWidget::setOn(bool on) { State s = on ? On : Off; if (m_value == s) return; @@ -181,7 +167,7 @@ namespace BlackGui setLed(); } - void CLed::toggleValue() + void CLedWidget::toggleValue() { if (m_value == Temporary || m_value == On) { @@ -194,19 +180,32 @@ namespace BlackGui setLed(); } - const QStringList &CLed::shapes() + void CLedWidget::paintEvent(QPaintEvent *) + { + // init style sheets with this widget + QStyleOption opt; + opt.init(this); + + // paint + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setBackgroundMode(Qt::TransparentMode); + m_renderer->render(&painter); + } + + const QStringList &CLedWidget::shapes() { static const QStringList shapes( {":/qled/icons/qled/circle_" , ":/qled/icons/qled/square_" , ":/qled/icons/qled/triang_" , ":/qled/icons/qled/round_"}); return shapes; } - const QStringList &CLed::colors() + const QStringList &CLedWidget::colors() { static const QStringList colors( { "red.svg", "green.svg", "yellow.svg", "grey.svg", "orange.svg", "purple.svg", "blue.svg", "black.svg" }); return colors; } - const QList &CLed::widths() + const QList &CLedWidget::widths() { static const QList widths({ 16, 16, 16, 16}); return widths; diff --git a/src/blackgui/led.h b/src/blackgui/led.h index 0be43d191..5abb2b72b 100644 --- a/src/blackgui/led.h +++ b/src/blackgui/led.h @@ -6,12 +6,7 @@ * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. * - * Copyright (C) 2010 by P. Sereno - * http://www.sereno-online.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation + * Class based on qLed: Copyright (C) 2010 by P. Sereno, http://www.sereno-online.com */ //! \file @@ -21,29 +16,19 @@ #include #include - -class QColor; -class QSvgRenderer; +#include +#include namespace BlackGui { - //! Displaying an LED + //! Displaying an LED as widget. Non copyable. //! \remarks Based on qLed - class CLed : public QWidget + class CLedWidget : public QWidget { Q_OBJECT Q_ENUMS(LedColor) Q_ENUMS(LedShape) - //! Value on/off - Q_PROPERTY(bool value READ value WRITE setOn) - //! Color when on - Q_PROPERTY(LedColor onColor READ onColor WRITE setOnColor) - //! Color when off - Q_PROPERTY(LedColor offColor READ offColor WRITE setOffColor) - //! Shape - Q_PROPERTY(LedShape shape READ shape WRITE setShape) - public: //! Colors @@ -56,29 +41,32 @@ namespace BlackGui //! States enum State { On, Off, Temporary }; - //! Constructor - CLed(QWidget *parent = nullptr); + //! Default constructor + CLedWidget(QWidget *parent = nullptr); //! Constructor - CLed(bool on, LedColor onColor, LedColor offColor, LedShape shape, QWidget *parent = nullptr); + CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName = "on", const QString &offName = "off", QWidget *parent = nullptr); //! Destructor - virtual ~CLed(); + virtual ~CLedWidget(); //! Value bool value() const { return m_value; } - //! On color - LedColor onColor() const { return m_onColor; } + //! Allows to set the led value {true,false} + void setOn(bool on); - //! Off color - LedColor offColor() const { return m_offColor; } + //! Toggle on / off + void toggleValue(); //! Shape LedShape shape() const { return m_shape; } - //! Allows to set the led value {true,false} - void setOn(bool on); + //! On color + LedColor onColor() const { return m_colorOn; } + + //! Off color + LedColor offColor() const { return m_colorOff; } //! Allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue} void setOnColor(LedColor color); @@ -89,31 +77,52 @@ namespace BlackGui //! Temporary color until next value change void setTemporaryColor(LedColor color); + //! Allows to change the led shape {Circle,Square,Triangle,Rounded rectangle} + void setShape(LedShape); + + //! Target width + void setTargetWidth(int width) { this->m_widthTarget = width; } + + //! Tool tip + QString getOnToolTip() const { return m_tooltipOn; } + + //! Tool tip + QString getOffToolTip() const { return m_tooltipOff; } + //! Tool tips void setToolTips(const QString &on, const QString &off); //! On tool tip void setOnToolTip(const QString &on); - //! Allows to change the led shape {Circle,Square,Triangle,Rounded rectangle} - void setShape(LedShape); - - //! Target width - void setTargetWidth(int width) { this->m_targetWidth = width; } - //! New values void setValues(LedColor onColor, LedColor offColor, LedShape shape, const QString &toolTipOn, const QString &toolTipOff, int width = -1); - //! Toggle on / off - void toggleValue(); + //! Render as pixmap, so it can be used with TableViews + QPixmap asPixmap() const; protected: State m_value = Off; //!< current value - LedColor m_onColor = Red; //!< On color - LedColor m_offColor = Grey; //!< Off color + LedColor m_colorOn = Red; //!< On color + LedColor m_colorOff = Grey; //!< Off color LedShape m_shape = Circle; //!< shape double m_whRatio = 1.0; //!< width/height ratio - int m_targetWidth = -1; //!< TargetWidth + int m_widthTarget = -1; //!< desired width + int m_heightCalculated = 1; //!< calculated height + + QString m_tooltipOn; //!< tooltip when on + QString m_tooltipOff; //!< tooltip when off + QString m_currentToolTip; //!< currently used tooltip + QScopedPointer m_renderer; //!< Renderer + + //! Paint event + virtual void paintEvent(QPaintEvent *event) override; + + //! Set / init LED + void setLed(LedColor ledColor = NoColor); + + //! Render to pixmap + QPixmap renderToPixmap() const; //! All shapes static const QStringList &shapes(); @@ -124,15 +133,13 @@ namespace BlackGui //! All target widths static const QList &widths(); - //! Paint event - void paintEvent(QPaintEvent *event); + //! Color string + static const QString &colorString(LedColor color); //!icons/qled/triang_purple.svg icons/qled/triang_red.svg icons/qled/triang_yellow.svg + icons/qled/round_black.svg + diff --git a/src/blackmisc/icons/qled/round_black.svg b/src/blackmisc/icons/qled/round_black.svg new file mode 100644 index 000000000..856a19afc --- /dev/null +++ b/src/blackmisc/icons/qled/round_black.svg @@ -0,0 +1,254 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + Openclipart + + + led rounded h black + 2009-11-04T15:27:55 + Glossy black button with rounded corners by Jean-Victor Balin. From OCAL 0.18 release. + https://openclipart.org/detail/28078/led-rounded-h-black-by-anonymous + + + Anonymous + + + + + black + button + glossy + + + + + + + + + + +