-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathTimeAndDate.qml
More file actions
657 lines (610 loc) · 26.1 KB
/
TimeAndDate.qml
File metadata and controls
657 lines (610 loc) · 26.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.15
import QtQml.Models 2.11
import org.deepin.dcc 1.0
import org.deepin.dtk 1.0 as D
import org.deepin.dtk.private 1.0 as P
import org.deepin.dtk.style 1.0 as DS
import ZoneInfoModel 1.0
// 时间和日期
DccObject {
DccObject {
id: dateTimeContent
name: "dateTimeContent"
parentName: "datetime"
weight: 10
pageType: DccObject.Item
page: ColumnLayout {
FontLoader {
id: webFont
source: "qrc:/builtin-font/resource/Outfit-Light.ttf"
}
Label {
id: timeLabel
height: contentHeight
Layout.leftMargin: 10
leftPadding: 0
rightPadding: 0
horizontalAlignment: Text.AlignLeft
font {
pointSize: 40
family: webFont.font.family
}
text: ("" + dccData.currentTime).trim()
}
Label {
id: dateLabel
height: contentHeight
Layout.leftMargin: 10
leftPadding: 0
rightPadding: 0
horizontalAlignment: Text.AlignLeft
font {
pointSize: 14
family: webFont.font.family
}
text: dccData.currentDate
}
}
}
// 时间同步
DccObject {
name: "dateTimeGroup"
parentName: "datetime"
weight: 12
pageType: DccObject.Item
page: DccGroupView {}
onParentItemChanged: item => {
if (item) {
item.topPadding = 10
item.bottomPadding = 5
}
}
DccObject {
id: ntpSettings
property bool ntpOn: dccData.ntpEnabled
name: "ntpSettings"
parentName: "dateTimeGroup"
displayName: qsTr("Auto sync time")
weight: 10
backgroundType: DccObject.Normal
pageType: DccObject.Editor
page: Switch {
checked: ntpSettings.ntpOn
onCheckedChanged: {
dccData.ntpEnabled = checked
}
}
}
DccObject {
id: dateAndTimeSettings
name: "dateAndTimeSettings"
parentName: "dateTimeGroup"
displayName: dccData.ntpEnabled ? qsTr("Ntp server") : qsTr("System date and time")
weight: 12
backgroundType: DccObject.Normal
pageType: DccObject.Editor
property bool showCustom: false
property string customAddr
onParentItemChanged: item => {
if (item) {
item.rightPadding = DS.Style.comboBox.spacing
}
}
page: Item {
implicitHeight: 36
implicitWidth: dccData.ntpEnabled ? 280 : 80
ComboBox {
id: comboBox
property var serverList: dccData.ntpServerList
flat: true
padding: 0
visible: dccData.ntpEnabled
anchors.fill: parent
hoverEnabled: true
model: serverList
// 不设置默认的话可能无法滚动(不显示上下箭头按钮)。。。
maxVisibleItems: serverList.length - 1
currentIndex: {
let index = serverList.indexOf(dccData.ntpServerAddress)
dateAndTimeSettings.showCustom = (index < 0)
if (index < 0)
dateAndTimeSettings.customAddr = dccData.ntpServerAddress
if (dccData.ntpServerAddress.length > 0)
dccData.previousServerAddress = dccData.ntpServerAddress
return index < 0 ? serverList.length - 1 : index
}
onActivated: function (index) {
if (dccData.ntpServerAddress.length > 0)
dccData.previousServerAddress = dccData.ntpServerAddress
dateAndTimeSettings.showCustom = (serverList[index] === qsTr("Customize"))
if (dateAndTimeSettings.showCustom) {
let savedCustomServer = dccData.getCustomNtpServer()
if (savedCustomServer.length > 0) {
dateAndTimeSettings.customAddr = savedCustomServer
dccData.ntpServerAddress = savedCustomServer
} else if (dateAndTimeSettings.customAddr.length > 0) {
dccData.ntpServerAddress = dateAndTimeSettings.customAddr
} else {
dccData.ntpServerAddress = ""
}
return
}
dccData.ntpServerAddress = serverList[index]
}
Component.onCompleted: {
let text = qsTr("Customize")
if (!comboBox.serverList.includes(text))
comboBox.serverList.push(text)
if (dccData.ntpServerAddress.length === 0 && dccData.previousServerAddress.length > 0) {
dccData.ntpServerAddress = dccData.previousServerAddress
}
let currentServer = dccData.ntpServerAddress
let index = comboBox.serverList.indexOf(currentServer)
if (index < 0) {
comboBox.currentIndex = comboBox.serverList.length - 1
dateAndTimeSettings.showCustom = true
dateAndTimeSettings.customAddr = currentServer
} else {
dateAndTimeSettings.showCustom = false
let savedCustomServer = dccData.getCustomNtpServer()
if (savedCustomServer.length > 0) {
dateAndTimeSettings.customAddr = savedCustomServer
}
}
}
}
Button {
id: settingsButton
visible: !dccData.ntpEnabled
anchors.fill: parent
property bool needShowDialog: false
text: qsTr("Settings")
implicitWidth: fm.advanceWidth(text) + 12
implicitHeight: 30
FontMetrics {
id: fm
}
Loader {
id: loader
active: settingsButton.needShowDialog
sourceComponent: DateTimeSettingDialog {
onClosing: {
settingsButton.needShowDialog = false
}
}
onLoaded: {
item.show()
}
}
onClicked: {
settingsButton.needShowDialog = true
}
}
}
onDeactive: {
if (dateAndTimeSettings.showCustom &&
dateAndTimeSettings.customAddr.length === 0) {
dateAndTimeSettings.showCustom = false
if (dccData.previousServerAddress.length > 0) {
dccData.ntpServerAddress = dccData.previousServerAddress
}
}
}
}
DccObject {
id: customNTPServer
name: "customNTPServer"
parentName: "dateTimeGroup"
displayName: qsTr("Server address")
visible: dccData.ntpEnabled && dateAndTimeSettings.showCustom
weight: 13
backgroundType: DccObject.Normal
pageType: DccObject.Editor
page: Item {
id: item
implicitHeight: addrErrorTip.visible ? 40 + addrErrorTip.height + 4 : 40
implicitWidth: 300
D.LineEdit {
id: addr
implicitWidth: 200
text: dateAndTimeSettings.customAddr
placeholderText: qsTr("Required")
showAlert: false
horizontalAlignment: background.visible ? TextInput.AlignLeft : TextInput.AlignRight
anchors{
rightMargin: 5
right: editBtn.left
top: parent.top
topMargin: (40 - addr.height) / 2
}
rightPadding: (!addr.readOnly && addr.text.length > 0 ? addr.clearButton.width : 5)
onReadOnlyChanged: {
addr.background.visible = !addr.readOnly
addr.clearButton.visible = !addr.readOnly && text.length > 0
addr.focus = !addr.readOnly
}
onTextChanged: {
if (addr.text.length > 0) {
addr.showAlert = false
addrErrorTip.visible = false
}
}
Component.onCompleted: {
Qt.callLater( function(){ addr.forceActiveFocus() } )
addr.readOnly = text.length > 0
}
D.AlertToolTip {
id: addrErrorTip
target: addr
text: qsTr("The ntp server address cannot be empty")
visible: false
timeout: 3000
}
}
D.IconButton {
id: editBtn
anchors.right: parent.right
anchors.verticalCenter: addr.verticalCenter
hoverEnabled: true
background: Rectangle {
anchors.fill: parent
property D.Palette pressedColor: D.Palette {
normal: Qt.rgba(0, 0, 0, 0.2)
normalDark: Qt.rgba(1, 1, 1, 0.25)
}
property D.Palette hoveredColor: D.Palette {
normal: Qt.rgba(0, 0, 0, 0.1)
normalDark: Qt.rgba(1, 1, 1, 0.1)
}
radius: DS.Style.control.radius
color: parent.pressed ? D.ColorSelector.pressedColor : (parent.hovered ? D.ColorSelector.hoveredColor : "transparent")
border {
color: parent.palette.highlight
width: parent.visualFocus ? DS.Style.control.focusBorderWidth : 0
}
}
icon {
name: addr.readOnly ? "dcc-edit" : "inactive"
width: 16
height: 16
}
onClicked: {
if (addr.text.length === 0) {
addr.showAlert = true
addrErrorTip.visible = true
return
}
addr.showAlert = false
addrErrorTip.visible = false
if (!addr.readOnly) {
dccData.ntpServerAddress = addr.text
}
addr.readOnly = !addr.readOnly
}
}
}
}
DccObject {
visible: false // 暂时隐藏,会导致逻辑很复杂
name: "12/24h"
parentName: "dateTimeGroup"
displayName: qsTr("Use 24-hour format")
weight: 14
backgroundType: DccObject.Normal
pageType: DccObject.Editor
page: Switch {
checked: dccData.use24HourFormat
onCheckedChanged: {
dccData.use24HourFormat = checked
}
}
}
}
// 系统时区
DccObject {
id: timezoneGroup
name: "timezoneGroup"
parentName: "datetime"
weight: 20
pageType: DccObject.Item
page: DccGroupView {
id: view
}
DccRepeater {
id: userTimezoneRepeater
model: dccData.userTimezoneModel()
delegate: ItemZoneComp {
name: "userTimezoneItem" + index
parentName: "timezoneGroup"
displayName: model.display
description: model.description
weight: 20 + 10 * (index + 1)
shift: model.shift
zoneId: model.zoneId
}
}
onParentItemChanged: item => { if (item) item.topPadding = 5 }
DccObject {
id: systemTimezone
name: "systemTimezone"
parentName: "timezoneGroup"
displayName: qsTr("system time zone")
weight: 12
backgroundType: DccObject.Normal
pageType: DccObject.Editor
onParentItemChanged: item => {
if (item) {
item.rightPadding = DS.Style.comboBox.spacing
}
}
page: Item {
id: systemTimezoneItem
implicitWidth: rowlayout.implicitWidth
implicitHeight: rowlayout.implicitHeight
property var model: dccData.zoneSearchModel()
property var currentIndex: dccData.currentTimeZoneIndex
property string saveZoneId: ""
RowLayout {
id: rowlayout
Label {
id: timezoneLabel
text: dccData.timeZoneDispalyName
}
D.IconLabel {
Layout.alignment: Qt.AlignRight | Qt.AlignHCenter
icon.name: "arrow_ordinary_down"
icon.palette: D.DTK.makeIconPalette(timezoneLabel.palette)
}
}
MouseArea {
id: mouseArea
anchors.fill: parent
Component.onCompleted: {
if (dccData.currentTimeZoneIndex >= 0) {
let model = dccData.zoneSearchModel()
systemTimezoneItem.saveZoneId = model.data(model.index(systemTimezoneItem.currentIndex, 0), ZoneInfoModel.ZoneIdRole)
}
}
Connections {
target: dccData
function onCurrentTimeZoneIndexChanged() {
if (dccData.currentTimeZoneIndex >= 0) {
let model = dccData.zoneSearchModel()
systemTimezoneItem.saveZoneId = model.data(model.index(systemTimezoneItem.currentIndex, 0), ZoneInfoModel.ZoneIdRole)
}
}
}
SearchableListViewPopup {
id: timezoneWindow
highlightedIndex: systemTimezoneItem.currentIndex
maxVisibleItems: 13
delegateModel: DelegateModel {
model: systemTimezoneItem.model
delegate: D.MenuItem {
useIndicatorPadding: true
width: timezoneWindow.viewWidth
text: model.display
font: D.DTK.fontManager.t6
highlighted: ListView.isCurrentItem
hoverEnabled: true
checkable: true
autoExclusive: true
checked: model.zoneId === systemTimezoneItem.saveZoneId
contentItem: RowLayout {
spacing: 8
Item {
Layout.preferredWidth: parent.parent.useIndicatorPadding ? 20 : 0
Layout.preferredHeight: parent.height
visible: parent.parent.useIndicatorPadding
}
Text {
Layout.fillWidth: true
Layout.alignment: Qt.AlignVCenter
text: parent.parent.text
font: D.DTK.fontManager.t6
color: parent.parent.palette.windowText
elide: Text.ElideRight
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
}
}
onHoveredChanged: {
if (hovered && !ListView.view.keyboardScrolling) {
timezoneWindow.setViewIndex(index)
}
}
onCheckedChanged: {
if (checked && model.zoneId != systemTimezoneItem.saveZoneId) {
let zoneId = model.zoneId
dccData.setSystemTimeZone(zoneId)
timezoneWindow.close()
}
}
}
}
onSearchTextChanged: {
let delegateModel = dccData.zoneSearchModel()
delegateModel.setFilterWildcard(timezoneWindow.searchText)
}
}
onClicked: function (mouse) {
if (!timezoneWindow.isVisible()) {
// 保持当前选中项可见,但居中显示
timezoneWindow.highlightedIndex = systemTimezoneItem.currentIndex
timezoneWindow.show()
timezoneWindow.setPositionByItem(parent)
}
}
}
}
}
DccObject {
name: "timezoneList"
parentName: "timezoneGroup"
displayName: qsTr("Timezone list")
weight: 12
backgroundType: DccObject.Normal
pageType: DccObject.Editor
page: RowLayout {
spacing: 10
Button {
id: addButton
text: qsTr("Add")
implicitHeight: 30
implicitWidth: 60
SearchableListViewPopup {
id: timezoneListWindow
delegateModel: DelegateModel {
model: dccData.zoneSearchModel()
delegate: D.MenuItem {
useIndicatorPadding: true
width: timezoneListWindow.viewWidth
text: model.display
font: D.DTK.fontManager.t6
highlighted: ListView.isCurrentItem
hoverEnabled: true
checkable: true
autoExclusive: true
contentItem: RowLayout {
spacing: 8
Item {
Layout.preferredWidth: parent.parent.useIndicatorPadding ? 20 : 0
Layout.preferredHeight: parent.height
visible: parent.parent.useIndicatorPadding
}
Text {
Layout.fillWidth: true
Layout.alignment: Qt.AlignVCenter
text: parent.parent.text
font: D.DTK.fontManager.t6
color: parent.parent.palette.windowText
elide: Text.ElideRight
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
}
}
onHoveredChanged: {
if (hovered && !ListView.view.keyboardScrolling) {
timezoneListWindow.setViewIndex(index)
}
}
onCheckedChanged: {
if (checked) {
let zoneId = model.zoneId
dccData.addUserTimeZoneById(zoneId)
timezoneListWindow.close()
}
}
}
}
maxVisibleItems: 13
onSearchTextChanged: {
let delegateModel = dccData.zoneSearchModel()
delegateModel.setFilterWildcard(searchText);
}
}
onClicked: {
if (!timezoneListWindow.isVisible()) {
timezoneListWindow.setPositionByItem(parent)
timezoneListWindow.highlightedIndex = 0
timezoneListWindow.show()
}
}
}
}
}
}
component ItemZoneComp: DccObject {
property int shift: 8
property string zoneId
backgroundType: DccObject.Normal
pageType: DccObject.Item
page: ItemDelegate {
id: itemZoneCompItemDelegate
visible: dccObj
hoverEnabled: true
implicitHeight: Math.max(50, textColumn.implicitHeight + 2)
icon.name: dccObj.icon
checkable: false
contentItem: Item {
id: contentLayout
Column {
id: textColumn
anchors {
left: parent.left
leftMargin: 50
verticalCenter: parent.verticalCenter
}
spacing: 2
Label {
id: display
text: dccObj.displayName
font: D.DTK.fontManager.t6
elide: Text.ElideRight
}
Label {
id: description
visible: text !== ""
font: D.DTK.fontManager.t10
text: dccObj.description
opacity: 0.5
elide: Text.ElideRight
}
}
}
TimezoneClock {
id: clock
width: 36
height: 36
shift: dccObj.shift
anchors {
left: itemZoneCompItemDelegate.left
leftMargin: 10
top: itemZoneCompItemDelegate.top
topMargin: (itemZoneCompItemDelegate.height - clock.height) / 2
}
}
D.IconButton {
id: removeButton
visible: itemZoneCompItemDelegate.hovered
icon.name: "dcc-delete"
icon.width: 16
icon.height: 16
implicitWidth: 32
implicitHeight: 32
anchors {
right: itemZoneCompItemDelegate.right
rightMargin: 10
verticalCenter: itemZoneCompItemDelegate.verticalCenter
}
background: Rectangle {
property D.Palette pressedColor: D.Palette {
normal: Qt.rgba(0, 0, 0, 0.2)
normalDark: Qt.rgba(1, 1, 1, 0.25)
}
property D.Palette hoveredColor: D.Palette {
normal: Qt.rgba(0, 0, 0, 0.1)
normalDark: Qt.rgba(1, 1, 1, 0.1)
}
radius: DS.Style.control.radius
color: parent.pressed ? D.ColorSelector.pressedColor : (parent.hovered ? D.ColorSelector.hoveredColor : "transparent")
border {
color: parent.palette.highlight
width: parent.visualFocus ? DS.Style.control.focusBorderWidth : 0
}
}
onClicked: {
console.log("need remove timezone", dccObj.displayName)
dccData.removeUserTimeZoneById(dccObj.zoneId)
}
}
background: DccItemBackground {
separatorVisible: true
}
}
}
}