Skip to content
Snippets Groups Projects
QGCButton.qml 4.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • import QtQuick 2.3
    import QtQuick.Controls 1.2
    
    import QtQuick.Controls.Styles 1.4
    
    Don Gagne's avatar
    Don Gagne committed
    
    import QGroundControl.Palette 1.0
    
    import QGroundControl.ScreenTools 1.0
    
    Don Gagne's avatar
    Don Gagne committed
    
    Button {
    
        activeFocusOnPress: true
    
    
    Gus Grubba's avatar
    Gus Grubba committed
        property bool   primary:        false                               ///< primary button for a group of buttons
        property real   pointSize:      ScreenTools.defaultFontPointSize    ///< Point size for button text
        property bool   showBorder:     _qgcPal.globalTheme === QGCPalette.Light
        property bool   iconLeft:       false
        property real   backRadius:     0
        property real   heightFactor:   0.5
    
        property var    _qgcPal:            QGCPalette { colorGroupEnabled: enabled }
        property bool   _showHighlight:     (pressed | hovered | checked) && !__forceHoverOff
    
    Don Gagne's avatar
    Don Gagne committed
    
    
    Don Gagne's avatar
    Don Gagne committed
        // 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
    
    
    dogmaphobic's avatar
    dogmaphobic committed
        property int __lastGlobalMouseX:    0
        property int __lastGlobalMouseY:    0
    
        property int _horizontalPadding:    ScreenTools.defaultFontPixelWidth
    
    Gus Grubba's avatar
    Gus Grubba committed
        property int _verticalPadding:      Math.round(ScreenTools.defaultFontPixelHeight * heightFactor)
    
    Don Gagne's avatar
    Don Gagne committed
    
    
    Don Gagne's avatar
    Don Gagne committed
        Connections {
            target: __behavior
            onMouseXChanged: {
    
    Don Gagne's avatar
    Don Gagne committed
                __lastGlobalMouseX = ScreenTools.mouseX()
                __lastGlobalMouseY = ScreenTools.mouseY()
    
    Don Gagne's avatar
    Don Gagne committed
            }
            onMouseYChanged: {
    
    Don Gagne's avatar
    Don Gagne committed
                __lastGlobalMouseX = ScreenTools.mouseX()
                __lastGlobalMouseY = ScreenTools.mouseY()
    
    Don Gagne's avatar
    Don Gagne committed
            }
    
            onEntered: { __forceHoverOff = false; hoverTimer.start() }
            onExited: { __forceHoverOff = false; hoverTimer.stop() }
    
    Don Gagne's avatar
    Don Gagne committed
        }
    
        Timer {
            id:         hoverTimer
            interval:   250
            repeat:     true
            onTriggered: {
    
    dogmaphobic's avatar
    dogmaphobic committed
                __forceHoverOff = (__lastGlobalMouseX !== ScreenTools.mouseX() || __lastGlobalMouseY !== ScreenTools.mouseY());
    
    Don Gagne's avatar
    Don Gagne committed
        style: ButtonStyle {
    
                /*! The padding between the background and the label components. */
                padding {
    
                    top:    _verticalPadding
                    bottom: _verticalPadding
                    left:   _horizontalPadding
                    right:  _horizontalPadding
    
                }
    
                /*! This defines the background of the button. */
    
                background: Rectangle {
    
    Gus Grubba's avatar
    Gus Grubba committed
                    id:             backRect
    
                    implicitWidth:  ScreenTools.implicitButtonWidth
                    implicitHeight: ScreenTools.implicitButtonHeight
    
    Gus Grubba's avatar
    Gus Grubba committed
                    radius:         backRadius
                    border.width:   showBorder ? 1 : 0
    
                    border.color:   _qgcPal.buttonText
                    color:          _showHighlight ?
                                        control._qgcPal.buttonHighlight :
                                        (primary ? control._qgcPal.primaryButton : control._qgcPal.button)
    
                }
    
                /*! This defines the label of the button.  */
                label: Item {
    
    Gus Grubba's avatar
    Gus Grubba committed
                    implicitWidth:          text.implicitWidth + icon.width
                    implicitHeight:         text.implicitHeight
                    baselineOffset:         text.y + text.baselineOffset
    
    Gus Grubba's avatar
    Gus Grubba committed
                    QGCColoredImage {
                        id:                     icon
                        source:                 control.iconSource
                        height:                 source === "" ? 0 : text.height
                        width:                  height
                        color:                  text.color
                        fillMode:               Image.PreserveAspectFit
                        sourceSize.height:      height
                        anchors.left:           control.iconLeft ? parent.left : undefined
                        anchors.leftMargin:     control.iconLeft ? ScreenTools.defaultFontPixelWidth : undefined
                        anchors.right:          !control.iconLeft ? parent.right : undefined
                        anchors.rightMargin:    !control.iconLeft ? ScreenTools.defaultFontPixelWidth : undefined
                        anchors.verticalCenter: parent.verticalCenter
                    }
    
    Gus Grubba's avatar
    Gus Grubba committed
                    Text {
                        id:                     text
                        anchors.centerIn:       parent
                        antialiasing:           true
                        text:                   control.text
                        font.pointSize:         pointSize
                        font.family:            ScreenTools.normalFontFamily
                        color:                  _showHighlight ?
                                                    control._qgcPal.buttonHighlightText :
                                                    (primary ? control._qgcPal.primaryButtonText : control._qgcPal.buttonText)
    
    Don Gagne's avatar
    Don Gagne committed
            }
    }