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<QSvgRenderer>
* Led, copyright
This commit is contained in:
Klaus Basan
2014-08-31 14:56:27 +02:00
parent f72ff4e495
commit 674a0b5665
6 changed files with 405 additions and 143 deletions

View File

@@ -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);

View File

@@ -56,7 +56,7 @@
</widget>
</item>
<item>
<widget class="BlackGui::CLed" name="led_Network" native="true"/>
<widget class="BlackGui::CLedWidget" name="led_Network" native="true"/>
</item>
<item>
<widget class="QLabel" name="lbl_Simulator">
@@ -75,7 +75,7 @@
</widget>
</item>
<item>
<widget class="BlackGui::CLed" name="led_Simulator" native="true">
<widget class="BlackGui::CLedWidget" name="led_Simulator" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
@@ -98,7 +98,7 @@
</widget>
</item>
<item>
<widget class="BlackGui::CLed" name="led_DBus" native="true">
<widget class="BlackGui::CLedWidget" name="led_DBus" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
@@ -124,7 +124,7 @@
</widget>
</item>
<item>
<widget class="BlackGui::CLed" name="led_Ptt" native="true"/>
<widget class="BlackGui::CLedWidget" name="led_Ptt" native="true"/>
</item>
<item>
<widget class="QLabel" name="lbl_Audio">
@@ -143,7 +143,7 @@
</widget>
</item>
<item>
<widget class="BlackGui::CLed" name="led_Audio" native="true"/>
<widget class="BlackGui::CLedWidget" name="led_Audio" native="true"/>
</item>
<item>
<spacer name="hs_InfoStatusBar">
@@ -162,7 +162,7 @@
</widget>
<customwidgets>
<customwidget>
<class>BlackGui::CLed</class>
<class>BlackGui::CLedWidget</class>
<extends>QWidget</extends>
<header>blackgui/led.h</header>
<container>1</container>

View File

@@ -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 <QPolygon>
#include <QtSvg>
#include <QSvgRenderer>
#include <QImage>
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<int>(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<int>(m_shape));
this->m_widthTarget = widths().at(static_cast<int>(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<int>(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<int> &CLed::widths()
const QList<int> &CLedWidget::widths()
{
static const QList<int> widths({ 16, 16, 16, 16});
return widths;

View File

@@ -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 <Qt>
#include <QWidget>
class QColor;
class QSvgRenderer;
#include <QSvgRenderer>
#include <QColor>
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<QSvgRenderer> 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<int> &widths();
//! Paint event
void paintEvent(QPaintEvent *event);
//! Color string
static const QString &colorString(LedColor color); //!<Color string
private:
QSvgRenderer *m_renderer = nullptr; //!< Renderer
QString m_tooltipOn;
QString m_tooltipOff;
void setLed(LedColor ledColor = NoColor); //!< Init LED
static const QString &colorString(LedColor color); //!<Color string
//! Fix widget after widths calculated
void firstTimeReInit();
};
}
#endif