CCIMXDesktop
 
Loading...
Searching...
No Matches
GaugeWidget.h
1#ifndef GaugeWidget_H
2#define GaugeWidget_H
3
4#include <QWidget>
5
14class GaugeWidget : public QWidget {
15 Q_OBJECT
21 Q_PROPERTY(double value
22 READ value
23 WRITE setValue
24 NOTIFY valueChanged)
25
26public:
28 static constexpr int DEF_MIN_VALUE { 0 };
29
31 static constexpr int DEF_MAX_VALUE { 100 };
32
34 static constexpr short TITLE_FONT_SZ { 12 };
35
37 static constexpr short CURVAL_FOUT_SZ { 10 };
38
40 static constexpr int ANIMATION_DURATION { 500 };
41
43 static constexpr float WIDGET_MIN_WIDTH { 200 };
44
46 static constexpr float WIDGET_MIN_HEIGHT { 200 };
47
49 static constexpr QColor BOARD_COLOR = QColor(160, 160, 160);
50
52 static constexpr QColor FROM_BOARD_COLOR = QColor(190, 190, 190);
53
55 static constexpr QColor TO_BOARD_COLOR = QColor(110, 110, 110);
56
58 static constexpr short BOARD_LEN = 1;
59
61 static constexpr short START_ANGLE { 225 };
62
64 static constexpr short TOTAL_ANGLE { 270 };
65
67 static constexpr short COLOR_GRAD_WIDTH { 8 };
68
70 static constexpr short COLOR_RADIUS { 90 };
71
73 static constexpr QColor START_COLOR = QColor(255, 0, 0);
74
76 static constexpr QColor END_COLOR = QColor(0, 255, 0);
77
79 static constexpr int TICK_CNT { 55 };
80
82 static constexpr short MAIN_TICK_WIDTH { 2 };
83
85 static constexpr short SUB_TICK_WIDTH { 1 };
86
88 static constexpr short MAIN_TICK_LENGTH { 8 };
89
91 static constexpr short SUB_TICK_LENGTH { 5 };
92
94 static constexpr short SUB_MAIN_RATE { 5 };
95
97 static constexpr QColor TICK_COLOR = QColor(255, 255, 255);
98
100 static constexpr short LABEL_FONT_SZ { 6 };
101
103 static constexpr QColor LABEL_COLOR = QColor(255, 255, 255);
104
109 explicit GaugeWidget(QWidget* parent = nullptr);
110
116 void setRange(int min, int max) {
117 max_value = max;
118 min_value = min;
119 update();
120 }
121
126 void setTitle(const QString& title) {
127 this->title = title;
128 update();
129 }
130
135 void setUnit(const QString& unit) {
136 this->unit = unit;
137 update();
138 }
139
144 inline double value() const { return current_value; }
145
150 void update_value(const double value);
151
152signals:
157 void valueChanged(double value);
158
159protected:
164 void paintEvent(QPaintEvent* event) override;
165
166private:
167 double max_value { DEF_MIN_VALUE };
168 double min_value { DEF_MAX_VALUE };
169 double current_value;
170
171 QFont title_font { "Arial", TITLE_FONT_SZ, QFont::Bold };
172 QFont value_font { "Arial", CURVAL_FOUT_SZ, QFont::Bold };
173 QFont label_font { "Arial", LABEL_FONT_SZ };
174
175 QString title;
176 QString unit;
177
182 void setValue(const double val) {
183 if (qFuzzyCompare(current_value, val))
184 return;
185 current_value = val;
186 emit valueChanged(val);
187 update();
188 }
189
190 void drawBackground(QPainter& p);
191 void drawArc(QPainter& p);
192 void drawTicks(QPainter& p);
193 void drawLabels(QPainter& p);
194 void drawNeedle(QPainter& p);
195 void drawCenter(QPainter& p);
196 void drawTexts(QPainter& p);
197};
198
199#endif
A customizable gauge widget for displaying numeric values.
Definition GaugeWidget.h:14
static constexpr QColor LABEL_COLOR
Color for labels.
Definition GaugeWidget.h:103
static constexpr short CURVAL_FOUT_SZ
Font size for the current value text.
Definition GaugeWidget.h:37
static constexpr QColor END_COLOR
Gradient end color for the gauge arc.
Definition GaugeWidget.h:76
double value() const
Get the current gauge value.
Definition GaugeWidget.h:144
static constexpr short MAIN_TICK_LENGTH
Length of main ticks.
Definition GaugeWidget.h:88
void setUnit(const QString &unit)
Set the unit text displayed with the value.
Definition GaugeWidget.h:135
static constexpr short COLOR_GRAD_WIDTH
Width of the color gradient arc.
Definition GaugeWidget.h:67
static constexpr int DEF_MIN_VALUE
Default minimum value of the gauge.
Definition GaugeWidget.h:28
void valueChanged(double value)
Signal emitted when the gauge value changes.
void setTitle(const QString &title)
Set the title text displayed on the gauge.
Definition GaugeWidget.h:126
void update_value(const double value)
Update the gauge value with optional animation.
Definition GaugeWidget.cpp:9
double value
property object
Definition GaugeWidget.h:24
void paintEvent(QPaintEvent *event) override
Paint event handler to render the gauge.
Definition GaugeWidget.cpp:18
static constexpr short SUB_TICK_LENGTH
Length of sub ticks.
Definition GaugeWidget.h:91
static constexpr QColor BOARD_COLOR
Base color for the gauge board.
Definition GaugeWidget.h:49
static constexpr QColor TICK_COLOR
Color of the ticks.
Definition GaugeWidget.h:97
static constexpr QColor TO_BOARD_COLOR
Gradient end color for board.
Definition GaugeWidget.h:55
static constexpr short TOTAL_ANGLE
Total sweep angle of gauge arc (degrees)
Definition GaugeWidget.h:64
static constexpr float WIDGET_MIN_WIDTH
Minimum widget width.
Definition GaugeWidget.h:43
static constexpr QColor FROM_BOARD_COLOR
Gradient start color for board.
Definition GaugeWidget.h:52
static constexpr int ANIMATION_DURATION
Duration of animation in milliseconds.
Definition GaugeWidget.h:40
static constexpr short TITLE_FONT_SZ
Font size for the title text.
Definition GaugeWidget.h:34
static constexpr short SUB_MAIN_RATE
Number of sub ticks per main tick.
Definition GaugeWidget.h:94
static constexpr short START_ANGLE
Start angle for gauge drawing (degrees)
Definition GaugeWidget.h:61
static constexpr short SUB_TICK_WIDTH
Width of sub ticks.
Definition GaugeWidget.h:85
static constexpr short COLOR_RADIUS
Radius for the gradient arc.
Definition GaugeWidget.h:70
static constexpr int DEF_MAX_VALUE
Default maximum value of the gauge.
Definition GaugeWidget.h:31
static constexpr short MAIN_TICK_WIDTH
Width of main ticks.
Definition GaugeWidget.h:82
static constexpr short LABEL_FONT_SZ
Font size for labels on ticks.
Definition GaugeWidget.h:100
static constexpr float WIDGET_MIN_HEIGHT
Minimum widget height.
Definition GaugeWidget.h:46
static constexpr int TICK_CNT
Total number of ticks on the gauge.
Definition GaugeWidget.h:79
static constexpr short BOARD_LEN
Width of the board outline.
Definition GaugeWidget.h:58
static constexpr QColor START_COLOR
Gradient start color for the gauge arc.
Definition GaugeWidget.h:73
void setRange(int min, int max)
Set the gauge range.
Definition GaugeWidget.h:116