swift
led.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 // Class based on qLed: Copyright (C) 2010 by P. Sereno, http://www.sereno-online.com
4 
5 #include "gui/led.h"
6 
7 #include <QImage>
8 #include <QList>
9 #include <QMouseEvent>
10 #include <QPainter>
11 #include <QPointer>
12 #include <QRgb>
13 #include <QSize>
14 #include <QStringList>
15 #include <QStyleOption>
16 #include <QSvgRenderer>
17 #include <Qt>
18 #include <QtGlobal>
19 
20 namespace swift::gui
21 {
22  CLedWidget::CLedWidget(QWidget *parent) : QWidget(parent), m_renderer(new QSvgRenderer)
23  {
24  this->setLed();
25  this->init();
26  }
27 
28  CLedWidget::CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName,
29  const QString &offName, int targetWidth, QWidget *parent)
30  : QWidget(parent), m_blinkState(on ? On : Off), m_colorOn(onColor), m_colorOff(offColor), m_shape(shape),
31  m_widthTarget(targetWidth), m_tooltipOn(onName), m_tooltipOff(offName), m_renderer(new QSvgRenderer(this))
32  {
33  this->setLed();
34  this->init();
35  }
36 
37  void CLedWidget::init()
38  {
39  m_resetTimer.setSingleShot(true);
40  m_resetTimer.setObjectName(this->objectName().isEmpty() ? "CLedWidget::ResetTimer" :
41  this->objectName() + "::ResetTimer");
42  }
43 
44  CLedWidget::~CLedWidget() { m_resetTimer.stop(); }
45 
46  void CLedWidget::setLed(LedColor ledColor)
47  {
48  Q_ASSERT_X(!m_renderer.isNull(), Q_FUNC_INFO, "no renderer");
49  if (!m_renderer) { return; }
50 
51  // load image, init renderer
52  QString ledShapeAndColor(shapes().at(static_cast<int>(m_shape)));
53  if (ledColor == NoColor)
54  {
55  switch (m_blinkState)
56  {
57  case On:
58  m_currentToolTip = m_tooltipOn;
59  ledShapeAndColor.append(CLedWidget::colorString(m_colorOn));
60  break;
61  case TriState:
62  m_currentToolTip = m_tooltipTriState;
63  ledShapeAndColor.append(CLedWidget::colorString(m_colorTriState));
64  break;
65  case Off:
66  default:
67  m_currentToolTip = m_tooltipOff;
68  ledShapeAndColor.append(CLedWidget::colorString(m_colorOff));
69  break;
70  }
71  }
72  else
73  {
74  if (ledColor == m_colorOn)
75  {
76  m_currentToolTip = m_tooltipOn;
77  ledShapeAndColor.append(CLedWidget::colorString(m_colorOn));
78  }
79  else if (ledColor == m_colorOff)
80  {
81  m_currentToolTip = m_tooltipOff;
82  ledShapeAndColor.append(CLedWidget::colorString(m_colorOff));
83  }
84  else
85  {
86  m_currentToolTip = m_tooltipTriState;
87  ledShapeAndColor.append(CLedWidget::colorString(m_colorTriState));
88  }
89  }
90  this->setToolTip(m_currentToolTip); // for widget
91 
92  // init renderer, load led.
93  m_renderer->load(ledShapeAndColor); // load by filename
94 
95  // original size
96  const QSize s = m_renderer->defaultSize();
97  m_whRatio = s.width() / s.height();
98 
99  // size
100  if (m_widthTarget < 0) { m_widthTarget = widths().at(static_cast<int>(m_shape)); }
101  const double h = m_widthTarget / m_whRatio;
102  m_heightCalculated = qRound(h);
103 
104  this->setFixedHeight(m_heightCalculated);
105  this->setFixedWidth(m_widthTarget);
106  this->update();
107  }
108 
109  QPixmap CLedWidget::renderToPixmap() const
110  {
111  Q_ASSERT(!m_renderer.isNull());
112 
113  // Prepare a QImage with desired characteritiscs
114  QImage image(QSize(m_widthTarget, m_heightCalculated), QImage::Format_ARGB32);
115  image.fill(qRgba(0, 0, 0, 0)); // transparent background
116 
117  // Get QPainter that paints to the image
118  QPainter painter(&image);
119  m_renderer->render(&painter);
120  return QPixmap::fromImage(image);
121  }
122 
123  const QString &CLedWidget::colorString(CLedWidget::LedColor color)
124  {
125  static const QString empty;
126  if (color == NoColor) { return empty; }
127  return colorFiles().at(static_cast<int>(color));
128  }
129 
130  void CLedWidget::resetState()
131  {
132  if (m_value == m_blinkState) { return; }
133  m_blinkState = m_value;
134  this->setLed();
135  }
136 
137  void CLedWidget::setToolTips(const QString &on, const QString &off, const QString &triState)
138  {
139  m_tooltipOn = on;
140  m_tooltipOff = off;
141  m_tooltipTriState = triState;
142  this->setLed();
143  }
144 
145  void CLedWidget::setOnToolTip(const QString &on)
146  {
147  m_tooltipOn = on;
148  this->setLed();
149  }
150 
151  void CLedWidget::setOffToolTip(const QString &off)
152  {
153  m_tooltipOff = off;
154  this->setLed();
155  }
156 
157  void CLedWidget::setTriStateToolTip(const QString &triStateTooltip)
158  {
159  m_tooltipTriState = triStateTooltip;
160  this->setLed();
161  }
162 
163  void CLedWidget::setTriStateValues(CLedWidget::LedColor color, const QString &tooltip)
164  {
165  m_tooltipTriState = tooltip;
166  m_colorTriState = color;
167  }
168 
170  {
171  if (color == m_colorOn) return;
172  m_colorOn = color;
173  this->setLed();
174  }
175 
177  {
178  if (color == m_colorOff) return;
179  m_colorOff = color;
180  this->setLed();
181  }
182 
184  {
185  if (color == m_colorOff) return;
186  m_colorTriState = color;
187  this->setLed();
188  }
189 
191  {
192  if (newShape == m_shape) return;
193  m_shape = newShape;
194  this->setLed();
195  }
196 
197  void CLedWidget::setValues(LedColor onColor, LedColor offColor, LedShape shape, const QString &toolTipOn,
198  const QString &toolTipOff, int width)
199  {
200  m_colorOn = onColor;
201  m_colorOff = offColor;
202  m_shape = shape;
203  m_tooltipOn = toolTipOn;
204  m_tooltipOff = toolTipOff;
205  m_widthTarget = width;
206  this->setLed();
207  }
208 
209  void CLedWidget::setValues(LedColor onColor, LedColor offColor, LedColor triStateColor, LedShape shape,
210  const QString &toolTipOn, const QString &toolTipOff, const QString &toolTipTriState,
211  int width)
212  {
213  m_colorOn = onColor;
214  m_colorOff = offColor;
215  m_colorTriState = triStateColor;
216  m_shape = shape;
217  m_tooltipOn = toolTipOn;
218  m_tooltipOff = toolTipOff;
219  m_tooltipTriState = toolTipTriState;
220  m_widthTarget = width;
221  this->setLed();
222  }
223 
224  QPixmap CLedWidget::asPixmap() const { return this->renderToPixmap(); }
225 
226  void CLedWidget::setOn(bool on, int resetTimeMs)
227  {
228  State s = on ? On : Off;
229  if (resetTimeMs > 0)
230  {
231  QPointer<CLedWidget> myself(this);
232  m_resetTimer.singleShot(resetTimeMs, this, [=] {
233  if (!myself) { return; }
234  this->resetState();
235  });
236  }
237  else
238  {
239  m_resetTimer.stop();
240  m_value = s;
241  }
242  if (m_blinkState == s) { return; }
243  m_blinkState = s;
244  this->setLed();
245  }
246 
247  void CLedWidget::blink(int resetTimeMs)
248  {
249  m_value = Off;
250  this->setOn(true, resetTimeMs);
251  }
252 
253  void CLedWidget::setTriState(int resetTimeMs)
254  {
255  if (resetTimeMs > 0) { m_resetTimer.singleShot(resetTimeMs, this, &CLedWidget::resetState); }
256  else
257  {
258  m_resetTimer.stop();
259  m_value = TriState;
260  }
261  if (m_blinkState == TriState) { return; }
262  m_blinkState = TriState;
263  this->setLed();
264  }
265 
267  {
268  m_blinkState = (m_blinkState == Off) ? On : Off;
269  this->setLed();
270  }
271 
272  void CLedWidget::paintEvent(QPaintEvent *)
273  {
274  // init style sheets with this widget
275  QStyleOption opt;
276  opt.initFrom(this);
277 
278  // paint
279  QPainter painter(this);
280  painter.setRenderHint(QPainter::Antialiasing, true);
281  painter.setBackgroundMode(Qt::TransparentMode);
282  m_renderer->render(&painter);
283  }
284 
285  void CLedWidget::mousePressEvent(QMouseEvent *event)
286  {
287  if (event->button() == Qt::LeftButton)
288  {
289  emit clicked();
290  event->accept();
291  }
292  else { QWidget::mousePressEvent(event); }
293  }
294 
295  const QStringList &CLedWidget::shapes()
296  {
297  static const QStringList shapes({ ":/qled/icons/qled/circle_", ":/qled/icons/qled/square_",
298  ":/qled/icons/qled/triang_", ":/qled/icons/qled/round_" });
299  return shapes;
300  }
301 
302  const QStringList &CLedWidget::colorFiles()
303  {
304  static const QStringList colors(
305  { "red.svg", "green.svg", "yellow.svg", "grey.svg", "orange.svg", "purple.svg", "blue.svg", "black.svg" });
306  return colors;
307  }
308 
309  const QList<int> &CLedWidget::widths()
310  {
311  static const QList<int> widths({ 16, 16, 16, 16 });
312  return widths;
313  }
314 } // namespace swift::gui
void setTriState(int resetTimeMs=-1)
Sets the 3rd state.
Definition: led.cpp:253
void setValues(LedColor onColor, LedColor offColor, LedShape shape, const QString &toolTipOn, const QString &toolTipOff, int width=-1)
New values dual state.
Definition: led.cpp:197
LedShape shape() const
Shape.
Definition: led.h:93
void setTriStateValues(LedColor color, const QString &tooltip)
Tri-state.
Definition: led.cpp:163
void setOn(bool on)
Allows to set the led value {true, false}.
Definition: led.h:81
void setOnColor(LedColor color)
Allows to change the On color {Red,Green,Yellow,Grey,Orange,Purple,blue}.
Definition: led.cpp:169
CLedWidget(QWidget *parent=nullptr)
Default constructor.
Definition: led.cpp:22
QPixmap asPixmap() const
Render as pixmap, so it can be used with TableViews.
Definition: led.cpp:224
virtual void mousePressEvent(QMouseEvent *event)
Mouse pressed.
Definition: led.cpp:285
void blink(int resetTimeMs=500)
Set to on for resetTimeMs.
Definition: led.cpp:247
void setOffToolTip(const QString &off)
Off tool tip.
Definition: led.cpp:151
LedColor triStateColor() const
Tri-state color.
Definition: led.h:102
LedColor onColor() const
On color.
Definition: led.h:96
LedColor
Colors.
Definition: led.h:36
void setTriStateColor(LedColor color)
Temporary color until next value change.
Definition: led.cpp:183
void setOffColor(LedColor color)
Allows to change the Off color {Red,Green,Yellow,Grey,Orange,Purple,blue}.
Definition: led.cpp:176
void setOnToolTip(const QString &on)
On tool tip.
Definition: led.cpp:145
LedShape
Shapes.
Definition: led.h:51
void setShape(LedShape)
Allows to change the led shape {Circle,Square,Triangle,Rounded rectangle}.
Definition: led.cpp:190
virtual void paintEvent(QPaintEvent *event)
Paint event.
Definition: led.cpp:272
virtual ~CLedWidget()
Destructor.
Definition: led.cpp:44
LedColor offColor() const
Off color.
Definition: led.h:99
void toggleValue()
Toggle on / off.
Definition: led.cpp:266
void clicked()
LED clicked.
void setToolTips(const QString &on, const QString &off, const QString &triState="tri-state")
Tool tips.
Definition: led.cpp:137
void setTriStateToolTip(const QString &triStateTooltip)
Tri-state tool tip.
Definition: led.cpp:157
GUI related classes.