From 17a84258837b14b5fea8848f3d238fc21b331e9f Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 27 Jul 2014 15:08:52 +0200 Subject: [PATCH] refs #304, added QLed widget --- src/blackgui/led.cpp | 204 +++++++++++++++++++++++++++++++++++++++++++ src/blackgui/led.h | 133 ++++++++++++++++++++++++++++ 2 files changed, 337 insertions(+) create mode 100644 src/blackgui/led.cpp create mode 100644 src/blackgui/led.h diff --git a/src/blackgui/led.cpp b/src/blackgui/led.cpp new file mode 100644 index 000000000..5fb83e0fa --- /dev/null +++ b/src/blackgui/led.cpp @@ -0,0 +1,204 @@ +/*************************************************************************** + * 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 * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "led.h" + +namespace BlackGui +{ + CLed::CLed(QWidget *parent) : QWidget(parent) + { + this->setLed(); + } + + CLed::CLed(bool on, CLed::LedColors onColor, CLed::LedColors offColor, CLed::LedShapes shape, QWidget *parent) : + QWidget(parent), m_value(on), m_onColor(onColor), m_offColor(offColor), m_shape(shape) + { + this->setLed(); + } + + void CLed::setLed(LedColors tempColor) + { + // load image, init renderer + QString ledShapeAndColor; + ledShapeAndColor = shapes().at(static_cast(this->m_shape)); + if (tempColor == NoColor) + { + if (m_value) + { + this->setToolTip(this->m_tooltipOn); + ledShapeAndColor.append(CLed::colorString(this->m_onColor)); + } + else + { + this->setToolTip(this->m_tooltipOff); + ledShapeAndColor.append(CLed::colorString(this->m_offColor)); + } + } + else + { + this->setToolTip("transition"); + ledShapeAndColor.append(CLed::colorString(tempColor)); + } + + // init renderer, allow re-init + bool firstTime = false; + if (!m_renderer) + { + firstTime = true; + m_renderer = new QSvgRenderer(this); + } + + m_renderer->load(ledShapeAndColor); + + // original size + QSize s = m_renderer->defaultSize(); + this->m_whRatio = s.width() / s.height(); + + // size + if (this->m_targetWidth < 0) + { + this->m_targetWidth = 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)); + + if (!firstTime) + { + // re-init + this->update(); + } + } + + const QString &CLed::colorString(CLed::LedColors 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) + { + this->m_tooltipOn = on; + this->m_tooltipOff = off; + if (this->m_value) + { + this->setToolTip(on); + } + else + { + this->setToolTip(off); + } + } + + void CLed::setOnToolTip(const QString &on) + { + this->setToolTips(on, this->m_tooltipOff); + } + + void CLed::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); + } + + void CLed::setOnColor(LedColors color) + { + if (color == this->m_onColor) return; + m_onColor = color; + setLed(); + } + + void CLed::setOffColor(LedColors color) + { + if (color == this->m_offColor) return; + m_offColor = color; + setLed(); + } + + void CLed::setTemporaryColor(CLed::LedColors color) + { + setLed(color); + } + + void CLed::setShape(LedShapes newShape) + { + if (newShape == this->m_shape) return; + m_shape = newShape; + setLed(); + } + + void CLed::setValues(CLed::LedColors onColor, CLed::LedColors offColor, CLed::LedShapes shape, const QString &toolTipOn, const QString &toolTipOff, int width) + { + m_onColor = onColor; + m_offColor = offColor; + m_shape = shape; + m_tooltipOn = toolTipOn; + m_tooltipOff = toolTipOff; + m_targetWidth = width; + setLed(); + } + + void CLed::setValue(bool value) + { + if (value == m_value) return; + m_value = value; + setLed(); + } + + void CLed::toggleValue() + { + m_value = !m_value; + setLed(); + } + + const QStringList &CLed::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() + { + 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() + { + static const QList widths({ 16 , 16, 16, 16}); + return widths; + } +} diff --git a/src/blackgui/led.h b/src/blackgui/led.h new file mode 100644 index 000000000..06785c2e4 --- /dev/null +++ b/src/blackgui/led.h @@ -0,0 +1,133 @@ +/*************************************************************************** + * 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 * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. * + ***************************************************************************/ + +#ifndef BLACKGUI_LED_H +#define BLACKGUI_LED_H + +#include +#include +#include + +class QColor; +class QSvgRenderer; + +namespace BlackGui +{ + //! Displaying an LED + //! \remarks Based on qLed + class CLed : public QWidget + { + Q_OBJECT + Q_ENUMS(LedColors) + Q_ENUMS(LedShapes) + + //! Value on/off + Q_PROPERTY(bool value READ value WRITE setValue) + //! Color when on + Q_PROPERTY(LedColors onColor READ onColor WRITE setOnColor) + //! Color when off + Q_PROPERTY(LedColors offColor READ offColor WRITE setOffColor) + //! Shape + Q_PROPERTY(LedShapes shape READ shape WRITE setShape) + + public: + + //! Colors + //! \remarks None has to be last entry + enum LedColors { Red = 0, Green, Yellow, Grey, Orange, Purple, Blue, Black, NoColor}; + + //! Shapes + enum LedShapes { Circle = 0, Square, Triangle, Rounded}; + + //! Constructor + CLed(QWidget *parent = nullptr); + + //! Constructor + CLed(bool on, LedColors onColor, LedColors offColor, LedShapes shape, QWidget *parent = nullptr); + + //! Destructor + virtual ~CLed(); + + //! Value + bool value() const { return m_value; } + + //! On color + LedColors onColor() const { return m_onColor; } + + //! Off color + LedColors offColor() const { return m_offColor; } + + //! Shape + LedShapes shape() const { return m_shape; } + + //! Allows to set the led value {true,false} + void setValue(bool); + + //! Allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue} + void setOnColor(LedColors color); + + //! Allows to change the Off color {Red,Green,Yellow,Grey,Orange,Purple,blue} + void setOffColor(LedColors color); + + //! Temporary color until next value change + void setTemporaryColor(LedColors color); + + //! 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(LedShapes); + + //! Target width + void setTargetWidth(int width) { this->m_targetWidth = width; } + + //! New values + void setValues(LedColors onColor, LedColors offColor, LedShapes shape, const QString &toolTipOn, const QString &toolTipOff, int width = -1); + + //! Toggle on / off + void toggleValue(); + + protected: + bool m_value = false; //!< current value + LedColors m_onColor = Red; //!< On color + LedColors m_offColor = Grey; //!< Off color + LedShapes m_shape = Circle; //!< shape + double m_whRatio = 1.0; //!< width/height ratio + int m_targetWidth = -1; //!< TargetWidth + + //! All shapes + static const QStringList &shapes(); + + //! All colors + static const QStringList &colors(); + + //! All target widths + static const QList &widths(); + + //! Paint event + void paintEvent(QPaintEvent *event); + + private: + QSvgRenderer *m_renderer = nullptr; //!< Renderer + QString m_tooltipOn; + QString m_tooltipOff; + void setLed(LedColors tempColor = NoColor); //!< Init LED + static const QString &colorString(LedColors color); //!