| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import QtQuick
- import Felgo 4.0
- /**
- * 上传进度圆形控件(灌水效果)。
- *
- * 业务背景:本设备一次只上传一张图片(上传队列串行,见 CurlFtpUploader::runLoop),
- * 所以进度用单个圆形水位表示即可,不需要按图片序号铺多格网格。
- *
- * 视觉:外圈细圆环 + 内部自下而上"灌水"填充,水面带正弦波纹并持续流动,
- * 圆心显示百分比。失败时整只圆环与水体转红,便于现场一眼区分。
- *
- * 实现:整块用 Canvas 绘制。
- * 注意 Item.clip 只裁剪矩形、不裁剪圆角,靠 clip 属性做圆形裁剪会失效;
- * Canvas 里用 arc + clip() 才是可靠的圆形裁剪方式。
- */
- Item {
- id: root
- // 进度 0~100
- property real percent: 0
- // 是否失败(决定配色)
- property bool failed: false
- // 圆形直径
- property real diameter: dp(56)
- readonly property color themeColor: failed ? "#e74c3c" : "#28a779"
- // 圆环线宽
- readonly property real ringWidth: dp(2)
- implicitWidth: diameter
- implicitHeight: diameter
- // 波纹相位:由动画驱动,变化时重绘 Canvas
- property real wavePhase: 0
- NumberAnimation on wavePhase {
- from: 0; to: Math.PI * 2
- duration: 1600
- loops: Animation.Infinite
- running: root.visible && root.percent > 0 && root.percent < 100
- }
- Canvas {
- id: canvas
- anchors.fill: parent
- renderStrategy: Canvas.Cooperative
- onWidthChanged: requestPaint()
- onHeightChanged: requestPaint()
- // percent / 配色 / 相位变化都要重绘
- Connections {
- target: root
- function onPercentChanged() { canvas.requestPaint() }
- function onFailedChanged() { canvas.requestPaint() }
- function onWavePhaseChanged() { canvas.requestPaint() }
- }
- onPaint: {
- var ctx = getContext("2d")
- ctx.reset()
- var w = width
- var h = height
- var cx = w / 2
- var cy = h / 2
- var r = Math.min(cx, cy) - root.ringWidth
- if (r <= 0) return
- var p = Math.max(0, Math.min(100, root.percent)) / 100
- // ---------- 底色 ----------
- ctx.beginPath()
- ctx.arc(cx, cy, r, 0, Math.PI * 2)
- ctx.fillStyle = "#66000000"
- ctx.fill()
- // ---------- 水体(圆形裁剪内自下而上填充)----------
- if (p > 0) {
- var levelY = cy + r - p * (r * 2) // 水面 y:p=0 在底部,p=1 在顶部
- var amp = Math.min(dp(2), r * 0.12) // 波纹振幅,随半径自适应
- ctx.save()
- ctx.beginPath()
- ctx.arc(cx, cy, r, 0, Math.PI * 2)
- ctx.clip()
- ctx.beginPath()
- ctx.moveTo(cx - r, h)
- ctx.lineTo(cx - r, levelY)
- // 水面:叠加两个谐波形成自然起伏
- var steps = 48
- for (var i = 0; i <= steps; i++) {
- var x = cx - r + (2 * r) * (i / steps)
- var y = levelY
- + Math.sin((i / steps) * Math.PI * 4 + root.wavePhase) * amp
- + Math.sin((i / steps) * Math.PI * 6 - root.wavePhase * 1.3) * (amp * 0.4)
- ctx.lineTo(x, y)
- }
- ctx.lineTo(cx + r, h)
- ctx.closePath()
- ctx.fillStyle = root.themeColor
- ctx.fill()
- ctx.restore()
- }
- // ---------- 外圈 ----------
- ctx.beginPath()
- ctx.arc(cx, cy, r + root.ringWidth / 2, 0, Math.PI * 2)
- ctx.strokeStyle = root.themeColor
- ctx.lineWidth = root.ringWidth
- ctx.stroke()
- }
- }
- // ---------- 圆心百分比 ----------
- AppText {
- anchors.centerIn: parent
- text: Math.round(root.percent) + "%"
- color: "white"
- font.bold: true
- font.pixelSize: sp(13)
- }
- }
|