QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ToolStripDropPanel.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3
4import QGroundControl
5import QGroundControl.Controls
6
7Item {
8 id: _root
9 visible: false
10
11 signal clicked()
12 property real radius: ScreenTools.isMobile ? ScreenTools.defaultFontPixelHeight * 1.75 : ScreenTools.defaultFontPixelHeight * 1.25
13 property real viewportMargins: 0
14 property var toolStrip
15
16 // Should be an enum but that get's into the whole problem of creating a singleton which isn't worth the effort
17 readonly property int dropLeft: 1
18 readonly property int dropRight: 2
19 readonly property int dropUp: 3
20 readonly property int dropDown: 4
21
22 readonly property real _arrowBaseHeight: radius // Height of vertical side of arrow
23 readonly property real _arrowPointWidth: radius * 0.666 // Distance from vertical side to point
24 readonly property real _dropMargin: ScreenTools.defaultFontPixelWidth
25
26 property var _dropEdgeTopPoint
27 property alias _dropDownComponent: panelLoader.sourceComponent
28 property real _viewportMaxTop: 0
29 property real _viewportMaxBottom: parent.parent.height - parent.y
30 property real _viewportMaxHeight: _viewportMaxBottom - _viewportMaxTop
31 property var _dropPanelCancel
32 property var _parentButton
33
34 function show(panelEdgeTopPoint, panelComponent, parentButton) {
35 _parentButton = parentButton
36 _dropEdgeTopPoint = panelEdgeTopPoint
37 _dropDownComponent = panelComponent
38 _calcPositions()
39 visible = true
40 _dropPanelCancel = dropPanelCancelComponent.createObject(toolStrip.parent)
41 }
42
43 function hide() {
44 if (_dropPanelCancel) {
45 _dropPanelCancel.destroy()
46 _parentButton.checked = false
47 visible = false
48 _dropDownComponent = undefined
49 }
50 }
51
52 function _calcPositions() {
53 var panelComponentWidth = panelLoader.item.width
54 var panelComponentHeight = panelLoader.item.height
55
56 dropDownItem.width = panelComponentWidth + (_dropMargin * 2) + _arrowPointWidth
57 dropDownItem.height = panelComponentHeight + (_dropMargin * 2)
58
59 dropDownItem.x = _dropEdgeTopPoint.x + _dropMargin
60 dropDownItem.y = _dropEdgeTopPoint.y -(dropDownItem.height / 2) + radius
61
62 // Validate that dropdown is within viewport
63 dropDownItem.y = Math.min(dropDownItem.y + dropDownItem.height, _viewportMaxBottom) - dropDownItem.height
64 dropDownItem.y = Math.max(dropDownItem.y, _viewportMaxTop)
65
66 // Adjust height to not exceed viewport bounds
67 dropDownItem.height = Math.min(dropDownItem.height, _viewportMaxHeight - dropDownItem.y)
68
69 // Arrow points
70 arrowCanvas.arrowPoint.y = (_dropEdgeTopPoint.y + radius) - dropDownItem.y
71 arrowCanvas.arrowPoint.x = 0
72 arrowCanvas.arrowBase1.x = _arrowPointWidth
73 arrowCanvas.arrowBase1.y = arrowCanvas.arrowPoint.y - (_arrowBaseHeight / 2)
74 arrowCanvas.arrowBase2.x = arrowCanvas.arrowBase1.x
75 arrowCanvas.arrowBase2.y = arrowCanvas.arrowBase1.y + _arrowBaseHeight
76 arrowCanvas.requestPaint()
77 } // function - _calcPositions
78
79 QGCPalette { id: qgcPal }
80
81 Component {
82 // Overlay which is used to cancel the panel when the user clicks away
83 id: dropPanelCancelComponent
84
85 MouseArea {
86 anchors.fill: parent
87 z: toolStrip.z - 1
88 onClicked: dropPanel.hide()
89 }
90 }
91
92 // This item is sized to hold the entirety of the drop panel including the arrow point
93 Item {
94 id: dropDownItem
95
96 DeadMouseArea {
97 anchors.fill: parent
98 }
99
100 Canvas {
101 id: arrowCanvas
102 anchors.fill: parent
103
104 property point arrowPoint: Qt.point(0, 0)
105 property point arrowBase1: Qt.point(0, 0)
106 property point arrowBase2: Qt.point(0, 0)
107
108 onPaint: {
109 var panelX = _arrowPointWidth
110 var panelY = 0
111 var panelWidth = parent.width - _arrowPointWidth
112 var panelHeight = parent.height
113
114 var context = getContext("2d")
115 context.reset()
116 context.beginPath()
117
118 context.moveTo(panelX, panelY) // top left
119 context.lineTo(panelX + panelWidth, panelY) // top right
120 context.lineTo(panelX + panelWidth, panelX + panelHeight) // bottom right
121 context.lineTo(panelX, panelY + panelHeight) // bottom left
122 context.lineTo(arrowBase2.x, arrowBase2.y)
123 context.lineTo(arrowPoint.x, arrowPoint.y)
124 context.lineTo(arrowBase1.x, arrowBase1.y)
125 context.lineTo(panelX, panelY) // top left
126
127 context.closePath()
128 context.fillStyle = qgcPal.windowShade
129 context.fill()
130 }
131 } // Canvas - arrowCanvas
132
133 QGCFlickable {
134 id: panelItemFlickable
135 anchors.margins: _dropMargin
136 anchors.leftMargin: _dropMargin + _arrowPointWidth
137 anchors.fill: parent
138 flickableDirection: Flickable.VerticalFlick
139 contentWidth: panelLoader.width
140 contentHeight: panelLoader.height
141
142 Loader {
143 id: panelLoader
144
145 onHeightChanged: _calcPositions()
146 onWidthChanged: _calcPositions()
147
148 property var dropPanel: _root
149 }
150 }
151 } // Item - dropDownItem
152}