-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMain.qml
More file actions
446 lines (383 loc) · 15.7 KB
/
Main.qml
File metadata and controls
446 lines (383 loc) · 15.7 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Controls.Fusion
ApplicationWindow {
id: mainWindow
width: 800
height: 600
visible: true
title: qsTr("Clock Quick Example")
// 颜色对话框
ColorDialog {
id: colorDialog
title: qsTr("Choose color")
}
// 保存默认颜色用于重置
property color defaultBorderColor: "#505050"
property color defaultBackgroundColor: "#323232"
property color defaultHourColor: "#f0f0f0"
property color defaultMinuteColor: "#dcdcdc"
property color defaultSecondColor: "#ff5050"
property color defaultTextColor: "#f0f0f0"
// 当前选择的颜色属性(用于颜色对话框)
property color selectedColorForDialog
RowLayout {
anchors.fill: parent
anchors.margins: 10
spacing: 20
// === 左侧:时钟显示区域 ===
ColumnLayout {
Layout.fillWidth: true
Layout.fillHeight: true
spacing: 10
// 时钟组件
Clock {
id: clock
Layout.fillWidth: true
Layout.fillHeight: true
// 初始颜色设置
borderColor: mainWindow.defaultBorderColor
backgroundColor: mainWindow.defaultBackgroundColor
hourColor: mainWindow.defaultHourColor
minuteColor: mainWindow.defaultMinuteColor
secondColor: mainWindow.defaultSecondColor
textColor: mainWindow.defaultTextColor
smoothSeconds: true
showSeconds: true
}
// 当前时间显示
Label {
id: timeLabel
Layout.alignment: Qt.AlignHCenter
text: qsTr("Current time: ") + mainWindow.formatTime(clock.currentTime)
font.pixelSize: 14
font.bold: true
}
}
// === 右侧:控制面板 ===
ColumnLayout {
Layout.fillWidth: true
Layout.fillHeight: true
spacing: 10
// 颜色设置组
GroupBox {
title: qsTr("Color Settings")
Layout.fillWidth: true
GridLayout {
columns: 2
anchors.fill: parent
// 第一列
Label {
text: qsTr("Border color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: borderColorButton
Layout.fillWidth: true
text: clock.borderColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = clock.borderColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
clock.borderColor = colorDialog.selectedColor;
borderColorButton.palette.buttonText = mainWindow.getContrastTextColor(clock.borderColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: clock.borderColor
border.color: "gray"
border.width: 1
}
}
Label {
text: qsTr("Background color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: backgroundColorButton
Layout.fillWidth: true
text: clock.backgroundColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = clock.backgroundColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
clock.backgroundColor = colorDialog.selectedColor;
backgroundColorButton.palette.buttonText = mainWindow.getContrastTextColor(clock.backgroundColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: clock.backgroundColor
border.color: "gray"
border.width: 1
}
}
Label {
text: qsTr("Hour hand color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: hourColorButton
Layout.fillWidth: true
text: clock.hourColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = clock.hourColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
clock.hourColor = colorDialog.selectedColor;
hourColorButton.palette.buttonText = mainWindow.getContrastTextColor(clock.hourColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: clock.hourColor
border.color: "gray"
border.width: 1
}
}
// 第二列
Label {
text: qsTr("Minute hand color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: minuteColorButton
Layout.fillWidth: true
text: clock.minuteColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = clock.minuteColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
clock.minuteColor = colorDialog.selectedColor;
minuteColorButton.palette.buttonText = mainWindow.getContrastTextColor(clock.minuteColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: clock.minuteColor
border.color: "gray"
border.width: 1
}
}
Label {
text: qsTr("Second hand color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: secondColorButton
Layout.fillWidth: true
text: clock.secondColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = clock.secondColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
clock.secondColor = colorDialog.selectedColor;
secondColorButton.palette.buttonText = mainWindow.getContrastTextColor(clock.secondColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: clock.secondColor
border.color: "gray"
border.width: 1
}
}
Label {
text: qsTr("Text color:")
Layout.alignment: Qt.AlignRight
}
Button {
id: textColorButton
Layout.fillWidth: true
text: clock.textColor.toString().toUpperCase()
onClicked: {
colorDialog.selectedColor = clock.textColor;
colorDialog.accepted.connect(function () {
if (colorDialog.selectedColor) {
clock.textColor = colorDialog.selectedColor;
textColorButton.palette.buttonText = mainWindow.getContrastTextColor(clock.textColor);
}
colorDialog.accepted.disconnect(arguments.callee);
});
colorDialog.open();
}
background: Rectangle {
color: clock.textColor
border.color: "gray"
border.width: 1
}
}
}
}
// 动画设置组
GroupBox {
title: qsTr("Animation Settings")
Layout.fillWidth: true
ColumnLayout {
anchors.fill: parent
CheckBox {
id: smoothSecondsCheckbox
text: qsTr("Smooth seconds")
checked: clock.smoothSeconds
onToggled: clock.smoothSeconds = checked
}
CheckBox {
id: showSecondsCheckbox
text: qsTr("Show seconds")
checked: clock.showSeconds
onToggled: clock.showSeconds = checked
}
}
}
// 快速主题组
GroupBox {
title: qsTr("Quick Themes")
Layout.fillWidth: true
GridLayout {
columns: 2
anchors.fill: parent
Button {
text: qsTr("Classic")
Layout.fillWidth: true
onClicked: mainWindow.applyClassicTheme()
}
Button {
text: qsTr("Dark")
Layout.fillWidth: true
onClicked: mainWindow.applyDarkTheme()
}
Button {
text: qsTr("Modern")
Layout.fillWidth: true
onClicked: mainWindow.applyModernTheme()
}
Button {
text: qsTr("Reset Colors")
Layout.fillWidth: true
onClicked: mainWindow.resetColors()
}
}
}
// 操作按钮
GroupBox {
title: qsTr("Actions")
Layout.fillWidth: true
RowLayout {
anchors.fill: parent
Button {
text: qsTr("Update Time")
Layout.fillWidth: true
onClicked: mainWindow.updateTime()
}
Button {
text: qsTr("Toggle Fullscreen")
Layout.fillWidth: true
onClicked: mainWindow.visibility === Window.Windowed ? mainWindow.showFullScreen() : mainWindow.showNormal()
}
}
}
// 状态信息
GroupBox {
title: qsTr("Status")
Layout.fillWidth: true
ColumnLayout {
anchors.fill: parent
Label {
text: qsTr("Smooth mode: ") + (clock.smoothSeconds ? qsTr("Enabled") : qsTr("Disabled"))
font.pixelSize: 12
}
Label {
text: qsTr("Seconds shown: ") + (clock.showSeconds ? qsTr("Yes") : qsTr("No"))
font.pixelSize: 12
}
Label {
text: qsTr("Last update: ") + mainWindow.formatTime(clock.currentTime)
font.pixelSize: 10
font.italic: true
}
}
}
}
}
// === 工具函数 ===
// 格式化时间显示
function formatTime(date) {
return date.toLocaleTimeString(Qt.locale(), "hh:mm:ss");
}
// 计算对比度文本颜色
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";
}
// 更新时间显示
function updateTime() {
clock.currentTime = new Date();
timeLabel.text = qsTr("Current time: ") + formatTime(clock.currentTime);
}
// === 主题函数 ===
function applyClassicTheme() {
clock.borderColor = "#505050";
clock.backgroundColor = "#323232";
clock.hourColor = "#f0f0f0";
clock.minuteColor = "#dcdcdc";
clock.secondColor = "#ff5050";
clock.textColor = "#f0f0f0";
}
function applyDarkTheme() {
clock.borderColor = "white";
clock.backgroundColor = "darkgray";
clock.hourColor = "white";
clock.minuteColor = "lightgray";
clock.secondColor = "yellow";
clock.textColor = "white";
}
function applyModernTheme() {
clock.borderColor = "#4682B4";
clock.backgroundColor = "#F0F8FF";
clock.hourColor = "#191970";
clock.minuteColor = "#4169E1";
clock.secondColor = "#DC143C";
clock.textColor = "#2F4F4F";
}
function resetColors() {
clock.borderColor = defaultBorderColor;
clock.backgroundColor = defaultBackgroundColor;
clock.hourColor = defaultHourColor;
clock.minuteColor = defaultMinuteColor;
clock.secondColor = defaultSecondColor;
clock.textColor = defaultTextColor;
}
// === 初始化 ===
Component.onCompleted: {
// 初始更新时间显示
updateTime();
// 设置定时更新状态
statusTimer.start();
}
// 状态更新定时器
Timer {
id: statusTimer
interval: 1000
running: true
repeat: true
}
}