QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PlanTreeView.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7import QGroundControl.FactControls
8import QGroundControl.PlanView
9
10/// Unified plan tree view showing Mission Items, GeoFence, and Rally Points
11/// as collapsible sections using a real TreeView with type-discriminating delegates.
12TreeView {
13 id: root
14 model: _missionController.visualItemsTree
15 clip: true
16 boundsBehavior: Flickable.StopAtBounds
17 reuseItems: false
18 pointerNavigationEnabled: false
19 selectionBehavior: TableView.SelectionDisabled
20 rowSpacing: 2
21
22 required property var editorMap
23 required property var planMasterController
24
25 signal editingLayerChangeRequested(int layer)
26
27 readonly property bool _createNewPlanMode: planMasterController.showCreateFromTemplate
28
29 on_CreateNewPlanModeChanged: {
30 if (_createNewPlanMode) {
31 var planFileRow = _rowFor(_missionController.planFileGroupIndex)
32 if (!root.isExpanded(planFileRow)) {
33 root.expand(planFileRow)
34 }
35 root.contentY = 0
36 }
37 }
38
39 property var _missionController: planMasterController.missionController
40 property var _geoFenceController: planMasterController.geoFenceController
41 property var _rallyPointController: planMasterController.rallyPointController
42
43 // Helper: convert a persistent model index to the current visual row
44 function _rowFor(modelIndex) { return root.rowAtIndex(modelIndex) }
45
46 // QGCFlickableScrollIndicator expects parent to have indicatorColor (provided by QGCFlickable/QGCListView)
47 property color indicatorColor: qgcPal.textPrimary
48
49 QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
50
51 QGCFlickableScrollIndicator { parent: root; orientation: QGCFlickableScrollIndicator.Horizontal }
52 QGCFlickableScrollIndicator { parent: root; orientation: QGCFlickableScrollIndicator.Vertical }
53
54 property int _lastMissionItemCount: 0
55
56 // The tree can be created mid-session when the tabbed panel setting is turned off, after the last visualItemsReset
57 Component.onCompleted: _resetToDefaultState()
58
59 function _resetToDefaultState() {
60 root.collapseRecursively()
61 if (_missionController.containsItems) {
62 // Non-empty plan: expand mission group
63 root.expand(_rowFor(_missionController.missionGroupIndex))
64 } else {
65 // Empty plan: expand Plan Info and Defaults, scroll to top
66 root.expand(_rowFor(_missionController.planFileGroupIndex))
67 root.expand(_rowFor(_missionController.defaultsGroupIndex))
68 root.contentY = 0
69 }
70 root._lastMissionItemCount = _missionController.visualItems ? _missionController.visualItems.count : 0
71 root.editingLayerChangeRequested(PlanEditLayers.layerMission)
72 }
73
74 Connections {
75 target: root._missionController.visualItems
76 function onCountChanged() {
77 var newCount = _missionController.visualItems ? _missionController.visualItems.count : 0
78 if (newCount > root._lastMissionItemCount) {
79 // First waypoint added — collapse Plan Info and Defaults
80 if (root._lastMissionItemCount <= 1 && newCount > 1) {
81 var planFileRow = _rowFor(_missionController.planFileGroupIndex)
82 if (root.isExpanded(planFileRow)) {
83 root.collapse(planFileRow)
84 }
85 var defaultsRow = _rowFor(_missionController.defaultsGroupIndex)
86 if (root.isExpanded(defaultsRow)) {
87 root.collapse(defaultsRow)
88 }
89 }
90 // Expand mission group (making Mission the active layer) and scroll to the new item
91 root.selectLayer("missionGroup")
92 // Scroll happens when the editor signals editorExpandedAndLoaded
93 }
94 root._lastMissionItemCount = newCount
95 }
96 }
97
98 Connections {
99 target: root._missionController
100 function onVisualItemsReset() {
101 root._resetToDefaultState()
102 }
103 function onPlanViewStateChanged() {
104 // Current item changed — bring it on-screen if completely off-screen.
105 // Fine-tuned scroll happens later via editorExpandedAndLoaded.
106 var item = _missionController.currentPlanViewItem
107 if (item) {
108 var modelIndex = _missionController.visualItemsTree.indexForObject(item)
109 var row = root.rowAtIndex(modelIndex)
110 if (row >= 0) {
111 root.forceLayout()
112 root.positionViewAtRow(row, TableView.Visible)
113 }
114 }
115 }
116 }
117
118 // The three groups which correspond to map editing layers. These are exclusive:
119 // expanding one collapses the others and makes it the active editing layer.
120
121 function _layerGroupRow(nodeType) {
122 switch (nodeType) {
123 case "missionGroup": return _rowFor(_missionController.missionGroupIndex)
124 case "fenceGroup": return _rowFor(_missionController.fenceGroupIndex)
125 case "rallyGroup": return _rowFor(_missionController.rallyGroupIndex)
126 }
127 return -1
128 }
129
130 function _collapseOtherLayerGroups(nodeType) {
131 for (var layerInfo of PlanEditLayers.layerInfos) {
132 if (layerInfo.nodeType !== nodeType) {
133 var row = _layerGroupRow(layerInfo.nodeType)
134 if (row >= 0 && root.isExpanded(row)) {
135 root.collapse(row)
136 }
137 }
138 }
139 }
140
141 // Public API: make a layer group the active editing layer and expand it,
142 // collapsing the other layer groups. Called by the layer tool buttons and group headers.
143 function selectLayer(nodeType) {
144 var layerInfo = PlanEditLayers.infoForNodeType(nodeType)
145 if (!layerInfo) {
146 return
147 }
148 if (!mainWindow.allowViewSwitch()) {
149 return
150 }
151 editingLayerChangeRequested(layerInfo.layer)
152 _collapseOtherLayerGroups(nodeType)
153 var targetRow = _layerGroupRow(nodeType)
154 if (targetRow >= 0) {
155 if (!root.isExpanded(targetRow)) {
156 root.expand(targetRow)
157 }
158 root.forceLayout()
159 root.positionViewAtRow(targetRow, TableView.AlignTop)
160 }
161 }
162
163 // Toggle expand/collapse for a non-layer group header (Plan Info, Defaults, Transform).
164 // Caller is responsible for calling allowViewSwitch() before invoking this.
165 function _toggleGroup(row) {
166 if (root.isExpanded(row)) {
167 root.collapse(row)
168 } else {
169 root.expand(row)
170 }
171 root.forceLayout()
172 }
173
174 // Subtitle text shown on group headers, varies by node type
175 function _groupSubtitle(nodeType) {
176 switch (nodeType) {
177 case "planFileGroup": return planMasterController.currentPlanFileName === "" ? qsTr("<Untitled>") : planMasterController.currentPlanFileName
178 case "missionGroup": return _missionController.visualItems ? qsTr("%1 items").arg(_missionController.visualItems.count - 1) : ""
179 case "rallyGroup": return _rallyPointController.points ? qsTr("%1 points").arg(_rallyPointController.points.count) : ""
180 default: return ""
181 }
182 }
183
184 // Icon shown on group headers, matches the layer switcher icons
185 function _groupIcon(nodeType) {
186 return PlanEditLayers.infoForNodeType(nodeType)?.icon ?? ""
187 }
188
189 // Coalesces multiple delegate height changes into a single forceLayout() call
190 Timer {
191 id: layoutTimer
192 interval: 0
193 running: false
194 repeat: false
195 onTriggered: root.forceLayout()
196 }
197
198 // Called by MissionItemEditor delegates when their editor height has settled.
199 function _scrollToMissionItem(delegateItem) {
200 root.forceLayout()
201 var bottomY = delegateItem.mapToItem(root.contentItem, 0, delegateItem.height).y
202 var neededContentY = bottomY - root.height
203 if (neededContentY > root.contentY) {
204 root.contentY = neededContentY
205 }
206 }
207
208 delegate: Item {
209 id: delegateRoot
210 implicitWidth: root.width
211 implicitHeight: (loader.item ? loader.item.height : 1) + (separatorLine.visible ? separatorLine.height + root.rowSpacing : 0)
212 enabled: !root._createNewPlanMode || _enabledInCreateMode
213 opacity: enabled || !root.editorMap ? 1 : root.editorMap._nonInteractiveOpacity
214 width: root.width
215
216 required property TreeView treeView
217 required property bool isTreeNode
218 required property bool expanded
219 required property bool hasChildren
220 required property int depth
221 required property int row
222 required property var model
223
224 readonly property var nodeObject: model.object
225 readonly property string nodeType: model.nodeType
226 readonly property bool separator: model.separator ?? false
227
228 // In create-new-plan mode, only the Plan Info and Defaults groups and their children are enabled
229 readonly property bool _enabledInCreateMode: nodeType === "planFileGroup" || nodeType === "planFileInfo"
230 || nodeType === "defaultsGroup" || nodeType === "defaultsInfo"
231
232 onImplicitHeightChanged: layoutTimer.restart()
233
234 readonly property string _qrcBase: "qrc:/qml/QGroundControl/PlanView/"
235
236 // We use setSource() instead of sourceComponent so that required properties
237 // (e.g. missionItem) are injected before internal bindings activate,
238 // preventing "Cannot read property of null" warnings.
239 Loader {
240 id: loader
241 width: parent.width
242
243 Component.onCompleted: {
244 switch (delegateRoot.nodeType) {
245 case "planFileGroup":
246 case "defaultsGroup":
247 case "missionGroup":
248 case "fenceGroup":
249 case "rallyGroup":
250 case "transformGroup":
251 sourceComponent = groupHeaderComponent
252 break
253 case "planFileInfo":
254 setSource(delegateRoot._qrcBase + "PlanInfoEditor.qml", {
255 objectName: "planTree_planFileInfo",
256 width: Qt.binding(() => delegateRoot.width),
257 planMasterController: root.planMasterController,
258 missionController: root._missionController,
259 editorMap: root.editorMap
260 })
261 break
262 case "defaultsInfo":
263 setSource(delegateRoot._qrcBase + "MissionDefaultsEditor.qml", {
264 width: Qt.binding(() => delegateRoot.width),
265 missionController: root._missionController,
266 planMasterController: root.planMasterController
267 })
268 break
269 case "missionItem":
270 if (delegateRoot.nodeObject) {
271 setSource(delegateRoot._qrcBase + "MissionItemEditor.qml", {
272 objectName: "planTree_missionItemEditor",
273 width: Qt.binding(() => delegateRoot.width),
274 map: root.editorMap,
275 missionItem: delegateRoot.nodeObject
276 })
277 }
278 break
279 case "fenceEditor":
280 if (delegateRoot.nodeObject) {
281 setSource(delegateRoot._qrcBase + "GeoFenceEditor.qml", {
282 objectName: "planTree_fenceEditor",
283 width: Qt.binding(() => delegateRoot.width),
284 myGeoFenceController: root._geoFenceController,
285 flightMap: root.editorMap
286 })
287 }
288 break
289 case "rallyHeader":
290 if (delegateRoot.nodeObject) {
291 setSource(delegateRoot._qrcBase + "RallyPointEditorHeader.qml", {
292 objectName: "planTree_rallyHeader",
293 width: Qt.binding(() => delegateRoot.width),
294 controller: root._rallyPointController
295 })
296 }
297 break
298 case "rallyItem":
299 if (delegateRoot.nodeObject) {
300 setSource(delegateRoot._qrcBase + "RallyPointItemEditor.qml", {
301 width: Qt.binding(() => delegateRoot.width),
302 rallyPoint: delegateRoot.nodeObject,
303 controller: root._rallyPointController
304 })
305 }
306 break
307 case "transformEditor":
308 setSource(delegateRoot._qrcBase + "TransformEditor.qml", {
309 width: Qt.binding(() => delegateRoot.width),
310 missionController: root._missionController
311 })
312 break
313 }
314 }
315
316 onLoaded: {
317 if (delegateRoot.nodeType === "missionItem" && item) {
318 item.clicked.connect(function() {
319 root._missionController.setCurrentPlanViewSeqNum(delegateRoot.nodeObject.sequenceNumber, false)
320 })
321 item.remove.connect(function() {
322 var viIndex = root._missionController.visualItemIndexForObject(delegateRoot.nodeObject)
323 if (viIndex > 0) {
324 root._missionController.removeVisualItem(viIndex)
325 }
326 })
327 item.selectNextNotReadyItem.connect(function() {
328 for (var i = 0; i < root._missionController.visualItems.count; i++) {
329 var vmi = root._missionController.visualItems.get(i)
330 if (vmi.readyForSaveState === VisualMissionItem.NotReadyForSaveData) {
331 root._missionController.setCurrentPlanViewSeqNum(vmi.sequenceNumber, true)
332 break
333 }
334 }
335 })
336 item.editorExpandedAndLoaded.connect(function() {
337 root._scrollToMissionItem(delegateRoot)
338 })
339 }
340 }
341 }
342
343 Rectangle {
344 id: separatorLine
345 anchors.margins: ScreenTools.defaultFontPixelWidth * 0.5
346 anchors.topMargin: root.rowSpacing
347 anchors.top: loader.bottom
348 anchors.left: parent.left
349 anchors.right: parent.right
350 height: 1
351 color: qgcPal.borderSubtle
352 visible: delegateRoot.separator
353 }
354
355 // ── Group header (Mission Items / GeoFence / Rally Points) ──
356 Component {
357 id: groupHeaderComponent
358
359 Rectangle {
360 objectName: "planTree_" + delegateRoot.nodeType + "Header"
361 width: delegateRoot.width
362 height: ScreenTools.implicitComboBoxHeight + ScreenTools.defaultFontPixelWidth
363 color: qgcPal.windowCard
364
365 RowLayout {
366 id: groupHeaderRow
367 spacing: ScreenTools.defaultFontPixelWidth * 0.5
368 anchors.verticalCenter: parent.verticalCenter
369 anchors.left: parent.left
370 anchors.right: parent.right
371 anchors.margins: ScreenTools.defaultFontPixelWidth * 0.5
372
373 QGCColoredImage {
374 Layout.alignment: Qt.AlignVCenter
375 Layout.preferredWidth: ScreenTools.defaultFontPixelHeight * 0.75
376 Layout.preferredHeight: Layout.preferredWidth
377 source: "/InstrumentValueIcons/cheveron-right.svg"
378 color: qgcPal.textPrimary
379 rotation: delegateRoot.expanded ? 90 : 0
380 }
381
382 QGCLabel {
383 Layout.alignment: Qt.AlignBaseline
384 text: delegateRoot.nodeObject ? delegateRoot.nodeObject.objectName : ""
385 font.bold: true
386 }
387
388 QGCColoredImage {
389 Layout.alignment: Qt.AlignVCenter
390 Layout.preferredWidth: ScreenTools.defaultFontPixelHeight * 0.75
391 Layout.preferredHeight: Layout.preferredWidth
392 source: root._groupIcon(delegateRoot.nodeType)
393 color: qgcPal.textPrimary
394 visible: source.toString() !== ""
395 }
396
397 QGCLabel {
398 Layout.alignment: Qt.AlignBaseline
399 Layout.fillWidth: true
400 text: root._groupSubtitle(delegateRoot.nodeType)
401 elide: Text.ElideRight
402 textFormat: Text.PlainText
403 font.pointSize: ScreenTools.smallFontPointSize
404 color: qgcPal.colorGrey
405 }
406 }
407
408 MouseArea {
409 anchors.fill: parent
410 onClicked: {
411 if (PlanEditLayers.infoForNodeType(delegateRoot.nodeType)) {
412 // Layer groups cannot be collapsed by clicking their header. The active
413 // editing layer always has its group expanded. selectLayer() handles
414 // the allowViewSwitch() guard.
415 root.selectLayer(delegateRoot.nodeType)
416 } else {
417 if (!mainWindow.allowViewSwitch()) {
418 return
419 }
420 root._toggleGroup(delegateRoot.row)
421 }
422 }
423 }
424 }
425 }
426
427 }
428}