QGCMapToolButton.qml 2.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls.Private 1.0

import QGroundControl.Controls 1.0
import QGroundControl.Palette 1.0
import QGroundControl.ScreenTools 1.0

Button {
    property var imageSource: undefined
    property var __qgcPal: QGCPalette { colorGroupEnabled: enabled }
    property bool __showHighlight: (pressed | hovered | checked) && !__forceHoverOff
    property ScreenTools __screenTools: ScreenTools { }

    // 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
    property int    __lastGlobalMouseX: 0
    property int    __lastGlobalMouseY: 0

    Connections {
        target: __behavior
        onMouseXChanged: {
            __lastGlobalMouseX = __screenTools.mouseX
            __lastGlobalMouseY = __screenTools.mouseY
        }
        onMouseYChanged: {
            __lastGlobalMouseX = __screenTools.mouseX
            __lastGlobalMouseY = __screenTools.mouseY
        }
        onEntered: { __forceHoverOff; false; hoverTimer.start() }
        onExited:  { __forceHoverOff; false; hoverTimer.stop()  }
    }

    Timer {
        id:         hoverTimer
        interval:   250
        repeat:     true
        onTriggered: {
            if (__lastGlobalMouseX != __screenTools.mouseX || __lastGlobalMouseY != __screenTools.mouseY) {
                __forceHoverOff = true
            } else {
                __forceHoverOff = false
            }
        }
    }

    style: ButtonStyle {
        /*! This defines the background of the button. */
        background: Item {
            property bool __checked: (control.checkable && control.checked)

            Rectangle {
                id: backgroundRectangle
                anchors.fill: parent
                color: __showHighlight ? __qgcPal.buttonHighlight : (__checked ? __qgcPal.buttonHighlight : __qgcPal.window);
            }

            QGCColoredImage {
                id: image
                anchors.fill: parent
                opacity: control.enabled ? 0.6 : 0.5
                source: imageSource
                color: __showHighlight ? __qgcPal.buttonHighlightText : (__checked ? __qgcPal.primaryButtonText : __qgcPal.buttonText)
            }
        }
    }
}