Debian Codegraph @

src/imports/Components/1.3/ComboButton.qml

raw
1 /*
2 * Copyright 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 1.3
19 import Lomiri.Components.Popups 1.3
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.3
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.3
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.3
81 import Lomiri.Components.ListItems 1.3
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.3
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.3
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.3
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 AbstractButton {
165 id: combo
166
167 /*!
168 \deprecated
169 If set to a color, the button has a stroke border instead of a filled
170 shape.
171 */
172 property color strokeColor
173 onStrokeColorChanged: console.warn(
174 "WARNING: ComboButton.strokeColor is deprecated."
175 )
176
177 /*!
178 \deprecated
179 The background color of the button.
180 */
181 property color color
182 onColorChanged: console.warn("WARNING: ComboButton.color is deprecated.")
183
184 /*!
185 \deprecated
186 The gradient used to fill the background of the button.
187 Standard Lomiri gradients are defined in \l LomiriColors.
188 If both a gradient and a color are specified, the gradient will be used.
189 */
190 property Gradient gradient
191 onGradientChanged: console.warn(
192 "WARNING: ComboButton.gradient is deprecated."
193 )
194
195 /*!
196 \deprecated
197 The property specifies the color of the dropdown button and the combo
198 list for both collapsed and expanded states. You can use \l expanded to
199 define different colors for expanded or collapsed states.
200 */
201 property color dropdownColor
202 onDropdownColorChanged: console.warn(
203 "WARNING: ComboButton.dropdownColor is deprecated."
204 )
205
206 /*!
207 The font used for the button's text.
208 */
209 property font font: __styleInstance.defaultFont
210
211 /*!
212 The position of the icon relative to the text. Options
213 are "left" and "right". The default value is "left".
214
215 If only text or only an icon is defined, this
216 property is ignored and the text or icon is
217 centered horizontally and vertically in the button.
218
219 Currently this is a string value. We are waiting for
220 support for enums:
221 https://bugreports.qt-project.org/browse/QTBUG-14861
222 */
223 property string iconPosition: "left"
224
225 /*!
226 Specifies whether the combo list is expanded or not. The default falue is
227 false.
228 */
229 property bool expanded: false
230
231 /*!
232 The property holds the height of the component when collapsed. By default
233 the value is the implicit height of the component.
234 */
235 property real collapsedHeight: implicitHeight
236
237 /*!
238 The property holds the maximum height value the component should expand.
239 When setting the property, remember to take into account the \l collapsedHeight
240 value. The best practice is to use bind it with collapsedHeight.
241 \qml
242 ComboButton {
243 text: "altered expandedHeight"
244 expandedHeight: collapsedHeight + units.gu(25)
245 }
246 \endqml
247
248 A value of -1 will instruct the component to expand the combo list as
249 much as its content height is.
250
251 The default value is \l collapsedHeight + 19.5 GU, so at least 3 ListItems
252 can fit in the combo list.
253 \sa collapsedHeight
254 */
255 property real expandedHeight: collapsedHeight + units.gu(19.5)
256
257 /*!
258 The property holds the maximum combo list height allowed based on the
259 \l expandedHeight and \l collapsedHeight values. It is a convenience property
260 that can be used to size the combo list content.
261 \qml
262 import QtQuick 2.4
263 import Lomiri.Components 1.3
264 import Lomiri.Components.ListItems 1.3
265 ComboButton {
266 id: combo
267 text: "Full comboList size"
268 ListView {
269 anchors {
270 left: parent.left
271 right: parent.right
272 }
273 height: combo.comboListHeight
274 model: 20
275 delegate: Standard {
276 text: "Action #" + modelData
277 }
278 }
279 }
280 \endqml
281
282 \sa collapsedHeight, expandedHeight
283 */
284 readonly property real comboListHeight: (expandedHeight < 0) ?
285 comboListHolder.height :
286 (expandedHeight - collapsedHeight -
287 (combo.__styleInstance ? combo.__styleInstance.comboListMargin : 0))
288
289 /*!
290 \qmlproperty list<Item> comboList
291 \default
292 Property holding the list of items to be shown in the combo list. Being
293 a default property children items declared will land in the combo list.
294 \note The component is not responsible for layouting the content. It only
295 provides scrolling abilities for the case the content exceeds the defined
296 expanded height.
297 */
298 default property alias comboList: comboListHolder.data
299
300 styleName: "ComboButtonStyle"
301 activeFocusOnTab: true
302
303 // update sensing area to report clicks only on the main button area
304 // area excluding dropDown button and combo list
305 Component.onCompleted: {
306 __mouseArea.anchors.rightMargin = Qt.binding(function() {
307 return combo.__styleInstance.dropDownWidth;
308 });
309 __mouseArea.anchors.bottomMargin = Qt.binding(function () {
310 return combo.expanded ? (combo.height - combo.collapsedHeight) : 0;
311 });
312 }
313
314 sensingMargins {
315 bottom: -(combo.height - combo.collapsedHeight)
316 right: -combo.__styleInstance.dropDownWidth
317 }
318 __mouseArea.objectName: "combobutton_mainbutton"
319 height: collapsedHeight + __styleInstance.comboListPanel.height
320
321 // dropdown button
322 AbstractButton {
323 id: dropDown
324 objectName: "combobutton_dropdown"
325 anchors {
326 right: parent.right
327 top: parent.top
328 }
329 width: combo.__styleInstance ? combo.__styleInstance.dropDownWidth : 0
330 height: combo.collapsedHeight
331 // open dropdown when pressed, not when clicked
332 onClicked: {
333 // toggle expanded
334 combo.expanded = !combo.expanded;
335 combo.forceActiveFocus()
336 }
337 }
338
339 Keys.onReleased: {
340 if (event.key == Qt.Key_Space) {
341 // Space should expand, not trigger
342 event.accepted = true;
343 combo.expanded = !combo.expanded;
344 }
345 }
346
347 // expansion list
348 Flickable {
349 id: comboHolder
350 objectName: "combobutton_contentflicker"
351 parent: combo.__styleInstance ? combo.__styleInstance.comboListHolder : combo
352 anchors.fill: parent
353 interactive: combo.expanded && !contentIsFlickable() && (combo.expandedHeight > 0)
354 flickableDirection: Flickable.VerticalFlick
355 contentHeight: comboListHolder.height
356
357 // consider PathView as Flickable in this case as well!
358 function contentIsFlickable() {
359 return (comboListHolder.children.length === 1) &&
360 comboListHolder.children[0].hasOwnProperty("flicking");
361 }
362
363 Item {
364 id: comboListHolder
365 objectName: "combobutton_combolist"
366 anchors {
367 left: parent.left
368 right: parent.right
369 }
370
371 // stretch children width to holder's width
372 // must do binding to height manually to avoid binding loops caused
373 // by the vertical stretching when the component is a single flickable
374 property bool stretch: (combo.expandedHeight > 0 && comboHolder.contentIsFlickable())
375 onStretchChanged: stretchChildren()
376 onChildrenChanged: stretchChildren()
377 function stretchChildren() {
378 for (var i in comboListHolder.children) {
379 var child = comboListHolder.children[i];
380 if (comboListHolder.stretch) {
381 child.anchors.left = undefined;
382 child.anchors.right = undefined;
383 child.anchors.fill = comboListHolder;
384 comboListHolder.height = Qt.binding(function() {
385 return combo.comboListHeight;
386 });
387 } else {
388 child.anchors.left = comboListHolder.left;
389 child.anchors.right = comboListHolder.right;
390 comboListHolder.height = Qt.binding(function() {
391 return comboListHolder.childrenRect.height;
392 });
393 }
394 }
395 }
396 }
397 }
398 Scrollbar {
399 parent: combo.__styleInstance ? combo.__styleInstance.comboListHolder : combo
400 flickableItem: comboHolder
401 }
402
403 /*
404 Drive the expansion height of the combo list holder
405 This is a common functionaity in all combo buttons, therefore we can have it
406 in the main component.
407 */
408 Binding {
409 target: combo.__styleInstance.comboListPanel
410 property: "height"
411 value: {
412 if (!expanded) {
413 return 0;
414 }
415 if (comboListHolder.stretch) {
416 return combo.comboListHeight + combo.__styleInstance.comboListMargin;
417 }
418
419 var h = comboListHolder.height;
420 var max = combo.comboListHeight + combo.__styleInstance.comboListMargin;
421 if (combo.expandedHeight < 0) {
422 return max;
423 }
424
425 return MathUtils.clamp(h, 0, max);
426 }
427 }
428 }