-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDashBoard.qml
More file actions
302 lines (261 loc) · 8.88 KB
/
DashBoard.qml
File metadata and controls
302 lines (261 loc) · 8.88 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
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Shapes
Item {
id: root
// === 仪表盘核心属性 ===
property real value: 0
property real minValue: 0
property real maxValue: 100
property real startAngle: 130
property real endAngle: 410
property string unit: "km/h"
property string title: "SPEED"
// === 刻度属性 ===
property int scaleMajor: 10 // 主刻度数量
property int scaleMinor: 5 // 每个主刻度间的次刻度数量
property color scaleColor: "#04A8AD"
property color scaleTextColor: "#04A8AD"
// === 颜色属性 ===
property color arcColor: "#383D4A" // 背景弧颜色
property color pointerColor: "#04B5C8" // 指针颜色
property color valueColor: "#2c3e50" // 数值颜色
property color titleColor: "#7f8c8d" // 标题颜色
property color backgroundColor: "transparent"
// === 尺寸属性 ===
property real arcWidth: Math.min(width, height) * 0.08
property real pointerWidth: Math.min(width, height) * 0.02
property real pointerLength: Math.min(width, height) * 0.45
// === 动画属性 ===
property int animationDuration: 800
property bool animationEnabled: true
// === 计算属性 ===
readonly property real totalAngle: endAngle - startAngle
readonly property real progress: (value - minValue) / (maxValue - minValue)
readonly property real currentAngle: startAngle + progress * totalAngle
readonly property real centerX: width / 2
readonly property real centerY: height / 2
readonly property real radius: Math.min(width, height) / 2 - arcWidth / 2
// === 信号 ===
signal valueIncreased(real newValue)
signal valueDecreased(real newValue)
signal valueReset
signal animationStarted(real oldValue, real newValue)
signal animationFinished(real value)
// === 私有数据 ===
QtObject {
id: privateData
property real previousValue: root.minValue
property bool isAnimating: false
}
// === 隐式尺寸 ===
implicitWidth: 200
implicitHeight: 200
// === 动画系统 ===
Behavior on value {
id: valueBehavior
enabled: root.animationEnabled && root.animationDuration > 0
NumberAnimation {
duration: root.animationDuration
easing.type: Easing.OutCubic
onStarted: {
privateData.isAnimating = true;
root.animationStarted(privateData.previousValue, root.value);
}
onStopped: {
privateData.isAnimating = false;
privateData.previousValue = root.value;
root.animationFinished(root.value);
}
}
}
// === 值变化处理 ===
onValueChanged: {
if (!privateData.isAnimating) {
privateData.previousValue = value;
// 直接设置值,检查增减
if (value > privateData.previousValue) {
root.valueIncreased(value);
} else if (value < privateData.previousValue) {
root.valueDecreased(value);
}
}
}
// === 外观组件 ===
// 背景
Rectangle {
anchors.fill: parent
color: root.backgroundColor
}
// 主形状容器
Shape {
id: progressShape
anchors.fill: parent
vendorExtensionsEnabled: true
preferredRendererType: Shape.CurveRenderer
// 背景圆弧
ShapePath {
id: baseArc
strokeColor: root.arcColor
fillColor: "transparent"
strokeWidth: root.arcWidth
PathAngleArc {
centerX: root.centerX
centerY: root.centerY
radiusX: root.radius
radiusY: root.radius
startAngle: root.startAngle
sweepAngle: root.totalAngle
}
}
}
// 刻度线
Repeater {
model: root.scaleMajor * root.scaleMinor + 1
Rectangle {
id: scale
required property int index
readonly property real angle: root.startAngle + index * (root.totalAngle / (root.scaleMajor * root.scaleMinor))
readonly property bool isMajor: index % root.scaleMinor === 0
readonly property real length: isMajor ? root.radius * 0.12 : root.radius * 0.06
readonly property real lineWidth: isMajor ? 2 : 1
width: lineWidth
height: length
color: root.scaleColor
antialiasing: true
transform: [
Rotation {
origin.x: scale.lineWidth / 2
origin.y: 0
angle: scale.angle - 90
},
Translate {
x: root.centerX + (root.radius - scale.length) * Math.cos(scale.angle * Math.PI / 180) - scale.lineWidth / 2
y: root.centerY + (root.radius - scale.length) * Math.sin(scale.angle * Math.PI / 180)
}
]
}
}
// 刻度数字
Repeater {
model: root.scaleMajor + 1
Text {
required property int index
readonly property real angle: root.startAngle + index * (root.totalAngle / root.scaleMajor)
readonly property real numberValue: root.minValue + index * (root.maxValue - root.minValue) / root.scaleMajor
text: numberValue.toFixed(1)
color: root.scaleTextColor
font.pixelSize: Math.min(root.width, root.height) * 0.05
antialiasing: true
x: root.centerX + (root.radius * 0.75) * Math.cos(angle * Math.PI / 180) - width / 2
y: root.centerY + (root.radius * 0.75) * Math.sin(angle * Math.PI / 180) - height / 2
}
}
// 指针
Shape {
id: pointer
x: root.centerX
y: root.centerY
width: root.pointerWidth
height: root.pointerLength
preferredRendererType: Shape.CurveRenderer
// 指针形状 - 简单的三角形,指向正下方
ShapePath {
strokeColor: root.pointerColor
fillColor: root.pointerColor
strokeWidth: 0
joinStyle: ShapePath.MiterJoin
PathMove {
x: 0
y: 0
}
PathLine {
x: -root.pointerWidth
y: root.radius / 20
}
PathLine {
x: root.pointerWidth
y: root.pointerLength
}
PathLine {
x: root.pointerWidth
y: root.radius / 20
}
PathLine {
x: 0
y: 0
}
}
rotation: root.currentAngle - 90
transformOrigin: Item.TopLeft
}
// 数值显示
Text {
id: valueText
anchors {
horizontalCenter: parent.horizontalCenter
verticalCenter: parent.verticalCenter
verticalCenterOffset: -parent.height * 0.1
}
text: root.value.toFixed(2) + " " + root.unit
color: root.valueColor
font {
pixelSize: Math.min(root.width, root.height) * 0.1
bold: true
family: "Arial"
}
horizontalAlignment: Text.AlignHCenter
}
// 标题
Text {
id: titleText
anchors {
top: valueText.bottom
horizontalCenter: parent.horizontalCenter
topMargin: Math.min(root.width, root.height) * 0.05
}
text: root.title
color: root.titleColor
font {
pixelSize: Math.min(root.width, root.height) * 0.06
capitalization: Font.AllUppercase
letterSpacing: 1.5
}
horizontalAlignment: Text.AlignHCenter
}
// === 公共方法 ===
function setValue(newValue) {
var clampedValue = Math.max(root.minValue, Math.min(root.maxValue, newValue));
if (clampedValue !== root.value) {
root.animationEnabled = false;
root.value = clampedValue;
root.animationEnabled = true;
}
}
function setValueAnimated(newValue) {
var clampedValue = Math.max(root.minValue, Math.min(root.maxValue, newValue));
if (clampedValue !== root.value) {
root.value = clampedValue;
}
}
function increaseValue(increment = 1.0) {
if (increment > 0) {
root.setValueAnimated(root.value + increment);
}
}
function decreaseValue(decrement = 1.0) {
if (decrement > 0) {
root.setValueAnimated(root.value - decrement);
}
}
function reset() {
if (root.value !== root.minValue) {
root.setValueAnimated(root.minValue);
root.valueReset();
}
}
// === 组件初始化 ===
Component.onCompleted: {
privateData.previousValue = root.value;
}
}