Debian Codegraph @

src/imports/Components/2.0/Page.qml

raw
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 2.4
18 import Lomiri.Components
19 import "pageUtils.js" as Utils
20
21 /*!
22 \qmltype Page
23 \inqmlmodule Lomiri.Components
24 \inherits StyledItem
25 \ingroup lomiri
26 \brief A page is the basic Item that represents a single view in
27 an Lomiri application. It is recommended to use the Page inside
28 the \l MainView or \l AdaptivePageLayout.
29
30 \l MainView provides a header for Pages it includes if no
31 \l header property was set. However, the application header is deprecated
32 and it is recommended to set the \l header property instead.
33
34 Anchors and height of a Page are automatically determined to align with
35 the header of the \l MainView, but can be overridden.
36 Page contents does not automatically leave space for the Page \l header,
37 so this must be taken into account when anchoring the contents of the Page.
38
39 Example:
40 \qml
41 import QtQuick 2.4
42 import Lomiri.Components
43
44 MainView {
45 width: units.gu(48)
46 height: units.gu(60)
47
48 Page {
49 header: PageHeader {
50 id: pageHeader
51 title: i18n.tr("Example page")
52
53 trailingActionBar.actions: [
54 Action {
55 iconName: "toolkit_input-search"
56 text: i18n.tr("Search")
57 }
58 ]
59 }
60
61 Label {
62 anchors {
63 horizontalCenter: parent.horizontalCenter
64 top: pageHeader.bottom
65 topMargin: units.gu(5)
66 }
67 text: i18n.tr("Hello world!")
68 }
69 }
70 }
71 \endqml
72 */
73 PageTreeNode {
74 id: page
75 anchors {
76 left: parent ? parent.left : undefined
77 bottom: parent ? parent.bottom : undefined
78 }
79 // Set width and height so that a parent Loader can be automatically resized
80 // to the size of the loaded Page.
81 width: parentNode ? parentNode.width - page.x : undefined
82 // FIXME: We no longer need to take the internal header height into account
83 // when we remove MainView's AppHeader.
84 height: parentNode ? page.flickable ? parentNode.height : parentNode.height - internal.headerHeight : undefined
85
86 /*!
87 \qmlproperty ActrionContext Page::actionContext
88 \readonly
89 \since Lomiri.Components 1.3
90 The action context of the page.
91 */
92 readonly property alias actionContext: localContext
93 ActionContext {
94 id: localContext
95 active: page.active
96 objectName: page.objectName + "Context"
97 }
98
99 /*!
100 \since Lomiri.Components 1.3
101 The header property for this page. Setting this property will reparent the
102 header to the page and disable the \l MainView's application header.
103 \qml
104 Page {
105 id: page
106 header: PageHeader {
107 title: "Page with header"
108 trailingActionBar.actions: [
109 Action { iconName: "settings" },
110 Action { iconName: "info" }
111 ]
112 flickable: myFlickable
113 }
114 }
115 \endqml
116 To avoid Page content being occluded by the header, the contents of the Page
117 should anchor to the bottom of the header. When the Page contents is flickable,
118 the contents does not need to be anchored to the header, but it is recommended
119 to use a \l PageHeader or \l Header component as the Page header, and set its
120 \l Header::flickable property so that the Flickable gets a top-margin that
121 leaves enough space for the header.
122 \sa PageHeader, Header
123 */
124 property Item header
125 onHeaderChanged: internal.updateHeader()
126 Component.onCompleted: internal.updateHeader()
127
128 /*! \internal */
129 isLeaf: true
130
131 /*! \deprecated */
132 property string title: parentNode && parentNode.hasOwnProperty("title") ? parentNode.title : ""
133 /*! \deprecated */
134 property Flickable flickable: Utils.getFlickableChild(page)
135 /*! \deprecated */
136 readonly property alias head: headerConfig
137 PageHeadConfiguration {
138 id: headerConfig
139 title: page.title
140 flickable: page.flickable
141 onFlickableChanged: internal.printDeprecationWarning()
142 onTitleChanged: internal.printDeprecationWarning()
143 onActionsChanged: internal.printDeprecationWarning()
144 onBackActionChanged: internal.printDeprecationWarning()
145 }
146
147 Item {
148 id: internal
149
150 property bool showDeprecationWarning: true
151 function printDeprecationWarning() {
152 if (internal.showDeprecationWarning) {
153 var titleStr = page;
154 if (page.title) {
155 titleStr += "\"" + page.title + "\"";
156 }
157 titleStr += ": "
158 print(titleStr + "In Lomiri.Components 1.3, the use of Page.title, Page.flickable and" +
159 " Page.head is deprecated. Use Page.header and the PageHeader component instead.");
160 internal.showDeprecationWarning = false;
161 }
162 }
163
164 property Item previousHeader: null
165 function updateHeader() {
166 internal.showDeprecationWarning = false;
167 if (internal.previousHeader) {
168 internal.previousHeader.parent = null;
169 }
170 if (page.header) {
171 internal.previousHeader = page.header;
172 page.header.parent = page;
173 } else {
174 internal.previousHeader = null;
175 }
176 }
177
178 ///////////////////////////////
179 // old header handling below //
180 ///////////////////////////////
181 property AppHeader header: page.__propagated && page.__propagated.header ? page.__propagated.header : null
182 // Used to position the Page when there is no flickable.
183 // When there is a flickable, the header will automatically position it.
184 property real headerHeight: internal.header && internal.header.visible ?
185 internal.header.height + internal.header.y : 0
186
187 // Note: The bindings below need to check whether headerConfig.contents
188 // is valid in the "value", even when that is required in the Binding's "when"
189 // property, to avoid TypeErrors while/after a page becomes (in)active.
190 //
191 // Note 2: contents.parent binding is made by PageHeadStyle.
192 property bool hasParent: headerConfig.contents &&
193 headerConfig.contents.parent
194
195 Binding {
196 target: headerConfig.contents
197 property: "visible"
198 value: page.active
199 when: headerConfig.contents
200 }
201 Binding {
202 target: headerConfig.contents
203 property: "anchors.verticalCenter"
204 value: internal.hasParent ? headerConfig.contents.parent.verticalCenter :
205 undefined
206 when: headerConfig.contents
207 }
208 Binding {
209 target: headerConfig.contents
210 property: "anchors.left"
211 value: internal.hasParent ? headerConfig.contents.parent.left : undefined
212 when: headerConfig.contents
213 }
214 }
215 }