QGCButton.qml 4.14 KB
Newer Older
1 2
import QtQuick 2.7
import QtQuick.Controls 2.1
3
import QtQuick.Controls.Styles 1.4
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 {
Don Gagne's avatar
Don Gagne committed
10 11
    property bool primary:      false                               ///< primary button for a group of buttons
    property real pointSize:    ScreenTools.defaultFontPointSize    ///< Point size for button text
12

13 14
    property var    _qgcPal:            QGCPalette { colorGroupEnabled: enabled }
    property bool   _showHighlight:     (pressed | hovered | checked) && !__forceHoverOff
dogmaphobic's avatar
dogmaphobic committed
15
    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

dogmaphobic's avatar
dogmaphobic committed
23 24
    property int __lastGlobalMouseX:    0
    property int __lastGlobalMouseY:    0
25 26
    property int _horizontalPadding:    ScreenTools.defaultFontPixelWidth
    property int _verticalPadding:      Math.round(ScreenTools.defaultFontPixelHeight / 2)
Don Gagne's avatar
Don Gagne committed
27

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

    Timer {
        id:         hoverTimer
        interval:   250
        repeat:     true
        onTriggered: {
dogmaphobic's avatar
dogmaphobic committed
47
            __forceHoverOff = (__lastGlobalMouseX !== ScreenTools.mouseX() || __lastGlobalMouseY !== ScreenTools.mouseY());
Don Gagne's avatar
Don Gagne committed
48 49
        }
    }
50

Don Gagne's avatar
Don Gagne committed
51
    style: ButtonStyle {
52 53
            /*! The padding between the background and the label components. */
            padding {
54 55 56 57
                top:    _verticalPadding
                bottom: _verticalPadding
                left:   _horizontalPadding
                right:  _horizontalPadding
58 59 60
            }

            /*! This defines the background of the button. */
61 62 63 64 65 66 67 68
            background: Rectangle {
                implicitWidth:  ScreenTools.implicitButtonWidth
                implicitHeight: ScreenTools.implicitButtonHeight
                border.width:   _showBorder ? 1: 0
                border.color:   _qgcPal.buttonText
                color:          _showHighlight ?
                                    control._qgcPal.buttonHighlight :
                                    (primary ? control._qgcPal.primaryButton : control._qgcPal.button)
69 70 71 72
            }

            /*! This defines the label of the button.  */
            label: Item {
73
                implicitWidth:          row.implicitWidth
dogmaphobic's avatar
dogmaphobic committed
74 75
                implicitHeight:         row.implicitHeight
                baselineOffset:         row.y + text.y + text.baselineOffset
76 77

                Row {
dogmaphobic's avatar
dogmaphobic committed
78 79 80
                    id:                 row
                    anchors.centerIn:   parent
                    spacing:            ScreenTools.defaultFontPixelWidth * 0.25
81 82

                    Image {
83
                        source:                 control.iconSource
84 85 86 87
                        anchors.verticalCenter: parent.verticalCenter
                    }

                    Text {
88
                        id:                     text
89
                        anchors.verticalCenter: parent.verticalCenter
90 91 92 93 94 95 96
                        antialiasing:           true
                        text:                   control.text
                        font.pointSize:         pointSize
                        font.family:            ScreenTools.normalFontFamily
                        color:                  _showHighlight ?
                                                    control._qgcPal.buttonHighlightText :
                                                    (primary ? control._qgcPal.primaryButtonText : control._qgcPal.buttonText)
97 98 99
                    }
                }
            }
Don Gagne's avatar
Don Gagne committed
100 101
        }
}