MauiKit Controls

ItemDelegate.qml
1/*
2 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20import QtQuick
21import QtQuick.Controls
22
23import org.mauikit.controls as Maui
24
25/**
26 * @inherit QtQuick.Controls.Control
27 * @brief ItemDelegate is the base for the MauiKit delegate controls.
28 * It is radically different from QQC2 ItemDelegate.
29 *
30 * <a href="https://doc.qt.io/qt-6/qml-qtquick-controls-control.html">This controls inherits from QQC2 Control, to checkout its inherited properties refer to the Qt Docs.</a>
31 *
32 * This is just a container with some predefined features to allow items using it to be drag and drop, and allow long-press selection to trigger contextual actions.
33 *
34 * Some of the controls that inherit from this are the GridBrowserDelegate adn ListBrowserDelegate.
35 *
36 * @section features Features
37 *
38 * Setting up the drag and drop feature requires a few lines. To start you need to set `draggable: true`, and after that set up what the drag data will contain.:
39 *
40 * @code
41 * ItemDelegate
42 * {
43 * draggable: true
44 *
45 * Drag.keys: ["text/uri-list"]
46 * Drag.mimeData: { "text/uri-list": "file:/path/one.txt,file:/path/two.txt,file:/path/three.txt" } //a dummy example of three paths in a single string separated by comma.
47 * }
48 * @endcode
49 *
50 * Another feature is to react to a long-press - to emulate a "right-click" - sent by a touch gesture in a mobile device, where it could mean a request to launch some contextual action menu for the item.
51 *
52 * @attention The long-press can either launch the drag-and-drop (DnD) or the contextual request via the `pressAndHold` signal. If after a long press the item is dragged while maintaining the pressed it will launch the DnD action, but if the long-press is released then it will launch the signal `pressAndHold`.
53 * @see pressAndHold
54 */
55Control
56{
57 id: control
58
59 hoverEnabled: !Maui.Handy.isMobile
60
61 padding: 0
62
63 focus: true
64
65 ToolTip.delay: 1000
66 ToolTip.timeout: 5000
67 ToolTip.visible: control.hovered && control.tooltipText
68 ToolTip.text: control.tooltipText
69
70 /**
71 * @brief The text used for the tool-tip, revealed when the item is hovered with the mouse cursor.
72 * This type of text usually presents to the user with some extra information about the item.
73 */
74 property string tooltipText
75
76
77 /**
78 * @brief The children items of this item will be place by default inside an Item.
79 * Ideally there is only one single child element. The children elements need to be positioned manually, using either anchors or coordinates and explicit sizes.
80 * @code
81 * Maui.ItemDelegate
82 * {
83 * Rectangle //the single child element
84 * {
85 * anchors.fill: parent
86 * color: "pink"
87 * }
88 * }
89 * @endcode
90 *
91 * @property list<QtObject> ItemDelegate::content
92 */
93 default property alias content : _content.data
94
95 /**
96 * @brief An alias to the MouseArea handling the press events.
97 * @note See Qt documentation on the MouseArea for more information.
98 * @property MouseArea ItemDelegate::mouseArea
99 */
100 readonly property alias mouseArea : _mouseArea
101
102 /**
103 * @brief Whether the item should respond to a drag event after have been pressed for a long time.
104 * If this is set to `true`, after the long press and a drag movement, the item contain will be captured as the Drag image source. And the drag target will be set to enable dropping the element.
105 * By default this is set to `false`.
106 */
107 property bool draggable: false
109 /**
110 * @brief Whether the item should be styled in a "selected/checked" state.
111 * This is an alias to the `highlighted` property.
112 * @property bool ItemDelegate::isCurrentItem
113 */
114 property alias isCurrentItem : control.highlighted
115
116 /**
117 * @brief Whether the item is currently being pressed.
118 * This is an alias to `mouseArea.containsPress` property.
119 * @property bool ItemDelegate::containsPress
120 */
121 property alias containsPress: _mouseArea.containsPress
122
123 /**
124 * @brief Whether the item is being highlighted. A visual clue will be use to indicate the highlighted state.
125 * By default this is set to `false`.
126 */
127 property bool highlighted: false
128
129 /**
130 * @brief The border radius of the background.
131 * @By default this is set to `Style.radiusV`, which picks the system preference for the radius of rounded elements corners.
132 */
133 property int radius: Maui.Style.radiusV
135 /**
136 * @brief Whether the item should be styled as a flat element. A flat element usually does not have any selected state or background.
137 * By default this property is set to `!Handy.isMobile"
138 * @see Handy::isMobile
139 */
140 property bool flat : !Maui.Handy.isMobile
141
142 /**
143 * @brief Emitted when the item has been pressed.
144 * @param mouse The object with the event information.
145 */
146 signal pressed(var mouse)
147
148 /**
149 * @brief Emitted when the item has been pressed and hold for a few seconds.
150 * @param mouse The object with the event information.
151 */
152 signal pressAndHold(var mouse)
153
154 /**
155 * @brief Emitted when the item has been clicked - this means that the item has been pressed and then released.
156 * @param mouse The object with the event information.
157 */
158 signal clicked(var mouse)
159
160 /**
161 * @brief Emitted when the item has been right clicked. Usually with a mouse device.
162 * @param mouse The object with the event information.
163 */
164 signal rightClicked(var mouse)
165
166 /**
167 * @brief Emitted when the item has been double clicked in a short period of time.
168 * @param mouse The object with the event information.
169 */
170 signal doubleClicked(var mouse)
171
172 Drag.active: mouseArea.drag.active && control.draggable
173 Drag.dragType: Drag.Automatic
174 // Drag.supportedActions: Qt.MoveAction
175 Drag.hotSpot.x: control.width / 2
176 Drag.hotSpot.y: control.height / 2
177
178
179 contentItem: Item
180 {
181 id: _content
182
183 SequentialAnimation on scale
184 {
185 id: xAnim
186 // Animations on properties start running by default
187 running: false
188 loops: 3
189 NumberAnimation { from: 1; to: 0.97; duration: 200; easing.type: Easing.InBack }
190 NumberAnimation { from: 0.97; to: 1; duration: 200; easing.type: Easing.InBack }
191 PauseAnimation { duration: 50 } // This puts a bit of time between the loop
192 }
193
194 // DragHandler
195 // {
196 // target: control
197 // dragThreshold: 100
198 // onActiveChanged:
199 // if (active) {
200 // control.grabToImage(function(result) {
201 // control.Drag.imageSource = result.url
202 // control.Drag.active = true
203 // })
204 // } else {
205 // control.Drag.active = false
206 // }
207 // }
208
209 MouseArea
210 {
211 id: _mouseArea
212 anchors.fill: parent
213
214 propagateComposedEvents: false
215 acceptedButtons: Qt.RightButton | Qt.LeftButton
216 // drag.filterChildren: true
217 drag.threshold: 100
218 drag.target: this
219 property bool pressAndHoldIgnored : false
220
221 onClicked: (mouse) =>
222 {
223 if(mouse.button === Qt.RightButton)
224 {
225 control.rightClicked(mouse)
226 }
227 else
228 {
229 control.clicked(mouse)
230 }
231 }
232
233 onDoubleClicked: (mouse) =>
234 {
235 control.doubleClicked(mouse)
236 }
237
238 onPressed: (mouse) =>
239 {
240 if(control.draggable && mouse.source !== Qt.MouseEventSynthesizedByQt)
241 {
242 drag.target = _mouseArea
243 control.grabToImage(function(result)
244 {
245 control.Drag.imageSource = result.url
246 })
247 }else
248 {
249 drag.target = null
250 }
251 //
252 _mouseArea.pressAndHoldIgnored = false
253 control.pressed(mouse)
254 }
255
256 onReleased : (mouse) =>
257 {
258 // _content.x = 0
259 // _content.y = 0
260 // if(control.draggable)
261 // {
262 // drag.target = null
263 // }
264 console.log("DROPPING DRAG", _mouseArea.pressAndHoldIgnored)
265
266 if(_mouseArea.pressAndHoldIgnored)
267 {
268 control.pressAndHold(mouse)
269 _mouseArea.pressAndHoldIgnored = false
270 }
271 }
272
273 onPressAndHold : (mouse) =>
274 {
275 xAnim.running = control.draggable || mouse.source === Qt.MouseEventSynthesizedByQt
276
277 _mouseArea.pressAndHoldIgnored = true
278
279 if(control.draggable && mouse.source === Qt.MouseEventSynthesizedByQt)
280 {
281 drag.target = _mouseArea
282 console.log("GETTING DRAG", _mouseArea.pressAndHoldIgnored)
283 control.grabToImage(function(result)
284 {
285 control.Drag.imageSource = result.url
286 })
287
288 }else
289 {
290 drag.target = null
291 control.pressAndHold(mouse)
292 }
293 }
294
295 onPositionChanged: (mouse) =>
296 {
297 // if(control.draggable)
298 // {
299 // console.log("MOVING DRAG", _mouseArea.pressAndHoldIgnored)
300 // _mouseArea.pressAndHoldIgnored = false
301 // mouse.accepted = true
302 // }
303 }
304
305
306 }
307 }
308
309 background: Rectangle
310 {
311 color: control.isCurrentItem || control.containsPress ? Maui.Theme.highlightColor : ( control.hovered ? Maui.Theme.hoverColor : "transparent")
312
313 radius: control.radius
314 }
315}
316
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 14 2025 11:53:05 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.