-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
329 lines (272 loc) · 11.8 KB
/
mainwindow.cpp
File metadata and controls
329 lines (272 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "mainwindow.h"
#include "batterywidget.h"
#include <QtWidgets>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 创建电池控件
auto *battery = new BatteryWidget(this);
// 创建数值控制
auto *valueSlider = new QSlider(Qt::Horizontal, this);
valueSlider->setRange(0, 100);
valueSlider->setValue(75);
auto *valueSpinBox = new QSpinBox(this);
valueSpinBox->setRange(0, 100);
valueSpinBox->setValue(75);
valueSpinBox->setSuffix("%");
// 创建报警阈值控制
auto *alarmLabel = new QLabel(tr("Alarm threshold:"), this);
auto *alarmSlider = new QSlider(Qt::Horizontal, this);
alarmSlider->setRange(0, 50);
alarmSlider->setValue(20);
auto *alarmSpinBox = new QSpinBox(this);
alarmSpinBox->setRange(0, 50);
alarmSpinBox->setValue(20);
alarmSpinBox->setSuffix("%");
// 创建充电状态控制
auto *chargingCheckbox = new QCheckBox(tr("Charging state"), this);
chargingCheckbox->setChecked(false);
// 创建颜色选择控件
auto *powerColorButton = new QPushButton(this);
auto *alarmColorButton = new QPushButton(this);
auto *borderColorButton = new QPushButton(this);
// 创建动画控制
auto *animationDurationSlider = new QSlider(Qt::Horizontal, this);
animationDurationSlider->setRange(100, 2000);
animationDurationSlider->setValue(500);
auto *durationLabel = new QLabel(tr("Animation duration: 500ms"), this);
// 创建操作按钮
auto *increaseButton = new QPushButton(tr("+10"), this);
auto *decreaseButton = new QPushButton(tr("-10"), this);
auto *resetButton = new QPushButton(tr("Reset"), this);
auto *animateToButton = new QPushButton(tr("Animate to 50"), this);
// 创建状态显示标签
auto *statusLabel = new QLabel(tr("Status: Normal"), this);
statusLabel->setAlignment(Qt::AlignCenter);
statusLabel->setFrameStyle(QFrame::Box);
statusLabel->setMinimumHeight(30);
// 布局设置 - 针对横向电池优化
auto *mainWidget = new QWidget(this);
auto *mainLayout = new QVBoxLayout(mainWidget);
// 顶部:电池显示区域
auto *batteryContainer = new QWidget(this);
auto *batteryLayout = new QHBoxLayout(batteryContainer);
batteryLayout->addStretch();
batteryLayout->addWidget(battery);
batteryLayout->addStretch();
// 控制面板区域
auto *controlPanel = new QWidget(this);
auto *controlLayout = new QGridLayout(controlPanel);
// 第一行:数值控制
int row = 0;
controlLayout->addWidget(new QLabel(tr("Current value:"), this), row, 0);
controlLayout->addWidget(valueSlider, row, 1);
controlLayout->addWidget(valueSpinBox, row, 2);
// 第二行:报警阈值
row++;
controlLayout->addWidget(alarmLabel, row, 0);
controlLayout->addWidget(alarmSlider, row, 1);
controlLayout->addWidget(alarmSpinBox, row, 2);
// 第三行:颜色控制
row++;
controlLayout->addWidget(new QLabel(tr("Power color:"), this), row, 0);
controlLayout->addWidget(powerColorButton, row, 1);
row++;
controlLayout->addWidget(new QLabel(tr("Alarm color:"), this), row, 0);
controlLayout->addWidget(alarmColorButton, row, 1);
row++;
controlLayout->addWidget(new QLabel(tr("Border color:"), this), row, 0);
controlLayout->addWidget(borderColorButton, row, 1);
// 第四行:动画控制
row++;
controlLayout->addWidget(durationLabel, row, 0);
controlLayout->addWidget(animationDurationSlider, row, 1);
// 第五行:操作按钮
row++;
controlLayout->addWidget(new QLabel(tr("Actions:"), this), row, 0);
controlLayout->addWidget(increaseButton, row, 1);
controlLayout->addWidget(animateToButton, row, 2);
row++;
controlLayout->addWidget(decreaseButton, row, 1);
controlLayout->addWidget(resetButton, row, 2);
// 第六行:充电控制
row++;
controlLayout->addWidget(new QLabel(tr("Charging:"), this), row, 0);
controlLayout->addWidget(chargingCheckbox, row, 1);
// 主布局组装
mainLayout->addWidget(batteryContainer);
mainLayout->addWidget(controlPanel);
mainLayout->addWidget(statusLabel);
setCentralWidget(mainWidget);
resize(600, 450); // 更适合横向电池的窗口大小
setWindowTitle(tr("Battery Widget Example"));
// ========== 颜色设置部分 ==========
// 统一的颜色按钮更新函数
auto updateColorButton = [](QPushButton *button, const QColor &color) {
auto colorName = color.name(QColor::HexArgb).toUpper();
// 计算相对亮度(sRGB颜色空间)
auto getRelativeLuminance = [](int r, int g, int b) {
auto normalize = [](double x) {
return x <= 0.03928 ? x / 12.92 : std::pow((x + 0.055) / 1.055, 2.4);
};
return 0.2126 * normalize(r / 255.0) + 0.7152 * normalize(g / 255.0)
+ 0.0722 * normalize(b / 255.0);
};
double luminance = getRelativeLuminance(color.red(), color.green(), color.blue());
// 根据WCAG标准选择对比度足够的文字颜色
QString textColor = luminance > 0.179 ? "black" : "white";
button->setStyleSheet(
QString("background-color: %1; color: %2; border: 1px solid gray; padding: 5px;")
.arg(colorName)
.arg(textColor));
button->setText(colorName);
};
// 初始化颜色按钮
updateColorButton(powerColorButton, battery->powerColor());
updateColorButton(alarmColorButton, battery->alarmColor());
updateColorButton(borderColorButton, battery->borderColor());
// ========== 信号连接 ==========
// 数值控制 - 避免循环信号
connect(valueSlider, &QSlider::valueChanged, this, [battery, valueSpinBox](int value) {
// 阻塞spinbox信号,避免循环
valueSpinBox->blockSignals(true);
battery->setValue(value);
valueSpinBox->setValue(value);
valueSpinBox->blockSignals(false);
});
connect(valueSpinBox,
QOverload<int>::of(&QSpinBox::valueChanged),
this,
[battery, valueSlider](int value) {
// 阻塞slider信号,避免循环
valueSlider->blockSignals(true);
battery->setValue(value);
valueSlider->setValue(value);
valueSlider->blockSignals(false);
});
// 报警阈值控制 - 避免循环信号
connect(alarmSlider, &QSlider::valueChanged, this, [battery, alarmSpinBox](int value) {
alarmSpinBox->blockSignals(true);
battery->setAlarmValue(value);
alarmSpinBox->setValue(value);
alarmSpinBox->blockSignals(false);
});
connect(alarmSpinBox,
QOverload<int>::of(&QSpinBox::valueChanged),
this,
[battery, alarmSlider](int value) {
alarmSlider->blockSignals(true);
battery->setAlarmValue(value);
alarmSlider->setValue(value);
alarmSlider->blockSignals(false);
});
// 充电状态
connect(chargingCheckbox, &QCheckBox::toggled, battery, &BatteryWidget::setCharging);
// 颜色设置
connect(powerColorButton,
&QPushButton::clicked,
this,
[this, battery, powerColorButton, updateColorButton]() {
QColor color = QColorDialog::getColor(battery->powerColor(),
this,
tr("Select Power Color"));
if (color.isValid()) {
battery->setPowerColor(color);
updateColorButton(powerColorButton, color);
}
});
connect(alarmColorButton,
&QPushButton::clicked,
this,
[this, battery, alarmColorButton, updateColorButton]() {
QColor color = QColorDialog::getColor(battery->alarmColor(),
this,
tr("Select Alarm Color"));
if (color.isValid()) {
battery->setAlarmColor(color);
updateColorButton(alarmColorButton, color);
}
});
connect(borderColorButton,
&QPushButton::clicked,
this,
[this, battery, borderColorButton, updateColorButton]() {
QColor color = QColorDialog::getColor(battery->borderColor(),
this,
tr("Select Border Color"));
if (color.isValid()) {
battery->setBorderColor(color);
updateColorButton(borderColorButton, color);
}
});
// 动画设置
connect(animationDurationSlider,
&QSlider::valueChanged,
this,
[battery, durationLabel](int value) {
battery->setAnimationDuration(value);
durationLabel->setText(tr("Animation duration: %1ms").arg(value));
});
// 操作按钮
connect(increaseButton, &QPushButton::clicked, this, [battery]() {
battery->increaseValue(10);
});
connect(decreaseButton, &QPushButton::clicked, this, [battery]() {
battery->decreaseValue(10);
});
connect(resetButton, &QPushButton::clicked, battery, &BatteryWidget::reset);
connect(animateToButton, &QPushButton::clicked, this, [battery]() {
battery->setValueAnimated(50);
});
// 电池信号连接 - 更新UI状态
connect(battery, &BatteryWidget::valueChanged, this, [valueSlider, valueSpinBox](int value) {
valueSlider->blockSignals(true);
valueSlider->setValue(value);
valueSlider->blockSignals(false);
valueSpinBox->blockSignals(true);
valueSpinBox->setValue(value);
valueSpinBox->blockSignals(false);
});
connect(battery, &BatteryWidget::valueIncreased, this, [](int newValue) {
qDebug() << "Value increased to:" << newValue;
});
connect(battery, &BatteryWidget::valueDecreased, this, [](int newValue) {
qDebug() << "Value decreased to:" << newValue;
});
connect(battery, &BatteryWidget::valueReset, this, []() { qDebug() << "Value reset"; });
connect(battery,
&BatteryWidget::animationStarted,
this,
[statusLabel](int oldValue, int newValue) {
statusLabel->setText(tr("Animating: %1% → %2%").arg(oldValue).arg(newValue));
statusLabel->setStyleSheet("color: orange;");
});
connect(battery, &BatteryWidget::animationFinished, this, [statusLabel](int value) {
statusLabel->setText(tr("Animation finished: %1%").arg(value));
statusLabel->setStyleSheet("color: green;");
});
connect(battery, &BatteryWidget::alarmStateChanged, this, [statusLabel](bool isAlarm) {
if (isAlarm) {
statusLabel->setText(tr("Status: Low battery alarm!"));
statusLabel->setStyleSheet("color: red; font-weight: bold;");
} else {
statusLabel->setText(tr("Status: Normal"));
statusLabel->setStyleSheet("");
}
});
connect(battery,
&BatteryWidget::chargingChanged,
this,
[statusLabel, chargingCheckbox](bool charging) {
chargingCheckbox->blockSignals(true);
chargingCheckbox->setChecked(charging);
chargingCheckbox->blockSignals(false);
if (charging) {
statusLabel->setText(tr("Status: Charging..."));
statusLabel->setStyleSheet("color: blue;");
}
});
// 初始化状态
battery->setValue(75);
}
MainWindow::~MainWindow() {}