QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AppSettings.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7import QGroundControl.AppSettings
8
9Rectangle {
10 id: settingsView
11 color: qgcPal.window
12 z: QGroundControl.zOrderTopMost
13
14 readonly property real _defaultTextHeight: ScreenTools.defaultFontPixelHeight
15 readonly property real _defaultTextWidth: ScreenTools.defaultFontPixelWidth
16 readonly property real _horizontalMargin: _defaultTextWidth / 2
17 readonly property real _verticalMargin: _defaultTextHeight / 2
18
19 property bool _first: true
20 property bool _commingFromRIDSettings: false
21 property int _selectedPageIndex: -1
22 property int _selectedSectionIndex: -1
23 property var _expandedPages: ({}) // pageIndex -> bool
24 property int _expandedRevision: 0 // bumped to trigger re-evaluation
25 property string _searchQuery: ""
26 property real _fullListWidth: 0 // Button column width with no search active
27
28 Binding on _fullListWidth {
29 when: settingsView._searchQuery.trim() === ""
30 value: buttonColumn.implicitWidth
31 restoreMode: Binding.RestoreNone
32 }
33
34 function _setExpanded(pageIndex, value) {
35 _expandedPages[pageIndex] = value
36 _expandedRevision++
37 }
38
39 function _isExpanded(pageIndex) {
40 void _expandedRevision // create binding dependency
41 return !!_expandedPages[pageIndex]
42 }
43
44 function _pageSections(entry) {
45 return entry && typeof entry.sections === "function" ? entry.sections() : []
46 }
47
48 function _pageAvailable(entry) {
49 if (!entry || entry.name === "Divider" ||
50 (typeof entry.pageVisible === "function" && !entry.pageVisible())) {
51 return false
52 }
53
54 var sections = _pageSections(entry)
55 return sections.length === 0 || sections.some(function(section) { return section.visible })
56 }
57
58 function _sectionAvailable(sections, sectionIndex) {
59 if (sectionIndex === -1) return true
60 for (var i = 0; i < sections.length; i++) {
61 if (sections[i].index === sectionIndex) return sections[i].visible
62 }
63 return false
64 }
65
66 // A divider shows only between two available pages. When consecutive dividers have no
67 // available page between them, only the first one shows.
68 function _dividerVisible(pageIndex) {
69 if (_searchQuery.trim() !== "") {
70 return false
71 }
72
73 let hasPageBefore = false
74 for (let i = pageIndex - 1; i >= 0; i--) {
75 let entry = settingsPagesModel.get(i)
76 if (entry.name === "Divider") {
77 return false
78 }
79 if (_pageAvailable(entry)) {
80 hasPageBefore = true
81 break
82 }
83 }
84 if (!hasPageBefore) {
85 return false
86 }
87
88 for (let j = pageIndex + 1; j < settingsPagesModel.count; j++) {
89 if (_pageAvailable(settingsPagesModel.get(j))) {
90 return true
91 }
92 }
93 return false
94 }
95
96 // Search: returns array of matching section indices for a page, or empty if no match
97 function _matchingSections(pageIndex) {
98 var query = _searchQuery.toLowerCase().trim()
99 if (query === "") return [] // empty = no filtering
100
101 var entry = settingsPagesModel.get(pageIndex)
102 if (!_pageAvailable(entry)) return []
103
104 var sections = _pageSections(entry)
105 var matches = []
106 for (var i = 0; i < sections.length; i++) {
107 if (!sections[i].visible) continue
108 for (var j = 0; j < sections[i].searchTerms.length; j++) {
109 if (sections[i].searchTerms[j].indexOf(query) !== -1) {
110 matches.push(sections[i].index)
111 break
112 }
113 }
114 }
115 return matches
116 }
117
118 // Does this page have any search matches? (or is search empty = show all)
119 function _pageMatchesSearch(pageIndex) {
120 if (_searchQuery.trim() === "") return true
121 return _matchingSections(pageIndex).length > 0
122 }
123
124 function _navigateTo(pageIndex, sectionIndex) {
125 var entry = settingsPagesModel.get(pageIndex)
126 if (!entry || entry.name === "Divider") return
127 if (!_pageAvailable(entry)) return
128 if (!_sectionAvailable(_pageSections(entry), sectionIndex)) return
129
130 var url = entry.url
131 _selectedSectionIndex = sectionIndex
132
133 if (_selectedPageIndex !== pageIndex) {
134 _selectedPageIndex = pageIndex
135 rightPanel.source = url
136 }
137
138 // Apply section filter after the page is loaded
139 if (rightPanel.item && typeof rightPanel.item.sectionFilter !== "undefined") {
140 rightPanel.item.sectionFilter = sectionIndex
141 }
142 }
143
144 function _navigateToFirstAvailablePage() {
145 for (var i = 0; i < settingsPagesModel.count; i++) {
146 if (_pageAvailable(settingsPagesModel.get(i))) {
147 _navigateTo(i, -1)
148 return
149 }
150 }
151
152 _selectedPageIndex = -1
153 _selectedSectionIndex = -1
154 rightPanel.source = ""
155 }
156
157 // settingsPage is the untranslated page name from SettingsPages.json
158 function showSettingsPage(settingsPage) {
159 for (var i = 0; i < settingsPagesModel.count; i++) {
160 var entry = settingsPagesModel.get(i)
161 if (entry && entry.nameKey === settingsPage) {
162 _navigateTo(i, -1)
163 break
164 }
165 }
166 }
167
168 // This need to block click event leakage to underlying map.
169 DeadMouseArea {
170 anchors.fill: parent
171 }
172
173 QGCPalette { id: qgcPal }
174
175 Component.onCompleted: {
176 if (globals.commingFromRIDIndicator) {
177 globals.commingFromRIDIndicator = false
178 showSettingsPage("Remote ID")
179 }
180
181 if (_selectedPageIndex === -1) {
182 _navigateToFirstAvailablePage()
183 }
184 }
185
186 Connections {
187 target: rightPanel
188 function onLoaded() {
189 if (rightPanel.item && typeof rightPanel.item.sectionFilter !== "undefined") {
190 rightPanel.item.sectionFilter = _selectedSectionIndex
191 }
192 }
193 }
194
195 SettingsPagesModel { id: settingsPagesModel }
196
197 Rectangle {
198 anchors.fill: leftPanel
199 anchors.margins: -_horizontalMargin * 2
200 color: qgcPal.windowPanel
201 border.color: qgcPal.borderSubtle
202 radius: ScreenTools.defaultBorderRadius
203 }
204
205 ColumnLayout {
206 id: leftPanel
207 width: Math.max(buttonColumn.implicitWidth, _fullListWidth, ScreenTools.isMobile ? 0 : ScreenTools.defaultFontPixelWidth * 22)
208 anchors.topMargin: _verticalMargin * 1.5
209 anchors.bottomMargin: _verticalMargin * 1.5
210 anchors.top: parent.top
211 anchors.bottom: parent.bottom
212 anchors.leftMargin: _horizontalMargin * 3
213 anchors.left: parent.left
214 spacing: _verticalMargin / 2
215
216 QGCTextField {
217 id: searchField
218 objectName: "settings_searchField"
219 Layout.fillWidth: true
220 placeholderText: qsTr("Search settings...")
221
222 onTextChanged: {
223 settingsView._searchQuery = text
224 }
225 }
226
227 QGCFlickable {
228 id: buttonList
229 objectName: "settings_buttonList"
230 Layout.fillWidth: true
231 Layout.fillHeight: true
232 contentHeight: buttonColumn.height + _verticalMargin
233 flickableDirection: Flickable.VerticalFlick
234 clip: true
235
236 ColumnLayout {
237 id: buttonColumn
238 width: buttonList.width
239 spacing: 0
240
241 Repeater {
242 id: buttonRepeater
243 model: settingsPagesModel
244
245 ColumnLayout {
246 id: pageColumn
247 spacing: 0
248 Layout.fillWidth: true
249
250 required property int index
251 required property var model
252
253 property string pageName: model.name ?? ""
254 property string pageUrl: model.url ?? ""
255 property string pageIconUrl: model.iconUrl ?? ""
256 property var pageVisible: model.pageVisible ?? function() { return true }
257 property var pageSections: pageVisible() ? settingsView._pageSections(model) : []
258 property var visiblePageSections: pageSections.filter(function(section) {
259 return section.visible
260 })
261 property bool pageAvailable: pageVisible() &&
262 (pageSections.length === 0 || visiblePageSections.length > 0)
263 property bool isSelected: settingsView._selectedPageIndex === index
264 property bool hasMultipleSections: visiblePageSections.length > 1
265 property bool isSearching: settingsView._searchQuery.trim() !== ""
266 property bool matchesSearch: pageAvailable && settingsView._pageMatchesSearch(index)
267 property bool isExpanded: hasMultipleSections && (isSearching ? matchesSearch : settingsView._isExpanded(index))
268
269 onPageSectionsChanged: {
270 let sections = pageSections ?? []
271 let visibleCount = sections.filter(function(section) { return section.visible }).length
272 if (isSelected && pageAvailable && settingsView._selectedSectionIndex !== -1 &&
273 (visibleCount <= 1 ||
274 !settingsView._sectionAvailable(sections, settingsView._selectedSectionIndex))) {
275 settingsView._navigateTo(index, -1)
276 }
277 }
278
279 onPageAvailableChanged: {
280 if (isSelected && !pageAvailable) {
281 settingsView._navigateToFirstAvailablePage()
282 }
283 }
284
285 visible: {
286 if (pageName === "Divider") return settingsView._dividerVisible(index)
287 if (!pageAvailable) return false
288 if (isSearching) return matchesSearch
289 return true
290 }
291
292 SidebarDivider {
293 objectName: pageName === "Divider" ? "settingsDivider_" + index : ""
294 visible: pageName === "Divider"
295 }
296
297 // Page button
298 SettingsButton {
299 Layout.fillWidth: true
300 objectName: "settingsButton_" + (model.nameKey ?? pageName)
301 text: pageName
302 icon.source: pageIconUrl
303 expandable: hasMultipleSections
304 expanded: isExpanded
305 checked: isSelected && settingsView._selectedSectionIndex === -1
306 visible: pageName !== "Divider" && pageAvailable
307
308 onClicked: {
309 if (mainWindow.allowViewSwitch()) {
310 settingsView._navigateTo(index, -1)
311 if (hasMultipleSections) {
312 // Toggle expand/collapse when re-clicking the same page
313 if (isSelected && isExpanded) {
314 settingsView._setExpanded(index, false)
315 } else if (!isExpanded) {
316 settingsView._setExpanded(index, true)
317 }
318 }
319 }
320 }
321
322 onToggleExpand: {
323 if (!mainWindow.allowViewSwitch()) {
324 return
325 }
326 var expanding = !isExpanded
327 settingsView._setExpanded(index, expanding)
328 if (!expanding && isSelected) {
329 settingsView._navigateTo(index, -1)
330 }
331 }
332 }
333
334 // Section sub-items (indented, shown when page is expanded)
335 Repeater {
336 model: isExpanded ? visiblePageSections : []
337
338 Button {
339 id: sectionBtn
340 Layout.fillWidth: true
341 padding: ScreenTools.defaultFontPixelWidth * 0.75
342 leftPadding: ScreenTools.defaultFontPixelWidth
343 hoverEnabled: !ScreenTools.isMobile
344
345 property int sectionIndex: modelData.index
346 property bool sectionChecked: pageColumn.isSelected && settingsView._selectedSectionIndex === sectionIndex
347 property bool sectionMatchesSearch: {
348 if (!pageColumn.isSearching) return true
349 var matches = settingsView._matchingSections(pageColumn.index)
350 return matches.indexOf(sectionIndex) !== -1
351 }
352 property color textColor: qgcPal.textPrimary
353 visible: sectionMatchesSearch
354
355 background: Rectangle {
356 color: sectionBtn.sectionChecked || sectionBtn.pressed ? qgcPal.accentSubtle : (sectionBtn.enabled && sectionBtn.hovered ? qgcPal.windowCard : "transparent")
357 border.width: sectionBtn.sectionChecked || sectionBtn.pressed ? 1 : 0
358 border.color: qgcPal.accent
359 radius: ScreenTools.defaultFontPixelWidth / 2
360 }
361
362 contentItem: QGCLabel {
363 text: modelData.name
364 color: sectionBtn.textColor
365 font.pointSize: ScreenTools.defaultFontPointSize * 0.9
366 horizontalAlignment: Text.AlignLeft
367 }
368
369 onClicked: {
370 if (mainWindow.allowViewSwitch()) {
371 settingsView._navigateTo(pageColumn.index, sectionIndex)
372 }
373 }
374 }
375 }
376 }
377 }
378 }
379 }
380 }
381
382 //-- Panel Contents
383 Loader {
384 id: rightPanel
385 objectName: "settings_rightPanel"
386 anchors.leftMargin: _horizontalMargin * 4
387 anchors.rightMargin: _horizontalMargin
388 anchors.topMargin: _verticalMargin
389 anchors.bottomMargin: _verticalMargin
390 anchors.left: leftPanel.right
391 anchors.right: parent.right
392 anchors.top: parent.top
393 anchors.bottom: parent.bottom
394 }
395}