-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMain.qml
More file actions
198 lines (175 loc) · 5.66 KB
/
Main.qml
File metadata and controls
198 lines (175 loc) · 5.66 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
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs
import QtQuick.Controls.Fusion
ApplicationWindow {
id: mainWindow
width: 360
height: 220
visible: true
title: qsTr("Loading Overlay Quick Example")
// 颜色对话框
ColorDialog {
id: colorDialog
title: qsTr("Choose color")
}
// 状态文本
property string statusText: qsTr("Ready")
// 自动隐藏定时器
Timer {
id: autoHideTimer
interval: 5000 // 5秒
repeat: false
onTriggered: {
loadingOverlay.hide();
mainWindow.statusText = qsTr("Loading overlay auto hidden");
}
}
ColumnLayout {
anchors.fill: parent
anchors.margins: 10
// 显示按钮
Button {
text: qsTr("Show Loading Overlay")
Layout.fillWidth: true
onClicked: {
// 设置文本
if (showTextCheckbox.checked && textEdit.text !== "") {
loadingOverlay.text = textEdit.text;
} else {
loadingOverlay.text = "";
}
// 显示加载层
loadingOverlay.show(textEdit.text);
// 更新状态
mainWindow.statusText = qsTr("Loading overlay shown");
// 设置自动隐藏
if (autoHideCheckbox.checked) {
autoHideTimer.stop();
autoHideTimer.interval = durationSpinBox.value * 1000;
autoHideTimer.start();
}
}
}
// 文本设置
RowLayout {
Layout.fillWidth: true
CheckBox {
id: showTextCheckbox
text: qsTr("Show Text")
checked: true
onToggled: {
if (checked && textEdit.text !== "") {
loadingOverlay.text = textEdit.text;
} else {
loadingOverlay.text = "";
}
}
}
TextField {
id: textEdit
Layout.fillWidth: true
placeholderText: qsTr("Enter loading text")
text: qsTr("Loading...")
onTextChanged: {
if (showTextCheckbox.checked) {
loadingOverlay.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("Text Color:")
}
Button {
id: textColorButton
Layout.fillWidth: true
text: loadingOverlay.textColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = loadingOverlay.textColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
loadingOverlay.textColor = colorDialog.selectedColor;
textColorButton.palette.buttonText = getContrastTextColor(loadingOverlay.textColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: loadingOverlay.textColor
border.color: "gray"
border.width: 1
}
}
}
// 状态显示
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 40
color: "transparent"
border.color: "gray"
border.width: 1
Label {
anchors.centerIn: parent
text: mainWindow.statusText
font.bold: true
}
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
}
}
// 加载层 - 覆盖整个窗口
LoadingOverlay {
id: loadingOverlay
// 监听属性变化
onTextChanged: mainWindow.statusText = qsTr("Text changed to: ") + loadingOverlay.text
onTextColorChanged: {
mainWindow.statusText = qsTr("Text color changed: ") + textColor;
}
}
Component.onCompleted: {
textColorButton.palette.buttonText = getContrastTextColor(loadingOverlay.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";
}
}