QGCHoverButton.qml 4.36 KB
Newer Older
1 2 3 4 5 6 7 8
import QtQuick 2.3
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0

// TODO: Use QT styles. Use default button style + custom style entries
import QGroundControl.ScreenTools 1.0
import QGroundControl.Palette 1.0

9
// TODO: use QT palette
10
Button {
Gus Grubba's avatar
Gus Grubba committed
11 12
    id:     button
    width:  contentLayoutItem.contentWidth + (contentMargins * 2)
13
    height: width
Gus Grubba's avatar
Gus Grubba committed
14
    flat:   true
15

Gus Grubba's avatar
Gus Grubba committed
16
    property color borderColor:     qgcPal.windowShadeDark
17

Gus Grubba's avatar
Gus Grubba committed
18 19 20 21
    property alias radius:          buttonBkRect.radius
    property alias fontPointSize:   innerText.font.pointSize
    property alias imageSource:     innerImage.source
    property alias contentWidth:    innerText.contentWidth
22

Gus Grubba's avatar
Gus Grubba committed
23 24
    property real  imageScale:      0.6
    property real  borderWidth:     0
25 26
    property real  contentMargins: innerText.height * 0.1

Gus Grubba's avatar
Gus Grubba committed
27 28
    property color _currentColor:           qgcPal.button
    property color _currentContentColor:    qgcPal.buttonText
29 30 31 32

    QGCPalette { id: qgcPalDisabled; colorGroupEnabled: false }

    // Initial state
Gus Grubba's avatar
Gus Grubba committed
33
    state:                  "Default"
34 35 36
    // Update state on status changed
    // Content Icon + Text
    contentItem: Item {
Gus Grubba's avatar
Gus Grubba committed
37 38 39
        id:                 contentLayoutItem
        anchors.fill:       parent
        anchors.margins:    contentMargins
40
        Column {
Gus Grubba's avatar
Gus Grubba committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54
            anchors.centerIn:   parent
            spacing:        contentMargins * 2
            QGCColoredImage {
                id:         innerImage
                height:     contentLayoutItem.height * imageScale
                width:      contentLayoutItem.width  * imageScale
                smooth:     true
                mipmap:     true
                color:      _currentContentColor
                fillMode:   Image.PreserveAspectFit
                antialiasing: true
                sourceSize.height:  height
                sourceSize.width:   width
                anchors.horizontalCenter: parent.horizontalCenter
55
            }
Gus Grubba's avatar
Gus Grubba committed
56 57 58 59 60
            QGCLabel {
                id:         innerText
                text:       button.text
                color:      _currentContentColor
                anchors.horizontalCenter: parent.horizontalCenter
61
            }
Gus Grubba's avatar
Gus Grubba committed
62 63
        }
    }
64 65

    background: Rectangle {
Gus Grubba's avatar
Gus Grubba committed
66 67 68 69 70 71
        id:                 buttonBkRect
        color:              _currentColor
        visible:            !flat
        border.width:       borderWidth
        border.color:       borderColor
        anchors.fill:       parent
72 73
    }

74
    // Change the colors based on button states
75 76 77 78 79
    states: [
        State {
            name: "Hovering"
            PropertyChanges {
                target: button;
Gus Grubba's avatar
Gus Grubba committed
80 81
                _currentColor:          (checked || pressed) ? qgcPal.buttonHighlight : qgcPal.hoverColor
                _currentContentColor:   qgcPal.buttonHighlightText
82 83 84 85
            }
            PropertyChanges {
                target: buttonBkRect
                visible: true
86 87 88 89 90 91
            }
        },
        State {
            name: "Default"
            PropertyChanges {
                target: button;
Gus Grubba's avatar
Gus Grubba committed
92 93
                _currentColor:          enabled ? ((checked || pressed) ? qgcPal.buttonHighlight : qgcPal.button) :         qgcPalDisabled.button
                _currentContentColor:   enabled ? ((checked || pressed) ? qgcPal.buttonHighlightText : qgcPal.buttonText) : qgcPalDisabled.buttonText
94 95 96 97
            }
            PropertyChanges {
                target: buttonBkRect
                visible: !flat || (checked || pressed)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            }
        }
    ]

    transitions: [
        Transition {
            from: ""; to: "Hovering"
            ColorAnimation { duration: 200 }
        },
        Transition {
            from: "*"; to: "Pressed"
            ColorAnimation { duration: 10 }
        }
    ]

    // Process hover events
    MouseArea {
Gus Grubba's avatar
Gus Grubba committed
115
        enabled:            !ScreenTools.isMobile
116
        propagateComposedEvents: true
Gus Grubba's avatar
Gus Grubba committed
117 118 119 120 121
        hoverEnabled:       true
        preventStealing:    true
        anchors.fill:       button
        onEntered:          button.state = 'Hovering'
        onExited:           button.state = 'Default'
122
        // Propagate events down
Gus Grubba's avatar
Gus Grubba committed
123 124 125 126 127 128
        onClicked:          { mouse.accepted = false; }
        onDoubleClicked:    { mouse.accepted = false; }
        onPositionChanged:  { mouse.accepted = false; }
        onPressAndHold:     { mouse.accepted = false; }
        onPressed:          { mouse.accepted = false }
        onReleased:         { mouse.accepted = false }
129 130
    }
}