TNormalProgress.qml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //NormalProgressBar.qml
  2. import QtQuick 2.12
  3. import QtQuick.Controls 2.12
  4. Item {
  5. id: r
  6. property int percent: 0
  7. implicitWidth: 200
  8. implicitHeight: 16
  9. //枚举, 表示右侧Bar的类型
  10. enum BarType {
  11. Text, //右侧放文本
  12. SucceedOrFailed, //右侧放图片表示成功和失败,没有100%就是失败
  13. NoBar //右侧不放东西
  14. }
  15. //只读属性,内置一些颜色
  16. readonly property color __backColor: "#f5f5f5"
  17. readonly property color __blueColor: "#1890ff"
  18. readonly property color __succeedColor: "#52c41a"
  19. readonly property color __failedColor: "#f5222d"
  20. //背景色,默认值
  21. property color backgroundColor: __backColor
  22. //前景色
  23. property color frontColor: {
  24. switch (barType) {
  25. case TNormalProgress.BarType.SucceedOrFailed:
  26. return percent === 100 ? __succeedColor : __failedColor
  27. default:
  28. return __blueColor
  29. }
  30. }
  31. //文字
  32. property string text: String("%1%").arg(percent)
  33. //渐变 0-180 除掉不能用的,165种渐变任你选
  34. property int gradientIndex: -1
  35. //闪烁特效
  36. property bool flicker: false
  37. //右侧Bar类型
  38. property var barType: TNormalProgress.BarType.Text
  39. Text {
  40. id: t
  41. enabled: barType === TNormalProgress.BarType.Text
  42. visible: enabled
  43. text: r.text
  44. anchors.verticalCenter: parent.verticalCenter
  45. anchors.right: parent.right
  46. }
  47. Image {
  48. id: image
  49. source: percent === 100 ? "qrc:/img/ok_circle.png" : "qrc:/img/fail_circle.png"
  50. height: parent.height
  51. width: height
  52. enabled: barType === TNormalProgress.BarType.SucceedOrFailed
  53. visible: enabled
  54. anchors.right: parent.right
  55. }
  56. property var __right: {
  57. switch (barType) {
  58. case TNormalProgress.BarType.Text:
  59. return t.left
  60. case TNormalProgress.BarType.SucceedOrFailed:
  61. return image.left
  62. default:
  63. return r.right
  64. }
  65. }
  66. Rectangle { //背景
  67. id: back
  68. anchors.left: parent.left
  69. anchors.right: __right
  70. anchors.rightMargin: 4
  71. height: parent.height
  72. radius: height / 2
  73. color: backgroundColor
  74. Rectangle { //前景
  75. id: front
  76. width: percent / 100 * parent.width
  77. height: parent.height
  78. radius: parent.radius
  79. color: frontColor
  80. gradient: gradientIndex === -1 ? null : gradientIndex
  81. Rectangle { //前景上的闪光特效
  82. id: flick
  83. height: parent.height
  84. width: 0
  85. radius: parent.radius
  86. color: Qt.lighter(parent.color, 1.2)
  87. enabled: flicker
  88. visible: enabled
  89. NumberAnimation on width {
  90. running: visible
  91. from: 0
  92. to: front.width
  93. duration: 1000
  94. loops: Animation.Infinite;
  95. }
  96. }
  97. }
  98. }
  99. }