Debian Codegraph @

src/imports/Components/1.2/PageStack.qml

raw
1 /*
2 * Copyright 2012-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 "stack.js" as Stack
19
20 /*!
21 \qmltype PageStack
22 \inqmlmodule Lomiri.Components
23 \ingroup lomiri
24 \brief A stack of \l Page items that is used for inter-Page navigation.
25 Pages on the stack can be popped, and new Pages can be pushed.
26 The page on top of the stack is the visible one.
27
28 PageStack should be used inside a \l MainView in order to automatically add
29 a header and toolbar to control the stack. The PageStack will automatically
30 set the header title to the title of the \l Page that is currently on top
31 of the stack, and the tools of the toolbar to the tools of the \l Page on top
32 of the stack. When more than one Pages are on the stack, the toolbar will
33 automatically feature a back-button that pop the stack when triggered.
34
35 The anchors of the PageStack are set to fill its parent by default. To use
36 left/right/top/bottom anchors, explicitly set anchors.fill of the PageStack to
37 undefined:
38 \qml
39 import QtQuick 2.4
40 import Lomiri.Components 1.2
41
42 MainView {
43 width: units.gu(40)
44 height: units.gu(71)
45
46 PageStack {
47 id: mainStack
48 anchors {
49 fill: undefined // unset the default to make the other anchors work
50 left: parent.left
51 right: parent.right
52 top: parent.top
53 bottom: rect.top
54 }
55 }
56
57 Rectangle {
58 id: rect
59 color: LomiriColors.red
60 anchors {
61 left: parent.left
62 right: parent.right
63 bottom: parent.bottom
64 }
65 height: units.gu(10)
66 }
67
68 Component.onCompleted: mainStack.push(Qt.resolvedUrl("MyPage.qml"))
69 }
70 \endqml
71
72 Pages that are defined inside the PageStack must initially set their visibility
73 to false to avoid the pages occluding the PageStack before they are pushed.
74 When pushing a \l Page, its visibility is automatically updated.
75
76 Example:
77 \qml
78 import QtQuick 2.4
79 import Lomiri.Components 1.2
80 import Lomiri.Components.ListItems 1.0 as ListItem
81
82 MainView {
83 width: units.gu(48)
84 height: units.gu(60)
85
86 PageStack {
87 id: pageStack
88 Component.onCompleted: push(page0)
89
90 Page {
91 id: page0
92 title: i18n.tr("Root page")
93 visible: false
94
95 Column {
96 anchors.fill: parent
97 ListItem.Standard {
98 text: i18n.tr("Page one")
99 onClicked: pageStack.push(page1, {color: LomiriColors.orange})
100 progression: true
101 }
102 ListItem.Standard {
103 text: i18n.tr("External page")
104 onClicked: pageStack.push(Qt.resolvedUrl("MyCustomPage.qml"))
105 progression: true
106 }
107 }
108 }
109
110 Page {
111 title: "Rectangle"
112 id: page1
113 visible: false
114 property alias color: rectangle.color
115 Rectangle {
116 id: rectangle
117 anchors {
118 fill: parent
119 margins: units.gu(5)
120 }
121 }
122 }
123 }
124 }
125 \endqml
126 As shown in the example above, the push() function can take an Item, Component or URL as input.
127 */
128
129 PageTreeNode {
130 id: pageStack
131 anchors.fill: parent
132
133 /*!
134 \internal
135 Please do not use this property any more. \l MainView now has a header
136 property that controls when the header is shown/hidden.
137 */
138 property bool __showHeader: true
139 QtObject {
140 property alias showHeader: pageStack.__showHeader
141 onShowHeaderChanged: print("__showHeader is deprecated. Do not use it.")
142 }
143
144 /*!
145 The current size of the stack
146 */
147 //FIXME: would prefer this be readonly, but readonly properties are only bound at
148 //initialisation. Trying to update it in push or pop fails. Not sure how to fix.
149 property int depth: 0
150
151 /*!
152 The currently active page
153 */
154 property Item currentPage: null
155
156 /*!
157 Push a page to the stack, and apply the given (optional) properties to the page.
158 The pushed page may be an Item, Component or URL.
159 The function returns the Item that was pushed, or the Item that was created from
160 the Component or URL. Depending on the animation of the header, the returned
161 Page may or may not be active and on top of the PageStack yet.
162 */
163 function push(page, properties) {
164 internal.finishPreviousAction();
165 internal.pageWrapper = internal.createWrapper(page, properties);
166 var pageObject = internal.pageWrapper.object;
167
168 if (internal.animateHeader && internal.stack.size() > 0) {
169 internal.headStyle.animateOutFinished.connect(internal.pushWrapperObject);
170 internal.headStyle.animateOut();
171 } else {
172 internal.pushWrapperObject();
173 }
174 return pageObject;
175 }
176
177 /*!
178 Pop the top item from the stack if the stack size is at least 1.
179 Do not do anything if 0 or 1 items are on the stack.
180 */
181 function pop() {
182 internal.finishPreviousAction();
183 if (internal.stack.size() < 1) {
184 print("WARNING: Trying to pop an empty PageStack. Ignoring.");
185 return;
186 }
187 // do not animate if there is no page to animate back in after popping
188 if (internal.animateHeader && internal.stack.size() > 1) {
189 internal.headStyle.animateOutFinished.connect(internal.popAndDestroy);
190 internal.headStyle.animateOut();
191 } else {
192 internal.popAndDestroy();
193 }
194 }
195
196 /*!
197 Deactivate the active page and clear the stack.
198 */
199 function clear() {
200 internal.finishPreviousAction();
201 while (internal.stack.size() > 0) {
202 internal.stack.top().active = false;
203 if (internal.stack.top().canDestroy) internal.stack.top().destroyObject();
204 internal.stack.pop();
205 }
206 internal.stackUpdated();
207 }
208
209 QtObject {
210 id: internal
211 property Item headStyle: (pageStack.__propagated
212 && pageStack.__propagated.header
213 && pageStack.__propagated.header.__styleInstance)
214 ? pageStack.__propagated.header.__styleInstance
215 : null
216
217 property bool animateHeader: pageStack.__propagated.hasOwnProperty("animateHeader") &&
218 pageStack.__propagated.animateHeader
219
220 // Call this function before pushing or popping to ensure correct order
221 // of pushes/pops on the stack. This terminates any currently running
222 // header transition.
223 function finishPreviousAction() {
224 // no action required when animating IN because the PageStack was
225 // already updated before that transition started.
226 if (internal.animateHeader && internal.headStyle.state == "OUT") {
227 // force instant update of the PageStack without waiting for
228 // the OUT animation to finish:
229 internal.headStyle.animateOutFinished();
230 }
231 }
232
233 // The PageWrapper to be pushed on the stack by pushWrapperObject().
234 property var pageWrapper: null
235
236 // Called when the header animate OUT transition finishes for push() or instantly
237 // when header animations are disabled.
238 function pushWrapperObject() {
239 if (internal.animateHeader) {
240 headStyle.animateOutFinished.disconnect(internal.pushWrapperObject);
241 }
242 if (internal.stack.size() > 0) internal.stack.top().active = false;
243 internal.stack.push(internal.pageWrapper);
244 internal.pageWrapper = null;
245 internal.stackUpdated();
246 }
247
248 // Called when header animate OUT transition finishes for pop() or instantly
249 // when header animations are disabled.
250 function popAndDestroy() {
251 if (internal.animateHeader) {
252 headStyle.animateOutFinished.disconnect(internal.popAndDestroy);
253 }
254 internal.stack.top().active = false;
255 if (internal.stack.top().canDestroy) internal.stack.top().destroyObject();
256 internal.stack.pop();
257 internal.stackUpdated();
258 }
259
260 /*!
261 The instance of the stack from javascript
262 */
263 property var stack: new Stack.Stack()
264
265 function createWrapper(page, properties) {
266 var wrapperComponent = Qt.createComponent("PageWrapper.qml");
267 var wrapperObject = wrapperComponent.createObject(pageStack);
268 wrapperObject.pageStack = pageStack;
269 wrapperObject.properties = properties;
270 // set reference last because it will trigger creation of the object
271 // with specified properties.
272 wrapperObject.reference = page;
273 return wrapperObject;
274 }
275
276 // Update depth and makes the Item on top of the stack active, and
277 // then animates IN the new header contents if header animations are enabled.
278 function stackUpdated() {
279 pageStack.depth = stack.size();
280 if (pageStack.depth > 0) {
281 internal.stack.top().active = true;
282 currentPage = stack.top().object;
283
284 if (internal.animateHeader) {
285 headStyle.animateIn();
286 }
287 } else {
288 currentPage = null;
289 }
290 }
291 }
292
293 /*!
294 \qmlproperty list<Object> data
295 Children of PageStack are placed in a separate item such that they are
296 not active by default until they are pushed on the PageStack.
297 */
298 default property alias data: inactiveNode.data
299 PageTreeNode {
300 anchors.fill: parent
301 id: inactiveNode
302 active: false
303 }
304 }