-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMain.qml
More file actions
349 lines (300 loc) · 10.4 KB
/
Copy pathMain.qml
File metadata and controls
349 lines (300 loc) · 10.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Controls.Fusion
ApplicationWindow {
id: mainWindow
width: 600
height: 450
visible: true
title: qsTr("Battery Quick Example")
// 颜色对话框
ColorDialog {
id: colorDialog
title: qsTr("Choose color")
}
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
// === 顶部:电池显示区域 ===
Battery {
id: battery
Layout.fillWidth: true
Layout.fillHeight: true
Layout.leftMargin: 100
Layout.rightMargin: 100
}
// === 控制面板区域 ===
GridLayout {
Layout.fillWidth: true
Layout.fillHeight: true
columns: 3
rowSpacing: 8
columnSpacing: 10
// 第一行:数值控制
Label {
text: qsTr("Current value:")
Layout.alignment: Qt.AlignRight
}
Slider {
id: valueSlider
Layout.fillWidth: true
from: 0
to: 100
value: battery.value
onMoved: {
if (pressed) {
battery.setValue(value);
}
}
}
SpinBox {
id: valueSpinBox
from: 0
to: 100
value: battery.value
editable: true
onValueModified: battery.setValue(value)
}
// 第二行:报警阈值
Label {
text: qsTr("Alarm threshold:")
Layout.alignment: Qt.AlignRight
}
Slider {
id: alarmSlider
Layout.fillWidth: true
from: 0
to: 50
value: 20
onMoved: {
if (pressed) {
battery.alarmValue = value;
}
}
}
SpinBox {
id: alarmSpinBox
from: 0
to: 50
value: 20
editable: true
onValueModified: battery.alarmValue = value
}
// 第三行:电源颜色
Label {
text: qsTr("Power color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: powerColorButton
Layout.fillWidth: true
text: battery.powerColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = battery.powerColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
battery.powerColor = colorDialog.selectedColor;
powerColorButton.palette.buttonText = getContrastTextColor(battery.powerColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: battery.powerColor
border.color: "gray"
border.width: 1
}
}
Item {
Layout.fillWidth: true
}
// 第四行:报警颜色
Label {
text: qsTr("Alarm color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: alarmColorButton
Layout.fillWidth: true
text: battery.alarmColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = battery.alarmColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
battery.alarmColor = colorDialog.selectedColor;
alarmColorButton.palette.buttonText = getContrastTextColor(battery.alarmColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: battery.alarmColor
border.color: "gray"
border.width: 1
}
}
Item {
Layout.fillWidth: true
}
// 第五行:边框颜色
Label {
text: qsTr("Border color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: borderColorButton
Layout.fillWidth: true
text: battery.borderColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = battery.borderColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
battery.borderColor = colorDialog.selectedColor;
borderColorButton.palette.buttonText = getContrastTextColor(battery.borderColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: battery.borderColor
border.color: "gray"
border.width: 1
}
}
Item {
Layout.fillWidth: true
}
// 第六行:动画时长
Label {
id: durationLabel
text: qsTr("Animation duration: 500ms")
Layout.columnSpan: 1
}
Slider {
id: animationDurationSlider
Layout.fillWidth: true
from: 100
to: 2000
value: 500
onValueChanged: {
battery.animationDuration = value;
durationLabel.text = qsTr("Animation duration: %1ms").arg(value);
}
}
Item {
Layout.fillWidth: true
}
// 第七行:操作按钮第一组
Label {
text: qsTr("Actions:")
Layout.alignment: Qt.AlignRight
}
Button {
text: qsTr("+10")
Layout.fillWidth: true
onClicked: battery.increaseValue(10)
}
Button {
text: qsTr("Animate to 50")
Layout.fillWidth: true
onClicked: battery.setValueAnimated(50)
}
// 第八行:操作按钮第二组
Item {} // 占位
Button {
text: qsTr("-10")
Layout.fillWidth: true
onClicked: battery.decreaseValue(10)
}
Button {
text: qsTr("Reset")
Layout.fillWidth: true
onClicked: battery.reset()
}
// 第九行:充电控制
Label {
text: qsTr("Charging:")
Layout.alignment: Qt.AlignRight
}
CheckBox {
id: chargingCheckbox
checked: battery.charging
onToggled: battery.charging = checked
Layout.columnSpan: 2
}
}
// === 状态显示 ===
Label {
id: statusLabel
Layout.fillWidth: true
text: qsTr("Status: Normal")
horizontalAlignment: Text.AlignHCenter
padding: 10
}
}
// === 信号连接 ===
Connections {
target: battery
function onAlarmStateChanged(isAlarm) {
if (isAlarm) {
statusLabel.text = qsTr("Status: Low battery alarm!");
statusLabel.color = "red";
statusLabel.font.bold = true;
} else {
statusLabel.text = qsTr("Status: Normal");
statusLabel.color = "";
statusLabel.font.bold = false;
}
}
function onChargingChanged() {
chargingCheckbox.checked = battery.charging;
if (battery.charging) {
statusLabel.text = qsTr("Status: Charging...");
statusLabel.color = "blue";
}
}
function onAnimationStarted(oldValue, newValue) {
statusLabel.text = qsTr("Animating: %1% → %2%").arg(oldValue).arg(newValue);
statusLabel.color = "orange";
}
function onAnimationFinished(value) {
statusLabel.text = qsTr("Animation finished: %1%").arg(value);
statusLabel.color = "green";
}
function onValueIncreased(newValue) {
console.log("Value increased to:", newValue);
}
function onValueDecreased(newValue) {
console.log("Value decreased to:", newValue);
}
function onValueReset() {
console.log("Value reset");
}
}
// === 初始化 ===
Component.onCompleted: {
battery.value = 75;
alarmSlider.value = 20;
alarmSpinBox.value = 20;
animationDurationSlider.value = 500;
powerColorButton.palette.buttonText = getContrastTextColor(battery.powerColor);
borderColorButton.palette.buttonText = getContrastTextColor(battery.borderColor);
alarmColorButton.palette.buttonText = getContrastTextColor(battery.alarmColor);
}
// 计算字体颜色的函数(WCAG标准)
function getContrastTextColor(bgColor) {
function normalize(x) {
return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}
function getRelativeLuminance(r, g, b) {
return 0.2126 * normalize(r / 255.0) + 0.7152 * normalize(g / 255.0) + 0.0722 * normalize(b / 255.0);
}
var luminance = getRelativeLuminance(bgColor.r * 255, bgColor.g * 255, bgColor.b * 255);
return luminance > 0.179 ? "black" : "white";
}
}