Debian Codegraph @

src/imports/Components/1.2/Tab.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
19 /*!
20 \qmltype Tab
21 \inqmlmodule Lomiri.Components
22 \ingroup lomiri
23 \brief Component to represent a single tab in a \l Tabs environment.
24
25 Examples: See \l Tabs.
26 */
27 PageTreeNode {
28 id: tab
29
30 anchors.fill: parent ? parent : undefined
31
32 /*!
33 The title that is shown on the tab button used to select this tab.
34 */
35 property string title
36
37 /*!
38 \deprecated
39 The location of the icon that is displayed inside the button used to select this tab (optional).
40 Either \l title or iconSource, or both must be defined.
41 Deprecated because our new tab buttons in the header do not display an icon.
42 */
43 property url iconSource
44
45 /*!
46 The contents of the Tab. Use a \l Page or a Loader that instantiates a Component or
47 loads an external \l Page.
48 When using a Loader, do not set the anchors or dimensions of the Loader so that the
49 \l Page can control the height and prevent overlapping the header.
50 Example:
51 \qml
52 import QtQuick 2.4
53 import Lomiri.Components 1.2
54 MainView {
55 width: units.gu(40)
56 height: units.gu(50)
57
58 Component {
59 id: pageComponent
60 Page {
61 Label {
62 anchors.centerIn: parent
63 text: "Loaded when tab is selected."
64 }
65 }
66 }
67 Tabs {
68 id: tabs
69 Tab {
70 title: i18n.tr("Simple page")
71 page: Page {
72 Label {
73 anchors.centerIn: parent
74 text: i18n.tr("Always loaded")
75 }
76 }
77 }
78 Tab {
79 id: loaderTab
80 title: i18n.tr("Page loader")
81 page: Loader {
82 // no anchors
83 id: loader
84 sourceComponent: tabs.selectedTab == loaderTab ? pageComponent : null
85 onStatusChanged: if (loader.status == Loader.Ready) console.log('Loaded')
86 }
87 }
88 }
89 }
90 \endqml
91 */
92 property Item page: null
93
94 // In case the page is a Loader with a Page inside, the Loader needs to be
95 // anchored to the bottom of the Tab. Width and height are set by the Page.
96 Binding {
97 target: page
98 property: "anchors.bottom"
99 value: tab.bottom
100 when: page
101 }
102
103 /*!
104 \qmlproperty int index
105 \readonly
106 The property holds the index of the tab within the Tabs.
107 */
108 readonly property alias index: internal.index
109
110 /*!
111 When page is updated, set its parent to be tab.
112 */
113 onPageChanged: if (page) page.parent = tab
114
115 /*!
116 The tab is active when it is the selected tab of its parent Tabs item.
117 Setting tab to active will automatically make child nodes active.
118 */
119 active: parentNode && parentNode.active &&
120 parentNode.hasOwnProperty("selectedTab") && parentNode.selectedTab === tab
121
122 visible: active
123
124 /*!
125 \internal
126 */
127 onTitleChanged: {
128 if (active) {
129 // ensure the parent node is an instance of Tabs
130 if (parentNode.hasOwnProperty("selectedTab")) {
131 parentNode.modelChanged();
132 }
133 }
134 }
135
136 /*!
137 \internal
138 */
139 property alias __protected: internal
140 QtObject {
141 id: internal
142 /*
143 Specifies the index of the Tab in Tabs.
144 */
145 property int index: -1
146
147 /*
148 Specifies whether the Tab has already been inserted in Tabs model or not.
149 Pre-declared tabs are added one by one automatically before Tabs component
150 completion, therefore we need this flag to exclude adding those Tab elements
151 again which were already added.
152 */
153 property bool inserted: false
154
155 /*
156 Specifies whether the Tab was created dynamically or not. A dynamically created
157 Tab is destroyed upon removal.
158 */
159 property bool dynamic: false
160
161 /*
162 This flag is used by the Tabs to determine whether the pre-declared Tab was removed
163 from the Tabs model or not. The flag guards adding back pre-declared tabs upon Tabs
164 component stack (children) change.
165 */
166 property bool removedFromTabs: false
167 }
168 }