-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMain.qml
More file actions
364 lines (329 loc) · 12.1 KB
/
Main.qml
File metadata and controls
364 lines (329 loc) · 12.1 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs
import QtQuick.Controls.Fusion
ApplicationWindow {
id: mainWindow
width: 450
height: 550
visible: true
title: qsTr("Loading Indicator Quick Example")
// 颜色对话框
ColorDialog {
id: colorDialog
title: qsTr("Choose color")
}
// 状态文本
property string statusText: qsTr("Ready")
// 自动隐藏定时器
Timer {
id: autoHideTimer
interval: 5000 // 5秒
repeat: false
onTriggered: {
loadingIndicator.hideOverlay();
mainWindow.statusText = qsTr("Loading indicator auto hidden");
}
}
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
// 显示/隐藏按钮
RowLayout {
Layout.fillWidth: true
Button {
text: qsTr("Show Loading Indicator")
Layout.fillWidth: true
onClicked: {
// 设置动画样式
loadingIndicator.animationStyle = styleComboBox.currentValue;
// 设置动画速度
loadingIndicator.animationSpeed = speedSlider.value;
// 设置文本
if (showTextCheckbox.checked && textEdit.text !== "") {
loadingIndicator.text = textEdit.text;
} else {
loadingIndicator.text = "";
}
// 显示加载指示器
loadingIndicator.showOverlay(testArea);
// 更新状态
mainWindow.statusText = qsTr("Loading indicator shown");
// 设置自动隐藏
if (autoHideCheckbox.checked) {
autoHideTimer.stop();
autoHideTimer.interval = durationSpinBox.value * 1000;
autoHideTimer.start();
}
}
}
Button {
text: qsTr("Hide Loading Indicator")
Layout.fillWidth: true
onClicked: {
loadingIndicator.hideOverlay();
autoHideTimer.stop();
mainWindow.statusText = qsTr("Loading indicator hidden");
}
}
}
// 动画样式选择
RowLayout {
Layout.fillWidth: true
Label {
text: qsTr("Animation Style:")
Layout.preferredWidth: 120
}
ComboBox {
id: styleComboBox
Layout.fillWidth: true
model: [
{
text: qsTr("Rotating Dots"),
value: LoadingIndicator.AnimationStyle.RotatingDots
},
{
text: qsTr("Pulsing Circle"),
value: LoadingIndicator.AnimationStyle.PulsingCircle
},
{
text: qsTr("Bouncing Bars"),
value: LoadingIndicator.AnimationStyle.BouncingBars
},
{
text: qsTr("Custom Movie"),
value: LoadingIndicator.AnimationStyle.CustomMovie
}
]
valueRole: "value"
textRole: "text"
onCurrentValueChanged: {
loadingIndicator.animationStyle = currentValue ? currentValue : 0;
// 如果选择了 CustomMovie,设置默认的 GIF 源
if (currentValue === LoadingIndicator.AnimationStyle.CustomMovie) {
loadingIndicator.setMovie("LoadingIndicator/resources/loading.gif");
}
}
}
}
// 动画速度控制
RowLayout {
Layout.fillWidth: true
Label {
text: qsTr("Animation Speed:")
Layout.preferredWidth: 120
}
Slider {
id: speedSlider
Layout.fillWidth: true
from: 50
to: 500
value: 100
onValueChanged: {
speedValueLabel.text = Math.round(value) + " ms";
loadingIndicator.animationSpeed = Math.round(value);
}
}
Label {
id: speedValueLabel
text: "100 ms"
Layout.preferredWidth: 60
}
}
// 文本设置
RowLayout {
Layout.fillWidth: true
CheckBox {
id: showTextCheckbox
text: qsTr("Show Text")
checked: true
onToggled: {
if (checked && textEdit.text !== "") {
loadingIndicator.text = textEdit.text;
} else {
loadingIndicator.text = "";
}
}
}
TextField {
id: textEdit
Layout.fillWidth: true
placeholderText: qsTr("Enter loading text")
text: qsTr("Loading...")
onTextChanged: {
if (showTextCheckbox.checked) {
loadingIndicator.text = text;
}
}
}
}
// 自动隐藏设置
RowLayout {
Layout.fillWidth: true
CheckBox {
id: autoHideCheckbox
text: qsTr("Auto Hide After:")
checked: true
}
SpinBox {
id: durationSpinBox
from: 1
to: 60
value: 5
}
Label {
text: qsTr("seconds")
}
Item {
Layout.fillWidth: true
}
}
// 颜色设置
GridLayout {
Layout.fillWidth: true
columns: 2
rowSpacing: 10
columnSpacing: 10
Label {
text: qsTr("Animation Color:")
}
Button {
id: dotColorButton
Layout.fillWidth: true
text: loadingIndicator.color.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = loadingIndicator.color;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
loadingIndicator.color = colorDialog.selectedColor;
dotColorButton.palette.buttonText = getContrastTextColor(loadingIndicator.color);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: loadingIndicator.color
border.color: "gray"
border.width: 1
}
}
Label {
text: qsTr("Background Color:")
}
Button {
id: bgColorButton
Layout.fillWidth: true
text: loadingIndicator.backgroundColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = loadingIndicator.backgroundColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
loadingIndicator.backgroundColor = colorDialog.selectedColor;
bgColorButton.palette.buttonText = getContrastTextColor(loadingIndicator.backgroundColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: loadingIndicator.backgroundColor
border.color: "gray"
border.width: 1
}
}
Label {
text: qsTr("Text Color:")
}
Button {
id: textColorButton
Layout.fillWidth: true
text: loadingIndicator.textColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = loadingIndicator.textColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
loadingIndicator.textColor = colorDialog.selectedColor;
textColorButton.palette.buttonText = getContrastTextColor(loadingIndicator.textColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: loadingIndicator.textColor
border.color: "gray"
border.width: 1
}
}
}
// 状态显示
Rectangle {
Layout.fillWidth: true
height: 40
color: "transparent"
border.color: "gray"
border.width: 1
Label {
anchors.centerIn: parent
text: mainWindow.statusText
font.bold: true
}
}
// 测试区域
Rectangle {
id: testArea
Layout.fillWidth: true
Layout.preferredHeight: 200
color: "transparent"
border.color: "darkgray"
border.width: 1
Label {
anchors.centerIn: parent
text: qsTr("Test loading area")
font.bold: true
}
// 加载指示器
LoadingIndicator {
id: loadingIndicator
visible: false
anchors.fill: parent
// 监听属性变化
onTextChanged: mainWindow.statusText = qsTr("Text changed to: ") + loadingIndicator.text
onTextColorChanged: {
mainWindow.currentTextColor = loadingIndicator.textColor;
mainWindow.statusText = qsTr("Text color changed: ") + loadingIndicator.textColor;
}
onColorChanged: {
mainWindow.statusText = qsTr("Animation color changed: ") + color;
}
onBackgroundColorChanged: {
mainWindow.statusText = qsTr("Background color changed: ") + backgroundColor;
}
onAnimationSpeedChanged: {
mainWindow.statusText = qsTr("Animation speed changed to %1 ms").arg(animationSpeed);
}
onAnimationStyleChanged: {
mainWindow.statusText = qsTr("Animation style changed");
}
}
}
}
Component.onCompleted: {
dotColorButton.palette.buttonText = getContrastTextColor(loadingIndicator.color);
bgColorButton.palette.buttonText = getContrastTextColor(loadingIndicator.backgroundColor);
textColorButton.palette.buttonText = getContrastTextColor(loadingIndicator.textColor);
}
// 计算字体颜色的函数(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";
}
}