| 1 |
/* |
| 2 |
* Copyright 2016 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 |
|
| 20 |
/*! |
| 21 |
\qmltype PageHeader |
| 22 |
\inqmlmodule Lomiri.Components |
| 23 |
\ingroup lomiri |
| 24 |
\brief The PageHeader shows a title with a leading and a trailing |
| 25 |
\l ActionBar that add action buttons to the header. |
| 26 |
|
| 27 |
The colors for foreground, background and the divider are configured |
| 28 |
in the style, so they may be set using \l StyleHints: |
| 29 |
\qml |
| 30 |
PageHeader { |
| 31 |
title: "Colors" |
| 32 |
StyleHints { |
| 33 |
foregroundColor: LomiriColors.orange |
| 34 |
backgroundColor: "black" |
| 35 |
dividerColor: LomiriColors.slate |
| 36 |
} |
| 37 |
} |
| 38 |
\endqml |
| 39 |
|
| 40 |
See \l Header properties that are inherited by PageHeader to control |
| 41 |
the visibility of the header. |
| 42 |
*/ |
| 43 |
Header { |
| 44 |
id: header |
| 45 |
anchors { |
| 46 |
left: parent ? parent.left : undefined |
| 47 |
right: parent ? parent.right : undefined |
| 48 |
} |
| 49 |
|
| 50 |
/*! |
| 51 |
The title to display in the header. |
| 52 |
Note that the title will be hidden if the \l contents Item is set. |
| 53 |
*/ |
| 54 |
property string title |
| 55 |
|
| 56 |
/*! |
| 57 |
Displayed under the title. |
| 58 |
Hidden when the \l contents Item is set. |
| 59 |
*/ |
| 60 |
property string subtitle |
| 61 |
|
| 62 |
/*! |
| 63 |
The contents item to display in the header. By default the contents is |
| 64 |
undefined, and setting it will disable showing of the title and subtitle. |
| 65 |
|
| 66 |
Example: |
| 67 |
\qml |
| 68 |
PageHeader { |
| 69 |
id: header |
| 70 |
title: "Welcome" |
| 71 |
contents: Rectangle { |
| 72 |
anchors.fill: parent |
| 73 |
color: LomiriColors.red |
| 74 |
Label { |
| 75 |
anchors.centerIn: parent |
| 76 |
text: header.title |
| 77 |
color: "white" |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
\endqml |
| 82 |
*/ |
| 83 |
property Item contents |
| 84 |
|
| 85 |
Component.onCompleted: contentsHolder.updateContents() |
| 86 |
onContentsChanged: contentsHolder.updateContents() |
| 87 |
onSubtitleChanged: contentsHolder.updateContents() |
| 88 |
|
| 89 |
Item { |
| 90 |
id: contentsHolder |
| 91 |
anchors { |
| 92 |
left: leading.right |
| 93 |
right: trailing.left |
| 94 |
top: parent.top |
| 95 |
leftMargin: leading.visible ? 0 : units.gu(1) |
| 96 |
rightMargin: trailing.visible ? 0 : units.gu(1) |
| 97 |
} |
| 98 |
height: header.__styleInstance ? header.__styleInstance.contentHeight : 0 |
| 99 |
Loader { |
| 100 |
id: titleLoader |
| 101 |
anchors.fill: parent |
| 102 |
} |
| 103 |
Loader { |
| 104 |
id: subtitleLoader |
| 105 |
anchors.fill: parent |
| 106 |
} |
| 107 |
|
| 108 |
property Item previousContents: null |
| 109 |
property Item previousContentsParent: null |
| 110 |
|
| 111 |
function updateContents() { |
| 112 |
if (!__styleInstance) return; // the style needs to be loaded first |
| 113 |
if (previousContents) { |
| 114 |
previousContents.parent = previousContentsParent; |
| 115 |
} |
| 116 |
if (header.contents) { |
| 117 |
titleLoader.sourceComponent = null; |
| 118 |
previousContents = header.contents; |
| 119 |
previousContentsParent = header.contents.parent; |
| 120 |
header.contents.parent = contentsHolder; |
| 121 |
} else { |
| 122 |
previousContents = null; |
| 123 |
previousContentsParent = null; |
| 124 |
titleLoader.sourceComponent = __styleInstance.titleComponent; |
| 125 |
if (!subtitle) { |
| 126 |
subtitleLoader.sourceComponent = null; |
| 127 |
} else { |
| 128 |
subtitleLoader.sourceComponent = __styleInstance.subtitleComponent; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// When the style changes, make sure that the titleLoader loads |
| 134 |
// the new titleComponent. |
| 135 |
property Item styleInstance: __styleInstance |
| 136 |
onStyleInstanceChanged: updateContents() |
| 137 |
} |
| 138 |
|
| 139 |
/*! |
| 140 |
The actions to be shown in the leading action bar. |
| 141 |
This property is automatically set by the |
| 142 |
\l AdaptivePageLayout and other navigation components to configure the |
| 143 |
back action for the \l Page. |
| 144 |
Application developers should not set this property, because the |
| 145 |
value may be overridden by Lomiri components that have navigation. |
| 146 |
Instead, set \l leadingActionBar's actions property. |
| 147 |
*/ |
| 148 |
property list<Action> navigationActions |
| 149 |
|
| 150 |
/*! |
| 151 |
\qmlproperty ActionBar leadingActionBar |
| 152 |
The \l ActionBar for the leading navigation actions. |
| 153 |
Example: |
| 154 |
\qml |
| 155 |
PageHeader { |
| 156 |
leadingActionBar.actions: [ |
| 157 |
Action { |
| 158 |
iconName: "back" |
| 159 |
text: "Back" |
| 160 |
} |
| 161 |
] |
| 162 |
} |
| 163 |
\endqml |
| 164 |
The default value of \l leadingActionBar actions is |
| 165 |
\l navigationActions, but that value can be changed to show |
| 166 |
different actions in front of the title. |
| 167 |
The leading action bar has only one slot. |
| 168 |
See \l ActionBar. |
| 169 |
*/ |
| 170 |
readonly property alias leadingActionBar: leading |
| 171 |
ActionBar { |
| 172 |
id: leading |
| 173 |
anchors { |
| 174 |
left: parent.left |
| 175 |
top: parent.top |
| 176 |
leftMargin: units.gu(1) |
| 177 |
} |
| 178 |
height: header.__styleInstance ? header.__styleInstance.contentHeight : 0 |
| 179 |
numberOfSlots: 1 |
| 180 |
delegate: header.__styleInstance.defaultActionDelegate |
| 181 |
actions: header.navigationActions |
| 182 |
visible: leading.width > 0 // at least 1 visible action |
| 183 |
StyleHints { |
| 184 |
ignoreUnknownProperties: false |
| 185 |
overflowIconName: "navigation-menu" |
| 186 |
backgroundColor: header.__styleInstance.backgroundColor |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/*! |
| 191 |
\qmlproperty ActionBar trailingActionBar |
| 192 |
The \l ActionBar with trailing actions. |
| 193 |
Example: |
| 194 |
\qml |
| 195 |
PageHeader { |
| 196 |
trailingActionBar { |
| 197 |
actions: [ |
| 198 |
Action { |
| 199 |
iconName: "settings" |
| 200 |
text: "first" |
| 201 |
}, |
| 202 |
Action { |
| 203 |
iconName: "info" |
| 204 |
text: "second" |
| 205 |
}, |
| 206 |
Action { |
| 207 |
iconName: "toolkit_input-search" |
| 208 |
text: "third" |
| 209 |
} |
| 210 |
] |
| 211 |
numberOfSlots: 2 |
| 212 |
} |
| 213 |
} |
| 214 |
\endqml |
| 215 |
By default the trailing action bar automatically adapts |
| 216 |
its number of slots to the available space in the range |
| 217 |
from 3 to 6. |
| 218 |
See \l ActionBar. |
| 219 |
*/ |
| 220 |
readonly property alias trailingActionBar: trailing |
| 221 |
ActionBar { |
| 222 |
id: trailing |
| 223 |
anchors { |
| 224 |
right: parent.right |
| 225 |
top: parent.top |
| 226 |
rightMargin: units.gu(1) |
| 227 |
} |
| 228 |
height: header.__styleInstance ? header.__styleInstance.contentHeight : 0 |
| 229 |
numberOfSlots: MathUtils.clamp(0.3*header.width/units.gu(4), 3, 6) |
| 230 |
delegate: header.__styleInstance.defaultActionDelegate |
| 231 |
visible: trailing.width > 0 // at least 1 visible action |
| 232 |
StyleHints { |
| 233 |
ignoreUnknownProperties: false |
| 234 |
backgroundColor: header.__styleInstance.backgroundColor |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/*! |
| 239 |
Item shown at the bottom of the header. |
| 240 |
The extension can be any Item, but it must have a height so that |
| 241 |
the PageHeader correctly adjusts its height for the extension to fit. |
| 242 |
The extension Item should anchor to the left, right and bottom of |
| 243 |
its parent so that it will be automatically positioned above the |
| 244 |
header divider. This property replaces the sections property. Sections |
| 245 |
can now be added to the header as follows: |
| 246 |
\qml |
| 247 |
PageHeader { |
| 248 |
title: "Header with sections" |
| 249 |
extension: Sections { |
| 250 |
anchors { |
| 251 |
left: parent.left |
| 252 |
leftMargin: units.gu(2) |
| 253 |
bottom: parent.bottom |
| 254 |
} |
| 255 |
model: ["one", "two", "three"] |
| 256 |
} |
| 257 |
} |
| 258 |
\endqml |
| 259 |
See \l Toolbar and \l Sections. |
| 260 |
*/ |
| 261 |
property Item extension |
| 262 |
|
| 263 |
onExtensionChanged: extensionHolder.updateExtension() |
| 264 |
Item { |
| 265 |
id: extensionHolder |
| 266 |
anchors { |
| 267 |
left: parent.left |
| 268 |
right: parent.right |
| 269 |
top: contentsHolder.bottom |
| 270 |
} |
| 271 |
height: header.extension ? header.extension.height : 0 |
| 272 |
|
| 273 |
property Item previousExtension: header.extension |
| 274 |
property Item previousExtensionParent: null |
| 275 |
|
| 276 |
function updateExtension() { |
| 277 |
if (previousExtension) { |
| 278 |
previousExtension.parent = previousExtensionParent; |
| 279 |
} |
| 280 |
if (header.extension) { |
| 281 |
previousExtension = header.extension; |
| 282 |
previousExtensionParent = header.extension.parent; |
| 283 |
header.extension.parent = extensionHolder; |
| 284 |
} else { |
| 285 |
previousExtension = null; |
| 286 |
previousExtensionParent = null; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/*! |
| 292 |
\qmlproperty Sections sections |
| 293 |
Sections shown at the bottom of the header. By default, |
| 294 |
the sections will only be visible if its actions or model |
| 295 |
is set. See \l Sections. |
| 296 |
\deprecated Use \l extension instead. |
| 297 |
*/ |
| 298 |
readonly property alias sections: sectionsItem |
| 299 |
Sections { |
| 300 |
id: sectionsItem |
| 301 |
anchors { |
| 302 |
left: parent.left |
| 303 |
leftMargin: units.gu(2) |
| 304 |
top: contentsHolder.bottom |
| 305 |
} |
| 306 |
visible: model && model.length > 0 && !header.extension |
| 307 |
height: visible ? implicitHeight : 0 |
| 308 |
} |
| 309 |
|
| 310 |
styleName: "PageHeaderStyle" |
| 311 |
} |