QGCHoverButton.qml 4.97 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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Button {
    id: button
    width: columnItem.contentWidth + contentLayoutItem.margins * 2
    height: width
    flat: true

    property color borderColor: qgcPal.windowShadeDark

    property alias radius: buttonBkRect.radius
    property alias fontPointSize: innerText.font.pointSize
    property alias imageSource: innerImage.source
    property alias contentWidth: innerText.contentWidth

    property real  imageScale: 0.8
    property real  borderWidth: 0
    property real  contentMargins: innerText.height * 0.1

27 28
    property color _currentColor: qgcPal.button
    property color _currentContentColor: qgcPal.buttonText
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

    QGCPalette { id: qgcPal }
    QGCPalette { id: qgcPalDisabled; colorGroupEnabled: false }

    // Initial state
    state:         "Default"
    // Update state on status changed
    onEnabledChanged: state = "Default"

    property real _contentVDist: innerImage.height/innerText.contentHeight

    // Content Icon + Text
    contentItem: Item {
        id: contentLayoutItem
        anchors.fill: parent
        anchors.margins: contentMargins

        Column {
            id: columnItem
            anchors.fill: parent

            Item {
                width: parent.width
                height: (contentLayoutItem.height - innerText.height)
                Image {
                    id: innerImage

                    anchors.centerIn: parent

                    height: parent.height * imageScale
                    width: parent.width * imageScale

                    visible: false
                    smooth: true
                    antialiasing: true
                    mipmap: true
                    fillMode: Image.PreserveAspectFit
                    sourceSize.height: height
                    sourceSize.width: width
                    horizontalAlignment: Image.AlignHCenter
                    verticalAlignment: Image.AlignVCenter
                }

                ColorOverlay {
                    id: imageOverlay
                    anchors.fill: innerImage
                    source: innerImage

                    color: _currentContentColor
                }
            }

            Text {
Gus Grubba's avatar
Gus Grubba committed
82 83 84 85 86 87 88 89
                id:                     innerText
                text:                   button.text
                color:                  _currentContentColor
                width:                  parent.width
                font.family:            ScreenTools.normalFontFamily
                font.pointSize:         ScreenTools.defaultFontPointSize
                horizontalAlignment:    Text.AlignHCenter
                verticalAlignment:      Text.AlignVCenter
90 91 92 93 94 95 96 97
            }
        } // Column - content
    } // Item - content

    background: Rectangle {
        id: buttonBkRect
        anchors.fill: parent
        color: _currentColor
98
        visible: !flat
99 100 101 102 103

        border.width: borderWidth
        border.color: borderColor
    }

104
    // Change the colors based on button states
105 106 107 108 109
    states: [
        State {
            name: "Hovering"
            PropertyChanges {
                target: button;
110 111 112 113 114 115
                _currentColor: (checked || pressed) ? qgcPal.buttonHighlight : qgcPal.hoverColor
                _currentContentColor: qgcPal.buttonHighlightText
            }
            PropertyChanges {
                target: buttonBkRect
                visible: true
116 117 118 119 120 121
            }
        },
        State {
            name: "Default"
            PropertyChanges {
                target: button;
122
                _currentColor: enabled ? ((checked || pressed) ? qgcPal.buttonHighlight : qgcPal.button) : qgcPalDisabled.button
Stefan Dunca's avatar
Stefan Dunca committed
123
                _currentContentColor: enabled ? ((checked || pressed) ? qgcPal.buttonHighlightText : qgcPal.buttonText) : qgcPalDisabled.buttonText
124 125 126 127
            }
            PropertyChanges {
                target: buttonBkRect
                visible: !flat || (checked || pressed)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            }
        }
    ]

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

    // Process hover events
    MouseArea {
        enabled: !ScreenTools.isMobile
        hoverEnabled: true
        propagateComposedEvents: true
        preventStealing: true
        anchors.fill: button
        onEntered: { button.state = 'Hovering'; }
        onExited: { button.state = 'Default'; }
        // Propagate events down
        onClicked: { mouse.accepted = false; }
        onDoubleClicked: { mouse.accepted = false; }
        onPositionChanged: { mouse.accepted = false; }
        onPressAndHold: { mouse.accepted = false; }
        onPressed: { mouse.accepted = false }
        onReleased: { mouse.accepted = false }
    }
}