SliderSwitch.qml 3.63 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10
import QtQuick                  2.5
import QtQuick.Controls         1.4

import QGroundControl.ScreenTools   1.0
import QGroundControl.Palette       1.0

/// The SliderSwitch control implements a sliding switch control similar to the power off
/// control on an iPhone.
Rectangle {
    id:     _root
11
    width:  label.contentWidth + (_diameter * 2.5) + (_border * 4)
Don Gagne's avatar
Don Gagne committed
12 13 14 15 16 17 18
    radius: height /2
    color:  qgcPal.window

    signal accept   ///< Action confirmed
    signal reject   ///< Action rejected

    property string confirmText ///< Text for slider
19
    property bool   showReject: false
Don Gagne's avatar
Don Gagne committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

    property real _border: 4
    property real _diameter: height - (_border * 2)

    QGCPalette { id: qgcPal; colorGroupEnabled: true }

    QGCLabel {
        id: label
        anchors.horizontalCenter:   parent.horizontalCenter
        anchors.verticalCenter:     parent.verticalCenter
        text:   "Slide to " + confirmText
    }

    Rectangle {
        id:         slider
        x:          _border
        y:          _border
        height:     _diameter
        width:      _diameter
        radius:     _diameter / 2
        color:      qgcPal.windowShade
        opacity:    0.8

43 44 45 46 47 48 49 50 51 52 53 54 55
        QGCColoredImage {
            anchors.centerIn:       parent
            width:                  parent.width  * 0.8
            height:                 parent.height * 0.8
            fillMode:               Image.PreserveAspectFit
            smooth:                 false
            mipmap:                 false
            color:                  qgcPal.text
            cache:                  false
            source:                 "/res/ArrowRight.svg"
        }

        /*
Don Gagne's avatar
Don Gagne committed
56 57 58 59 60
        QGCLabel {
            anchors.horizontalCenter:   parent.horizontalCenter
            anchors.verticalCenter:     parent.verticalCenter
            text: ">"
        }
61
        */
Don Gagne's avatar
Don Gagne committed
62

Don Gagne's avatar
Don Gagne committed
63 64 65 66 67 68 69 70 71
        MouseArea {
            id:             sliderDragArea
            anchors.fill:   parent
            onClicked:      _root.accept()
            drag.target:    slider
            drag.axis:      Drag.XAxis
            drag.minimumX:  _border
            drag.maximumX:  _maxXDrag

72
            property real _maxXDrag:    _root.width - ((_diameter + _border) * (showReject ? 2 : 1))
Don Gagne's avatar
Don Gagne committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            property bool dragActive:   drag.active

            onDragActiveChanged: {
                if (!sliderDragArea.drag.active) {
                    if (slider.x > _maxXDrag - _border) {
                        _root.accept()
                    }
                    slider.x = _border
                }
            }
        }
    }

    Rectangle {
        id:                     cancel
        anchors.rightMargin:    _border
        anchors.right:          parent.right
        anchors.verticalCenter: parent.verticalCenter
        height:                 _diameter
        width:                  _diameter
        radius:                 _diameter / 2
        color:                  qgcPal.windowShade
        opacity:                0.8
96 97 98 99 100 101 102 103 104 105
        visible:                showReject

        Image {
            anchors.centerIn:       parent
            width:                  parent.width  * 0.8
            height:                 parent.height * 0.8
            fillMode:               Image.PreserveAspectFit
            smooth:                 true
            source:                 "/res/cancel.svg"
        }
Don Gagne's avatar
Don Gagne committed
106

107
        /*
Don Gagne's avatar
Don Gagne committed
108 109 110 111 112
        QGCLabel {
            anchors.horizontalCenter:   parent.horizontalCenter
            anchors.verticalCenter:     parent.verticalCenter
            text: "X"
        }
113
        */
Don Gagne's avatar
Don Gagne committed
114

Don Gagne's avatar
Don Gagne committed
115 116 117 118 119 120
        MouseArea {
            anchors.fill:   parent
            onClicked:      _root.reject()
        }
    }
}