Debian Codegraph @

src/imports/Components/1.2/MainView.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 Lomiri.PerformanceMetrics 1.0
19 import QtQuick.Window 2.0
20
21 /*! \internal */
22 // Documentation is in MainView.qdoc
23 MainViewBase {
24 id: mainView
25
26 /*!
27 \qmlproperty bool automaticOrientation
28 */
29 property bool automaticOrientation: false
30 property bool useDeprecatedToolbar: true
31
32 /*!
33 \internal
34 Use default property to ensure children added do not draw over the toolbar.
35 */
36 default property alias contentsItem: contents.data
37 OrientationHelper {
38 id: canvas
39
40 automaticOrientation: false
41 anchorToKeyboard: mainView.anchorToKeyboard
42
43 // clip the contents so that it does not overlap the header
44 Item {
45 id: contentsClipper
46 anchors {
47 left: parent.left
48 right: parent.right
49 top: headerItem.bottom
50 bottom: parent.bottom
51 }
52 // only clip when necessary
53 // ListView headers may be positioned at the top, independent from
54 // flickable.contentY, so do not clip depending on activePage.flickable.contentY.
55 clip: headerItem.bottomY > 0 && internal.activePage && internal.activePage.flickable
56
57 Item {
58 id: contents
59 anchors {
60 fill: parent
61
62 // move the whole contents up if the toolbar is locked and opened otherwise the toolbar will obscure part of the contents
63 bottomMargin: mainView.useDeprecatedToolbar &&
64 toolbarLoader.item.locked && toolbarLoader.item.opened ?
65 toolbarLoader.item.height + toolbarLoader.item.triggerSize : 0
66 // compensate so that the actual y is always 0
67 topMargin: -parent.y
68 }
69 }
70
71 MouseArea {
72 id: contentsArea
73 anchors.fill: contents
74 // This mouse area will be on top of the page contents, but
75 // under the toolbar and header.
76 // It is used for detecting interaction with the page contents
77 // which can close the toolbar and take a tab bar out of selection mode.
78
79 onPressed: {
80 mouse.accepted = false;
81 if (mainView.useDeprecatedToolbar) {
82 if (!toolbarLoader.item.locked) {
83 toolbarLoader.item.close();
84 }
85 }
86 if (headerItem.tabBar && !headerItem.tabBar.alwaysSelectionMode) {
87 headerItem.tabBar.selectionMode = false;
88 }
89 }
90 propagateComposedEvents: true
91 enabled: mainView.useDeprecatedToolbar
92 }
93 }
94
95 /*!
96 Animate header and toolbar.
97 */
98 property bool animate: true
99
100 Component {
101 id: toolbarComponent
102 Toolbar {
103 parent: canvas
104 onPressedChanged: {
105 if (!pressed) return;
106 if (headerItem.tabBar !== null) {
107 headerItem.tabBar.selectionMode = false;
108 }
109 }
110 animate: canvas.animate
111 tools: internal.activePage ? internal.activePage.tools : null
112 }
113 }
114
115 Loader {
116 id: toolbarLoader
117 sourceComponent: mainView.useDeprecatedToolbar ? toolbarComponent : null
118 }
119
120 /*!
121 The header of the MainView. Can be used to obtain the height of the header
122 in \l Page to determine the area for the \l Page to fill.
123 */
124 AppHeader {
125 // This objectName is used in the MainView autopilot custom proxy object
126 // in order to select the application header.
127 // Also used in tst_header_locked_visible.qml.
128 objectName: "MainView_Header"
129 id: headerItem
130 property real bottomY: headerItem.y + headerItem.height
131 animate: canvas.animate
132 dividerColor: Qt.darker(mainView.headerColor, 1.1)
133 panelColor: Qt.lighter(mainView.headerColor, 1.1)
134
135 title: internal.activePage ? internal.activePage.title : ""
136 flickable: internal.activePage ? internal.activePage.flickable : null
137 pageStack: internal.activePage ? internal.activePage.pageStack : null
138
139 PageHeadConfiguration {
140 id: headerConfig
141 // for backwards compatibility with deprecated tools property
142 actions: internal.activePage ?
143 getActionsFromTools(internal.activePage.tools) : null
144
145 backAction: internal.activePage && internal.activePage.tools &&
146 internal.activePage.tools.hasOwnProperty("back") &&
147 internal.activePage.tools.back &&
148 internal.activePage.tools.back.hasOwnProperty("action") ?
149 internal.activePage.tools.back.action : null
150
151 function getActionsFromTools(tools) {
152 if (!tools || !tools.hasOwnProperty("contents")) {
153 // tools is not of type ToolbarActions. Not supported.
154 return null;
155 }
156
157 var actionList = [];
158 for (var i in tools.contents) {
159 var item = tools.contents[i];
160 if (item && item.hasOwnProperty("action") && item.action !== null) {
161 var action = item.action;
162 if (action.hasOwnProperty("iconName") && action.hasOwnProperty("text")) {
163 // it is likely that the action is of type Action.
164 actionList.push(action);
165 }
166 }
167 }
168 return actionList;
169 }
170 }
171
172 contents: internal.activePage ?
173 internal.activePage.__customHeaderContents : null
174
175 // FIXME: This can be simplified a lot when we drop support for using
176 // the deprecated tools property.
177 config: internal.activePage && internal.activePage.hasOwnProperty("head") &&
178 (internal.activePage.head.actions.length > 0 ||
179 internal.activePage.head.backAction !== null ||
180 internal.activePage.head.contents !== null ||
181 internal.activePage.head.sections.model !== undefined) ?
182 internal.activePage.head : headerConfig
183
184 property Item tabBar: null
185 Binding {
186 target: headerItem
187 property: "tabBar"
188 value: headerItem.__styleInstance.__tabBar
189 when: headerItem.__styleInstance &&
190 headerItem.__styleInstance.hasOwnProperty("__tabBar")
191 }
192
193 Connections {
194 // no connections are made when target is null
195 target: headerItem.tabBar
196 function onPressedChanged() {
197 if (mainView.useDeprecatedToolbar) {
198 if (headerItem.tabBar.pressed) {
199 if (!toolbarLoader.item.locked) toolbarLoader.item.close();
200 }
201 }
202 }
203 }
204
205 // 'window' is defined by QML between startup and showing on the screen.
206 // There is no signal for when it becomes available and re-declaring it is not safe.
207 property bool windowActive: window !== null
208 function updateWindowTitle() {
209 if (window) {
210 window.title = headerItem.title;
211 }
212 }
213 onWindowActiveChanged: {
214 updateWindowTitle();
215 }
216 onTitleChanged: {
217 updateWindowTitle();
218 }
219
220 useDeprecatedToolbar: mainView.useDeprecatedToolbar
221 }
222
223 Connections {
224 target: Qt.application
225 function onActiveChanged() {
226 if (Qt.application.active) {
227 canvas.animate = false;
228 headerItem.show();
229 if (headerItem.tabBar) {
230 headerItem.tabBar.selectionMode = true;
231 }
232 if (mainView.useDeprecatedToolbar) {
233 if (!toolbarLoader.item.locked) toolbarLoader.item.open();
234 }
235 canvas.animate = true;
236 }
237 }
238 }
239 }
240
241 Item {
242 id: internal
243
244 // Even when using MainView 1.1, we still support Page 1.0.
245 // we use PageTreeNode being the base class of all pages.
246 property PageTreeNode activePage: isPage(mainView.activeLeafNode) ? mainView.activeLeafNode : null
247
248 function isPage(item) {
249 return item && item.hasOwnProperty("__isPageTreeNode") && item.__isPageTreeNode &&
250 item.hasOwnProperty("title") && item.hasOwnProperty("tools");
251 }
252 }
253
254 __propagated: QtObject {
255 /*!
256 \internal
257 The header that will be propagated to the children in the page tree node.
258 It is used by Tabs to bind header's tabsModel.
259 */
260 property AppHeader header: headerItem
261
262 /*!
263 \internal
264 \deprecated
265 The toolbar that will be propagated to the children in the page tree node.
266 */
267 property Toolbar toolbar: toolbarLoader.item
268
269 /*!
270 \internal
271 Tabs needs to know whether to use a TabBar or the new header.
272 */
273 property alias useDeprecatedToolbar: mainView.useDeprecatedToolbar
274
275 /*!
276 \internal
277 The action manager that has the global context for the MainView's actions,
278 and to which a local context can be added for each Page that has actions.actions.
279 */
280 property var actionManager: mainView.actionManager
281 }
282 }