|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Shapes |
| 3 | + |
| 4 | +Item { |
| 5 | + id: root |
| 6 | + |
| 7 | + // === 公共属性 === |
| 8 | + property real value: 0 |
| 9 | + property real minValue: 0 |
| 10 | + property real maxValue: 100 |
| 11 | + property real startAngle: 130 |
| 12 | + property real endAngle: 410 |
| 13 | + property bool showPercent: true |
| 14 | + property string title: "Progress" |
| 15 | + |
| 16 | + // 颜色属性 |
| 17 | + property color arcColor: "#4da1ff" |
| 18 | + property color textColor: "#4da1ff" |
| 19 | + property color titleColor: "#505050" |
| 20 | + property color baseColor: "#b3b3b3" |
| 21 | + property color backgroundColor: "transparent" |
| 22 | + |
| 23 | + // 动画属性 |
| 24 | + property int animationDuration: 500 |
| 25 | + |
| 26 | + // 尺寸属性 |
| 27 | + property real arcWidth: Math.min(width, height) * 0.1 |
| 28 | + |
| 29 | + // === 计算属性 === |
| 30 | + readonly property bool isAnimating: privateData.animationRunning |
| 31 | + readonly property real progress: (value - minValue) / (maxValue - minValue) |
| 32 | + readonly property real totalAngle: endAngle - startAngle |
| 33 | + readonly property real progressAngle: totalAngle * progress |
| 34 | + readonly property real currentStartAngle: endAngle - progressAngle |
| 35 | + |
| 36 | + // === 信号定义 === (只定义QML不会自动生成的信号) |
| 37 | + signal valueIncreased(real newValue) |
| 38 | + signal valueDecreased(real newValue) |
| 39 | + signal valueReset |
| 40 | + signal animationStarted(real oldValue, real newValue) |
| 41 | + signal animationFinished(real value) |
| 42 | + |
| 43 | + // === 私有数据 === |
| 44 | + QtObject { |
| 45 | + id: privateData |
| 46 | + property bool animationRunning: false |
| 47 | + property real previousValue: root.value |
| 48 | + property bool updatingFromExternal: false |
| 49 | + } |
| 50 | + |
| 51 | + // === 隐式尺寸 === |
| 52 | + implicitWidth: 200 |
| 53 | + implicitHeight: 200 |
| 54 | + |
| 55 | + // === 动画系统 === |
| 56 | + Behavior on value { |
| 57 | + id: valueAnimation |
| 58 | + enabled: root.animationDuration > 0 && privateData.animationRunning |
| 59 | + NumberAnimation { |
| 60 | + duration: root.animationDuration |
| 61 | + easing.type: Easing.OutCubic |
| 62 | + |
| 63 | + onStarted: { |
| 64 | + root.animationStarted(privateData.previousValue, root.value); |
| 65 | + } |
| 66 | + |
| 67 | + onStopped: { |
| 68 | + privateData.animationRunning = false; |
| 69 | + root.animationFinished(root.value); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // === 值变化处理 === |
| 75 | + onValueChanged: { |
| 76 | + if (!privateData.updatingFromExternal) { |
| 77 | + // 直接设置值,无动画 |
| 78 | + privateData.previousValue = value; |
| 79 | + |
| 80 | + // 检查数值增减 |
| 81 | + if (value > privateData.previousValue) { |
| 82 | + valueIncreased(value); |
| 83 | + } else if (value < privateData.previousValue) { |
| 84 | + valueDecreased(value); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // === 外观组件 === |
| 90 | + |
| 91 | + // 背景 |
| 92 | + Rectangle { |
| 93 | + id: backgroundRect |
| 94 | + anchors.fill: parent |
| 95 | + color: root.backgroundColor |
| 96 | + radius: Math.min(width, height) * 0.5 |
| 97 | + } |
| 98 | + |
| 99 | + // 使用 Shape 绘制圆弧 |
| 100 | + Shape { |
| 101 | + id: progressShape |
| 102 | + anchors.fill: parent |
| 103 | + vendorExtensionsEnabled: true |
| 104 | + |
| 105 | + // 背景圆弧 |
| 106 | + ShapePath { |
| 107 | + id: baseArc |
| 108 | + strokeColor: root.baseColor |
| 109 | + fillColor: "transparent" |
| 110 | + strokeWidth: root.arcWidth |
| 111 | + capStyle: ShapePath.RoundCap |
| 112 | + |
| 113 | + PathAngleArc { |
| 114 | + centerX: root.width / 2 |
| 115 | + centerY: root.height / 2 |
| 116 | + radiusX: Math.min(root.width, root.height) / 2 - root.arcWidth / 2 |
| 117 | + radiusY: Math.min(root.width, root.height) / 2 - root.arcWidth / 2 |
| 118 | + startAngle: root.startAngle |
| 119 | + sweepAngle: root.totalAngle |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // 进度圆弧 |
| 124 | + ShapePath { |
| 125 | + id: progressArc |
| 126 | + strokeColor: root.arcColor |
| 127 | + fillColor: "transparent" |
| 128 | + strokeWidth: root.arcWidth |
| 129 | + capStyle: ShapePath.RoundCap |
| 130 | + |
| 131 | + PathAngleArc { |
| 132 | + centerX: root.width / 2 |
| 133 | + centerY: root.height / 2 |
| 134 | + radiusX: Math.min(root.width, root.height) / 2 - root.arcWidth / 2 |
| 135 | + radiusY: Math.min(root.width, root.height) / 2 - root.arcWidth / 2 |
| 136 | + startAngle: root.startAngle |
| 137 | + sweepAngle: root.progressAngle |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // 数值文本 |
| 143 | + Text { |
| 144 | + id: valueText |
| 145 | + anchors.centerIn: parent |
| 146 | + anchors.verticalCenterOffset: parent.height * 0.1 |
| 147 | + text: { |
| 148 | + if (root.showPercent) { |
| 149 | + var percent = ((root.value - root.minValue) / (root.maxValue - root.minValue)) * 100; |
| 150 | + return percent.toFixed(2) + "%"; |
| 151 | + } else { |
| 152 | + return root.value.toFixed(2); |
| 153 | + } |
| 154 | + } |
| 155 | + color: root.textColor |
| 156 | + font.pixelSize: Math.min(root.width, root.height) / 10.0 |
| 157 | + font.bold: true |
| 158 | + horizontalAlignment: Text.AlignHCenter |
| 159 | + verticalAlignment: Text.AlignVCenter |
| 160 | + } |
| 161 | + |
| 162 | + // 标题文本 |
| 163 | + Text { |
| 164 | + id: titleText |
| 165 | + anchors { |
| 166 | + top: valueText.bottom |
| 167 | + horizontalCenter: parent.horizontalCenter |
| 168 | + topMargin: Math.min(root.width, root.height) * 0.05 |
| 169 | + } |
| 170 | + text: root.title |
| 171 | + color: root.titleColor |
| 172 | + font.pixelSize: Math.min(root.width, root.height) / 15.0 |
| 173 | + horizontalAlignment: Text.AlignHCenter |
| 174 | + verticalAlignment: Text.AlignVCenter |
| 175 | + } |
| 176 | + |
| 177 | + // === 公共方法 === |
| 178 | + function setValue(newValue) { |
| 179 | + var clampedValue = Math.max(root.minValue, Math.min(root.maxValue, newValue)); |
| 180 | + if (clampedValue !== root.value) { |
| 181 | + privateData.previousValue = root.value; |
| 182 | + privateData.animationRunning = false; |
| 183 | + privateData.updatingFromExternal = true; |
| 184 | + root.value = clampedValue; |
| 185 | + privateData.updatingFromExternal = false; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + function setValueAnimated(newValue) { |
| 190 | + var clampedValue = Math.max(root.minValue, Math.min(root.maxValue, newValue)); |
| 191 | + if (clampedValue !== root.value) { |
| 192 | + privateData.previousValue = root.value; |
| 193 | + privateData.animationRunning = true; |
| 194 | + privateData.updatingFromExternal = true; |
| 195 | + root.value = clampedValue; |
| 196 | + privateData.updatingFromExternal = false; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + function increaseValue(increment) { |
| 201 | + var actualIncrement = increment || 1.0; |
| 202 | + if (actualIncrement <= 0) |
| 203 | + return; |
| 204 | + |
| 205 | + var newValue = Math.min(root.maxValue, root.value + actualIncrement); |
| 206 | + if (newValue !== root.value) { |
| 207 | + root.setValueAnimated(newValue); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + function decreaseValue(decrement) { |
| 212 | + var actualDecrement = decrement || 1.0; |
| 213 | + if (actualDecrement <= 0) |
| 214 | + return; |
| 215 | + |
| 216 | + var newValue = Math.max(root.minValue, root.value - actualDecrement); |
| 217 | + if (newValue !== root.value) { |
| 218 | + root.setValueAnimated(newValue); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + function reset() { |
| 223 | + if (root.value !== root.minValue) { |
| 224 | + root.setValueAnimated(root.minValue); |
| 225 | + root.valueReset(); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + // === 组件完成时初始化 === |
| 230 | + Component.onCompleted: { |
| 231 | + // 初始化动画值 |
| 232 | + privateData.previousValue = root.value; |
| 233 | + } |
| 234 | +} |
0 commit comments