| 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.Components as Toolkit |
| 19 |
import Lomiri.PerformanceMetrics 1.0 |
| 20 |
import QtQuick.Window 2.2 |
| 21 |
|
| 22 |
/*! |
| 23 |
\qmltype MainView |
| 24 |
\inqmlmodule Lomiri.Components |
| 25 |
\ingroup lomiri |
| 26 |
\inherits StyledItem |
| 27 |
\brief MainView is the root Item that should be used for all applications. |
| 28 |
|
| 29 |
The simplest way to use a MainView is to include a single \l Page object |
| 30 |
inside the MainView: |
| 31 |
\qml |
| 32 |
import QtQuick 2.4 |
| 33 |
import Lomiri.Components |
| 34 |
|
| 35 |
MainView { |
| 36 |
width: units.gu(48) |
| 37 |
height: units.gu(60) |
| 38 |
|
| 39 |
Page { |
| 40 |
header: PageHeader { |
| 41 |
id: pageHeader |
| 42 |
title: "Simple page" |
| 43 |
} |
| 44 |
Button { |
| 45 |
anchors { |
| 46 |
horizontalCenter: parent.horizontalCenter |
| 47 |
top: pageHeader.bottom |
| 48 |
topMargin: units.gu(5) |
| 49 |
} |
| 50 |
width: units.gu(15) |
| 51 |
text: "Push me" |
| 52 |
onClicked: print("Click!") |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
\endqml |
| 57 |
It is not required to set the anchors of the \l Page as it will automatically fill its parent. |
| 58 |
|
| 59 |
Do not include multiple Pages directly inside the MainView, but use \l AdaptivePageLayout |
| 60 |
inside MainView to navigate between several Pages. |
| 61 |
|
| 62 |
If the \l Page inside the MainView includes a Flickable, set the flickable property of |
| 63 |
the PageHeader to automatically hide and show the header when the user scrolls up or down: |
| 64 |
\qml |
| 65 |
import QtQuick 2.4 |
| 66 |
import Lomiri.Components |
| 67 |
|
| 68 |
MainView { |
| 69 |
width: units.gu(48) |
| 70 |
height: units.gu(60) |
| 71 |
|
| 72 |
Page { |
| 73 |
header: PageHeader { |
| 74 |
title: "Page with Flickable" |
| 75 |
flickable: myFlickable |
| 76 |
} |
| 77 |
|
| 78 |
Flickable { |
| 79 |
id: myFlickable |
| 80 |
anchors.fill: parent |
| 81 |
contentHeight: column.height |
| 82 |
|
| 83 |
Column { |
| 84 |
id: column |
| 85 |
Repeater { |
| 86 |
model: 100 |
| 87 |
Label { |
| 88 |
text: "line "+index |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
\endqml |
| 96 |
The same header behavior is automatic when using a ListView instead of a Flickable in the above |
| 97 |
example. |
| 98 |
|
| 99 |
The examples above show how to include a single \l Page inside a MainView, but more |
| 100 |
advanced application structures are possible using \l AdaptivePageLayout. |
| 101 |
*/ |
| 102 |
Toolkit.MainViewBase { |
| 103 |
id: mainView |
| 104 |
|
| 105 |
/*! |
| 106 |
\internal |
| 107 |
Hint the default width of the MainView |
| 108 |
*/ |
| 109 |
implicitWidth: units.gu(45) |
| 110 |
/*! |
| 111 |
\internal |
| 112 |
Hint the default height of the MainView |
| 113 |
*/ |
| 114 |
implicitHeight: units.gu(75) |
| 115 |
|
| 116 |
/*! \deprecated */ |
| 117 |
property bool automaticOrientation: false |
| 118 |
|
| 119 |
/*! |
| 120 |
\internal |
| 121 |
Use default property to ensure children added do not draw over the header. |
| 122 |
*/ |
| 123 |
default property alias contentsItem: contents.data |
| 124 |
Item { |
| 125 |
id: canvas |
| 126 |
anchors { |
| 127 |
fill: parent |
| 128 |
bottomMargin: mainView.anchorToKeyboard && |
| 129 |
LomiriApplication.inputMethod.visible ? |
| 130 |
LomiriApplication.inputMethod.keyboardRectangle.height : 0 |
| 131 |
} |
| 132 |
|
| 133 |
Item { |
| 134 |
id: contents |
| 135 |
anchors { |
| 136 |
fill: parent |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/*! |
| 141 |
The header of the MainView. Can be used to obtain the height of the header |
| 142 |
in \l Page to determine the area for the \l Page to fill. |
| 143 |
*/ |
| 144 |
AppHeader { |
| 145 |
// This objectName is used in the MainView autopilot custom proxy object |
| 146 |
// in order to select the application header. |
| 147 |
// Also used in tst_header_locked_visible.qml. |
| 148 |
objectName: "MainView_Header" |
| 149 |
id: headerItem |
| 150 |
property real bottomY: headerItem.y + headerItem.height |
| 151 |
dividerColor: Qt.darker(mainView.backgroundColor, 1.1) |
| 152 |
panelColor: Qt.lighter(mainView.backgroundColor, 1.1) |
| 153 |
backgroundColor: mainView.backgroundColor |
| 154 |
|
| 155 |
title: internal.activePage ? internal.activePage.title : "" |
| 156 |
pageStack: internal.activePage ? internal.activePage.pageStack : null |
| 157 |
|
| 158 |
contents: internal.activePage && |
| 159 |
internal.activePage.hasOwnProperty("__customHeaderContents") ? |
| 160 |
internal.activePage.__customHeaderContents : null |
| 161 |
|
| 162 |
Toolkit.PageHeadConfiguration { |
| 163 |
id: defaultConfig |
| 164 |
// Used when there is no active Page, or a Page 1.0 is used which |
| 165 |
// does not have a PageHeadConfiguration. |
| 166 |
} |
| 167 |
config: visible && internal.activePage && |
| 168 |
internal.activePage.hasOwnProperty("head") ? |
| 169 |
internal.activePage.head : defaultConfig |
| 170 |
|
| 171 |
// don't show the application header if the page has its own header. |
| 172 |
visible: !(internal.activePage && |
| 173 |
internal.activePage.hasOwnProperty("header") && |
| 174 |
internal.activePage.header) && |
| 175 |
internal.activePage |
| 176 |
|
| 177 |
height: visible ? implicitHeight : 0 |
| 178 |
|
| 179 |
// 'window' is defined by QML between startup and showing on the screen. |
| 180 |
// There is no signal for when it becomes available and re-declaring it is not safe. |
| 181 |
property bool windowActive: window !== null |
| 182 |
function updateWindowTitle() { |
| 183 |
if (window) { |
| 184 |
window.title = headerItem.title; |
| 185 |
} |
| 186 |
} |
| 187 |
onWindowActiveChanged: { |
| 188 |
updateWindowTitle(); |
| 189 |
} |
| 190 |
onTitleChanged: { |
| 191 |
updateWindowTitle(); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
Connections { |
| 196 |
target: Qt.application |
| 197 |
function onActiveChanged() { |
| 198 |
if (Qt.application.active) { |
| 199 |
if (!(headerItem.config && |
| 200 |
headerItem.config.hasOwnProperty("locked") && |
| 201 |
headerItem.config.locked)) { |
| 202 |
headerItem.exposed = true; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
Item { |
| 210 |
id: internal |
| 211 |
|
| 212 |
// Even when using MainView 1.1, we still support Page 1.0. |
| 213 |
// PageBase (=Page 1.0) is the superclass of Page 1.1. |
| 214 |
property Item activePage: isPage(mainView.activeLeafNode) ? mainView.activeLeafNode : null |
| 215 |
|
| 216 |
function isPage(item) { |
| 217 |
return item && item.hasOwnProperty("__isPageTreeNode") && |
| 218 |
item.__isPageTreeNode && |
| 219 |
item.hasOwnProperty("title") && |
| 220 |
item.hasOwnProperty("flickable") && |
| 221 |
item.hasOwnProperty("active") && |
| 222 |
item.hasOwnProperty("pageStack") |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
__propagated: QtObject { |
| 227 |
/*! |
| 228 |
\internal |
| 229 |
The header that will be propagated to the children in the page tree node. |
| 230 |
It is used by Tabs to bind header's tabsModel. |
| 231 |
*/ |
| 232 |
property AppHeader header: headerItem |
| 233 |
|
| 234 |
/*! |
| 235 |
\internal |
| 236 |
The action manager that has the global context for the MainView's actions, |
| 237 |
and to which a local context can be added for each Page that has actions.actions. |
| 238 |
*/ |
| 239 |
property var actionManager: mainView.actionManager |
| 240 |
|
| 241 |
/*! |
| 242 |
\internal |
| 243 |
Used by PageStack. This property only exists in MainView 1.2 and later. |
| 244 |
*/ |
| 245 |
readonly property bool animateHeader: false |
| 246 |
} |
| 247 |
|
| 248 |
backgroundColor: theme.palette.normal.background |
| 249 |
} |