DropButton.qml 9.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

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

Item {
    id: _root
Don Gagne's avatar
Don Gagne committed
11
    z:  QGroundControl.zOrderWidgets
Don Gagne's avatar
Don Gagne committed
12

13
    signal          clicked()
14 15
    property alias  buttonImage:        roundButton.buttonImage
    property alias  rotateImage:        roundButton.rotateImage
dogmaphobic's avatar
dogmaphobic committed
16
    property real   radius:             ScreenTools.isMobile ? ScreenTools.defaultFontPixelHeight * 1.75 : ScreenTools.defaultFontPixelHeight * 1.25
Don Gagne's avatar
Don Gagne committed
17 18 19
    property int    dropDirection:      dropDown
    property alias  dropDownComponent:  dropDownLoader.sourceComponent
    property real   viewportMargins:    0
Don Gagne's avatar
Don Gagne committed
20
    property real   topMargin:          parent.height - ScreenTools.availableHeight
21
    property alias  lightBorders:       roundButton.lightBorders
Don Gagne's avatar
Don Gagne committed
22 23 24 25 26 27 28 29 30 31

    width:  radius * 2
    height: radius * 2

    // Should be an enum but that get's into the whole problem of creating a singleton which isn't worth the effort
    readonly property int dropLeft:     1
    readonly property int dropRight:    2
    readonly property int dropUp:       3
    readonly property int dropDown:     4

dogmaphobic's avatar
dogmaphobic committed
32 33 34
    readonly property real _arrowBaseWidth:     radius             // Width of long side of arrow
    readonly property real _arrowPointHeight:   radius * 0.666     // Height is long side to point
    readonly property real _dropCornerRadius:   ScreenTools.defaultFontPixelWidth * 0.5
Don Gagne's avatar
Don Gagne committed
35 36 37 38 39
    readonly property real _dropCornerRadiusX2: _dropCornerRadius * 2
    readonly property real _dropMargin:         _dropCornerRadius
    readonly property real _dropMarginX2:       _dropMargin * 2

    property real   _viewportMaxLeft:   -x + viewportMargins
dogmaphobic's avatar
dogmaphobic committed
40
    property real   _viewportMaxRight:  parent.width  - (viewportMargins * 2) - x
Don Gagne's avatar
Don Gagne committed
41
    property real   _viewportMaxTop:    -y + viewportMargins + topMargin
Don Gagne's avatar
Don Gagne committed
42 43
    property real   _viewportMaxBottom: parent.height - (viewportMargins * 2) - y

44 45
    // Set up ExclusiveGroup support. We use the checked property to drive visibility of drop down.

46
    property alias checked: roundButton.checked
47 48 49 50 51 52 53 54 55 56 57
    property ExclusiveGroup exclusiveGroup: null

    onExclusiveGroupChanged: {
        if (exclusiveGroup) {
            exclusiveGroup.bindCheckable(_root)
        }
    }

    function hideDropDown() {
        checked = false
    }
Don Gagne's avatar
Don Gagne committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    function _calcPositions() {
        var dropComponentWidth = dropDownLoader.item.width
        var dropComponentHeight = dropDownLoader.item.height
        var dropRectWidth = dropComponentWidth + _dropMarginX2
        var dropRectHeight = dropComponentHeight + _dropMarginX2

        dropItemHolderRect.width = dropRectWidth
        dropItemHolderRect.height = dropRectHeight

        dropDownItem.width = dropComponentWidth + _dropMarginX2
        dropDownItem.height = dropComponentHeight + _dropMarginX2

        if (dropDirection == dropUp || dropDirection == dropDown) {
            dropDownItem.height += _arrowPointHeight

            dropDownItem.x = -(dropDownItem.width / 2) + radius

            dropItemHolderRect.x = 0

            if (dropDirection == dropUp) {
                dropDownItem.y = -(dropDownItem.height + _dropMargin)

                dropItemHolderRect.y = 0
            } else {
83
                dropDownItem.y = roundButton.height + _dropMargin
Don Gagne's avatar
Don Gagne committed
84 85 86 87 88 89 90 91 92

                dropItemHolderRect.y = _arrowPointHeight
            }

            // Validate that dropdown is within viewport
            dropDownItem.x = Math.max(dropDownItem.x, _viewportMaxLeft)
            dropDownItem.x = Math.min(dropDownItem.x + dropDownItem.width, _viewportMaxRight) - dropDownItem.width

            // Arrow points
93
            arrowCanvas.arrowPoint.x = (roundButton.x + radius) - dropDownItem.x
Don Gagne's avatar
Don Gagne committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            if (dropDirection == dropUp) {
                arrowCanvas.arrowPoint.y = dropDownItem.height
                arrowCanvas.arrowBase1.x = arrowCanvas.arrowPoint.x - (_arrowBaseWidth / 2)
                arrowCanvas.arrowBase1.y = arrowCanvas.arrowPoint.y - _arrowPointHeight
                arrowCanvas.arrowBase2.x = arrowCanvas.arrowBase1.x + _arrowBaseWidth
                arrowCanvas.arrowBase2.y = arrowCanvas.arrowBase1.y
            } else {
                arrowCanvas.arrowPoint.y = 0
                arrowCanvas.arrowBase1.x = arrowCanvas.arrowPoint.x + (_arrowBaseWidth / 2)
                arrowCanvas.arrowBase1.y = _arrowPointHeight
                arrowCanvas.arrowBase2.x = arrowCanvas.arrowBase1.x - _arrowBaseWidth
                arrowCanvas.arrowBase2.y = arrowCanvas.arrowBase1.y
            }
        } else {
            dropDownItem.width += _arrowPointHeight

            dropDownItem.y = -(dropDownItem.height / 2) + radius

            dropItemHolderRect.y = 0

            if (dropDirection == dropLeft) {
Don Gagne's avatar
Don Gagne committed
115
                dropDownItem.x = -(dropDownItem.width + _dropMargin)
Don Gagne's avatar
Don Gagne committed
116 117 118

                dropItemHolderRect.x = 0
            } else {
119
                dropDownItem.x = roundButton.width + _dropMargin
Don Gagne's avatar
Don Gagne committed
120 121 122 123 124 125 126 127 128

                dropItemHolderRect.x = _arrowPointHeight
            }

            // Validate that dropdown is within viewport
            dropDownItem.y = Math.max(dropDownItem.y, _viewportMaxTop)
            dropDownItem.y = Math.min(dropDownItem.y + dropDownItem.height, _viewportMaxBottom) - dropDownItem.height

            // Arrow points
129
            arrowCanvas.arrowPoint.y = (roundButton.y + radius) - dropDownItem.y
Don Gagne's avatar
Don Gagne committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            if (dropDirection == dropLeft) {
                arrowCanvas.arrowPoint.x = dropDownItem.width
                arrowCanvas.arrowBase1.x = arrowCanvas.arrowPoint.x - _arrowPointHeight
                arrowCanvas.arrowBase1.y = arrowCanvas.arrowPoint.y - (_arrowBaseWidth / 2)
                arrowCanvas.arrowBase2.x = arrowCanvas.arrowBase1.x
                arrowCanvas.arrowBase2.y = arrowCanvas.arrowBase1.y + _arrowBaseWidth
            } else {
                arrowCanvas.arrowPoint.x = 0
                arrowCanvas.arrowBase1.x = _arrowPointHeight
                arrowCanvas.arrowBase1.y = arrowCanvas.arrowPoint.y - (_arrowBaseWidth / 2)
                arrowCanvas.arrowBase2.x = arrowCanvas.arrowBase1.x
                arrowCanvas.arrowBase2.y = arrowCanvas.arrowBase1.y + _arrowBaseWidth
            }
        }
        arrowCanvas.requestPaint()
    } // function - _calcPositions

    QGCPalette { id: qgcPal }

    MouseArea {
        x:          _viewportMaxLeft
        y:          _viewportMaxTop
        width:      _viewportMaxRight -_viewportMaxLeft
        height:     _viewportMaxBottom - _viewportMaxTop
154
        visible:    checked
155 156 157 158
        onClicked:  {
            checked = false
            _root.clicked()
        }
Don Gagne's avatar
Don Gagne committed
159 160
    }

161 162 163 164
    RoundButton {
        id:             roundButton
        radius:         parent.width / 2
        onClicked:  {
Don Gagne's avatar
Don Gagne committed
165
            _calcPositions()
166 167
            _root.clicked()
        }
168
    }
Don Gagne's avatar
Don Gagne committed
169 170 171

    Item {
        id:         dropDownItem
172
        visible:    checked
Don Gagne's avatar
Don Gagne committed
173

Don Gagne's avatar
Don Gagne committed
174
        Canvas {
Don Gagne's avatar
Don Gagne committed
175 176 177
            id:             arrowCanvas
            anchors.fill:   parent

178 179 180
            property point arrowPoint: Qt.point(0, 0)
            property point arrowBase1: Qt.point(0, 0)
            property point arrowBase2: Qt.point(0, 0)
Don Gagne's avatar
Don Gagne committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

            onPaint: {
                var context = getContext("2d")
                context.reset()
                context.beginPath()

                context.moveTo(dropItemHolderRect.x, dropItemHolderRect.y)
                if (dropDirection == dropDown) {
                    context.lineTo(arrowBase2.x, arrowBase2.y)
                    context.lineTo(arrowPoint.x, arrowPoint.y)
                    context.lineTo(arrowBase1.x, arrowBase1.y)
                }
                context.lineTo(dropItemHolderRect.x + dropItemHolderRect.width, dropItemHolderRect.y)
                if (dropDirection == dropLeft) {
                    context.lineTo(arrowBase2.x, arrowBase2.y)
                    context.lineTo(arrowPoint.x, arrowPoint.y)
                    context.lineTo(arrowBase1.x, arrowBase1.y)
                }
                context.lineTo(dropItemHolderRect.x + dropItemHolderRect.width, dropItemHolderRect.y + dropItemHolderRect.height)
                if (dropDirection == dropUp) {
                    context.lineTo(arrowBase2.x, arrowBase2.y)
                    context.lineTo(arrowPoint.x, arrowPoint.y)
                    context.lineTo(arrowBase1.x, arrowBase1.y)
                }
                context.lineTo(dropItemHolderRect.x, dropItemHolderRect.y + dropItemHolderRect.height)
                if (dropDirection == dropRight) {
                    context.lineTo(arrowBase2.x, arrowBase2.y)
                    context.lineTo(arrowPoint.x, arrowPoint.y)
                    context.lineTo(arrowBase1.x, arrowBase1.y)
                }
                context.lineTo(dropItemHolderRect.x, dropItemHolderRect.y)

                context.closePath()
214
                context.fillStyle = qgcPal.windowShade
Don Gagne's avatar
Don Gagne committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                context.fill()
            }
        } // Canvas - arrowCanvas

        Item {
            id:     dropItemHolderRect

            Loader {
                id: dropDownLoader
                x:  _dropMargin
                y:  _dropMargin
            }
        }
    } // Item - dropDownItem
}