QGCButton.qml 4.43 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
4
import QtQuick.Controls.Private 1.0
Don Gagne's avatar
Don Gagne committed
5 6

import QGroundControl.Palette 1.0
7
import QGroundControl.ScreenTools 1.0
Don Gagne's avatar
Don Gagne committed
8 9

Button {
10

11
    property bool primary:      false                                   ///< primary button for a group of buttons
12

13 14 15
    property var    _qgcPal:            QGCPalette { colorGroupEnabled: enabled }
    property bool   _showHighlight:     (pressed | hovered | checked) && !__forceHoverOff
    property bool   _showBorder:        _qgcPal.globalTheme == QGCPalette.Light
Don Gagne's avatar
Don Gagne committed
16

Don Gagne's avatar
Don Gagne committed
17 18 19 20 21
    // This fixes the issue with button hover where if a Button is near the edge oa QQuickWidget you can
    // move the mouse fast enough such that the MouseArea does not trigger an onExited. This is turn
    // cause the hover property to not be cleared correctly.

    property bool __forceHoverOff: false
Don Gagne's avatar
Don Gagne committed
22

Don Gagne's avatar
Don Gagne committed
23 24
    property int __lastGlobalMouseX: 0
    property int __lastGlobalMouseY: 0
Don Gagne's avatar
Don Gagne committed
25

Don Gagne's avatar
Don Gagne committed
26 27 28
    Connections {
        target: __behavior
        onMouseXChanged: {
Don Gagne's avatar
Don Gagne committed
29 30
            __lastGlobalMouseX = ScreenTools.mouseX()
            __lastGlobalMouseY = ScreenTools.mouseY()
Don Gagne's avatar
Don Gagne committed
31 32
        }
        onMouseYChanged: {
Don Gagne's avatar
Don Gagne committed
33 34
            __lastGlobalMouseX = ScreenTools.mouseX()
            __lastGlobalMouseY = ScreenTools.mouseY()
Don Gagne's avatar
Don Gagne committed
35 36 37 38 39 40 41 42 43 44 45
        }
        onEntered: { __forceHoverOff; false; hoverTimer.start() }
        onExited: { __forceHoverOff; false; hoverTimer.stop() }
    }

    Timer {
        id:         hoverTimer
        interval:   250
        repeat:     true

        onTriggered: {
Don Gagne's avatar
Don Gagne committed
46
            if (__lastGlobalMouseX != ScreenTools.mouseX() || __lastGlobalMouseY != ScreenTools.mouseY()) {
Don Gagne's avatar
Don Gagne committed
47 48 49
                __forceHoverOff = true
            } else {
                __forceHoverOff = false
Don Gagne's avatar
Don Gagne committed
50
            }
Don Gagne's avatar
Don Gagne committed
51 52
        }
    }
53

Don Gagne's avatar
Don Gagne committed
54
    style: ButtonStyle {
55 56 57 58 59 60 61 62 63 64 65 66
            /*! The padding between the background and the label components. */
            padding {
                top: 4
                left: 4
                right:  control.menu !== null ? Math.round(TextSingleton.implicitHeight * 0.5) : 4
                bottom: 4
            }

            /*! This defines the background of the button. */
            background: Item {
                property bool down: control.pressed || (control.checkable && control.checked)
                implicitWidth: Math.round(TextSingleton.implicitHeight * 4.5)
67
                implicitHeight: ScreenTools.isMobile ? ScreenTools.defaultFontPixelHeight * 3 * 0.75 : Math.max(25, Math.round(TextSingleton.implicitHeight * 1.2))
68 69

                Rectangle {
70
                    anchors.fill:   parent
71 72 73 74 75
                    border.width:   _showBorder ? 1: 0
                    border.color:   _qgcPal.buttonText
                    color:          _showHighlight ?
                                        control._qgcPal.buttonHighlight :
                                        (primary ? control._qgcPal.primaryButton : control._qgcPal.button)
76 77 78 79 80
                }

                Image {
                    id: imageItem
                    visible: control.menu !== null
81
                    source: "/qmlimages/arrow-down.png"
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: padding.right
                    opacity: control.enabled ? 0.6 : 0.5
                }
            }

            /*! This defines the label of the button.  */
            label: Item {
                implicitWidth: row.implicitWidth
                implicitHeight: row.implicitHeight
                baselineOffset: row.y + text.y + text.baselineOffset

                Row {
                    id: row
                    anchors.centerIn: parent
                    spacing: 2

                    Image {
                        source: control.iconSource
                        anchors.verticalCenter: parent.verticalCenter
                    }

                    Text {
106
                        id:             text
107
                        antialiasing:   true
108
                        text:           control.text
Don Gagne's avatar
Don Gagne committed
109
                        font.pixelSize: ScreenTools.defaultFontPixelSize
110

111
                        anchors.verticalCenter: parent.verticalCenter
112

113 114 115
                        color: _showHighlight ?
                            control._qgcPal.buttonHighlightText :
                            (primary ? control._qgcPal.primaryButtonText : control._qgcPal.buttonText)
116 117 118
                    }
                }
            }
Don Gagne's avatar
Don Gagne committed
119 120
        }
}