QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PipView.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Window
3
4import QGroundControl
5import QGroundControl.Controls
6
7Item {
8 id: _root
9 width: _pipSize
10 height: _pipSize * (9/16)
11 visible: item2 && item2.pipState !== item2.pipState.window && show
12
13 property var item1: null // Required
14 property var item2: null // Optional, may come and go
15 property string item1IsFullSettingsKey // Settings key to save whether item1 was saved in full mode
16 property bool show: true
17
18 // Area covering the parent, in PipView coordinates: just the restore button while collapsed
19 readonly property rect occupiedRect: _isExpanded ? Qt.rect(0, 0, width, height) : Qt.rect(showPip.x, showPip.y, showPip.width, showPip.height)
20
21 readonly property string _pipExpandedSettingsKey: "IsPIPVisible"
22
23 property var _fullItem
24 property var _pipOrWindowItem
25 property alias _windowContentItem: window.contentItem
26 property alias _pipContentItem: pipContent
27 property bool _isExpanded: true
28 property real _pipSize: parent.width * 0.2
29 property real _maxSize: 0.75 // Percentage of parent control size
30 property real _minSize: 0.10
31 property bool _componentComplete: false
32
33 Component.onCompleted: {
34 _initForItems()
35 _componentComplete = true
36 }
37
38 // item1 can swap at runtime (fly view map engine Loader), not just item2
39 onItem1Changed: _initForItems()
40 onItem2Changed: _initForItems()
41
42 function showWindow() {
43 window.width = _root.width
44 window.height = _root.height
45 window.show()
46 }
47
48 function _initForItems() {
49 if (!item1) {
50 // Engine swap in flight: wait for the new item1
51 _fullItem = null
52 _pipOrWindowItem = null
53 return
54 }
55 var item1IsFull = QGroundControl.loadBoolGlobalSetting(item1IsFullSettingsKey, true)
56 if (item1 && item2) {
57 item1.pipState.state = item1IsFull ? item1.pipState.fullState : item1.pipState.pipState
58 item2.pipState.state = item1IsFull ? item2.pipState.pipState : item2.pipState.fullState
59 _fullItem = item1IsFull ? item1 : item2
60 _pipOrWindowItem = item1IsFull ? item2 : item1
61 } else {
62 item1.pipState.state = item1.pipState.fullState
63 _fullItem = item1
64 _pipOrWindowItem = null
65 }
66 _setPipIsExpanded(QGroundControl.loadBoolGlobalSetting(_pipExpandedSettingsKey, true))
67 }
68
69 function _swapPip() {
70 if (!item1 || !item2) {
71 // Engine swap in flight (see _initForItems)
72 return
73 }
74 var item1IsFull = false
75 if (item1.pipState.state === item1.pipState.fullState) {
76 item1.pipState.state = item1.pipState.pipState
77 item2.pipState.state = item2.pipState.fullState
78 _fullItem = item2
79 _pipOrWindowItem = item1
80 item1IsFull = false
81 } else {
82 item1.pipState.state = item1.pipState.fullState
83 item2.pipState.state = item2.pipState.pipState
84 _fullItem = item1
85 _pipOrWindowItem = item2
86 item1IsFull = true
87 }
88 QGroundControl.saveBoolGlobalSetting(item1IsFullSettingsKey, item1IsFull)
89 }
90
91 function _setPipIsExpanded(isExpanded) {
92 QGroundControl.saveBoolGlobalSetting(_pipExpandedSettingsKey, isExpanded)
93 _isExpanded = isExpanded
94 }
95
96 Window {
97 id: window
98 visible: false
99 onClosing: {
100 var item = contentItem.children[0]
101 if (item) {
102 item.pipState.state = item.pipState.pipState
103 }
104 }
105 }
106
107 Item {
108 id: pipContent
109 anchors.fill: parent
110 visible: _isExpanded
111 clip: true
112 }
113
114 MouseArea {
115 id: pipMouseArea
116 anchors.fill: parent
117 enabled: _isExpanded
118 preventStealing: true
119 hoverEnabled: true
120 onClicked: _swapPip()
121 }
122
123 // MouseArea to drag in order to resize the PiP area
124 MouseArea {
125 id: pipResize
126 anchors.fill: pipResizeIcon
127 preventStealing: true
128 cursorShape: Qt.PointingHandCursor
129
130 property real initialX: 0
131 property real initialWidth: 0
132
133 onPressed: (mouse) => {
134 // Remove the anchor so the our mouse coordinates stay in the same original place for drag tracking
135 pipResize.anchors.fill = undefined
136 pipResize.initialX = mouse.x
137 pipResize.initialWidth = _root.width
138 }
139
140 onReleased: pipResize.anchors.fill = pipResizeIcon
141
142 // Drag
143 onPositionChanged: (mouse) => {
144 if (pipResize.pressed) {
145 var parentWidth = _root.parent.width
146 var newWidth = pipResize.initialWidth + mouse.x - pipResize.initialX
147 if (newWidth < parentWidth * _maxSize && newWidth > parentWidth * _minSize) {
148 _pipSize = newWidth
149 }
150 }
151 }
152 }
153
154 // Resize icon
155 Image {
156 id: pipResizeIcon
157 source: "/qmlimages/pipResize.svg"
158 fillMode: Image.PreserveAspectFit
159 mipmap: true
160 anchors.right: parent.right
161 anchors.top: parent.top
162 visible: _isExpanded && (ScreenTools.isMobile || pipMouseArea.containsMouse)
163 height: ScreenTools.defaultFontPixelHeight * 2.5
164 width: ScreenTools.defaultFontPixelHeight * 2.5
165 sourceSize.height: height
166 }
167
168 // Check min/max constraints on pip size when when parent is resized
169 Connections {
170 target: _root.parent
171
172 function onWidthChanged() {
173 if (!_componentComplete) {
174 // Wait until first time setup is done
175 return
176 }
177 var parentWidth = _root.parent.width
178 if (_root.width > parentWidth * _maxSize) {
179 _pipSize = parentWidth * _maxSize
180 } else if (_root.width < parentWidth * _minSize) {
181 _pipSize = parentWidth * _minSize
182 }
183 }
184 }
185
186 // Pip to Window
187 Image {
188 id: popupPIP
189 source: "/qmlimages/PiP.svg"
190 mipmap: true
191 fillMode: Image.PreserveAspectFit
192 anchors.left: parent.left
193 anchors.top: parent.top
194 visible: _isExpanded && !ScreenTools.isMobile && pipMouseArea.containsMouse
195 height: ScreenTools.defaultFontPixelHeight * 2.5
196 width: ScreenTools.defaultFontPixelHeight * 2.5
197 sourceSize.height: height
198
199 MouseArea {
200 anchors.fill: parent
201 onClicked: {
202 if (_pipOrWindowItem) {
203 _pipOrWindowItem.pipState.state = _pipOrWindowItem.pipState.windowState
204 }
205 }
206 }
207 }
208
209 Image {
210 id: hidePIP
211 source: "/qmlimages/pipHide.svg"
212 mipmap: true
213 fillMode: Image.PreserveAspectFit
214 anchors.left: parent.left
215 anchors.bottom: parent.bottom
216 visible: _isExpanded && (ScreenTools.isMobile || pipMouseArea.containsMouse)
217 height: ScreenTools.defaultFontPixelHeight * 2.5
218 width: ScreenTools.defaultFontPixelHeight * 2.5
219 sourceSize.height: height
220 MouseArea {
221 anchors.fill: parent
222 onClicked: _root._setPipIsExpanded(false)
223 }
224 }
225
226 Rectangle {
227 id: showPip
228 anchors.left : parent.left
229 anchors.bottom: parent.bottom
230 height: ScreenTools.defaultFontPixelHeight * 2
231 width: ScreenTools.defaultFontPixelHeight * 2
232 radius: ScreenTools.defaultFontPixelHeight / 3
233 visible: !_isExpanded
234 color: _fullItem.pipState.isDark ? Qt.rgba(0,0,0,0.75) : Qt.rgba(0,0,0,0.5)
235 Image {
236 width: parent.width * 0.75
237 height: parent.height * 0.75
238 sourceSize.height: height
239 source: "/res/buttonRight.svg"
240 mipmap: true
241 fillMode: Image.PreserveAspectFit
242 anchors.verticalCenter: parent.verticalCenter
243 anchors.horizontalCenter: parent.horizontalCenter
244 }
245 MouseArea {
246 anchors.fill: parent
247 onClicked: _root._setPipIsExpanded(true)
248 }
249 }
250}