QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCGroupBox.qml
Go to the documentation of this file.
1pragma ComponentBehavior: Bound
2
3import QtQuick
4import QtQuick.Controls
5import QtQuick.Layouts
6
7import QGroundControl
8import QGroundControl.Controls
9
10import "QGCGroupBox.js" as GroupBox
11
12/// Bordered content area with rounded header buttons sitting on the top border.
13/// - Group box: set heading to show a single non-interactive header button.
14/// - Tabs: add QGCGroupBoxTab children. Each visible tab gets a header button and only the current tab's content is shown.
15/// Children which are not tabs are always shown. heading is ignored when there are tabs.
16Item {
17 id: control
18 // Matches the previous ColumnLayout based SettingsGroupLayout, which filled the width of a parent layout
19 Layout.fillWidth: true
20 implicitWidth: Math.max(_contentColumn.implicitWidth + _contentInset * 2, _headerImplicitWidth + _headerInset * 2)
21 implicitHeight: _contentColumn.y + _contentColumn.implicitHeight + _contentInset
22
23 default property alias contentData: _contentLayout.data
24
25 property alias contentSpacing: _contentLayout.spacing
26
27 property string heading
28 property string headingDescription
29 property real headingPointSize: ScreenTools.defaultFontPointSize
30 property color outerBorderColor: QGroundControl.globalPalette.borderSubtle
31 property color fillColor: showBorder ? QGroundControl.globalPalette.windowPanel : "transparent"
32 property bool showBorder: true
33 property bool showDividers: true
34 property bool wrapTabs: false ///< Wrap tab buttons onto multiple rows instead of eliding them to fit on one row
35 property int currentIndex: 0 ///< Index of the current QGCGroupBoxTab, -1 if no tab is visible
36
37 /// Distance from the top of the group box to the content, for sizing content to an available height
38 readonly property real contentTopInset: _contentColumn.y + _contentLayout.y
39 /// Distance from the left, right and bottom edges of the group box to the content
40 readonly property real contentSideInset: _contentInset
41
42 property real _margins: ScreenTools.defaultFontPixelHeight / 2
43 property real _contentInset: showBorder ? _margins : 0
44 property real _headerInset: showBorder ? _margins : 0
45 property real _headerButtonSpacing: ScreenTools.defaultFontPixelWidth / 2
46 property real _headerButtonHeight: Math.round(_headerButtonFontMetrics.height + ScreenTools.defaultFontPixelHeight * 0.4)
47 property var _tabs: []
48 property Item _currentTab: currentIndex >= 0 && currentIndex < _tabs.length ? _tabs[currentIndex] : null
49 property var _shownHeaderButtonImplicitWidths: {
50 let widths = []
51 for (const headerButton of _header.children) {
52 if (headerButton.shown) {
53 widths.push(headerButton.implicitWidth)
54 }
55 }
56 return widths
57 }
58 property int _shownHeaderButtonCount: {
59 let count = 0
60 for (const headerButton of _header.children) {
61 if (headerButton.shown) {
62 count++
63 }
64 }
65 return count
66 }
67 property real _headerImplicitWidth: _shownHeaderButtonImplicitWidths.reduce((sum, width) => sum + width, 0) +
68 _headerButtonSpacing * Math.max(_shownHeaderButtonImplicitWidths.length - 1, 0)
69 property real _maxHeaderButtonWidth: wrapTabs ? _header.width : GroupBox.maxHeaderButtonWidth(_shownHeaderButtonImplicitWidths, _header.width, _headerButtonSpacing)
70 property Item _dividerLayout: GroupBox.dividerLayout(_tabs.length > 0, _currentTab, _contentLayout)
71
72 // Sorted top to bottom for divider visibility checks
73 property var _ySortedChildren: {
74 if (!showDividers || !_dividerLayout) {
75 return []
76 }
77 let children = []
78 for (const child of _dividerLayout.children) {
79 children.push(child)
80 }
81 children.sort((a, b) => a.y - b.y)
82 return children
83 }
84
85 // Right edge of the first row of header buttons, which is where the top border line resumes
86 property real _headerFirstRowRight: {
87 let right = 0
88 for (const headerButton of _header.children) {
89 if (headerButton.shown && headerButton.y === 0) {
90 right = Math.max(right, headerButton.x + headerButton.width)
91 }
92 }
93 return right
94 }
95 property color _dividerColor: QGroundControl.globalPalette.borderSubtle
96 property var _dividerItemStates: _ySortedChildren.map(function(item) {
97 return item ? { visible: item.visible, isRepeater: _isRepeater(item) } : null
98 })
99
100 function _updateTabs() {
101 let tabs = []
102 for (const child of _contentLayout.children) {
103 if (child._isQGCGroupBoxTab === true) {
104 child._groupBox = control
105 tabs.push(child)
106 }
107 }
108 // Reassigning the model rebuilds every header button, so only do it when the tabs actually changed
109 if (!GroupBox.sameItems(tabs, _tabs)) {
110 _tabs = tabs
111 }
112 _validateCurrentIndex()
113 }
114
115 function _validateCurrentIndex() {
116 if (_tabs.length === 0) {
117 return
118 }
119 const index = GroupBox.validTabIndex(currentIndex, _tabs.map(tab => tab.tabVisible))
120 if (index !== currentIndex) {
121 currentIndex = index
122 }
123 }
124
125 function _isRepeater(item) {
126 return item && item.toString().startsWith("QQuickRepeater")
127 }
128
129 onCurrentIndexChanged: _validateCurrentIndex()
130
131 component HeaderButton: AbstractButton {
132 id: headerButton
133 width: groupBox ? Math.min(implicitWidth, groupBox._maxHeaderButtonWidth) : implicitWidth
134 implicitWidth: leftPadding + rightPadding + buttonContent.implicitWidth
135 implicitHeight: groupBox ? groupBox._headerButtonHeight : 0
136 leftPadding: ScreenTools.defaultFontPixelWidth * 1.5
137 rightPadding: leftPadding
138 hoverEnabled: _isTabChoice && !ScreenTools.isMobile
139 focusPolicy: _isTabChoice ? Qt.ClickFocus : Qt.NoFocus
140 visible: shown
141
142 property Item groupBox: null
143 property bool shown: true
144 property bool interactive: true
145 property string iconSource
146 property Item badge: null
147
148 // A lone heading or tab is a title rather than a choice, so it is drawn as a quiet outlined label
149 property bool _isTabChoice: interactive && groupBox !== null && groupBox._shownHeaderButtonCount > 1
150 // Disabled buttons are never marked as selected with the accent since the disabled accent colors have too
151 // little contrast. A disabled selected tab is marked with a heavier border instead.
152 property bool _selected: _isTabChoice && checked && enabled
153 property bool _disabledSelected: _isTabChoice && checked && !enabled
154 property color _textColor: buttonPal.textPrimary
155 property Item _adoptedBadge: null
156
157 // Moves the badge into the header button, releasing a previously adopted badge which has been replaced
158 function _adoptBadge() {
159 if (_adoptedBadge && _adoptedBadge !== badge) {
160 _adoptedBadge.parent = null
161 }
162 if (badge) {
163 badge.parent = badgeHost
164 }
165 _adoptedBadge = badge
166 }
167
168 QGCPalette { id: buttonPal; colorGroupEnabled: headerButton.enabled }
169
170 background: Rectangle {
171 radius: ScreenTools.defaultBorderRadius
172 color: {
173 if (headerButton._selected) {
174 return buttonPal.accentSubtle
175 }
176 return headerButton.groupBox ? headerButton.groupBox.fillColor : "transparent"
177 }
178 border.width: headerButton._disabledSelected ? 2 : 1
179 border.color: {
180 if (headerButton._selected) {
181 return buttonPal.accent
182 }
183 if (headerButton._disabledSelected) {
184 return buttonPal.textPrimary
185 }
186 if (headerButton.enabled && headerButton._isTabChoice && (headerButton.hovered || headerButton.pressed)) {
187 return buttonPal.accent
188 }
189 return headerButton.groupBox ? headerButton.groupBox.outerBorderColor : buttonPal.borderSubtle
190 }
191 }
192
193 contentItem: RowLayout {
194 id: buttonContent
195 spacing: ScreenTools.defaultFontPixelWidth / 2
196
197 QGCColoredImage {
198 Layout.alignment: Qt.AlignHCenter
199 height: ScreenTools.defaultFontPixelHeight
200 width: height
201 sourceSize.height: height
202 fillMode: Image.PreserveAspectFit
203 source: headerButton.iconSource
204 color: headerButton._textColor
205 visible: headerButton.iconSource !== ""
206 }
207
208 QGCLabel {
209 Layout.fillWidth: true
210 text: headerButton.text
211 color: headerButton._textColor
212 font.pointSize: headerButton.groupBox ? headerButton.groupBox.headingPointSize : ScreenTools.defaultFontPointSize
213 font.bold: !headerButton._isTabChoice
214 elide: Text.ElideRight
215 visible: text !== ""
216 }
217
218 Item {
219 id: badgeHost
220 implicitWidth: headerButton.badge && headerButton.badge.visible ? headerButton.badge.width : 0
221 implicitHeight: headerButton.badge ? headerButton.badge.height : 0
222 visible: headerButton.badge !== null
223 }
224 }
225
226 onBadgeChanged: _adoptBadge()
227
228 // Change handlers do not run for the initial value, so a badge set at creation is adopted here
229 Component.onCompleted: _adoptBadge()
230 }
231
232 FontMetrics {
233 id: _headerButtonFontMetrics
234 font.family: ScreenTools.normalFontFamily
235 font.pointSize: control.headingPointSize
236 }
237
238 // The border is drawn in pieces so the top edge stops either side of the header buttons, which are filled with
239 // fillColor so they read as tabs sitting on the edge of the panel.
240 Item {
241 id: outline
242 y: _header.visible ? control._headerButtonHeight / 2 : 0
243 width: control.width
244 height: control.height - y
245 visible: control.showBorder
246
247 property real _radius: control._margins
248 property var _topSegments: GroupBox.topBorderSegments(width, _radius,
249 _header.visible ? _header.x - control._headerButtonSpacing : _radius,
250 _header.visible ? _header.x + control._headerFirstRowRight + control._headerButtonSpacing : _radius)
251
252 Rectangle {
253 anchors.fill: parent
254 radius: outline._radius
255 color: control.fillColor
256 }
257
258 // Sides and bottom, then the top left and top right corners
259 Repeater {
260 model: [
261 Qt.rect(0, outline._radius, outline.width, Math.max(outline.height - outline._radius, 0)),
262 Qt.rect(0, 0, outline._radius, outline._radius),
263 Qt.rect(outline.width - outline._radius, 0, outline._radius, outline._radius)
264 ]
265
266 Item {
267 id: outlinePiece
268 x: modelData.x
269 y: modelData.y
270 width: modelData.width
271 height: modelData.height
272 clip: true
273
274 required property rect modelData
275
276 Rectangle {
277 x: -outlinePiece.x
278 y: -outlinePiece.y
279 width: outline.width
280 height: outline.height
281 radius: outline._radius
282 color: "transparent"
283 border.color: control.outerBorderColor
284 border.width: 1
285 }
286 }
287 }
288
289 Repeater {
290 model: outline._topSegments
291
292 Rectangle {
293 x: modelData.x
294 width: modelData.width
295 height: 1
296 color: control.outerBorderColor
297
298 required property var modelData
299 }
300 }
301 }
302
303 Flow {
304 id: _header
305 x: control._headerInset
306 width: control.width - control._headerInset * 2
307 spacing: control._headerButtonSpacing
308 visible: control._shownHeaderButtonImplicitWidths.length > 0
309
310 HeaderButton {
311 groupBox: control
312 text: control.heading
313 shown: control._tabs.length === 0 && control.heading !== ""
314 checked: true
315 interactive: false
316 }
317
318 Repeater {
319 model: control._tabs
320
321 HeaderButton {
322 required property var modelData
323 required property int index
324
325 groupBox: control
326
327 objectName: modelData.buttonObjectName
328 text: modelData.title
329 iconSource: modelData.iconSource
330 badge: modelData.badge
331 shown: modelData.tabVisible
332 checked: index === control.currentIndex
333 onClicked: control.currentIndex = index
334 }
335 }
336 }
337
338 ColumnLayout {
339 id: _contentColumn
340 x: control._contentInset
341 y: _header.visible ? _header.height + control._margins / 2 : control._contentInset
342 width: control.width - control._contentInset * 2
343 height: control.height - y - control._contentInset
344 spacing: control._margins / 2
345
346 QGCLabel {
347 Layout.fillWidth: true
348 text: control.headingDescription
349 wrapMode: Text.WordWrap
350 font.pointSize: ScreenTools.smallFontPointSize
351 color: QGroundControl.globalPalette.textSecondary
352 visible: text !== ""
353 }
354
355 ColumnLayout {
356 id: _contentLayout
357 Layout.fillWidth: true
358 // Layouts cap their maximum width to their implicit width when no child fills the width
359 Layout.maximumWidth: Number.POSITIVE_INFINITY
360 // Keep content within the border even when a child's minimum width is wider than the group box
361 Layout.minimumWidth: 0
362 Layout.fillHeight: true
363 spacing: control._margins * (control.showDividers ? 2 : 1)
364
365 onChildrenChanged: control._updateTabs()
366 }
367 }
368
369 Item {
370 id: dividerOverlay
371 x: _contentColumn.x + (control._dividerLayout && control._dividerLayout !== _contentLayout ? control._dividerLayout.x : 0)
372 y: _contentColumn.y + _contentLayout.y + (control._dividerLayout && control._dividerLayout !== _contentLayout ? control._dividerLayout.y : 0)
373 width: control._dividerLayout ? control._dividerLayout.width : _contentLayout.width
374
375 Repeater {
376 model: control._ySortedChildren.length
377
378 Rectangle {
379 id: divider
380 y: _contentItem ? _contentItem.y + _contentItem.height + control._dividerLayout.spacing / 2 : 0
381 width: dividerOverlay.width
382 height: 1
383 color: control._dividerColor
384 visible: GroupBox.dividerVisibleAfter(divider.index, control._dividerItemStates)
385
386 required property int index
387 property var _contentItem: index < control._ySortedChildren.length ? control._ySortedChildren[index] : undefined
388 }
389 }
390 }
391
392 Component.onCompleted: _updateTabs()
393}