-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathClock.qml
More file actions
159 lines (136 loc) · 4.47 KB
/
Clock.qml
File metadata and controls
159 lines (136 loc) · 4.47 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
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Shapes
Item {
id: root
// 属性定义
property color borderColor: "#505050"
property color backgroundColor: "#323232"
property color hourColor: "#f0f0f0"
property color minuteColor: "#dcdcdc"
property color secondColor: "#ff5050"
property color textColor: "#f0f0f0"
property bool smoothSeconds: true
property bool showSeconds: true
// 内部计算属性
property real centerX: width / 2
property real centerY: height / 2
property real radius: Math.min(centerX, centerY) * 0.9
property real borderWidth: radius / 20
property real scaleLength: radius / 15
property real numberRadius: radius * 0.75
// 当前时间
property date currentTime: new Date()
// 定时更新
Timer {
interval: root.smoothSeconds ? 200 : 1000
running: true
repeat: true
onTriggered: root.currentTime = new Date()
}
// 背景圆
Shape {
anchors.fill: parent
preferredRendererType: Shape.CurveRenderer
ShapePath {
strokeWidth: root.borderWidth
strokeColor: root.borderColor
fillColor: "transparent"
capStyle: ShapePath.RoundCap
joinStyle: ShapePath.RoundJoin
PathAngleArc {
centerX: root.centerX
centerY: root.centerY
radiusX: root.radius - root.borderWidth / 2
radiusY: root.radius - root.borderWidth / 2
startAngle: 0
sweepAngle: 360
}
}
ShapePath {
strokeColor: "transparent"
fillColor: root.backgroundColor
capStyle: ShapePath.RoundCap
joinStyle: ShapePath.RoundJoin
PathAngleArc {
centerX: root.centerX
centerY: root.centerY
radiusX: root.radius
radiusY: root.radius
startAngle: 0
sweepAngle: 360
}
}
}
// 刻度
Repeater {
model: 60
Rectangle {
required property int index
readonly property bool isHour: index % 5 === 0
readonly property real angle: index * 6 * Math.PI / 180
x: root.centerX + (root.radius - root.scaleLength) * Math.cos(angle) - width / 2
y: root.centerY + (root.radius - root.scaleLength) * Math.sin(angle) - height / 2
width: isHour ? root.radius * 0.02 : root.radius * 0.01
height: root.scaleLength * (isHour ? 1.0 : 0.7)
color: root.textColor
rotation: index * 6 + 90
transformOrigin: Item.Bottom
antialiasing: true
}
}
// 数字
Repeater {
model: 12
Text {
required property int index
readonly property real angle: (index - 3) * 30 * Math.PI / 180
readonly property real xPos: root.centerX + root.numberRadius * Math.cos(angle)
readonly property real yPos: root.centerY + root.numberRadius * Math.sin(angle)
x: xPos - width / 2
y: yPos - height / 2
text: index + 1
color: root.textColor
font.pixelSize: Math.max(10, root.radius * 0.1)
font.bold: true
antialiasing: true
}
}
// 时针
Hand {
centerX: root.centerX
centerY: root.centerY
length: root.radius * 0.5
width: root.radius * 0.01
color: root.hourColor
angle: (root.currentTime.getHours() % 12 + root.currentTime.getMinutes() / 60) * 30
}
// 分针
Hand {
centerX: root.centerX
centerY: root.centerY
length: root.radius * 0.7
width: root.radius * 0.008
color: root.minuteColor
angle: (root.currentTime.getMinutes() + root.currentTime.getSeconds() / 60) * 6
}
// 秒针
Hand {
centerX: root.centerX
centerY: root.centerY
length: root.radius * 0.8
width: root.radius * 0.004
color: root.secondColor
angle: root.currentTime.getSeconds() * 6 + (root.smoothSeconds ? root.currentTime.getMilliseconds() / 1000 * 6 : 0)
visible: root.showSeconds
}
// 中心点 - 修正版本
Rectangle {
width: root.radius / 20 * 2
height: width
x: root.centerX - width / 2
y: root.centerY - height / 2
radius: width / 2
color: root.hourColor
}
}