QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCTabBar.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7
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.
11RowLayout {
12 property int currentIndex: 0
13
14 id: control
15 spacing: 0
16
17 property bool _preventCurrentIndexBindingLoop: false
18 property var _buttons: []
19
20 function _updateButtons() {
21 let btns = []
22 for (var i = 0; i < control.children.length; i++) {
23 if (control.children[i].hasOwnProperty("checkable")) {
24 btns.push(control.children[i])
25 }
26 }
27 _buttons = btns
28 buttonGroup.buttons = _buttons
29 _selectCurrentIndexButton()
30 }
31
32 function _selectCurrentIndexButton() {
33 if (_buttons.length === 0) return
34
35 _preventCurrentIndexBindingLoop = true
36
37 if (control.currentIndex === -1) {
38 for (var i = 0; i < _buttons.length; i++) {
39 _buttons[i].checked = false
40 }
41 } else {
42 let index = Math.min(Math.max(control.currentIndex, 0), _buttons.length - 1)
43 if (_buttons[index].visible) {
44 _buttons[index].checked = true
45 } else {
46 // Select the first visible tab if the current index is not visible
47 index = -1
48 for (var j = 0; j < _buttons.length; j++) {
49 if (_buttons[j].visible) {
50 _buttons[j].checked = true
51 index = j
52 break
53 }
54 }
55 }
56 // Sync currentIndex to the actually-selected button so it never remains stale
57 if (control.currentIndex !== index) {
58 control.currentIndex = index
59 }
60 }
61
62 _preventCurrentIndexBindingLoop = false
63 }
64
65 Component.onCompleted: _updateButtons()
66 onChildrenChanged: _updateButtons()
67 onCurrentIndexChanged: _selectCurrentIndexButton()
68 onVisibleChildrenChanged: _selectCurrentIndexButton()
69
70 ButtonGroup {
71 id: buttonGroup
72
73 onCheckedButtonChanged: {
74 if (control._preventCurrentIndexBindingLoop) return
75
76 for (var i = 0; i < control._buttons.length; i++) {
77 if (control._buttons[i] === checkedButton) {
78 control.currentIndex = i
79 break
80 }
81 }
82 }
83 }
84}