QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
DropPanel.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7
8/// Drop panel that displays positioned next to the specified click position.
9/// By default the panel drops to the right of the click position. If there isn't
10/// enough room to the right then the panel will drop to the left.
11Popup {
12 id: _root
13 padding: _innerMargin
14 leftPadding: _dropRight ? _innerMargin + _arrowPointWidth : _innerMargin
15 rightPadding: _dropRight ? _innerMargin : _innerMargin + _arrowPointWidth
16 modal: true
17 focus: true
18 closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
19 clip: false
20 dim: false
21
22 property var sourceComponent // Component to display within the popup
23 property var clickRect: Qt.rect(0, 0, 0, 0) // Rectangle of clicked item - used to position drop down
24 property var dropViewPort: Qt.rect(0, 0, parent.width, parent.height) // Available viewport for dropdown
25
26 property var _qgcPal: QGroundControl.globalPalette
27 property real _innerMargin: ScreenTools.defaultFontPixelWidth * 0.5 // Margin between content and rectanglular portion of background
28 property real _arrowPointWidth: ScreenTools.defaultFontPixelWidth * 2 // Distance from vertical side to point
29 property real _arrowPointPositionY: height / 2
30 property bool _dropRight: true
31
32 onAboutToShow: {
33 // Panel defaults to dropping to the right of click position
34 let xPos = clickRect.x + clickRect.width
35
36 // If there isn't room to the right then we switch to drop to the left
37 if (xPos + _root.width > dropViewPort.x + dropViewPort.width) {
38 _dropRight = false
39 xPos = clickRect.x - _root.width
40 }
41
42 // Default position of panel is vertically centered on click position
43 let yPos = clickRect.y + (clickRect.height / 2)
44 yPos -= _root.height / 2
45
46 // Make sure panel is within viewport
47 let originalYPos = yPos
48 yPos = Math.max(yPos, dropViewPort.y)
49 yPos = Math.min(yPos, dropViewPort.y + dropViewPort.height - _root.height)
50
51 _root.x = xPos
52 _root.y = yPos
53
54 // Adjust arrow position back to point at click position
55 _arrowPointPositionY += originalYPos - yPos
56 }
57
58 background: Item {
59 implicitWidth: contentItem.implicitWidth + _innerMargin * 2 + _arrowPointWidth
60 implicitHeight: contentItem.implicitHeight + _innerMargin * 2
61
62 Rectangle {
63 x: _dropRight ? _arrowPointWidth : 0
64 radius: ScreenTools.defaultFontPixelHeight / 2
65 width: parent.implicitWidth - _arrowPointWidth
66 height: parent.implicitHeight
67 color: _qgcPal.window
68 }
69
70 // Arrowhead
71 Canvas {
72 x: _dropRight ? 0 : parent.width - _arrowPointWidth
73 y: _arrowPointPositionY - _arrowPointWidth
74 width: _arrowPointWidth
75 height: _arrowPointWidth * 2
76
77 onPaint: {
78 var context = getContext("2d")
79 context.reset()
80 context.beginPath()
81 context.moveTo(_dropRight ? 0 : _arrowPointWidth, _arrowPointWidth)
82 context.lineTo(_dropRight ? _arrowPointWidth : 0, 0)
83 context.lineTo(_dropRight ? _arrowPointWidth : 0, _arrowPointWidth * 2)
84 context.closePath()
85 context.fillStyle = _qgcPal.window
86 context.fill()
87 }
88 }
89 }
90
91 contentItem: SettingsGroupLayout {
92 Loader {
93 sourceComponent: _root.sourceComponent
94 }
95 }
96}