swift
led.h
Go to the documentation of this file.
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 
6 
7 #ifndef SWIFT_GUI_LEDWIDGET_H
8 #define SWIFT_GUI_LEDWIDGET_H
9 
10 #include <QList>
11 #include <QObject>
12 #include <QPixmap>
13 #include <QScopedPointer>
14 #include <QString>
15 #include <QTimer>
16 #include <QWidget>
17 
18 #include "gui/swiftguiexport.h"
19 
20 class QMouseEvent;
21 class QPaintEvent;
22 class QSvgRenderer;
23 
24 namespace swift::gui
25 {
28  class SWIFT_GUI_EXPORT CLedWidget : public QWidget
29  {
30  Q_OBJECT
31 
32  public:
35  enum LedColor
36  {
37  Red = 0,
38  Green,
39  Yellow,
40  Grey,
41  Orange,
42  Purple,
43  Blue,
44  Black,
45  NoColor
46  };
47  Q_ENUM(LedColor)
48 
49 
50  enum LedShape
51  {
52  Circle = 0,
53  Square,
54  Triangle,
55  Rounded
56  };
57  Q_ENUM(LedShape)
58 
59 
60  enum State
61  {
62  On,
63  Off,
64  TriState
65  };
66 
68  CLedWidget(QWidget *parent = nullptr);
69 
71  CLedWidget(bool on, LedColor onColor, LedColor offColor, LedShape shape, const QString &onName = "on",
72  const QString &offName = "off", int targetWidth = -1, QWidget *parent = nullptr);
73 
75  virtual ~CLedWidget() override;
76 
78  bool value() const { return m_blinkState; }
79 
81  void setOn(bool on) { this->setOn(on, -1); }
82 
84  void blink(int resetTimeMs = 500);
85 
87  void setTriState(int resetTimeMs = -1);
88 
90  void toggleValue();
91 
93  LedShape shape() const { return m_shape; }
94 
96  LedColor onColor() const { return m_colorOn; }
97 
99  LedColor offColor() const { return m_colorOff; }
100 
102  LedColor triStateColor() const { return m_colorTriState; }
103 
105  void setOnColor(LedColor color);
106 
108  void setOffColor(LedColor color);
109 
111  void setTriStateColor(LedColor color);
112 
114  void setShape(LedShape);
115 
117  void setTargetWidth(int width)
118  {
119  this->m_widthTarget = width;
120  this->setLed();
121  }
122 
124  QString getOnToolTip() const { return m_tooltipOn; }
125 
127  QString getOffToolTip() const { return m_tooltipOff; }
128 
130  QString getTriStateToolTip() const { return m_tooltipTriState; }
131 
133  void setToolTips(const QString &on, const QString &off, const QString &triState = "tri-state");
134 
136  void setOnToolTip(const QString &on);
137 
139  void setOffToolTip(const QString &off);
140 
142  void setTriStateToolTip(const QString &triStateTooltip);
143 
145  void setTriStateValues(LedColor color, const QString &tooltip);
146 
148  void setValues(LedColor onColor, LedColor offColor, LedShape shape, const QString &toolTipOn,
149  const QString &toolTipOff, int width = -1);
150 
152  void setValues(LedColor onColor, LedColor offColor, LedColor triStateColor, LedShape shape,
153  const QString &toolTipOn, const QString &toolTipOff, const QString &toolTipTriState,
154  int width = -1);
155 
157  QPixmap asPixmap() const;
158 
159  signals:
161  void clicked();
162 
163  private:
164  State m_blinkState = Off;
165  State m_value = Off;
166  LedColor m_colorOn = Yellow;
167  LedColor m_colorOff = Black;
168  LedColor m_colorTriState = Blue;
169  LedShape m_shape = Circle;
170  double m_whRatio = 1.0;
171  int m_widthTarget = -1;
172  int m_heightCalculated = 1;
173 
174  QString m_tooltipOn = "on";
175  QString m_tooltipOff = "off";
176  QString m_tooltipTriState = "tri-state";
177  QString m_currentToolTip = "off";
178  QScopedPointer<QSvgRenderer> m_renderer;
179  QTimer m_resetTimer;
180 
182  void init();
183 
185  void setOn(bool on, int resetTimeMs);
186 
188  void resetState();
189 
191  virtual void paintEvent(QPaintEvent *event) override;
192 
194  virtual void mousePressEvent(QMouseEvent *event) override;
195 
197  void setLed(LedColor ledColor = NoColor);
198 
200  QPixmap renderToPixmap() const;
201 
203  static const QStringList &shapes();
204 
206  static const QStringList &colorFiles();
207 
209  static const QList<int> &widths();
210 
212  static const QString &colorString(LedColor color);
213  };
214 } // namespace swift::gui
215 #endif // SWIFT_GUI_LEDWIDGET_H
Displaying an LED as widget. Non copyable.
Definition: led.h:29
QString getTriStateToolTip() const
Tool tip.
Definition: led.h:130
LedShape shape() const
Shape.
Definition: led.h:93
void setOn(bool on)
Allows to set the led value {true, false}.
Definition: led.h:81
QString getOnToolTip() const
Tool tip.
Definition: led.h:124
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
LedShape
Shapes.
Definition: led.h:51
bool value() const
Value.
Definition: led.h:78
QString getOffToolTip() const
Tool tip.
Definition: led.h:127
void setTargetWidth(int width)
Target width.
Definition: led.h:117
LedColor offColor() const
Off color.
Definition: led.h:99
void clicked()
LED clicked.
GUI related classes.
#define SWIFT_GUI_EXPORT
Export a class or function from the library.