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