QGCButton.qml 4.11 KB
Newer Older
1 2
import QtQuick 2.3
import QtQuick.Controls 1.2
3
import QtQuick.Controls.Styles 1.4
Don Gagne's avatar
Don Gagne committed
4 5

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

Button {
Don Gagne's avatar
Don Gagne committed
9 10
    property bool primary:      false                               ///< primary button for a group of buttons
    property real pointSize:    ScreenTools.defaultFontPointSize    ///< Point size for button text
11

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

Don Gagne's avatar
Don Gagne committed
16 17 18 19 20
    // 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
21

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

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

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

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

            /*! This defines the background of the button. */
60 61 62 63 64 65 66 67
            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)
68 69 70 71
            }

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

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

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

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