QtIndustrialWidgets 2.3.0
Modern, High-Performance Industrial Instruments & SCADA Widgets for Qt
Loading...
Searching...
No Matches
IndustrialSwitch.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Paolo Sereno <paolomsereno@gmail.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#pragma once
8
10#include <QtWidgets/QWidget>
11#include <QtGui/QColor>
12#include <memory>
13
35namespace QtIndustrialWidgets {
36
37class IndustrialSwitchPrivate;
38
40{
41 Q_OBJECT
42
43 Q_PROPERTY(SwitchType switchType READ switchType WRITE setSwitchType NOTIFY appearanceChanged)
44 Q_PROPERTY(int positionCount READ positionCount WRITE setPositionCount NOTIFY appearanceChanged)
45 Q_PROPERTY(int position READ position WRITE setPosition NOTIFY positionChanged)
46 Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
47 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY appearanceChanged)
48 Q_PROPERTY(bool hasSafetyGuard READ hasSafetyGuard WRITE setHasSafetyGuard NOTIFY appearanceChanged)
49 Q_PROPERTY(bool isGuardOpen READ isGuardOpen WRITE setGuardOpen NOTIFY guardToggled)
50 Q_PROPERTY(bool animated READ isAnimated WRITE setAnimated NOTIFY appearanceChanged)
51 Q_PROPERTY(bool hasLed READ hasLed WRITE setHasLed NOTIFY appearanceChanged)
52 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY appearanceChanged)
53 Q_PROPERTY(QString labelOff READ labelOff WRITE setLabelOff NOTIFY appearanceChanged)
54 Q_PROPERTY(QString labelOn READ labelOn WRITE setLabelOn NOTIFY appearanceChanged)
55 Q_PROPERTY(QString labelCenter READ labelCenter WRITE setLabelCenter NOTIFY appearanceChanged)
56 Q_PROPERTY(QColor plateColor READ plateColor WRITE setPlateColor NOTIFY appearanceChanged)
57 Q_PROPERTY(QColor leverColor READ leverColor WRITE setLeverColor NOTIFY appearanceChanged)
58 Q_PROPERTY(QColor ledColor READ ledColor WRITE setLedColor NOTIFY appearanceChanged)
59 Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY appearanceChanged)
60 Q_PROPERTY(QColor guardColor READ guardColor WRITE setGuardColor NOTIFY appearanceChanged)
61
62public:
64 enum class SwitchType {
65 ToggleLever,
66 Rocker
67 };
68 Q_ENUM(SwitchType)
69
70
74 explicit IndustrialSwitch(QWidget *parent = nullptr);
75 ~IndustrialSwitch() override;
76
78 [[nodiscard]] SwitchType switchType() const;
80 [[nodiscard]] int positionCount() const;
82 [[nodiscard]] int position() const;
84 [[nodiscard]] bool isChecked() const;
86 [[nodiscard]] Qt::Orientation orientation() const;
88 [[nodiscard]] bool hasSafetyGuard() const;
90 [[nodiscard]] bool isGuardOpen() const;
92 [[nodiscard]] bool isAnimated() const;
94 [[nodiscard]] bool hasLed() const;
96 [[nodiscard]] QString label() const;
98 [[nodiscard]] QString labelOff() const;
100 [[nodiscard]] QString labelOn() const;
102 [[nodiscard]] QString labelCenter() const;
103
105 [[nodiscard]] QColor plateColor() const;
107 [[nodiscard]] QColor leverColor() const;
109 [[nodiscard]] QColor ledColor() const;
111 [[nodiscard]] QColor textColor() const;
113 [[nodiscard]] QColor guardColor() const;
114
120 [[nodiscard]] QSize sizeHint() const override;
128 [[nodiscard]] QSize minimumSizeHint() const override;
129
130public Q_SLOTS:
132 void setSwitchType(SwitchType type);
134 void setPositionCount(int count);
136 void setPosition(int position);
138 void setChecked(bool checked);
140 void toggle();
142 void setOrientation(Qt::Orientation orientation);
144 void setHasSafetyGuard(bool guard);
146 void setGuardOpen(bool open);
148 void setAnimated(bool animated);
150 void setHasLed(bool hasLed);
152 void setLabel(const QString &label);
154 void setLabelOff(const QString &label);
156 void setLabelOn(const QString &label);
158 void setLabelCenter(const QString &label);
160 void setPlateColor(const QColor &color);
162 void setLeverColor(const QColor &color);
164 void setLedColor(const QColor &color);
166 void setTextColor(const QColor &color);
168 void setGuardColor(const QColor &color);
169
170Q_SIGNALS:
172 void positionChanged(int position);
174 void toggled(bool checked);
176 void guardToggled(bool isOpen);
178 void appearanceChanged();
179
180protected:
181 void paintEvent(QPaintEvent *event) override;
182 void resizeEvent(QResizeEvent *event) override;
183 void changeEvent(QEvent *event) override;
184 void mousePressEvent(QMouseEvent *event) override;
185 void mouseMoveEvent(QMouseEvent *event) override;
186 void mouseReleaseEvent(QMouseEvent *event) override;
187 void keyPressEvent(QKeyEvent *event) override;
188 void wheelEvent(QWheelEvent *event) override;
189
190private:
191 void renderStaticBackground();
192 void drawToggleLever(QPainter &painter, const QRectF &switchArea, double currentPos);
193 void drawRocker(QPainter &painter, const QRectF &switchArea, double currentPos);
194 void drawSafetyGuard(QPainter &painter, const QRectF &switchArea);
195 void drawLed(QPainter &painter, const QPointF &center, double radius, bool active);
196 void drawScrew(QPainter &painter, const QPointF &center, double radius);
197 QRectF calculateSwitchRect() const;
198 QRectF calculateGuardRect() const;
199
200 std::unique_ptr<IndustrialSwitchPrivate> d_ptr;
201 Q_DECLARE_PRIVATE(IndustrialSwitch)
202};
203
204} // namespace QtIndustrialWidgets
Definition IndustrialSwitch.h:40
Definition AnalysisChart.h:21
Library export macros and version definitions for QtIndustrialWidgets.
#define QTINDUSTRIALWIDGETS_EXPORT
Export/import macro for shared library symbol visibility across dynamic linking boundaries.
Definition qtindustrialwidgets_global.h:25