Gauge.qml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import QtQuick 2.7
  2. import QtQuick.Controls 2.0
  3. import QtQuick.Controls.Styles 1.4
  4. import QtQuick.Extras 1.4
  5. import QtQuick.Extras.Private 1.0
  6. import QtGraphicalEffects 1.0
  7. CircularGauge {
  8. id: gauge
  9. property int tickmarkStepSize: 100
  10. property int labelStepSize: 100
  11. property real realValue:0
  12. style: CircularGaugeStyle {
  13. id:circularGaugeStyle
  14. tickmarkStepSize:gauge.tickmarkStepSize
  15. minorTickmarkCount:4
  16. labelStepSize: gauge.labelStepSize
  17. labelInset: outerRadius / 2.2
  18. tickmarkInset: outerRadius / 4.2
  19. minorTickmarkInset: outerRadius / 4.2
  20. minimumValueAngle: -144
  21. maximumValueAngle: 144
  22. background: Rectangle {
  23. implicitHeight: gauge.height
  24. implicitWidth: gauge.width
  25. color: "black"
  26. anchors.centerIn: parent
  27. radius: 360
  28. Image {
  29. anchors.fill: parent
  30. source: "qrc:/img/background.svg"
  31. asynchronous: true
  32. sourceSize {
  33. width: width
  34. }
  35. }
  36. Canvas {
  37. renderStrategy: Canvas.Threaded
  38. property int value: gauge.value
  39. anchors.fill: parent
  40. onValueChanged: requestPaint()
  41. function degreesToRadians(degrees) {
  42. return degrees * (Math.PI / 180);
  43. }
  44. onPaint: {
  45. var ctx = getContext("2d");
  46. ctx.reset();
  47. ctx.beginPath();
  48. ctx.strokeStyle = "black"
  49. //ctx.strokeStyle = "green"
  50. ctx.lineWidth = outerRadius
  51. ctx.arc(outerRadius,
  52. outerRadius,
  53. outerRadius - ctx.lineWidth / 2,
  54. degreesToRadians(valueToAngle(gauge.value) - 90),
  55. degreesToRadians(valueToAngle(gauge.maximumValue + 1) - 90));
  56. ctx.stroke();
  57. }
  58. }
  59. }
  60. needle: Item {
  61. y: -outerRadius * 0.78
  62. height: outerRadius * 0.27
  63. Image {
  64. id: needle
  65. source: "qrc:/img/needle.svg"
  66. height: parent.height
  67. width: height * 0.1
  68. asynchronous: true
  69. antialiasing: true
  70. }
  71. Glow {
  72. anchors.fill: needle
  73. radius: 5
  74. samples: 10
  75. color: "white"
  76. source: needle
  77. }
  78. }
  79. foreground: Item {
  80. Text {
  81. id: speedLabel
  82. anchors.centerIn: parent
  83. text: gauge.value.toFixed(1) //realValue.toFixed(1)
  84. font.pixelSize: outerRadius * 0.2
  85. color: "white"
  86. antialiasing: true
  87. }
  88. }
  89. tickmarkLabel: Text {
  90. font.pixelSize: 15//Math.max(6, outerRadius * 0.05)
  91. text: styleData.value
  92. color: styleData.value <= gauge.value ? "LightSalmon" : "#777776"
  93. antialiasing: true
  94. }
  95. tickmark: Image {
  96. source: "qrc:/img/tickmark.svg"
  97. width: outerRadius * 0.018
  98. height: outerRadius * 0.15
  99. antialiasing: true
  100. asynchronous: true
  101. }
  102. minorTickmark: Rectangle {
  103. implicitWidth: outerRadius * 0.01
  104. implicitHeight: outerRadius * 0.03
  105. antialiasing: true
  106. smooth: true
  107. //color: styleData.value <= gauge.value ? "white" : "darkGray"
  108. color: styleData.value <= gauge.value ? "green" : "darkGray"
  109. }
  110. }
  111. }