UploadWaterProgress.qml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import QtQuick
  2. import Felgo 4.0
  3. /**
  4. * 上传进度圆形控件(灌水效果)。
  5. *
  6. * 业务背景:本设备一次只上传一张图片(上传队列串行,见 CurlFtpUploader::runLoop),
  7. * 所以进度用单个圆形水位表示即可,不需要按图片序号铺多格网格。
  8. *
  9. * 视觉:外圈细圆环 + 内部自下而上"灌水"填充,水面带正弦波纹并持续流动,
  10. * 圆心显示百分比。失败时整只圆环与水体转红,便于现场一眼区分。
  11. *
  12. * 实现:整块用 Canvas 绘制。
  13. * 注意 Item.clip 只裁剪矩形、不裁剪圆角,靠 clip 属性做圆形裁剪会失效;
  14. * Canvas 里用 arc + clip() 才是可靠的圆形裁剪方式。
  15. */
  16. Item {
  17. id: root
  18. // 进度 0~100
  19. property real percent: 0
  20. // 是否失败(决定配色)
  21. property bool failed: false
  22. // 圆形直径
  23. property real diameter: dp(56)
  24. readonly property color themeColor: failed ? "#e74c3c" : "#28a779"
  25. // 圆环线宽
  26. readonly property real ringWidth: dp(2)
  27. implicitWidth: diameter
  28. implicitHeight: diameter
  29. // 波纹相位:由动画驱动,变化时重绘 Canvas
  30. property real wavePhase: 0
  31. NumberAnimation on wavePhase {
  32. from: 0; to: Math.PI * 2
  33. duration: 1600
  34. loops: Animation.Infinite
  35. running: root.visible && root.percent > 0 && root.percent < 100
  36. }
  37. Canvas {
  38. id: canvas
  39. anchors.fill: parent
  40. renderStrategy: Canvas.Cooperative
  41. onWidthChanged: requestPaint()
  42. onHeightChanged: requestPaint()
  43. // percent / 配色 / 相位变化都要重绘
  44. Connections {
  45. target: root
  46. function onPercentChanged() { canvas.requestPaint() }
  47. function onFailedChanged() { canvas.requestPaint() }
  48. function onWavePhaseChanged() { canvas.requestPaint() }
  49. }
  50. onPaint: {
  51. var ctx = getContext("2d")
  52. ctx.reset()
  53. var w = width
  54. var h = height
  55. var cx = w / 2
  56. var cy = h / 2
  57. var r = Math.min(cx, cy) - root.ringWidth
  58. if (r <= 0) return
  59. var p = Math.max(0, Math.min(100, root.percent)) / 100
  60. // ---------- 底色 ----------
  61. ctx.beginPath()
  62. ctx.arc(cx, cy, r, 0, Math.PI * 2)
  63. ctx.fillStyle = "#66000000"
  64. ctx.fill()
  65. // ---------- 水体(圆形裁剪内自下而上填充)----------
  66. if (p > 0) {
  67. var levelY = cy + r - p * (r * 2) // 水面 y:p=0 在底部,p=1 在顶部
  68. var amp = Math.min(dp(2), r * 0.12) // 波纹振幅,随半径自适应
  69. ctx.save()
  70. ctx.beginPath()
  71. ctx.arc(cx, cy, r, 0, Math.PI * 2)
  72. ctx.clip()
  73. ctx.beginPath()
  74. ctx.moveTo(cx - r, h)
  75. ctx.lineTo(cx - r, levelY)
  76. // 水面:叠加两个谐波形成自然起伏
  77. var steps = 48
  78. for (var i = 0; i <= steps; i++) {
  79. var x = cx - r + (2 * r) * (i / steps)
  80. var y = levelY
  81. + Math.sin((i / steps) * Math.PI * 4 + root.wavePhase) * amp
  82. + Math.sin((i / steps) * Math.PI * 6 - root.wavePhase * 1.3) * (amp * 0.4)
  83. ctx.lineTo(x, y)
  84. }
  85. ctx.lineTo(cx + r, h)
  86. ctx.closePath()
  87. ctx.fillStyle = root.themeColor
  88. ctx.fill()
  89. ctx.restore()
  90. }
  91. // ---------- 外圈 ----------
  92. ctx.beginPath()
  93. ctx.arc(cx, cy, r + root.ringWidth / 2, 0, Math.PI * 2)
  94. ctx.strokeStyle = root.themeColor
  95. ctx.lineWidth = root.ringWidth
  96. ctx.stroke()
  97. }
  98. }
  99. // ---------- 圆心百分比 ----------
  100. AppText {
  101. anchors.centerIn: parent
  102. text: Math.round(root.percent) + "%"
  103. color: "white"
  104. font.bold: true
  105. font.pixelSize: sp(13)
  106. }
  107. }