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