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

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)
12
    height: label.height * 2.5
Don Gagne's avatar
Don Gagne committed
13 14 15 16 17 18
    radius: height /2
    color:  qgcPal.window

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

19 20
    property string confirmText                         ///< Text for slider
    property alias  fontPointSize: label.font.pointSize ///< Point size for text
Don Gagne's avatar
Don Gagne committed
21 22 23 24 25 26 27

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

    QGCPalette { id: qgcPal; colorGroupEnabled: true }

    QGCLabel {
Don Gagne's avatar
Don Gagne committed
28
        id:                         label
Don Gagne's avatar
Don Gagne committed
29 30
        anchors.horizontalCenter:   parent.horizontalCenter
        anchors.verticalCenter:     parent.verticalCenter
Don Gagne's avatar
Don Gagne committed
31
        text:                       qsTr("Slide to %1").arg(confirmText)
Don Gagne's avatar
Don Gagne committed
32 33 34 35 36 37 38 39 40 41 42 43
    }

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

44 45 46 47
        QGCColoredImage {
            anchors.centerIn:       parent
            width:                  parent.width  * 0.8
            height:                 parent.height * 0.8
dogmaphobic's avatar
dogmaphobic committed
48
            sourceSize.height:      height
49 50 51
            fillMode:               Image.PreserveAspectFit
            smooth:                 false
            mipmap:                 false
52 53


54 55 56 57 58
            color:                  qgcPal.text
            cache:                  false
            source:                 "/res/ArrowRight.svg"
        }

Don Gagne's avatar
Don Gagne committed
59 60 61 62 63 64 65 66
        MouseArea {
            id:             sliderDragArea
            anchors.fill:   parent
            drag.target:    slider
            drag.axis:      Drag.XAxis
            drag.minimumX:  _border
            drag.maximumX:  _maxXDrag

Don Gagne's avatar
Don Gagne committed
67
            property real _maxXDrag:    _root.width - (_diameter + _border)
Don Gagne's avatar
Don Gagne committed
68 69 70 71 72 73 74 75 76 77 78 79 80
            property bool dragActive:   drag.active

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