-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBattery.qml
More file actions
278 lines (246 loc) · 7.9 KB
/
Copy pathBattery.qml
File metadata and controls
278 lines (246 loc) · 7.9 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
import QtQuick
Item {
id: root
// === 属性定义 ===
property int value: 75
property int alarmValue: 20
property color borderColor: "#2c2c2e"
property color powerColor: "#34c759"
property color alarmColor: "#ff453a"
property int animationDuration: 500
property bool charging: false
// === 信号定义 ===
signal valueIncreased(int newValue)
signal valueDecreased(int newValue)
signal valueReset
signal animationStarted(int oldValue, int newValue)
signal animationFinished(int value)
signal animationStopped(int currentValue)
signal alarmStateChanged(bool isInAlarm)
// === 计算属性 ===
readonly property bool isInAlarmState: value <= alarmValue
readonly property bool isAnimating: privateData.animationRunning
readonly property color currentPowerColor: isInAlarmState ? alarmColor : powerColor
// === 尺寸建议 ===
implicitWidth: 150
implicitHeight: 80
// === 私有数据 ===
QtObject {
id: privateData
property bool wasInAlarmState: false
property bool animationRunning: false
property int previousValue: root.value
property bool updatingFromExternal: false
}
// === 动画系统 ===
Behavior on value {
id: valueAnimation
enabled: root.animationDuration > 0 && privateData.animationRunning
NumberAnimation {
duration: root.animationDuration
easing.type: Easing.InOutQuad
onStarted: {
privateData.animationRunning = true;
root.animationStarted(privateData.previousValue, root.value);
}
onStopped: {
privateData.animationRunning = false;
root.animationFinished(root.value);
root.checkAlarmState();
}
}
}
// === 状态监控 ===
onValueChanged: {
if (!privateData.updatingFromExternal) {
root.checkAlarmState();
}
privateData.previousValue = value;
}
onAlarmValueChanged: checkAlarmState()
// === 电池外观组件 ===
// 电池主体
Rectangle {
id: rootBody
width: parent.width - rootHead.width
height: parent.height
radius: Math.max(2, height * (1.0 / 30.0))
border {
color: root.borderColor
width: Math.max(1, height * 0.03)
}
// 电池腔体背景(内凹深度感)
gradient: Gradient {
GradientStop {
position: 0.0
color: "#f2f2f7"
}
GradientStop {
position: 1.0
color: "#e5e5ea"
}
}
// 电量填充
Rectangle {
id: powerLevel
width: (rootBody.width - rootBody.border.width * 2) * Math.max(0, Math.min(100, root.value)) / 100
height: rootBody.height - rootBody.border.width * 2
x: rootBody.border.width
y: rootBody.border.width
radius: Math.max(0, rootBody.radius - rootBody.border.width)
// 渐变填充(上浅下深,立体感)
gradient: Gradient {
GradientStop {
position: 0.0
color: Qt.lighter(root.currentPowerColor, 1.12)
}
GradientStop {
position: 1.0
color: Qt.darker(root.currentPowerColor, 1.18)
}
}
}
// 数值显示
Text {
id: valueText
text: root.value + "%"
font {
pixelSize: Math.max(12, rootBody.height * 0.5)
bold: true
}
color: {
if (root.charging)
return "#1c1c1e";
return root.isInAlarmState ? root.alarmColor : "#1c1c1e";
}
anchors {
top: parent.top
bottom: parent.bottom
left: parent.left
}
width: root.charging ? parent.width * 0.65 : parent.width
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
// 充电符号
Text {
id: chargingSymbol
visible: root.charging
text: "⚡"
font.pixelSize: Math.max(12, rootBody.height * 0.5)
color: root.isInAlarmState ? root.alarmColor : "#1c1c1e"
anchors {
top: parent.top
bottom: parent.bottom
left: valueText.right
right: parent.right
}
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
}
// 电池头部
Rectangle {
id: rootHead
width: Math.max(5, parent.width * (1.0 / 11.0))
height: Math.max(8, rootBody.height / 3.0)
x: rootBody.width - 1
y: (parent.height - height) / 2
color: root.borderColor
}
// 充电脉冲动画(opacity 0.5 ↔ 1.0)
SequentialAnimation {
id: chargingPulse
running: root.charging
loops: Animation.Infinite
NumberAnimation {
target: chargingSymbol
property: "opacity"
from: 0.5
to: 1.0
duration: 600
easing.type: Easing.InOutQuad
}
NumberAnimation {
target: chargingSymbol
property: "opacity"
from: 1.0
to: 0.5
duration: 600
easing.type: Easing.InOutQuad
}
}
// 充电停止时重置 opacity
onChargingChanged: {
if (!charging) {
chargingSymbol.opacity = 1.0;
}
}
// === 公共方法 ===
function setValue(newValue) {
var clampedValue = Math.max(0, Math.min(100, newValue));
if (clampedValue !== value) {
privateData.previousValue = value;
privateData.animationRunning = false;
privateData.updatingFromExternal = true;
value = clampedValue;
privateData.updatingFromExternal = false;
}
}
function setValueAnimated(newValue) {
var clampedValue = Math.max(0, Math.min(100, newValue));
if (clampedValue !== value) {
privateData.previousValue = value;
privateData.animationRunning = true;
privateData.updatingFromExternal = true;
value = clampedValue;
privateData.updatingFromExternal = false;
}
}
function increaseValue(increment) {
if (increment <= 0)
return;
var actualIncrement = increment || 1;
var newValue = Math.min(100, value + actualIncrement);
if (newValue !== value) {
privateData.previousValue = value;
setValueAnimated(newValue);
valueIncreased(newValue);
}
}
function decreaseValue(decrement) {
if (decrement <= 0)
return;
var actualDecrement = decrement || 1;
var newValue = Math.max(0, value - actualDecrement);
if (newValue !== value) {
privateData.previousValue = value;
setValueAnimated(newValue);
valueDecreased(newValue);
}
}
function reset() {
if (value !== 0) {
privateData.previousValue = value;
setValue(0);
valueReset();
}
}
function startCharging() {
charging = true;
}
function stopCharging() {
charging = false;
}
function toggleCharging() {
charging = !charging;
}
// === 私有方法 ===
function checkAlarmState() {
var currentAlarmState = (value <= alarmValue);
if (currentAlarmState !== privateData.wasInAlarmState) {
alarmStateChanged(currentAlarmState);
privateData.wasInAlarmState = currentAlarmState;
}
}
}