QGCButton.qml 4.94 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 {
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 25
    property int __lastGlobalMouseX:    0
    property int __lastGlobalMouseY:    0
    property int __padding:             Math.round(ScreenTools.defaultFontPixelHeight * 0.5)
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 {
Gus Grubba's avatar
Gus Grubba committed
53
                top:    __padding * 0.5
dogmaphobic's avatar
dogmaphobic committed
54 55
                left:   __padding
                right:  control.menu !== null ? Math.round(ScreenTools.defaultFontPixelHeight) : __padding
Gus Grubba's avatar
Gus Grubba committed
56
                bottom: __padding * 0.5
57 58 59 60 61
            }

            /*! This defines the background of the button. */
            background: Item {
                property bool down: control.pressed || (control.checkable && control.checked)
dogmaphobic's avatar
dogmaphobic committed
62
                implicitWidth:      Math.round(ScreenTools.defaultFontPixelWidth * 4.5)
dogmaphobic's avatar
dogmaphobic committed
63
                implicitHeight:     ScreenTools.isMobile ? Math.max(25, Math.round(ScreenTools.defaultFontPixelHeight * 2)) : Math.max(25, Math.round(ScreenTools.defaultFontPixelHeight * 1.2))
64 65

                Rectangle {
66
                    anchors.fill:   parent
67 68
                    border.width:   _showBorder ? 1: 0
                    border.color:   _qgcPal.buttonText
69
                    //radius:       3
70 71 72
                    color:          _showHighlight ?
                                        control._qgcPal.buttonHighlight :
                                        (primary ? control._qgcPal.primaryButton : control._qgcPal.button)
73 74 75
                }

                Image {
dogmaphobic's avatar
dogmaphobic committed
76 77 78
                    id:                     imageItem
                    visible:                control.menu !== null
                    source:                 "/qmlimages/arrow-down.png"
79
                    anchors.verticalCenter: parent.verticalCenter
dogmaphobic's avatar
dogmaphobic committed
80 81 82
                    anchors.right:          parent.right
                    anchors.rightMargin:    __padding
                    opacity:                control.enabled ? 0.6 : 0.5
83 84 85 86 87
                }
            }

            /*! This defines the label of the button.  */
            label: Item {
dogmaphobic's avatar
dogmaphobic committed
88 89 90
                implicitWidth:          control.menu === null ? row.implicitWidth : row.implicitWidth + ScreenTools.defaultFontPixelWidth
                implicitHeight:         row.implicitHeight
                baselineOffset:         row.y + text.y + text.baselineOffset
91 92

                Row {
dogmaphobic's avatar
dogmaphobic committed
93 94 95
                    id:                 row
                    anchors.centerIn:   parent
                    spacing:            ScreenTools.defaultFontPixelWidth * 0.25
96 97 98 99 100 101 102

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

                    Text {
103
                        id:             text
104
                        antialiasing:   true
105
                        text:           control.text
Don Gagne's avatar
Don Gagne committed
106
                        font.pointSize: pointSize
107
                        font.family:    ScreenTools.normalFontFamily
108
                        anchors.verticalCenter: parent.verticalCenter
109 110 111
                        color: _showHighlight ?
                            control._qgcPal.buttonHighlightText :
                            (primary ? control._qgcPal.primaryButtonText : control._qgcPal.buttonText)
112 113 114
                    }
                }
            }
Don Gagne's avatar
Don Gagne committed
115 116
        }
}