| 1 |
/* |
| 2 |
* Copyright 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 |
| 18 |
import QtQuick.Layouts |
| 19 |
import Lomiri.Components |
| 20 |
import Lomiri.Components.Private |
| 21 |
|
| 22 |
/*! |
| 23 |
\qmltype AdaptivePageLayout |
| 24 |
\inqmlmodule Lomiri.Components |
| 25 |
\since Lomiri.Components |
| 26 |
\ingroup lomiri |
| 27 |
\brief View with multiple columns of Pages. |
| 28 |
|
| 29 |
The component provides a flexible way of viewing a stack of pages in one or |
| 30 |
more columns. Unlike in PageStack, there can be more than one Page active at |
| 31 |
a time, depending on the number of the columns in the view. |
| 32 |
|
| 33 |
AdaptivePageLayout stores pages added in a tree. Pages are added relative to a |
| 34 |
given page, either as sibling (\l addPageToCurrentColumn) or as child |
| 35 |
(\l addPageToNextColumn). This means that removing a non-leaf page from the Page |
| 36 |
tree will remove all its children from the page tree. |
| 37 |
|
| 38 |
The columns are populated from left to right. The column a page is added to is |
| 39 |
detected based on the source page that is given to the functions adding the page. |
| 40 |
The pages can be added either to the same column the source page resides or to |
| 41 |
the column next to the source page. Giving a null value to the source page will |
| 42 |
add the page to the leftmost column of the view. |
| 43 |
|
| 44 |
The primary page, the very first page must be specified either through the |
| 45 |
\l primaryPage or \l primaryPageSource properties. \l primaryPage can only |
| 46 |
hold a Page instance, \l primaryPageSource can either be a Component or a |
| 47 |
url to a document defining a Page. \l primaryPageSource has precedence over |
| 48 |
\l primaryPage, and when set it will report the loaded Page through \l primaryPage |
| 49 |
property, and will replace any value set into that property. |
| 50 |
|
| 51 |
\qml |
| 52 |
import QtQuick 2.4 |
| 53 |
import Lomiri.Components |
| 54 |
|
| 55 |
MainView { |
| 56 |
width: units.gu(100) |
| 57 |
height: units.gu(60) |
| 58 |
|
| 59 |
AdaptivePageLayout { |
| 60 |
anchors.fill: parent |
| 61 |
primaryPage: page1 |
| 62 |
Page { |
| 63 |
id: page1 |
| 64 |
header: PageHeader { |
| 65 |
id: header |
| 66 |
title: "Main Page" |
| 67 |
} |
| 68 |
Column { |
| 69 |
anchors.top: header.bottom |
| 70 |
Button { |
| 71 |
text: "Add Page2 above " + page1.title |
| 72 |
onClicked: page1.pageStack.addPageToCurrentColumn(page1, page2) |
| 73 |
} |
| 74 |
Button { |
| 75 |
text: "Add Page3 next to " + page1.title |
| 76 |
onClicked: page1.pageStack.addPageToNextColumn(page1, page3) |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
Page { |
| 81 |
id: page2 |
| 82 |
header: PageHeader { |
| 83 |
title: "Page #2" |
| 84 |
} |
| 85 |
} |
| 86 |
Page { |
| 87 |
id: page3 |
| 88 |
header: PageHeader { |
| 89 |
title: "Page #3" |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
\endqml |
| 95 |
|
| 96 |
\note Observe the use of the \c Page::pageStack property in the example above. |
| 97 |
The same property is used to share the AdaptivePageLayout instance the Page is |
| 98 |
used in, therefore the same page can be used in a PageStack or in an AdaptivePageLayout. |
| 99 |
However implementations must make sure the desired PageStack or AdaptivePageLayout |
| 100 |
function exists in the instance before using it. |
| 101 |
|
| 102 |
AdaptivePageLayout supports adaptive column handling. When the number of columns changes at |
| 103 |
runtime the pages are automatically rearranged. |
| 104 |
|
| 105 |
By default the component splits the layout in two columns when the width of the |
| 106 |
layout exceeds 80 grid units. The first column is sized to 40 grid unit width and |
| 107 |
the second one to fill the rest of the remaining space. When the 80 grid unit breakpoint |
| 108 |
is reached, the component will switch from one column to two, and vice versa. |
| 109 |
These defaults can be overridden through the \l layouts property by defining the |
| 110 |
possible layouts, their column sizing and the breakpoints when the layouts should |
| 111 |
be activated. PageColumn configurations must appear in the same order (from left |
| 112 |
to right) as the columns appear in the layout. If none of the layouts condition |
| 113 |
is met, a one column layout will be used. |
| 114 |
|
| 115 |
\qml |
| 116 |
import QtQuick 2.4 |
| 117 |
import Lomiri.Components |
| 118 |
|
| 119 |
MainView { |
| 120 |
width: units.gu(100) |
| 121 |
height: units.gu(60) |
| 122 |
|
| 123 |
AdaptivePageLayout { |
| 124 |
anchors.fill: parent |
| 125 |
primaryPageSource: page1Component |
| 126 |
layouts: PageColumnsLayout { |
| 127 |
when: width > units.gu(80) |
| 128 |
// column #0 |
| 129 |
PageColumn { |
| 130 |
minimumWidth: units.gu(30) |
| 131 |
maximumWidth: units.gu(60) |
| 132 |
preferredWidth: units.gu(40) |
| 133 |
} |
| 134 |
// column #1 |
| 135 |
PageColumn { |
| 136 |
fillWidth: true |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
Component { |
| 141 |
id: page1Component |
| 142 |
Page { |
| 143 |
id: page1 |
| 144 |
header: PageHeader { |
| 145 |
id: header |
| 146 |
title: "Main page" |
| 147 |
} |
| 148 |
Column { |
| 149 |
anchors.top: header.bottom |
| 150 |
Button { |
| 151 |
text: "Add Page2 above " + page1.title |
| 152 |
onClicked: page1.pageStack.addPageToCurrentColumn(page1, page2) |
| 153 |
} |
| 154 |
Button { |
| 155 |
text: "Add Page3 next to " + page1.title |
| 156 |
onClicked: page1.pageStack.addPageToNextColumn(page1, page3) |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
Page { |
| 162 |
id: page2 |
| 163 |
header: PageHeader { |
| 164 |
title: "Page #2" |
| 165 |
} |
| 166 |
} |
| 167 |
Page { |
| 168 |
id: page3 |
| 169 |
header: PageHeader { |
| 170 |
title: "Page #3" |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
\endqml |
| 176 |
|
| 177 |
A column is considered to be resizable if the \l PageColumn::minimumWidth and |
| 178 |
\l PageColumn::maximumWidth configuration differs. This implies that if a column |
| 179 |
is not meant to be resized, it should have \l PageColumn::minimumWidth and |
| 180 |
\l PageColumn::maximumWidth set to the same value. In the example above, the |
| 181 |
first column can be resized to a minimum of 30, and a maximum of 60 grid units, |
| 182 |
and the preferred width is set to 40 grid units. This width is set every time |
| 183 |
the layout is activated. |
| 184 |
|
| 185 |
When a \l Page with the \l Page::header property set is added to an |
| 186 |
AdaptivePageLayout, the AdaptivePageLayout will synchronize the height of that |
| 187 |
header with the height of the headers in other columns, i.e., the height of |
| 188 |
each of the headers will be set to the maximum implicitHeight of all the headers. |
| 189 |
|
| 190 |
\sa PageStack, PageColumnsLayout, PageColumn |
| 191 |
*/ |
| 192 |
|
| 193 |
PageTreeNode { |
| 194 |
id: layout |
| 195 |
|
| 196 |
LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft |
| 197 |
LayoutMirroring.childrenInherit: true |
| 198 |
|
| 199 |
Page { |
| 200 |
// AdaptivePageLayout has its own split headers, so |
| 201 |
// disable the application header. |
| 202 |
id: appHeaderControlPage |
| 203 |
head { |
| 204 |
locked: true |
| 205 |
visible: false |
| 206 |
} |
| 207 |
// title is set in attachPage() when the attached Page.column === 0 |
| 208 |
} |
| 209 |
|
| 210 |
/*! |
| 211 |
The property holds the first Page which will be added to the view. If the |
| 212 |
view has more than one column, the page will be added to the leftmost column. |
| 213 |
The property can only hold a Page instance. When changed runtime (not by the |
| 214 |
AdaptivePageLayout component itself), the \l primaryPageSource property |
| 215 |
will be reset. |
| 216 |
*/ |
| 217 |
property Page primaryPage |
| 218 |
|
| 219 |
/*! |
| 220 |
The property specifies the source of the primaryPage in case the primary |
| 221 |
page is created from a Component or loaded from an external document. It |
| 222 |
has precedence over \l primaryPage. |
| 223 |
*/ |
| 224 |
property var primaryPageSource |
| 225 |
|
| 226 |
/*! |
| 227 |
The property drives the way the pages should be loaded, synchronously or |
| 228 |
asynchronously. Defaults to true. |
| 229 |
*/ |
| 230 |
property bool asynchronous: true |
| 231 |
|
| 232 |
/*! |
| 233 |
\qmlproperty int columns |
| 234 |
\readonly |
| 235 |
The property holds the number of columns shown in the layout. |
| 236 |
*/ |
| 237 |
readonly property alias columns: d.columns |
| 238 |
|
| 239 |
/*! |
| 240 |
The property holds the different layout configurations overriding the default |
| 241 |
configurations. Defaults to an empty list. |
| 242 |
\sa PageColumnsLayout |
| 243 |
*/ |
| 244 |
property list<PageColumnsLayout> layouts |
| 245 |
|
| 246 |
/*! |
| 247 |
\qmlmethod object addPageToCurrentColumn(Item sourcePage, var page[, var properties]) |
| 248 |
Adds a \c page to the column the \c sourcePage resides in and removes all pages |
| 249 |
from the higher columns. \c page can be a Component or a file. |
| 250 |
\c properties is a JSON object containing properties to be set when page |
| 251 |
is created. \c sourcePage must be active. |
| 252 |
|
| 253 |
The function creates the new page asynchronously if the new \c page to be |
| 254 |
added is a Component or a QML document and the \l asynchronous property is |
| 255 |
set to true. In this case the function returns an incubator which can be |
| 256 |
used to track the page creation. For more about incubation in QML and creating |
| 257 |
components asynchronously, see |
| 258 |
\l {http://doc.qt.io/qt-5/qml-qtqml-component.html#incubateObject-method} |
| 259 |
{Component.incubateObject()}. |
| 260 |
The following example removes an element from the list model whenever the |
| 261 |
page opened in the second column is closed. Note, the example must be run |
| 262 |
on desktop or on a device with at least 90 grid units screen width. |
| 263 |
\qml |
| 264 |
import QtQuick 2.4 |
| 265 |
import Lomiri.Components |
| 266 |
|
| 267 |
MainView { |
| 268 |
width: units.gu(90) |
| 269 |
height: units.gu(70) |
| 270 |
|
| 271 |
Component { |
| 272 |
id: page2Component |
| 273 |
Page { |
| 274 |
header: PageHeader { |
| 275 |
id: header |
| 276 |
title: "Second Page" |
| 277 |
} |
| 278 |
Button { |
| 279 |
anchors.top: header.bottom |
| 280 |
text: "Close me" |
| 281 |
onClicked: pageStack.removePages(pageStack.primaryPage); |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
AdaptivePageLayout { |
| 287 |
id: pageLayout |
| 288 |
anchors.fill: parent |
| 289 |
primaryPage: Page { |
| 290 |
header: PageHeader { |
| 291 |
title: "Primary Page" |
| 292 |
flickable: listView |
| 293 |
} |
| 294 |
ListView { |
| 295 |
id: listView |
| 296 |
anchors.fill: parent |
| 297 |
model: 10 |
| 298 |
delegate: ListItem { |
| 299 |
Label { text: modelData } |
| 300 |
onClicked: { |
| 301 |
var incubator = pageLayout.addPageToNextColumn(pageLayout.primaryPage, page2Component); |
| 302 |
if (incubator && incubator.status == Component.Loading) { |
| 303 |
incubator.onStatusChanged = function(status) { |
| 304 |
if (status == Component.Ready) { |
| 305 |
// connect page's destruction to decrement model |
| 306 |
incubator.object.Component.destruction.connect(function() { |
| 307 |
listView.model--; |
| 308 |
}); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
\endqml |
| 319 |
|
| 320 |
\sa {http://doc.qt.io/qt-5/qml-qtqml-component.html#incubateObject-method}{Component.incubateObject} |
| 321 |
*/ |
| 322 |
function addPageToCurrentColumn(sourcePage, page, properties) { |
| 323 |
var nextColumn = d.columnForPage(sourcePage) + 1; |
| 324 |
d.tree.prune(nextColumn); |
| 325 |
return d.addPageToColumn(nextColumn - 1, sourcePage, page, properties, function() { |
| 326 |
// Clear all following columns. If we do not do this, we have no way to |
| 327 |
// determine which page needs to be on top when the view is resized |
| 328 |
// to have a single column. |
| 329 |
for (var i = nextColumn; i < d.columns; i++) { |
| 330 |
d.updatePageForColumn(i); |
| 331 |
} |
| 332 |
}); |
| 333 |
} |
| 334 |
|
| 335 |
/*! |
| 336 |
\qmlmethod Item addPageToNextColumn(Item sourcePage, var page[, var properties]) |
| 337 |
Remove all previous pages from the next column (relative to the column that |
| 338 |
holds \c sourcePage) and all following columns, and then add \c page to the next column. |
| 339 |
If \c sourcePage is located in the |
| 340 |
rightmost column, the new page will be pushed to the same column as \c sourcePage. |
| 341 |
The return value is the same as in \l addPageToCurrentColumn case. |
| 342 |
*/ |
| 343 |
function addPageToNextColumn(sourcePage, page, properties) { |
| 344 |
var nextColumn = d.columnForPage(sourcePage) + 1; |
| 345 |
var wrappers = d.tree.prune(nextColumn); |
| 346 |
return d.addPageToColumn(nextColumn, sourcePage, page, properties, function() { |
| 347 |
for (var i = nextColumn; i < d.columns; i++) { |
| 348 |
d.updatePageForColumn(i); |
| 349 |
} |
| 350 |
for (var i in wrappers) { |
| 351 |
wrappers[i].destroyObject(); |
| 352 |
} |
| 353 |
}); |
| 354 |
} |
| 355 |
|
| 356 |
/*! |
| 357 |
\qmlmethod void removePages(Item page) |
| 358 |
The function removes and deletes all pages up to and including \c page |
| 359 |
is reached. If the \a page is the same as the \l primaryPage, only its child |
| 360 |
pages will be removed. |
| 361 |
*/ |
| 362 |
function removePages(page) { |
| 363 |
d.removeAllPages(page, page !== layout.primaryPage); |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
internals |
| 368 |
*/ |
| 369 |
|
| 370 |
Component.onCompleted: { |
| 371 |
// check layout configuration |
| 372 |
if (layouts.length > 0) { |
| 373 |
d.prepareLayouts(); |
| 374 |
} else { |
| 375 |
d.relayout(); |
| 376 |
} |
| 377 |
if (primaryPageSource) { |
| 378 |
/* |
| 379 |
From Qt5.6 onwards, the change signal for a var type property (see |
| 380 |
bug https://bugreports.qt.io/browse/QTBUG-42255) comes after the |
| 381 |
component completion, due to elements being processed in parallel |
| 382 |
by the compiler. This causes the page to be created twice for the |
| 383 |
primaryPage, which ends up in failure for the second time that causes |
| 384 |
the primaryPage to be set to null. Thus no other page can be added |
| 385 |
afterwards. |
| 386 |
*/ |
| 387 |
if (d.prevPrimaryPageSource === undefined) { |
| 388 |
d.prevPrimaryPageSource = primaryPageSource; |
| 389 |
} |
| 390 |
d.createPrimaryPage(primaryPageSource); |
| 391 |
} else if (primaryPage) { |
| 392 |
d.createPrimaryPage(primaryPage); |
| 393 |
} |
| 394 |
d.completed = true; |
| 395 |
} |
| 396 |
onPrimaryPageChanged: { |
| 397 |
if (!d.completed || d.internalUpdate) { |
| 398 |
return; |
| 399 |
} |
| 400 |
// reset the primaryPageSource |
| 401 |
d.internalPropertyUpdate("primaryPageSource", undefined); |
| 402 |
// clear the layout |
| 403 |
d.purgeLayout(); |
| 404 |
// add the new page if valid |
| 405 |
if (primaryPage !== null) { |
| 406 |
d.createPrimaryPage(primaryPage); |
| 407 |
} |
| 408 |
} |
| 409 |
onPrimaryPageSourceChanged: { |
| 410 |
if (!d.completed) { |
| 411 |
return; |
| 412 |
} |
| 413 |
if (d.internalUpdate) { |
| 414 |
d.prevPrimaryPageSource = primaryPageSource; |
| 415 |
return; |
| 416 |
} |
| 417 |
if (d.prevPrimaryPageSource === primaryPageSource) { |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
// remove all pages first |
| 422 |
d.purgeLayout(); |
| 423 |
d.prevPrimaryPageSource = primaryPageSource; |
| 424 |
// create the new primary page if a valid component is specified |
| 425 |
if (primaryPageSource) { |
| 426 |
d.createPrimaryPage(primaryPageSource); |
| 427 |
} else { |
| 428 |
d.internalPropertyUpdate("primaryPage", null); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
onLayoutsChanged: { |
| 433 |
if (d.completed) { |
| 434 |
// only deal with this if the layouts array changes after completion |
| 435 |
// to avoid unnecessary rendering |
| 436 |
d.prepareLayouts(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
/*! \internal - for testing purposes */ |
| 441 |
readonly property alias __d:d |
| 442 |
QtObject { |
| 443 |
id: d |
| 444 |
|
| 445 |
property bool internalUpdate: false |
| 446 |
property bool completed: false |
| 447 |
property var tree: Tree{} |
| 448 |
|
| 449 |
property int columns: !layout.layouts.length ? |
| 450 |
(layout.width >= units.gu(80) ? 2 : 1) : |
| 451 |
(activeLayout ? activeLayout.data.length : 1) |
| 452 |
property PageColumnsLayout activeLayout: null |
| 453 |
property list<PageColumnsLayout> prevLayouts |
| 454 |
property Page prevPrimaryPage |
| 455 |
property var prevPrimaryPageSource |
| 456 |
|
| 457 |
/*! internal */ |
| 458 |
onColumnsChanged: { |
| 459 |
if (columns <= 0) { |
| 460 |
console.warn("There must be a minimum of one column set."); |
| 461 |
columns = 1; |
| 462 |
} |
| 463 |
d.relayout(); |
| 464 |
} |
| 465 |
property real defaultColumnWidth: units.gu(40) |
| 466 |
onDefaultColumnWidthChanged: body.updateHeaderHeight(0) |
| 467 |
|
| 468 |
function internalPropertyUpdate(propertyName, value) { |
| 469 |
internalUpdate = true; |
| 470 |
layout[propertyName] = value; |
| 471 |
internalUpdate = false; |
| 472 |
} |
| 473 |
|
| 474 |
function createPrimaryPage(source) { |
| 475 |
var wrapper = d.createWrapper(source); |
| 476 |
if (wrapper.incubator) { |
| 477 |
wrapper.pageLoaded.connect(finalizeAddingPage.bind(wrapper)); |
| 478 |
wrapper.incubator.onStatusChanged = function (status) { |
| 479 |
if (status === Component.Ready) { |
| 480 |
internalPropertyUpdate("primaryPage", wrapper.incubator.object); |
| 481 |
prevPrimaryPage = wrapper.incubator.object; |
| 482 |
} |
| 483 |
} |
| 484 |
} else { |
| 485 |
finalizeAddingPage(wrapper); |
| 486 |
prevPrimaryPage = wrapper.object; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
// remove all pages, including primaryPage |
| 491 |
function purgeLayout() { |
| 492 |
if (prevPrimaryPage) { |
| 493 |
removeAllPages(prevPrimaryPage, true); |
| 494 |
prevPrimaryPage = null; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
function createWrapper(page, properties) { |
| 499 |
var wrapperObject = pageWrapperComponent.createObject(hiddenPages, {synchronous: !layout.asynchronous}); |
| 500 |
wrapperObject.pageStack = layout; |
| 501 |
wrapperObject.properties = properties; |
| 502 |
// set reference last because it will trigger creation of the object |
| 503 |
// with specified properties. |
| 504 |
wrapperObject.reference = page; |
| 505 |
return wrapperObject; |
| 506 |
} |
| 507 |
|
| 508 |
function addWrappedPage(pageWrapper) { |
| 509 |
pageWrapper.parentWrapper = d.getWrapper(pageWrapper.parentPage); |
| 510 |
tree.add(pageWrapper.column, pageWrapper.parentWrapper, pageWrapper); |
| 511 |
var targetColumn = MathUtils.clamp(pageWrapper.column, 0, d.columns - 1); |
| 512 |
// replace page holder's child |
| 513 |
var holder = body.children[targetColumn]; |
| 514 |
holder.detachCurrentPage(); |
| 515 |
holder.attachPage(pageWrapper); // sets pageWrapper.pageHolder |
| 516 |
|
| 517 |
// set the back action for Page.header: |
| 518 |
var page = pageWrapper.object; |
| 519 |
if (page && page.hasOwnProperty("header") && page.header && |
| 520 |
page.header.hasOwnProperty("navigationActions")) { |
| 521 |
// Page.header is an instance of PageHeader. |
| 522 |
var backAction = backActionComponent.createObject( |
| 523 |
pageWrapper, { 'wrapper': pageWrapper } ); |
| 524 |
page.header.navigationActions = [ backAction ] ; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
function getWrapper(page) { |
| 529 |
if (page && page.hasOwnProperty("parentNode")) { |
| 530 |
var w = page.parentNode; |
| 531 |
if (w && w.hasOwnProperty("object") && w.hasOwnProperty("reference")) { |
| 532 |
if (w.object === page) { |
| 533 |
return w; |
| 534 |
} else { |
| 535 |
console.error("Page is not wrapped by its parentNode. This should not happen!"); |
| 536 |
return null; |
| 537 |
} |
| 538 |
} else { |
| 539 |
// invalid wrapper |
| 540 |
return null; |
| 541 |
} |
| 542 |
} else { |
| 543 |
// invalid page |
| 544 |
return null; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
function columnForPage(page) { |
| 549 |
var wrapper = d.getWrapper(page); |
| 550 |
return wrapper ? wrapper.column : 0; |
| 551 |
} |
| 552 |
|
| 553 |
function finalizeAddingPage(newWrapper) { |
| 554 |
if (newWrapper === undefined) newWrapper = this; |
| 555 |
d.addWrappedPage(newWrapper); |
| 556 |
} |
| 557 |
|
| 558 |
function addPageToColumn(column, sourcePage, page, properties, cleanupFunc) { |
| 559 |
if (column < 0) { |
| 560 |
console.warn("Column must be >= 0."); |
| 561 |
return; |
| 562 |
} |
| 563 |
if (!sourcePage) { |
| 564 |
console.warn("No sourcePage specified. Page will not be added."); |
| 565 |
return; |
| 566 |
} |
| 567 |
var sourceWrapper = d.getWrapper(sourcePage); |
| 568 |
if (d.tree.index(sourceWrapper) === -1) { |
| 569 |
console.warn("sourcePage must be added to the view to add new page."); |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
// Check that the Page was not already added. |
| 574 |
if (typeof page !== "string" && !page.createObject) { |
| 575 |
// page is neither a url or a Component so it must be a Page object. |
| 576 |
|
| 577 |
var oldWrapper = getWrapper(page); |
| 578 |
if (oldWrapper && d.tree.index(oldWrapper) !== -1) { |
| 579 |
console.warn("Cannot add a Page that was already added."); |
| 580 |
return null; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
var newWrapper = d.createWrapper(page, properties); |
| 585 |
newWrapper.parentPage = sourcePage; |
| 586 |
newWrapper.column = column; |
| 587 |
if (newWrapper.incubator) { |
| 588 |
newWrapper.pageLoaded.connect(cleanupFunc); |
| 589 |
newWrapper.pageLoaded.connect(finalizeAddingPage.bind(newWrapper)); |
| 590 |
} else { |
| 591 |
cleanupFunc(); |
| 592 |
finalizeAddingPage(newWrapper); |
| 593 |
} |
| 594 |
|
| 595 |
return newWrapper.incubator; |
| 596 |
} |
| 597 |
|
| 598 |
// removes all pages from the layout, and may include the page itself |
| 599 |
function removeAllPages(page, inclusive) { |
| 600 |
inclusive = typeof inclusive !== 'undefined' ? inclusive : true; |
| 601 |
var nodeToRemove = d.getWrapper(page); |
| 602 |
var removedNodes = d.tree.chop(nodeToRemove, inclusive); |
| 603 |
for (var i = removedNodes.length-1; i >= 0; i--) { |
| 604 |
var node = removedNodes[i]; |
| 605 |
updatePageForColumn(node.column); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
// update the page for the specified column |
| 610 |
function updatePageForColumn(column) { |
| 611 |
var effectiveColumn = MathUtils.clamp(column, 0, d.columns - 1); |
| 612 |
var columnHolder = body.children[effectiveColumn]; |
| 613 |
var newWrapper = tree.top(effectiveColumn, effectiveColumn < d.columns - 1); |
| 614 |
var oldWrapper = columnHolder.pageWrapper; |
| 615 |
|
| 616 |
if (newWrapper !== oldWrapper) { |
| 617 |
columnHolder.detachCurrentPage(); |
| 618 |
oldWrapper.parent = null; |
| 619 |
if (newWrapper) { |
| 620 |
columnHolder.attachPage(newWrapper); |
| 621 |
} |
| 622 |
oldWrapper.destroyObject(); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
// prepares layout management, listens on layout condition changes and performs re-layouting |
| 627 |
function prepareLayouts() { |
| 628 |
// disconnect from the previous layouts |
| 629 |
for (var i = 0; i < prevLayouts.length; i++) { |
| 630 |
prevLayouts[i].whenChanged.disconnect(updateLayout); |
| 631 |
} |
| 632 |
prevLayouts = layouts; |
| 633 |
for (var j = 0; j < layouts.length; j++) { |
| 634 |
layouts[j].whenChanged.connect(updateLayout); |
| 635 |
} |
| 636 |
// first time evaluation |
| 637 |
updateLayout(); |
| 638 |
} |
| 639 |
|
| 640 |
// function called when one of the layout condition is satisfied |
| 641 |
function updateLayout() { |
| 642 |
var newLayout = null; |
| 643 |
for (var i = 0; i < layouts.length; i++) { |
| 644 |
// get the first affirmative condition |
| 645 |
if (layouts[i].when) { |
| 646 |
newLayout = layouts[i]; |
| 647 |
break; |
| 648 |
} |
| 649 |
} |
| 650 |
d.activeLayout = newLayout; |
| 651 |
} |
| 652 |
|
| 653 |
// relayouts when column count changes |
| 654 |
function relayout() { |
| 655 |
if (body.children.length === d.columns) return; |
| 656 |
if (body.children.length > d.columns) { |
| 657 |
// need to remove few columns, the last ones |
| 658 |
while (body.children.length > d.columns) { |
| 659 |
var holder = body.children[body.children.length - 1]; |
| 660 |
holder.detachCurrentPage(); |
| 661 |
holder.parent = null; |
| 662 |
holder.destroy(); |
| 663 |
} |
| 664 |
} else { |
| 665 |
var prevColumns = body.children.length; |
| 666 |
|
| 667 |
// add columns |
| 668 |
for (var i = 0; i < d.columns - prevColumns; i++) { |
| 669 |
pageHolderComponent.createObject(body); |
| 670 |
} |
| 671 |
} |
| 672 |
rearrangePages(); |
| 673 |
} |
| 674 |
|
| 675 |
function rearrangePages() { |
| 676 |
for (var column = d.columns - 1; column >= 0; column--) { |
| 677 |
var holder = body.children[column]; |
| 678 |
var pageWrapper = tree.top(column, column < (d.columns - 1)); |
| 679 |
if (!pageWrapper) { |
| 680 |
continue; |
| 681 |
} |
| 682 |
if (!pageWrapper.parent) { |
| 683 |
// this should never happen, so if it does, we have a bug! |
| 684 |
console.error("Found a page which wasn't parented anywhere!", pageWrapper.object.title); |
| 685 |
continue; |
| 686 |
} |
| 687 |
// detach current page from holder if differs |
| 688 |
if (holder.pageWrapper !== pageWrapper) { |
| 689 |
holder.detachCurrentPage(); |
| 690 |
} |
| 691 |
if (pageWrapper.parent === holder.hiddenPool || pageWrapper.parent === hiddenPages) { |
| 692 |
// add the page to the column |
| 693 |
holder.attachPage(pageWrapper); |
| 694 |
} else if (pageWrapper.pageHolder !== holder) { |
| 695 |
// detach the pageWrapper from its holder |
| 696 |
if (pageWrapper.pageHolder) { |
| 697 |
pageWrapper.pageHolder.detachCurrentPage(); |
| 698 |
} |
| 699 |
// then attach to this holder |
| 700 |
holder.attachPage(pageWrapper); |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
Component{ |
| 707 |
id: pageWrapperComponent |
| 708 |
PageWrapper{ |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
// An instance will be added to each Page with |
| 714 |
Component { |
| 715 |
id: backActionComponent |
| 716 |
|
| 717 |
Action { |
| 718 |
// used when the Page has a Page.header property set. |
| 719 |
id: backAction |
| 720 |
objectName: "apl_back_action" |
| 721 |
iconName: "back" |
| 722 |
text: "Back" |
| 723 |
|
| 724 |
// set when backAction is created. |
| 725 |
property PageWrapper wrapper |
| 726 |
onTriggered: layout.removePages(wrapper.object) |
| 727 |
|
| 728 |
visible: { |
| 729 |
var parentWrapper = d.tree.parent(wrapper); |
| 730 |
|
| 731 |
if (!parentWrapper) |
| 732 |
// Root node has no parent node. |
| 733 |
return false; |
| 734 |
|
| 735 |
if (!wrapper.pageHolder) { |
| 736 |
// columns are being re-arranged. |
| 737 |
return false; |
| 738 |
} |
| 739 |
// wrapper.column is the virtual column, pageHolder.column the actual column. |
| 740 |
var column = wrapper.pageHolder.column; |
| 741 |
var nextInColumn = d.tree.top(column, column < d.columns - 1, 1); |
| 742 |
return parentWrapper === nextInColumn; |
| 743 |
} |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
// Page holder component, can have only one Page as child at a time, all stacked pages |
| 748 |
// will be parented into hiddenPool |
| 749 |
Component { |
| 750 |
id: pageHolderComponent |
| 751 |
// Page uses the height of the parentNode for its height, so make |
| 752 |
// the holder a PageTreeNode that determines the Page height. |
| 753 |
PageTreeNode { |
| 754 |
id: holder |
| 755 |
active: false |
| 756 |
objectName: "ColumnHolder" + column |
| 757 |
property PageWrapper pageWrapper: PageWrapper{} |
| 758 |
|
| 759 |
property int column |
| 760 |
property alias config: subHeader.config |
| 761 |
property PageColumn metrics: (d.activeLayout && d.activeLayout.data[column]) |
| 762 |
? d.activeLayout.data[column] |
| 763 |
: defaultMetrics |
| 764 |
|
| 765 |
PageColumn { |
| 766 |
id: defaultMetrics |
| 767 |
fillWidth: (holder.column + 1) === d.columns |
| 768 |
minimumWidth: d.defaultColumnWidth |
| 769 |
} |
| 770 |
|
| 771 |
readonly property real dividerThickness: units.dp(1) |
| 772 |
readonly property alias hiddenPool: hiddenItem |
| 773 |
|
| 774 |
Layout.fillWidth: metrics.fillWidth |
| 775 |
Layout.fillHeight: true |
| 776 |
Layout.preferredWidth: MathUtils.clamp(metrics.preferredWidth, metrics.minimumWidth, metrics.maximumWidth) |
| 777 |
Layout.minimumWidth: metrics.minimumWidth |
| 778 |
Layout.maximumWidth: metrics.maximumWidth |
| 779 |
|
| 780 |
property var page: pageWrapper ? pageWrapper.object : null |
| 781 |
property bool customHeader: page && page.hasOwnProperty("header") && |
| 782 |
page.header |
| 783 |
onCustomHeaderChanged: { |
| 784 |
// do not change the holderBody anchors until the new page |
| 785 |
// for the column has been set to prevent geometry changes in |
| 786 |
// the current/previous page. |
| 787 |
if (page) { |
| 788 |
if (customHeader) { |
| 789 |
holderBody.anchors.top = holderBody.parent.top; |
| 790 |
} else { |
| 791 |
holderBody.anchors.top = subHeader.bottom; |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
onPageChanged: body.updateHeaderHeight(0) |
| 797 |
Connections { |
| 798 |
target: page |
| 799 |
function onHeaderChanged() { body.updateHeaderHeight(0) } |
| 800 |
} |
| 801 |
Connections { |
| 802 |
ignoreUnknownSignals: true |
| 803 |
target: page ? page.header : null |
| 804 |
function onImplicitHeightChanged() { |
| 805 |
if (page.header.hasOwnProperty("automaticHeight") && |
| 806 |
page.header.automaticHeight) { |
| 807 |
body.updateHeaderHeight(page.header.implicitHeight) |
| 808 |
} |
| 809 |
} |
| 810 |
function onAutomaticHeightChanged() { body.updateHeaderHeight(0) } |
| 811 |
} |
| 812 |
|
| 813 |
// prevent the pages from taking the app header height into account. |
| 814 |
__propagated: null |
| 815 |
Item { |
| 816 |
id: holderBody |
| 817 |
objectName: parent.objectName + "Body" |
| 818 |
anchors { |
| 819 |
top: subHeader.bottom // updated in onCustomHeaderChanged |
| 820 |
bottom: parent.bottom |
| 821 |
left: parent.left |
| 822 |
right: parent.right |
| 823 |
rightMargin: verticalDivider.width > 0 |
| 824 |
? dividerThickness + verticalDivider.width |
| 825 |
: 0 |
| 826 |
} |
| 827 |
Item { |
| 828 |
id: hiddenItem |
| 829 |
anchors.fill: parent |
| 830 |
visible: false |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
// FIXME: subHeader is deprecated and will be replaced by Page.header. See bug #1583587. |
| 835 |
property alias head: subHeader |
| 836 |
StyledItem { |
| 837 |
id: subHeader |
| 838 |
anchors { |
| 839 |
left: parent.left |
| 840 |
top: parent.top |
| 841 |
right: parent.right |
| 842 |
rightMargin: dividerThickness + verticalDivider.width |
| 843 |
} |
| 844 |
height: body.headerHeight |
| 845 |
|
| 846 |
styleName: "PageHeadStyle" |
| 847 |
objectName: "Header" + column |
| 848 |
|
| 849 |
property real preferredHeight: subHeader.__styleInstance ? |
| 850 |
subHeader.__styleInstance.implicitHeight : |
| 851 |
0 |
| 852 |
onPreferredHeightChanged: { |
| 853 |
body.updateHeaderHeight(preferredHeight); |
| 854 |
} |
| 855 |
|
| 856 |
property PageHeadConfiguration config: null |
| 857 |
property Item contents: null |
| 858 |
|
| 859 |
|
| 860 |
property AppHeader appHeader: layout.__propagated && layout.__propagated.header ? |
| 861 |
layout.__propagated.header : null |
| 862 |
property color dividerColor: appHeader ? appHeader.dividerColor : "black" |
| 863 |
property color panelColor: appHeader ? appHeader.panelColor : "grey" |
| 864 |
property color backgroundColor: appHeader ? appHeader.backgroundColor : "white" |
| 865 |
|
| 866 |
// Enable red outline in the PageHeadStyle. See bug #1583636. |
| 867 |
property bool showDeprecatedWarning: true |
| 868 |
|
| 869 |
visible: !customHeader && holder.pageWrapper && holder.pageWrapper.active |
| 870 |
|
| 871 |
// The multiColumn, page and showBackButton properties are used in |
| 872 |
// PageHeadStyle to show/hide the back button. |
| 873 |
property var multiColumn: layout |
| 874 |
property alias page: holder.page // used in PageHeadStyle for the back button. |
| 875 |
property bool showBackButton: { |
| 876 |
if (!page) { |
| 877 |
return false; |
| 878 |
} |
| 879 |
var parentWrapper = d.tree.parent(holder.pageWrapper); |
| 880 |
|
| 881 |
if (!parentWrapper) { |
| 882 |
// Root node has no parent node. |
| 883 |
return false; |
| 884 |
} |
| 885 |
|
| 886 |
var nextInColumn = d.tree.top(holder.column, holder.column < d.columns - 1, 1); |
| 887 |
return parentWrapper === nextInColumn; |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
Rectangle { |
| 892 |
id: verticalDivider |
| 893 |
anchors { |
| 894 |
top: parent.top |
| 895 |
bottom: parent.bottom |
| 896 |
right: parent.right |
| 897 |
rightMargin: dividerThickness |
| 898 |
} |
| 899 |
width: (column === (d.columns - 1)) ? 0 : units.dp(1) |
| 900 |
color: theme.palette.normal.base |
| 901 |
MouseArea { |
| 902 |
id: resizerSensing |
| 903 |
objectName: "Divider" |
| 904 |
enabled: verticalDivider.width > 0 |
| 905 |
anchors { |
| 906 |
fill: parent |
| 907 |
leftMargin: enabled ? -units.gu(1) : 0 |
| 908 |
rightMargin: enabled ? -units.gu(1) : 0 |
| 909 |
} |
| 910 |
cursorShape: Qt.SizeHorCursor |
| 911 |
drag { |
| 912 |
axis: Drag.XAxis |
| 913 |
target: resizer |
| 914 |
smoothed: false |
| 915 |
minimumX: holder.Layout.minimumWidth |
| 916 |
maximumX: holder.Layout.maximumWidth |
| 917 |
} |
| 918 |
onPressed: resizer.x = holder.Layout.preferredWidth |
| 919 |
} |
| 920 |
states: State { |
| 921 |
name: "active" |
| 922 |
when: resizerSensing.pressed |
| 923 |
PropertyChanges { |
| 924 |
target: verticalDivider |
| 925 |
color: Qt.darker(theme.palette.normal.background, 1.7) |
| 926 |
} |
| 927 |
} |
| 928 |
transitions: Transition { |
| 929 |
from: "" |
| 930 |
to: "*" |
| 931 |
reversible: true |
| 932 |
ColorAnimation { |
| 933 |
target: verticalDivider |
| 934 |
property: "color" |
| 935 |
duration: LomiriAnimation.SlowDuration |
| 936 |
} |
| 937 |
} |
| 938 |
} |
| 939 |
Item { |
| 940 |
id: resizer |
| 941 |
height: parent.height |
| 942 |
onXChanged: holder.Layout.preferredWidth = x |
| 943 |
} |
| 944 |
|
| 945 |
function attachPage(wrapper) { |
| 946 |
pageWrapper = wrapper; |
| 947 |
pageWrapper.parent = holderBody; |
| 948 |
pageWrapper.pageHolder = holder; |
| 949 |
pageWrapper.active = true; |
| 950 |
|
| 951 |
if (pageWrapper.object.hasOwnProperty("head")) { |
| 952 |
subHeader.config = pageWrapper.object.head; |
| 953 |
} |
| 954 |
if (pageWrapper.column === 0) { |
| 955 |
// set the application title |
| 956 |
appHeaderControlPage.title = pageWrapper.object.title; |
| 957 |
} |
| 958 |
} |
| 959 |
function detachCurrentPage() { |
| 960 |
if (!pageWrapper) return undefined; |
| 961 |
var wrapper = pageWrapper; |
| 962 |
// remove header |
| 963 |
wrapper.active = false; |
| 964 |
subHeader.config = null; |
| 965 |
pageWrapper = null; |
| 966 |
wrapper.parent = hiddenItem; |
| 967 |
wrapper.pageHolder = null; |
| 968 |
return wrapper; |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
/*! \internal */ |
| 974 |
// Pages declared as children will be placed directly into hiddenPages |
| 975 |
default property alias data: hiddenPages.data |
| 976 |
PageTreeNode { |
| 977 |
id: hiddenPages |
| 978 |
objectName: "HiddenPagePool" |
| 979 |
visible: false |
| 980 |
active: false |
| 981 |
// make sure nothing is shown eventually |
| 982 |
clip: true |
| 983 |
anchors.fill: parent |
| 984 |
} |
| 985 |
|
| 986 |
// Holds the columns holding the pages visible. Each column has only one page |
| 987 |
// as child, the invisible stacked ones are all stored in the hiddenPool |
| 988 |
// component within each column. The stack keeps the column index onto which |
| 989 |
// those should be moved once they become visible. |
| 990 |
RowLayout { |
| 991 |
id: body |
| 992 |
objectName: "body" |
| 993 |
anchors.fill: parent |
| 994 |
spacing: 0 |
| 995 |
|
| 996 |
property real headerHeight: 0 |
| 997 |
|
| 998 |
function updateHeaderHeight(newHeight) { |
| 999 |
var page; |
| 1000 |
var i; |
| 1001 |
if (newHeight > body.headerHeight) { |
| 1002 |
body.headerHeight = newHeight; |
| 1003 |
} else { |
| 1004 |
var h = 0; |
| 1005 |
var subHeight = 0; |
| 1006 |
for (i = 0; i < children.length; i++) { |
| 1007 |
page = children[i].page; |
| 1008 |
if (page && page.hasOwnProperty("header") && page.header) { |
| 1009 |
if (page.header.hasOwnProperty("automaticHeight") && |
| 1010 |
page.header.automaticHeight) { |
| 1011 |
subHeight = page.header.implicitHeight; |
| 1012 |
} |
| 1013 |
} else { |
| 1014 |
subHeight = children[i].head.preferredHeight; |
| 1015 |
} |
| 1016 |
if (subHeight > h) h = subHeight; |
| 1017 |
} |
| 1018 |
body.headerHeight = h; |
| 1019 |
} |
| 1020 |
|
| 1021 |
// Update the Page.header heights. |
| 1022 |
for (i = 0; i < body.children.length; i++) { |
| 1023 |
page = body.children[i].page; |
| 1024 |
if (page && page.hasOwnProperty("header") && page.header && |
| 1025 |
page.header.hasOwnProperty("automaticHeight") && |
| 1026 |
page.header.automaticHeight) { |
| 1027 |
page.header.height = headerHeight; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
onChildrenChanged: { |
| 1033 |
// all children should have Layout.fillWidth false, except the last one |
| 1034 |
for (var i = 0; i < children.length; i++) { |
| 1035 |
children[i].column = i; |
| 1036 |
} |
| 1037 |
updateHeaderHeight(0); |
| 1038 |
} |
| 1039 |
} |
| 1040 |
} |