Debian Codegraph @

src/imports/Components/2.0/Tabs.qml

raw
1 /*
2 * Copyright 2012 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 Tabs
22 \inqmlmodule Lomiri.Components
23 \ingroup lomiri
24 \deprecated
25 \brief The Tabs class provides an environment where multible \l Tab
26 children can be added, and the user is presented with a tab
27 bar with tab buttons to select different tab pages.
28 This component is DEPRECATED. See http://design.lomiri.com/apps/patterns/navigation
29
30 Tabs must be placed inside a \l MainView so that it will automatically
31 have a header that shows the tabs that can be selected, and the toolbar
32 which contains the tools of the \l Page in the currently selected \l Tab.
33
34 Example:
35 \qml
36 import QtQuick 2.4
37 import Lomiri.Components
38 import Lomiri.Components.ListItems as ListItem
39
40 MainView {
41 width: units.gu(48)
42 height: units.gu(60)
43
44 Tabs {
45 id: tabs
46 Tab {
47 title: i18n.tr("Simple page")
48 page: Page {
49 Label {
50 id: label
51 anchors.centerIn: parent
52 text: "A centered label"
53 }
54 }
55 }
56 Tab {
57 id: externalTab
58 title: i18n.tr("External")
59 page: Loader {
60 parent: externalTab
61 anchors {
62 left: parent.left
63 right: parent.right
64 bottom: parent.bottom
65 }
66 source: (tabs.selectedTab === externalTab) ? Qt.resolvedUrl("MyCustomPage.qml") : ""
67 }
68 }
69 Tab {
70 title: i18n.tr("List view")
71 page: Page {
72 ListView {
73 clip: true
74 anchors.fill: parent
75 model: 20
76 delegate: ListItem.Standard {
77 iconName: "compose"
78 text: "Item "+modelData
79 }
80 }
81 }
82 }
83 }
84 }
85
86 \endqml
87 As the example above shows, an external \l Page inside a \l Tab can be loaded using a Loader.
88 Note that setting the top anchor or the height of the Loader would override the \l Page height.
89 We avoid this because the \l Page automatically adapts its height to accommodate for the header.
90
91 It is possible to use a Repeater to generate tabs, but when doing so, ensure that the Repeater
92 is declared inside the Tabs at the end, because otherwise the shuffling of
93 the order of children by the Repeater can cause incorrect ordering of the tabs.
94
95 The \l {http://design.lomiri.com/apps/global-patterns/navigation}{Navigation Patterns} specify that
96 a tabs header should never be combined with the back button of a \l PageStack. The only way to
97 combine Tabs and \l PageStack that avoids this is by pushing the Tabs as the first page on the
98 \l PageStack, and pushing other pages on top of that, as is shown in the following example:
99
100 \qml
101 import QtQuick 2.4
102 import Lomiri.Components
103
104 MainView {
105 id: mainView
106 width: units.gu(38)
107 height: units.gu(50)
108
109 PageStack {
110 id: pageStack
111 Component.onCompleted: push(tabs)
112
113 Tabs {
114 id: tabs
115 Tab {
116 title: "Tab 1"
117 page: Page {
118 Button {
119 anchors.centerIn: parent
120 onClicked: pageStack.push(page3)
121 text: "Press"
122 }
123 }
124 }
125 Tab {
126 title: "Tab 2"
127 page: Page {
128 Label {
129 anchors.centerIn: parent
130 text: "Use header to navigate between tabs"
131 }
132 }
133 }
134 }
135 Page {
136 id: page3
137 visible: false
138 title: "Page on stack"
139 Label {
140 anchors.centerIn: parent
141 text: "Press back to return to the tabs"
142 }
143 }
144 }
145 }
146 \endqml
147 */
148 Toolkit.PageTreeNode {
149 id: tabs
150 anchors.fill: parent
151
152 Component.onCompleted: {
153 print("Tabs component is deprecated. " +
154 "See http://design.lomiri.com/apps/patterns/navigation");
155 }
156
157 /*!
158 \qmlproperty int selectedTabIndex
159 The index of the currently selected tab.
160 The first tab is 0, and -1 means that no tab is selected.
161 The initial value is 0 if Tabs has contents, or -1 otherwise.
162 */
163 property alias selectedTabIndex: tabsModel.selectedIndex
164
165 /*!
166 The currently selected tab.
167 */
168 readonly property Tab selectedTab: (selectedTabIndex < 0) || (tabsModel.count <= selectedTabIndex) ?
169 null : tabsModel.get(selectedTabIndex).tab
170
171 /*!
172 The page of the currently selected tab.
173 */
174 readonly property Item currentPage: selectedTab ? selectedTab.page : null
175
176 /*!
177 \deprecated
178 The \l TabBar that will be shown in the header
179 and provides scrollable tab buttons.
180 This property is DEPRECATED. TabBar is now part of the header style.
181 */
182 property TabBar tabBar: internal.header && internal.header.__styleInstance &&
183 internal.header.__styleInstance.hasOwnProperty("__tabBar") ?
184 internal.header.__styleInstance.__tabBar : null
185
186 /*!
187 Children are placed in a separate item that has functionality to extract the Tab items.
188 \qmlproperty list<Item> tabChildren
189 */
190 default property alias tabChildren: tabStack.data
191
192 /*!
193 \qmlproperty int count
194 Contains the number of tabs in the Tabs component.
195 */
196 readonly property alias count: tabsModel.count
197
198 /*!
199 \deprecated
200 Used by the tabs style to update the tabs header with the titles of all the tabs.
201 This signal is used in an intermediate step in transitioning the tabs to a new
202 implementation and may be removed in the future.
203 */
204 signal modelChanged()
205
206 /*!
207 \internal
208 tst_tabs.qml needs access to the model to verify that Repeaters inside Tabs works.
209 */
210 property var __model: tabsModel
211
212 /*!
213 \internal
214 required by TabsStyle
215 */
216 ListModel {
217 id: tabsModel
218
219 property bool updateDisabled: false
220
221 /*!
222 The index of the selected tab.
223 */
224 property int selectedIndex: tabsModel.count > 0 ? 0 : -1
225
226 function listModel(tab) {
227 return {"title": tab.title, "tab": tab};
228 }
229
230 function updateTabList(tabsList) {
231 var offset = 0;
232 var tabIndex;
233 for (var i in tabsList) {
234 var tab = tabsList[i];
235 if (internal.isTab(tab)) {
236 tabIndex = i - offset;
237 // make sure we have the right parent
238 tab.parent = tabStack;
239
240 if (!tab.__protected.inserted) {
241 tab.__protected.index = tabIndex;
242 tab.__protected.inserted = true;
243 insert(tabIndex, listModel(tab));
244 } else if (!tab.__protected.removedFromTabs && tabsModel.count > tab.index) {
245 get(tab.index).title = tab.title;
246 }
247
248 // always makes sure that tabsModel has the same order as tabsList
249 move(tab.__protected.index, tabIndex, 1);
250 reindex();
251 } else {
252 // keep track of children that are not tabs so that we compute
253 // the right index for actual tabs
254 offset += 1;
255 }
256 }
257 internal.sync();
258 }
259
260 function reindex(from) {
261 var start = 0;
262 if (from !== undefined) {
263 start = from + 1;
264 }
265
266 for (var i = start; i < count; i++) {
267 var tab = get(i).tab;
268 tab.__protected.index = i;
269 }
270 }
271 }
272
273 // FIXME: this component is not really needed, as it doesn't really bring any
274 // value; should be removed in a later MR
275 Item {
276 anchors.fill: parent
277 id: tabStack
278
279 onChildrenChanged: {
280 internal.connectToRepeaters(tabStack.children);
281 tabsModel.updateTabList(tabStack.children);
282 }
283 }
284
285 /*
286 This timer is used when tabs are created using Repeaters. Repeater model
287 element moves (shuffling) are causing re-stacking of the tab stack children
288 which may not be realized at the time the rowsMoved/columnsMoved or layoutChanged
289 signals are triggered. Therefore we use an idle timer to update the tabs
290 model, so the tab stack is re-stacked by then.
291 */
292 Timer {
293 id: updateTimer
294 interval: 1
295 running: false
296 onTriggered: {
297 tabsModel.updateTabList(tabStack.children);
298 internal.sync();
299 }
300 }
301
302 Item {
303 id: internal
304 property AppHeader header: tabs.__propagated ? tabs.__propagated.header : null
305
306 /*
307 List of connected Repeaters to avoid repeater "hammering" of itemAdded() signal.
308 */
309 property var repeaters: []
310
311 function sync() {
312 if (tabBar && tabBar.__styleInstance && tabBar.__styleInstance.hasOwnProperty("sync")) {
313 tabBar.__styleInstance.sync();
314 }
315 if ((tabs.active && internal.header) &&
316 !(internal.header.config &&
317 internal.header.config.hasOwnProperty("locked") &&
318 internal.header.config.locked)) {
319 internal.header.exposed = true;
320 }
321 // deprecated, however use it till we remove it completely
322 tabs.modelChanged();
323 }
324
325 function isTab(item) {
326 if (item && item.hasOwnProperty("__isPageTreeNode")
327 && item.__isPageTreeNode && item.hasOwnProperty("title")
328 && item.hasOwnProperty("page")) {
329 return true;
330 } else {
331 return false;
332 }
333 }
334
335 function isRepeater(item) {
336 return (item && item.hasOwnProperty("itemAdded"));
337 }
338
339 function connectToRepeaters(children) {
340 for (var i = 0; i < children.length; i++) {
341 var child = children[i];
342 if (internal.isRepeater(child) && internal.repeaters && (internal.repeaters.indexOf(child) < 0)) {
343 internal.connectRepeater(child);
344 }
345 }
346 }
347 /* When inserting a delegate into its parent the Repeater does it in 3
348 steps:
349 1) sets the parent of the delegate thus inserting it in the list of
350 children in a position that does not correspond to the position of
351 the corresponding item in the model. At that point the
352 childrenChanged() signal is emitted.
353 2) reorder the delegate to match the position of the corresponding item
354 in the model.
355 3) emits the itemAdded() signal.
356
357 We need to update the list of tabs (tabsModel) when the children are in the
358 adequate order hence the workaround below. It connects to the itemAdded()
359 signal of any repeater it finds and triggers an update of the tabsModel.
360
361 Somewhat related Qt bug report:
362 https://bugreports.qt-project.org/browse/QTBUG-32438
363 */
364 function updateTabsModel() {
365 tabsModel.updateTabList(tabStack.children);
366 }
367
368 /*
369 Connects a Repeater and stores it so further connects will not happen to
370 the same repeater avoiding in this way hammering.
371 */
372 function connectRepeater(repeater) {
373 // store repeater
374 repeaters.push(repeater);
375
376 // connect destruction signal so we have a proper cleanup
377 repeater.Component.onDestruction.connect(internal.disconnectRepeater.bind(repeater));
378
379 // connect repeater's itemAdded and itemRemoved signals
380 repeater.itemAdded.connect(internal.updateTabsModel);
381 repeater.itemRemoved.connect(internal.removeTabFromModel);
382
383 // check if the repeater's model is set, if not, connect to modelChanged to catch that
384 if (repeater.model === undefined) {
385 repeater.modelChanged.connect(internal.connectRepeaterModelChanges.bind(repeater));
386 } else {
387 connectRepeaterModelChanges(repeater);
388 }
389 }
390
391 /*
392 Disconnects the given repeater signals.
393 */
394 function disconnectRepeater() {
395 this.itemAdded.disconnect(internal.updateTabsModel);
396 this.itemRemoved.disconnect(internal.removeTabFromModel);
397 this.modelChanged.disconnect(internal.connectRepeaterModelChanges);
398 }
399
400 /*
401 Connects a Repeater's model change signals so we get notified whenever those change.
402 This can be called either by the Repeater's modelChanged() signal, in which case the
403 parameter is undefined, or from the connectRepeater() in case the model is given for
404 the Repeater.
405 */
406 function connectRepeaterModelChanges(repeater) {
407 if (repeater === undefined) {
408 repeater = this;
409 }
410
411 /*
412 Omit model types which are not derived from object (i.e. are [object Number]
413 or [object Array] typed).
414
415 JS 'instanceof' operator does not return true for all types of arrays (i.e
416 for property var array: [....] it returns false). The safest way to detect
417 whether the model is really an object we use the toString() prototype of
418 the generic JS Object.
419
420 Inspired from http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
421 */
422 if (Object.prototype.toString.call(repeater.model) !== "[object Object]") {
423 return;
424 }
425
426 // other models are most likely derived from QAbstractItemModel,
427 // therefore we can safely connect to the signals to get notified about refreshes
428 repeater.model.rowsMoved.connect(updateTimer.restart);
429 repeater.model.columnsMoved.connect(updateTimer.restart);
430 repeater.model.layoutChanged.connect(updateTimer.restart);
431 }
432
433 // clean items removed trough a repeater
434 function removeTabFromModel(index, item) {
435 // cannot use index as that one is relative to the Repeater's model, therefore
436 // we need to look after the Tabs models' role to find out which item to remove
437 for (var i = 0; i < tabsModel.count; i++) {
438 if (tabsModel.get(i).tab === item) {
439 tabsModel.remove(i);
440 break;
441 }
442 }
443 tabsModel.reindex();
444 }
445 }
446
447 Binding {
448 target: internal.header
449 property: "tabsModel"
450 value: tabsModel
451 when: internal.header && tabs.active
452 }
453 }