ProgressIndicator.qml 3.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
import QtQml 2.2
import QtQuick 2.0
import QtLocation 5.3

MapQuickItem {
    id: root

    width: 20
    height: 20
    anchorPoint.x: width/2
    anchorPoint.y: height/2

    property color primaryColor: "orange"
    property color secondaryColor: "lightblue"

    property real centerWidth: width / 2
    property real centerHeight: height / 2
    property real radius: Math.min(canvas.width, canvas.height) / 2

    property real minimumValue: 0
    property real maximumValue: 100
    property real currentValue: 33

    // this is the angle that splits the circle in two arcs
    // first arc is drawn from 0 radians to angle radians
    // second arc is angle radians to 2*PI radians
    property real angle: (currentValue - minimumValue) / (maximumValue - minimumValue) * 2 * Math.PI

    // we want both circle to start / end at 12 o'clock
    // without this offset we would start / end at 9 o'clock
    property real angleOffset: -Math.PI / 2

    property string text: ""

    signal clicked()

    onPrimaryColorChanged: canvas.requestPaint()
    onSecondaryColorChanged: canvas.requestPaint()
    onMinimumValueChanged: canvas.requestPaint()
    onMaximumValueChanged: canvas.requestPaint()
    onCurrentValueChanged: canvas.requestPaint()

    // draws two arcs (portion of a circle)
    // fills the circle with a lighter secondary color
    // when pressed
    sourceItem: Canvas {
        id: canvas
        width: root.width
        height: root.height
        antialiasing: true

        onPaint: {
            var ctx = getContext("2d");
            ctx.save();

            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // fills the mouse area when pressed
            // the fill color is a lighter version of the
            // secondary color

            if (mouseArea.pressed) {
                ctx.beginPath();
                ctx.lineWidth = 1;
                ctx.fillStyle = Qt.lighter(root.secondaryColor, 1.25);
                ctx.arc(root.centerWidth,
                        root.centerHeight,
                        root.radius,
                        0,
                        2*Math.PI);
                ctx.fill();
            }

            // First, thinner arc
            // From angle to 2*PI

            ctx.beginPath();
            ctx.lineWidth = 1;
            ctx.strokeStyle = primaryColor;
            ctx.arc(root.centerWidth,
                    root.centerHeight,
                    root.radius,
                    angleOffset + root.angle,
                    angleOffset + 2*Math.PI);
            ctx.stroke();


            // Second, thicker arc
            // From 0 to angle

            ctx.beginPath();
            ctx.lineWidth = 3;
            ctx.strokeStyle = root.secondaryColor;
            ctx.arc(root.centerWidth,
                    root.centerHeight,
                    root.radius,
                    root.angleOffset,
                    root.angleOffset + root.angle);
            ctx.stroke();

            ctx.restore();
        }

        Text {
            anchors.centerIn: parent

            text: root.text
            color: root.primaryColor
        }

        MouseArea {
            id: mouseArea

            anchors.fill: parent
            onClicked: root.clicked()
            onPressedChanged: canvas.requestPaint()
        }
    }
}