Debian Codegraph @

src/imports/Components/1.2/ToolbarItems.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
19 /*!
20 \qmltype ToolbarItems
21 \inqmlmodule Lomiri.Components
22 \ingroup lomiri
23 \deprecated
24 \brief Row of Items to be placed in a toolbar.
25
26 \b DEPRECATED: DO NOT USE THIS. USE \b head.actions IN \l Page INSTEAD.
27
28 Each \l Page has a tools property that can be set to change the tools of toolbar supplied
29 by \l MainView when the \l Page is active. Each ToolbarItems consists of a set of
30 Items and several properties that specify the behavior of the toolbar when the \l Page
31 is active.
32
33 When a \l Page is used inside a \l MainView, \l Tabs or \l PageStack, the toolbar will automatically show
34 the tools of the active \l Page. When the active \l Page inside the \l Tabs or \l PageStack
35 is updated by changing the selected \l Tab or by pushing/popping a \l Page on the \l PageStack,
36 the toolbar will automatically hide, except if the new active \l Page has the \l locked property set.
37
38 It is recommended to use \l ToolbarButton inside the ToolbarItems to define the buttons that will
39 be visible to the user:
40 \qml
41 import QtQuick 2.4
42 import Lomiri.Components 1.2
43
44 MainView {
45 width: units.gu(50)
46 height: units.gu(50)
47
48 Page {
49 title: "Tools example"
50 Label {
51 anchors.centerIn: parent
52 text: "Custom back button\nToolbar locked"
53 }
54 tools: ToolbarItems {
55 ToolbarButton {
56 action: Action {
57 text: "button"
58 iconName: "compose"
59 onTriggered: print("success!")
60 }
61 }
62 locked: true
63 opened: true
64 }
65 }
66 }
67 \endqml
68
69 However, it is possible to include non-\l ToolbarButton Items inside ToolbarItems, and to mix
70 ToolbarButtons and other Items (for example standard Buttons). ToolbarButtons automatically span
71 the full height of the toolbar, and other Items you will probably want to center vertically:
72 \qml
73 import QtQuick 2.4
74 import Lomiri.Components 1.2
75
76 MainView {
77 width: units.gu(50)
78 height: units.gu(50)
79
80 Page {
81 title: "Tools example"
82 Label {
83 anchors.centerIn: parent
84 text: "buttons!"
85 }
86 tools: ToolbarItems {
87 ToolbarButton {
88 action: Action {
89 text: "toolbar"
90 iconName: "compose"
91 onTriggered: print("success!")
92 }
93 }
94 Button {
95 anchors.verticalCenter: parent.verticalCenter
96 text: "standard"
97 }
98 }
99 }
100 }
101 \endqml
102 */
103 Item {
104 id: toolbarItems
105 anchors.fill: parent
106
107 /*!
108 Default property, holds the content which will shown in the toolbar.
109 \qmlproperty list<Object> contents
110 */
111 default property alias contents: toolsContainer.data
112
113 /*!
114 The back button. If it is visible, it will be shown on the left-side of the toolbar.
115 If there is a \l PageStack with depth greater than 1, the back button will be
116 visible and triggering it will pop the page on top of the stack. If there is no
117 \l PageStack with depth greater than 1, the back button is hidden by default
118
119 The following example shows how to have a classic cancel button that is always
120 visible in the toolbar, instead of the default toolbar-styled back button:
121 \qml
122 import QtQuick 2.4
123 import Lomiri.Components 1.2
124
125 MainView {
126 width: units.gu(50)
127 height: units.gu(50)
128
129 Page {
130 title: "Custom back button"
131 tools: ToolbarItems {
132 back: Button {
133 text: "cancel"
134 }
135 }
136 }
137 }
138 \endqml
139 */
140 property Item back: ToolbarButton {
141 objectName: "back_toolbar_button"
142 iconSource: Qt.resolvedUrl("../artwork/back.png")
143 text: i18n.dtr('lomiri-ui-toolkit', "Back")
144 visible: toolbarItems.pageStack && toolbarItems.pageStack.depth > 1
145 /*!
146 If there is a \l PageStack of sufficient depth, triggering the back button
147 will pop the \l Page on top of the \l PageStack.
148 */
149 onTriggered: if (toolbarItems.pageStack && toolbarItems.pageStack.depth > 1) toolbarItems.pageStack.pop()
150 }
151
152 /*!
153 PageStack for the back button. \l Page will automatically set the pageStack property
154 of its tools.
155 */
156 property Item pageStack: null
157
158 /*!
159 The toolbar is opened.
160 When the toolbar is not locked, this value is automatically updated
161 when the toolbar is opened/closed by user interaction or by other events (such as changing
162 the active \l Page).
163 */
164 property bool opened: false
165
166 /*!
167 The toolbar cannot be opened/closed by bottom-edge swipes.
168 If the ToolbarItems contains no visible Items, it is automatically
169 locked (in closed state).
170 */
171 property bool locked: !internal.hasVisibleItems()
172
173 QtObject {
174 id: internal
175 /*
176 Determine whether this ToolbarItems has any visible Items
177 */
178 function hasVisibleItems() {
179 if (back && back.visible) return true;
180 for (var i=0; i < toolsContainer.children.length; i++) {
181 if (toolsContainer.children[i].visible) return true;
182 }
183 return false;
184 }
185 }
186
187 Item {
188 id: backContainer
189 anchors {
190 left: parent.left
191 top: parent.top
192 bottom: parent.bottom
193 leftMargin: units.gu(2)
194 }
195 width: childrenRect.width
196
197 // internal link to the previous back Item to unset its parent
198 // when toolbarItems.back is updated.
199 property Item previousBackItem: null
200
201 function updateBackItem() {
202 if (backContainer.previousBackItem) backContainer.previousBackItem.parent = null;
203 backContainer.previousBackItem = toolbarItems.back;
204 if (toolbarItems.back) toolbarItems.back.parent = backContainer;
205 }
206
207 Connections {
208 target: toolbarItems
209 function onBackChanged() { backContainer.updateBackItem() }
210 Component.onCompleted: backContainer.updateBackItem()
211 }
212 }
213
214 Row {
215 id: toolsContainer
216 anchors {
217 right: parent.right
218 bottom: parent.bottom
219 top: parent.top
220 rightMargin: units.gu(2)
221 }
222 spacing: units.gu(1)
223 }
224 }