Debian Codegraph @

src/imports/Components/1.2/PageTreeNode.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 import Lomiri.Components 1.2
19
20 /*!
21 \internal
22 \qmltype PageTreeNode
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri
25 \brief The common parent of \l Page, \l MainView, \l PageStack and \l Tabs.
26
27 It is used to propagate properties such as \l header and \l toolbar from a
28 \l MainView (the root node) to each \l Page (leaf node) in the tree.
29 */
30 StyledItem {
31 id: node
32
33 /*!
34 \internal
35 Used to determine whether an Item is a PageTreeNode
36 */
37 property bool __isPageTreeNode: true
38
39 /*! \internal */
40 onParentChanged: internal.updatePageTree()
41 /*! \internal */
42 Component.onCompleted: internal.updatePageTree()
43
44 /*!
45 \deprecated
46 The header of the node. Propagates down from the root node.
47 This property is DEPRECATED.
48 */
49 property Item header: node.__propagated ? node.__propagated.header : null
50
51 /*!
52 \deprecated
53 The toolbar of the node. Propagates down from the root node.
54 This property is DEPRECATED.
55 */
56 property Toolbar toolbar: node.__propagated && node.__propagated.hasOwnProperty("toolbar")
57 ? node.__propagated.toolbar : null
58
59 /*!
60 \internal
61 QtObject containing all the properties that are propagated from the
62 root (MainView) of a page tree to its leafs (Pages).
63 This object contains properties such as the header and toolbar that are
64 instantiated by the MainView.
65
66 This property is internal because the derived classes (MainView and Page)
67 need to access it, but other components using those classes should not have
68 access to it.
69 */
70 property QtObject __propagated: node.parentNode ? node.parentNode.__propagated : null
71
72 /*!
73 At any time, there is exactly one path from the root node to a Page leaf node
74 where all nodes are active. All other nodes are not active. This is used by
75 \l Tabs and \l PageStack to determine which of multiple nodes in the Tabs or
76 PageStack is the currently active one.
77 */
78 property bool active: node.parentNode ? node.parentNode.active : false
79
80 /*!
81 The \l PageStack that this Page has been pushed on, or null if it is not
82 part of a PageStack. This value is automatically set for pages that are pushed
83 on a PageStack, and propagates to its child nodes.
84 */
85 // Note: pageStack is not included in the propagated property because there may
86 // be multiple PageStacks in a single page tree.
87 property Item pageStack: node.parentNode ? node.parentNode.pageStack : null
88
89 /*!
90 The parent node of the current node in the page tree.
91 */
92 property Item parentNode: null
93
94 /*!
95 The leaf node that is active.
96 */
97 property Item activeLeafNode
98
99 /*!
100 Whether or not this node is a leaf, that is if it has no descendant that are nodes.
101 Pages are leafs, and they don't have descendants in the PageTree.
102 */
103 property bool isLeaf: false
104
105 // turn on focusing
106 activeFocusOnPress: true
107
108 Binding {
109 target: node.parentNode
110 property: "activeLeafNode"
111 value: node.isLeaf ? node : node.activeLeafNode
112 when: node.active
113 }
114
115 Item {
116 id: internal
117
118 function isPageTreeNode(object) {
119 // FIXME: Use QuickUtils.className() when it becomes available.
120 return (object && object.hasOwnProperty("__isPageTreeNode") && object.__isPageTreeNode);
121 }
122
123 /*!
124 Return the parent node in the page tree, or null if the item is the root node or invalid.
125 */
126 function getParentPageTreeNode(item) {
127 var node = null;
128 if (item) {
129 var i = item.parent;
130 while (i) {
131 if (internal.isPageTreeNode(i)) {
132 if (i.isLeaf) {
133 // children of a leaf are not part of the tree
134 node = null;
135 print("WARNING! " +
136 "Do not put Page/Tabs/PageStack inside another "+
137 "Page because that causes confusion which is the "+
138 "active page that sets the title and actions.");
139 } else {
140 // current node is part of the tree with i as its parent.
141 node = i;
142 }
143 break;
144 }
145 i = i.parent;
146 }
147 }
148 return node;
149 }
150
151 /*!
152 Find the parent node.
153 */
154 function updatePageTree() {
155 node.parentNode = internal.getParentPageTreeNode(node);
156 }
157 }
158 }