Debian Codegraph @

src/imports/Components/2.0/TabBar.qml

raw
1 /*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components as Toolkit
19
20 /*!
21 \qmltype TabBar
22 \inqmlmodule Lomiri.Components
23 \ingroup lomiri
24 \deprecated
25 \brief Tab bar that will be shown in the header when \l Tabs is active.
26 This component does not need to be instantiated by the developer, it is
27 automatically created by the \l Tabs.
28 */
29 Toolkit.StyledItem {
30 id: tabBar
31
32 /*!
33 \deprecated
34 The \l Tabs item that tab bar belongs to.
35 Will be automatically set by \l Tabs when the TabBar is created.
36 */
37 // tabsItem is of type Tabs, but using that type would cause an include loop
38 property Item tabsItem
39 /*! \internal */
40 onTabsItemChanged: console.error("tabsItem property is deprecated. TabBar functionality no longer requires it.")
41
42 /*!
43 The model containing the tabs to be controlled by the TabBar. The tabs are
44 visualized by the style, displaying controlling elements based on the data
45 specified by the roles. The default style mandates the existence of either
46 the \b title or \b tab role, but different styles may require to have other
47 roles (e.g. image, color). The order the role existence is checked is also
48 determined by the style component, Default style checks the existence of the
49 \b tab role first, and if not defined will use the \b title role.
50 */
51 property var model: null
52
53 /*!
54 The user is interacting with the tab bar.
55 Depends on the style pressed property.
56 */
57 readonly property bool pressed: __styleInstance !== null && __styleInstance.hasOwnProperty("pressed") ?
58 __styleInstance.pressed : false
59
60 /*!
61 An inactive tab bar only displays the currently selected tab,
62 and an active tab bar can be interacted with to select a tab.
63 */
64 property bool selectionMode: true
65
66 /*!
67 The property holds the index of the selected Tab item.
68 Note: Setting this property is DEPRECATED. Set the selectedIndex of the model instead.
69 */
70 property int selectedIndex: (model && internal.modelChecked) ? model.selectedIndex : -1
71
72 /*! \internal */
73 onSelectedIndexChanged: {
74 if (!model) return;
75 if (tabBar.selectedIndex !== model.selectedIndex) {
76 console.warn("Setting TabBar.selectedIndex is DEPRECATED. Set selectedIndex of the model instead");
77 tabBar.selectedIndex = Qt.binding(function() { return (model && internal.modelChecked) ? model.selectedIndex : -1 });
78 }
79 }
80
81 /*!
82 Do not deactivate the tab bar after a specified idle time or when the user selects a new tab.
83 Off by default.
84 */
85 property bool alwaysSelectionMode: false
86
87 /*!
88 Automatically activate the tab bar when \l alwaysSelectionMode is set.
89 */
90 onAlwaysSelectionModeChanged: {
91 if (tabBar.alwaysSelectionMode) selectionMode = true;
92 }
93
94 /*!
95 Show animations when the state changes. Default: true.
96 */
97 property bool animate: true
98
99 implicitHeight: units.gu(7.5)
100 activeFocusOnPress: true
101 styleName: "TabBarStyle"
102
103 QtObject {
104 id: internal
105
106 property bool modelChecked: true;
107
108 // returns true if the roles are good, false otherwise.
109 function checkRoles() {
110 if (tabBar.model.count <= 0)
111 return false;
112
113 modelChecked = true;
114 var f = tabBar.model.get(0);
115 if (f.tab === undefined && f.title === undefined) {
116 console.error("TabBar model must provide either tab or title role.");
117 tabBar.model = null;
118 return false;
119 }
120 if (f.tab !== undefined && f.tab.title === undefined) {
121 console.error("TabBar model's tab role must have title property.");
122 tabBar.model = null;
123 return false;
124 }
125 return true;
126 }
127 }
128
129 /*! \internal */
130 onModelChanged: {
131 internal.modelChecked = true;
132
133 if (!model)
134 return;
135
136 if (!model.hasOwnProperty("selectedIndex")) {
137 console.error("TabBar model must have selectedIndex property defined.");
138 tabBar.model = null;
139 return;
140 }
141
142 if (!model.hasOwnProperty("count")) {
143 console.error("TabBar model must have count property defined.");
144 tabBar.model = null;
145 return;
146 }
147
148 if (!model.hasOwnProperty("get")) {
149 console.error("TabBar model must provide get() function.");
150 tabBar.model = null;
151 return;
152 }
153
154 if (model.count > 0) {
155 if (internal.checkRoles()) {
156 model.selectedIndex = Math.max(Math.min(model.selectedIndex, model.count - 1), 0);
157 }
158 } else {
159 internal.modelChecked = false;
160 }
161 }
162
163 Connections {
164 target: !internal.modelChecked ? tabBar.model : null
165 function onCountChanged() { internal.checkRoles(); }
166 }
167 }