QGCMapToolButton.qml 2.38 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
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

    // 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: {
Don Gagne's avatar
Don Gagne committed
26 27
            __lastGlobalMouseX = ScreenTools.mouseX()
            __lastGlobalMouseY = ScreenTools.mouseY()
28 29
        }
        onMouseYChanged: {
Don Gagne's avatar
Don Gagne committed
30 31
            __lastGlobalMouseX = ScreenTools.mouseX()
            __lastGlobalMouseY = ScreenTools.mouseY()
32 33 34 35 36 37 38 39 40 41
        }
        onEntered: { __forceHoverOff; false; hoverTimer.start() }
        onExited:  { __forceHoverOff; false; hoverTimer.stop()  }
    }

    Timer {
        id:         hoverTimer
        interval:   250
        repeat:     true
        onTriggered: {
Don Gagne's avatar
Don Gagne committed
42
            if (__lastGlobalMouseX != ScreenTools.mouseX() || __lastGlobalMouseY != ScreenTools.mouseY()) {
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
                __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)
            }
        }
    }
}