6import QGroundControl.Controls
8// We implement our own TabBar to get around the fact that QtQuick.Controls TabBar does not
9// support hiding tabs. This version supports hiding tabs by setting the visible property
10// on the QGCTabButton instances.
12 property int currentIndex: 0
17 property bool _preventCurrentIndexBindingLoop: false
18 property var _buttons: []
20 function _updateButtons() {
22 for (var i = 0; i < control.children.length; i++) {
23 if (control.children[i].hasOwnProperty("checkable")) {
24 btns.push(control.children[i])
28 buttonGroup.buttons = _buttons
29 _selectCurrentIndexButton()
32 function _updateSeparators() {
33 for (var i = 0; i < _buttons.length; i++) {
34 if (!_buttons[i].visible || _buttons[i].checked) {
35 _buttons[i]._showSeparator = false
38 // Find the next visible button
39 var nextVisible = null
40 for (var j = i + 1; j < _buttons.length; j++) {
41 if (_buttons[j].visible) {
42 nextVisible = _buttons[j]
46 _buttons[i]._showSeparator = nextVisible !== null && !nextVisible.checked
50 function _selectCurrentIndexButton() {
51 if (_buttons.length === 0) return
53 _preventCurrentIndexBindingLoop = true
55 if (control.currentIndex === -1) {
56 for (var i = 0; i < _buttons.length; i++) {
57 _buttons[i].checked = false
60 let index = Math.min(Math.max(control.currentIndex, 0), _buttons.length - 1)
61 if (_buttons[index].visible) {
62 _buttons[index].checked = true
64 // Select the first visible tab if the current index is not visible
66 for (var j = 0; j < _buttons.length; j++) {
67 if (_buttons[j].visible) {
68 _buttons[j].checked = true
74 // Sync currentIndex to the actually-selected button so it never remains stale
75 if (control.currentIndex !== index) {
76 control.currentIndex = index
80 _preventCurrentIndexBindingLoop = false
84 Component.onCompleted: _updateButtons()
85 onChildrenChanged: _updateButtons()
86 onCurrentIndexChanged: _selectCurrentIndexButton()
87 onVisibleChildrenChanged: _selectCurrentIndexButton()
91 // When becoming visible, ensure the current index is valid and a button is selected
93 _selectCurrentIndexButton()
101 onCheckedButtonChanged: {
102 if (control._preventCurrentIndexBindingLoop) return
104 for (var i = 0; i < control._buttons.length; i++) {
105 if (control._buttons[i] === checkedButton) {
106 control.currentIndex = i