| 1 |
import QtQuick 2.0 |
| 2 |
import QtQml 2.1 |
| 3 |
|
| 4 |
Canvas { |
| 5 |
id: canvas |
| 6 |
//width: 240 |
| 7 |
//height: 240 |
| 8 |
|
| 9 |
property color primaryColor: "darkorange" |
| 10 |
property color secondaryColor: "steelblue" |
| 11 |
|
| 12 |
property real centerWidth: width / 2 |
| 13 |
property real centerHeight: height / 2 |
| 14 |
property real radius: Math.min(canvas.width - 8, canvas.height - 8) / 2 |
| 15 |
|
| 16 |
property real minimumValue: 0 |
| 17 |
property real maximumValue: 100 |
| 18 |
property real currentValue: 0 |
| 19 |
// To show on click |
| 20 |
property real overallValue: -1 |
| 21 |
property real viewValue: (overallValue > -1 && mouseArea.pressed) ? overallValue : currentValue |
| 22 |
property real curId: 0 |
| 23 |
property real maxId: 0 |
| 24 |
|
| 25 |
// this is the angle that splits the circle in two arcs |
| 26 |
// first arc is drawn from 0 radians to angle radians |
| 27 |
// second arc is angle radians to 2*PI radians |
| 28 |
property real angle: (viewValue - minimumValue) / (maximumValue - minimumValue) * 2 * Math.PI |
| 29 |
|
| 30 |
// we want both circle to start / end at 12 o'clock |
| 31 |
// without this offset we would start / end at 9 o'clock |
| 32 |
property real angleOffset: -Math.PI / 2 |
| 33 |
|
| 34 |
property string text: "" |
| 35 |
property string statusText: "" |
| 36 |
|
| 37 |
signal clicked() |
| 38 |
|
| 39 |
onPrimaryColorChanged: requestPaint() |
| 40 |
onSecondaryColorChanged: requestPaint() |
| 41 |
onMinimumValueChanged: requestPaint() |
| 42 |
onMaximumValueChanged: requestPaint() |
| 43 |
onHeightChanged: requestPaint(); |
| 44 |
// Keeps turning off |
| 45 |
onViewValueChanged: { requestPaint(); antialiasing = true; } |
| 46 |
|
| 47 |
onPaint: { |
| 48 |
var ctx = getContext("2d"); |
| 49 |
ctx.save(); |
| 50 |
|
| 51 |
ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 52 |
|
| 53 |
// fills the mouse area when pressed |
| 54 |
// the fill color is a lighter version of the |
| 55 |
// secondary color |
| 56 |
|
| 57 |
if (mouseArea.pressed) { |
| 58 |
ctx.beginPath(); |
| 59 |
ctx.lineWidth = 1; |
| 60 |
ctx.fillStyle = Qt.lighter(canvas.primaryColor, 1.25); |
| 61 |
ctx.arc(canvas.centerWidth, |
| 62 |
canvas.centerHeight, |
| 63 |
canvas.radius, |
| 64 |
0, |
| 65 |
2*Math.PI); |
| 66 |
ctx.fill(); |
| 67 |
} |
| 68 |
|
| 69 |
// First, thinner arc |
| 70 |
// From angle to 2*PI |
| 71 |
|
| 72 |
ctx.beginPath(); |
| 73 |
ctx.lineWidth = 3; |
| 74 |
ctx.strokeStyle = primaryColor; |
| 75 |
ctx.arc(canvas.centerWidth, |
| 76 |
canvas.centerHeight, |
| 77 |
canvas.radius, |
| 78 |
angleOffset + canvas.angle, |
| 79 |
angleOffset + 2*Math.PI); |
| 80 |
ctx.stroke(); |
| 81 |
|
| 82 |
|
| 83 |
// Second, thicker arc |
| 84 |
// From 0 to angle |
| 85 |
|
| 86 |
ctx.beginPath(); |
| 87 |
ctx.lineWidth = 8; |
| 88 |
ctx.strokeStyle = canvas.secondaryColor; |
| 89 |
ctx.arc(canvas.centerWidth, |
| 90 |
canvas.centerHeight, |
| 91 |
canvas.radius, |
| 92 |
canvas.angleOffset, |
| 93 |
canvas.angleOffset + canvas.angle); |
| 94 |
ctx.stroke(); |
| 95 |
|
| 96 |
ctx.restore(); |
| 97 |
} |
| 98 |
|
| 99 |
Column { |
| 100 |
anchors.centerIn: parent |
| 101 |
spacing: height / 6 |
| 102 |
add: Transition { |
| 103 |
NumberAnimation { |
| 104 |
duration: 100 |
| 105 |
properties: "y" |
| 106 |
easing.type: Easing.OutBounce |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
Text { |
| 111 |
text: canvas.statusText |
| 112 |
font.bold: true |
| 113 |
anchors.horizontalCenter: parent.horizontalCenter |
| 114 |
} |
| 115 |
|
| 116 |
Text { |
| 117 |
text: canvas.text.substring(0, 35) |
| 118 |
//color: canvas.primaryColor |
| 119 |
anchors.horizontalCenter: parent.horizontalCenter |
| 120 |
} |
| 121 |
// Show count if we are looking at individual percents |
| 122 |
Text { |
| 123 |
visible: !mouseArea.pressed && maxId > 0; |
| 124 |
text: qsTr("%1 of %2").arg(curId).arg(maxId) + translator.lang |
| 125 |
anchors.horizontalCenter: parent.horizontalCenter |
| 126 |
} |
| 127 |
Text { |
| 128 |
text: viewValue + "%" |
| 129 |
font.bold: true |
| 130 |
scale: mouseArea.pressed ? 2.0 : 1.0 |
| 131 |
Behavior on scale { NumberAnimation { duration: 100 } } |
| 132 |
color: canvas.secondaryColor |
| 133 |
anchors.horizontalCenter: parent.horizontalCenter |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
MouseArea { |
| 138 |
id: mouseArea |
| 139 |
|
| 140 |
anchors.fill: parent |
| 141 |
onClicked: canvas.clicked(); |
| 142 |
onPressedChanged: canvas.requestPaint() |
| 143 |
} |
| 144 |
} |