SliderSwitch.qml 2.76 KB
Newer Older
1 2
import QtQuick                  2.3
import QtQuick.Controls         1.2
Don Gagne's avatar
Don Gagne committed
3 4 5 6 7 8 9

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 {
10 11
    id:             _root
    implicitWidth:  label.contentWidth + (_diameter * 2.5) + (_border * 4)
DonLakeFlyer's avatar
DonLakeFlyer committed
12
    implicitHeight: label.height * 2.5
13
    radius:         height /2
14
    color:          qgcPal.windowShade
Don Gagne's avatar
Don Gagne committed
15 16 17 18

    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
31
        text:                       confirmText
32
        color:                      qgcPal.buttonText
Don Gagne's avatar
Don Gagne committed
33 34 35 36 37 38 39 40 41
    }

    Rectangle {
        id:         slider
        x:          _border
        y:          _border
        height:     _diameter
        width:      _diameter
        radius:     _diameter / 2
42
        color:      qgcPal.primaryButton
Don Gagne's avatar
Don Gagne committed
43

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
            color:                  qgcPal.buttonText
53 54 55 56
            cache:                  false
            source:                 "/res/ArrowRight.svg"
        }

57 58
    }

DonLakeFlyer's avatar
DonLakeFlyer committed
59
    QGCMouseArea {
60 61
        id:                 sliderDragArea
        anchors.leftMargin: -ScreenTools.defaultFontPixelWidth * 15
DonLakeFlyer's avatar
DonLakeFlyer committed
62
        fillItem:           slider
63 64 65 66 67 68 69 70 71
        drag.target:        slider
        drag.axis:          Drag.XAxis
        drag.minimumX:      _border
        drag.maximumX:      _maxXDrag
        preventStealing:    true

        property real _maxXDrag:    _root.width - (_diameter + _border)
        property bool dragActive:   drag.active
        property real _dragOffset:  1
Don Gagne's avatar
Don Gagne committed
72

73
        //Component.onCompleted: console.log(height, ScreenTools.minTouchPixels)
DonLakeFlyer's avatar
DonLakeFlyer committed
74

75 76 77
        onPressed: {
            mouse.x
        }
Don Gagne's avatar
Don Gagne committed
78

79 80 81 82
        onDragActiveChanged: {
            if (!sliderDragArea.drag.active) {
                if (slider.x > _maxXDrag - _border) {
                    _root.accept()
Don Gagne's avatar
Don Gagne committed
83
                }
84
                slider.x = _border
Don Gagne's avatar
Don Gagne committed
85 86 87 88
            }
        }
    }
}