Debian Codegraph @

src/imports/Components/1.2/ComboButton.qml

raw
1 /*
2 * Copyright 2014 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.Popups 1.0
19 import Lomiri.Components 1.2 as Toolkit
20
21 /*!
22 \qmltype ComboButton
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri
25 \brief Lomiri button providing a drop-down panel visualizing custom options.
26
27 The component is composed of three main blocks: \b {main button}, \b {dropdown}
28 and \b {combo list}.
29
30 The \a {main button} holds the main functionailty of the component, and it is
31 located at the left-top side of the expanded button. The \a clicked() signal
32 is triggered only when this button is pressed.
33
34 The \a {dropdown} is a button located on the right of the main button. Its
35 functionality is to drive the component's expanded state.
36
37 The \a {combo list} is a panel showing the content specified in \l comboList
38 property when expanded. The content is stretched horizontally to the component's
39 width, and its height is controlled by the \l expandedHeight property as follows:
40 \list
41 \li If the content height is smaller than the value of \l expandedHeight, the combo
42 list will be expanded only to the height of the content.
43 \qml
44 import QtQuick 2.4
45 import Lomiri.Components 1.2
46 ComboButton {
47 text: "smaller content"
48 Rectangle {
49 height: units.gu(5) // smaller than the default expandedHeight
50 color: "blue"
51 }
52 }
53 \endqml
54 \li If the content height is greater than \l expandedHeight, the combo list will
55 expand till the height specified by the property and the content will be
56 scrolled; in case the combo list content is one single Flickable, ListView,
57 GridView or PathView, the content scrolling will be provided by the content
58 itself.
59 \qml
60 import QtQuick 2.4
61 import Lomiri.Components 1.2
62 ComboButton {
63 text: "long scrolled content"
64 Column {
65 Repeater {
66 model: 5
67 spacing: units.gu(1)
68 Rectangle {
69 height: units.gu(5)
70 color: "blue"
71 }
72 }
73 }
74 }
75 \endqml
76 \li In case the content is a single Flickable, ListView, GridView or PathView,
77 the content will be filling the entire combo list area defined.
78 \qml
79 import QtQuick 2.4
80 import Lomiri.Components 1.2
81 import Lomiri.Components.ListItems 1.0
82 ComboButton {
83 text: "listview"
84 ListView {
85 model: 10
86 delegate: Standard {
87 text: "Item #" + modelData
88 }
89 }
90 }
91 \endqml
92 \li Vertical anchoring of combo list content to its parent is not possible
93 as the expansion calculation is done based on the combo list content height.
94 If the content wants to take the size of the entire combo list, it should
95 bind its height to the \l comboListHeight property.
96 \qml
97 import QtQuick 2.4
98 import Lomiri.Components 1.2
99 ComboButton {
100 id: combo
101 text: "smaller content"
102 Rectangle {
103 height: combo.comboListHeight
104 color: "blue"
105 }
106 }
107 \endqml
108 \li In case the expansion needs to be the size of the combo list content, the \l
109 expandedHeight should be set to -1.
110 \qml
111 import QtQuick 2.4
112 import Lomiri.Components 1.2
113 ComboButton {
114 text: "auto-sized content"
115 expandedHeight: -1
116 Column {
117 Repeater {
118 model: 5
119 spacing: units.gu(1)
120 Button {
121 text: "Button #" + modelData
122 }
123 }
124 }
125 }
126 \endqml
127 \endlist
128
129 The combo list can be expanded/collapsed either through the \l expanded property
130 or by clicking on the dropdown button. It is not collapsed when pressing the main
131 button or clicking on the combo list. In order to do an auto-collapsing button
132 you must reset the expanded property (set it to false) when the main button is
133 clicked or when a selection is taken from the combo list content. The following
134 example illustrates a possible implementation.
135
136 \qml
137 import QtQuick 2.4
138 import Lomiri.Components 1.2
139
140 ComboButton {
141 id: combo
142 text: "Auto closing"
143 expanded: true
144 expandedHeight: units.gu(30)
145 onClicked: expanded = false
146 LomiriListView {
147 width: parent.width
148 height: combo.comboListHeight
149 model: 20
150 delegate: Standard {
151 text: "Action #" + modelData
152 onClicked: {
153 combo.text = text;
154 combo.expanded = false;
155 }
156 }
157 }
158 }
159 \endqml
160
161 \section2 Styling
162 The style of the component is defined in \l ComboButtonStyle.
163 */
164 Toolkit.Button {
165 id: combo
166
167 /*!
168 Specifies whether the combo list is expanded or not. The default falue is
169 false.
170 */
171 property bool expanded: false
172
173 /*!
174 The property holds the height of the component when collapsed. By default
175 the value is the implicit height of the component.
176 */
177 property real collapsedHeight: implicitHeight
178
179 /*!
180 The property holds the maximum height value the component should expand.
181 When setting the property, remember to take into account the \l collapsedHeight
182 value. The best practice is to use bind it with collapsedHeight.
183 \qml
184 ComboButton {
185 text: "altered expandedHeight"
186 expandedHeight: collapsedHeight + units.gu(25)
187 }
188 \endqml
189
190 A value of -1 will instruct the component to expand the combo list as
191 much as its content height is.
192
193 The default value is \l collapsedHeight + 19.5 GU, so at least 3 ListItems
194 can fit in the combo list.
195 \sa collapsedHeight
196 */
197 property real expandedHeight: collapsedHeight + units.gu(19.5)
198
199 /*!
200 The property holds the maximum combo list height allowed based on the
201 \l expandedHeight and \l collapsedHeight values. It is a convenience property
202 that can be used to size the combo list content.
203 \qml
204 import QtQuick 2.4
205 import Lomiri.Components 1.2
206 import Lomiri.Components.ListItems 0.1
207 ComboButton {
208 id: combo
209 text: "Full comboList size"
210 ListView {
211 anchors {
212 left: parent.left
213 right: parent.right
214 }
215 height: combo.comboListHeight
216 model: 20
217 delegate: Standard {
218 text: "Action #" + modelData
219 }
220 }
221 }
222 \endqml
223
224 \sa collapsedHeight, expandedHeight
225 */
226 readonly property real comboListHeight: (expandedHeight < 0) ?
227 comboListHolder.height :
228 (expandedHeight - collapsedHeight -
229 (combo.__styleInstance ? combo.__styleInstance.comboListMargin : 0))
230
231 /*!
232 \qmlproperty list<Item> comboList
233 \default
234 Property holding the list of items to be shown in the combo list. Being
235 a default property children items declared will land in the combo list.
236 \note The component is not responsible for layouting the content. It only
237 provides scrolling abilities for the case the content exceeds the defined
238 expanded height.
239 */
240 default property alias comboList: comboListHolder.data
241
242 /* ----------------- Color and font configurations ----------------- */
243 /*!
244 The property specifies the color of the dropdown button and the combo list
245 for both collapsed and expanded states. You can use \l expanded to define
246 different colors for expanded or collapsed states.
247 */
248 property color dropdownColor: __styleInstance ? __styleInstance.defaultDropdownColor : color
249
250
251 style: Theme.createStyleComponent("ComboButtonStyle.qml", combo)
252
253 Component.onCompleted: {
254 // update mouse area to report clicks only on the main button area
255 // area excluding dropDown button and combo list
256 // we must do separate bindings as __mouseArea is a read-only property
257 __mouseArea.anchors.fill = undefined;
258 __mouseArea.anchors.left = Qt.binding(function() {return combo.left;});
259 __mouseArea.anchors.top = Qt.binding(function() {return combo.top;});
260 __mouseArea.anchors.right = Qt.binding(function() {return combo.right;});
261 __mouseArea.anchors.rightMargin = Qt.binding(function() {return combo.__styleInstance.dropDownWidth + combo.__styleInstance.dropDownSeparatorWidth;});
262 __mouseArea.height = Qt.binding(function() {return collapsedHeight;});
263 // for autopilot, set the main button name
264 __mouseArea.objectName = "combobutton_mainbutton";
265
266 // bind a height calculation to avoid unwanted change
267 combo.height = Qt.binding(function() {return collapsedHeight + __styleInstance.comboListPanel.height});
268 }
269
270 // dropdown button
271 AbstractButton {
272 id: dropDown
273 objectName: "combobutton_dropdown"
274 anchors {
275 right: parent.right
276 top: parent.top
277 }
278 width: combo.__styleInstance ? combo.__styleInstance.dropDownWidth : 0
279 height: combo.collapsedHeight
280 // open dropdown when pressed, not when clicked
281 onClicked: {
282 // toggle expanded
283 combo.expanded = !combo.expanded;
284 }
285 }
286
287 // expansion list
288 Flickable {
289 id: comboHolder
290 objectName: "combobutton_contentflicker"
291 parent: combo.__styleInstance ? combo.__styleInstance.comboListHolder : combo
292 anchors.fill: parent
293 interactive: combo.expanded && !contentIsFlickable() && (combo.expandedHeight > 0)
294 flickableDirection: Flickable.VerticalFlick
295 contentHeight: comboListHolder.height
296
297 // consider PathView as Flickable in this case as well!
298 function contentIsFlickable() {
299 return (comboListHolder.children.length === 1) &&
300 comboListHolder.children[0].hasOwnProperty("flicking");
301 }
302
303 Item {
304 id: comboListHolder
305 objectName: "combobutton_combolist"
306 anchors {
307 left: parent.left
308 right: parent.right
309 }
310
311 // stretch children width to holder's width
312 // must do binding to height manually to avoid binding loops caused
313 // by the vertical stretching when the component is a single flickable
314 property bool stretch: (combo.expandedHeight > 0 && comboHolder.contentIsFlickable())
315 onStretchChanged: stretchChildren()
316 onChildrenChanged: stretchChildren()
317 function stretchChildren() {
318 for (var i in comboListHolder.children) {
319 var child = comboListHolder.children[i];
320 if (comboListHolder.stretch) {
321 child.anchors.left = undefined;
322 child.anchors.right = undefined;
323 child.anchors.fill = comboListHolder;
324 comboListHolder.height = Qt.binding(function() {
325 return combo.comboListHeight;
326 });
327 } else {
328 child.anchors.left = comboListHolder.left;
329 child.anchors.right = comboListHolder.right;
330 comboListHolder.height = Qt.binding(function() {
331 return comboListHolder.childrenRect.height;
332 });
333 }
334 }
335 }
336 }
337 }
338 Scrollbar {
339 flickableItem: comboHolder
340 }
341
342 /*
343 Drive the expansion height of the combo list holder
344 This is a common functionaity in all combo buttons, therefore we can have it
345 in the main component.
346 */
347 Binding {
348 target: combo.__styleInstance.comboListPanel
349 property: "height"
350 value: {
351 if (!expanded) {
352 return 0;
353 }
354 if (comboListHolder.stretch) {
355 return combo.comboListHeight + combo.__styleInstance.comboListMargin;
356 }
357
358 var h = comboListHolder.height;
359 var max = combo.comboListHeight + combo.__styleInstance.comboListMargin;
360 if (combo.expandedHeight < 0) {
361 return max;
362 }
363
364 return Toolkit.MathUtils.clamp(h, 0, max);
365 }
366 }
367 }