1pragma ComponentBehavior: Bound
8import QGroundControl.Controls
10import "QGCGroupBox.js" as GroupBox
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.
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
23 default property alias contentData: _contentLayout.data
25 property alias contentSpacing: _contentLayout.spacing
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
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
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: {
51 for (const headerButton of _header.children) {
52 if (headerButton.shown) {
53 widths.push(headerButton.implicitWidth)
58 property int _shownHeaderButtonCount: {
60 for (const headerButton of _header.children) {
61 if (headerButton.shown) {
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)
72 // Sorted top to bottom for divider visibility checks
73 property var _ySortedChildren: {
74 if (!showDividers || !_dividerLayout) {
78 for (const child of _dividerLayout.children) {
81 children.sort((a, b) => a.y - b.y)
85 // Right edge of the first row of header buttons, which is where the top border line resumes
86 property real _headerFirstRowRight: {
88 for (const headerButton of _header.children) {
89 if (headerButton.shown && headerButton.y === 0) {
90 right = Math.max(right, headerButton.x + headerButton.width)
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
100 function _updateTabs() {
102 for (const child of _contentLayout.children) {
103 if (child._isQGCGroupBoxTab === true) {
104 child._groupBox = control
108 // Reassigning the model rebuilds every header button, so only do it when the tabs actually changed
109 if (!GroupBox.sameItems(tabs, _tabs)) {
112 _validateCurrentIndex()
115 function _validateCurrentIndex() {
116 if (_tabs.length === 0) {
119 const index = GroupBox.validTabIndex(currentIndex, _tabs.map(tab => tab.tabVisible))
120 if (index !== currentIndex) {
125 function _isRepeater(item) {
126 return item && item.toString().startsWith("QQuickRepeater")
129 onCurrentIndexChanged: _validateCurrentIndex()
131 component HeaderButton: AbstractButton {
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
142 property Item groupBox: null
143 property bool shown: true
144 property bool interactive: true
145 property string iconSource
146 property Item badge: null
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
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
163 badge.parent = badgeHost
165 _adoptedBadge = badge
168 QGCPalette { id: buttonPal; colorGroupEnabled: headerButton.enabled }
170 background: Rectangle {
171 radius: ScreenTools.defaultBorderRadius
173 if (headerButton._selected) {
174 return buttonPal.accentSubtle
176 return headerButton.groupBox ? headerButton.groupBox.fillColor : "transparent"
178 border.width: headerButton._disabledSelected ? 2 : 1
180 if (headerButton._selected) {
181 return buttonPal.accent
183 if (headerButton._disabledSelected) {
184 return buttonPal.textPrimary
186 if (headerButton.enabled && headerButton._isTabChoice && (headerButton.hovered || headerButton.pressed)) {
187 return buttonPal.accent
189 return headerButton.groupBox ? headerButton.groupBox.outerBorderColor : buttonPal.borderSubtle
193 contentItem: RowLayout {
195 spacing: ScreenTools.defaultFontPixelWidth / 2
198 Layout.alignment: Qt.AlignHCenter
199 height: ScreenTools.defaultFontPixelHeight
201 sourceSize.height: height
202 fillMode: Image.PreserveAspectFit
203 source: headerButton.iconSource
204 color: headerButton._textColor
205 visible: headerButton.iconSource !== ""
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
220 implicitWidth: headerButton.badge && headerButton.badge.visible ? headerButton.badge.width : 0
221 implicitHeight: headerButton.badge ? headerButton.badge.height : 0
222 visible: headerButton.badge !== null
226 onBadgeChanged: _adoptBadge()
228 // Change handlers do not run for the initial value, so a badge set at creation is adopted here
229 Component.onCompleted: _adoptBadge()
233 id: _headerButtonFontMetrics
234 font.family: ScreenTools.normalFontFamily
235 font.pointSize: control.headingPointSize
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.
242 y: _header.visible ? control._headerButtonHeight / 2 : 0
244 height: control.height - y
245 visible: control.showBorder
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)
254 radius: outline._radius
255 color: control.fillColor
258 // Sides and bottom, then the top left and top right corners
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)
270 width: modelData.width
271 height: modelData.height
274 required property rect modelData
280 height: outline.height
281 radius: outline._radius
283 border.color: control.outerBorderColor
290 model: outline._topSegments
294 width: modelData.width
296 color: control.outerBorderColor
298 required property var modelData
305 x: control._headerInset
306 width: control.width - control._headerInset * 2
307 spacing: control._headerButtonSpacing
308 visible: control._shownHeaderButtonImplicitWidths.length > 0
312 text: control.heading
313 shown: control._tabs.length === 0 && control.heading !== ""
322 required property var modelData
323 required property int index
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
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
347 Layout.fillWidth: true
348 text: control.headingDescription
349 wrapMode: Text.WordWrap
350 font.pointSize: ScreenTools.smallFontPointSize
351 color: QGroundControl.globalPalette.textSecondary
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)
365 onChildrenChanged: control._updateTabs()
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
376 model: control._ySortedChildren.length
380 y: _contentItem ? _contentItem.y + _contentItem.height + control._dividerLayout.spacing / 2 : 0
381 width: dividerOverlay.width
383 color: control._dividerColor
384 visible: GroupBox.dividerVisibleAfter(divider.index, control._dividerItemStates)
386 required property int index
387 property var _contentItem: index < control._ySortedChildren.length ? control._ySortedChildren[index] : undefined
392 Component.onCompleted: _updateTabs()