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 1.3 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 contentItem: MouseArea
179 {
180 id: _mouseArea
181
182 propagateComposedEvents: false
183 acceptedButtons: Qt.RightButton | Qt.LeftButton
184
185 property bool pressAndHoldIgnored : false
186
187 onClicked: (mouse) =>
188 {
189 if(mouse.button === Qt.RightButton)
190 {
191 control.rightClicked(mouse)
192 }
193 else
194 {
195 control.clicked(mouse)
196 }
197 }
198
199 onDoubleClicked: (mouse) =>
200 {
201 control.doubleClicked(mouse)
202 }
203
204 onPressed: (mouse) =>
205 {
206 if(control.draggable && mouse.source !== Qt.MouseEventSynthesizedByQt)
207 {
208 drag.target = _content
209 control.grabToImage(function(result)
210 {
211 control.Drag.imageSource = result.url
212 })
213 }else
214 {
215 // drag.target = null
216 }
217 //
218 _mouseArea.pressAndHoldIgnored = false
219 control.pressed(mouse)
220 }
221
222 onReleased : (mouse) =>
223 {
224 _content.x = 0
225 _content.y = 0
226 // if(control.draggable)
227 // {
228 // drag.target = null
229 // }
230 console.log("DROPPING DRAG", _mouseArea.pressAndHoldIgnored)
231
232 if(!mouseArea.drag.active && _mouseArea.pressAndHoldIgnored)
233 {
234 control.pressAndHold(mouse)
235 _mouseArea.pressAndHoldIgnored = false
236 }
237 }
238
239 onPressAndHold : (mouse) =>
240 {
241 xAnim.running = control.draggable || mouse.source === Qt.MouseEventSynthesizedByQt
242
243 _mouseArea.pressAndHoldIgnored = true
244
245 if(control.draggable && mouse.source === Qt.MouseEventSynthesizedByQt)
246 {
247 drag.target = _content
248 console.log("GETTING DRAG", _mouseArea.pressAndHoldIgnored)
249 control.grabToImage(function(result)
250 {
251 control.Drag.imageSource = result.url
252 })
253
254 }else
255 {
256 // drag.target = null
257 control.pressAndHold(mouse)
258 }
259 }
260
261 onPositionChanged: (mouse) =>
262 {
263 if(control.draggable)
264 {
265 console.log("MOVING DRAG", _mouseArea.pressAndHoldIgnored)
266 _mouseArea.pressAndHoldIgnored = false
267 mouse.accepted = true
268 }
269 }
270
271 Item
272 {
273 id: _content
274
275 height: parent.height
276 width: parent.width
277
278 SequentialAnimation on y
279 {
280 id: xAnim
281 // Animations on properties start running by default
282 running: false
283 loops: 3
284 NumberAnimation { from: 0; to: -10; duration: 200; easing.type: Easing.InBack }
285 NumberAnimation { from: -10; to: 0; duration: 200; easing.type: Easing.OutBack }
286 PauseAnimation { duration: 50 } // This puts a bit of time between the loop
287 }
288 }
289 }
290
291 background: Rectangle
292 {
293 color: control.isCurrentItem || control.containsPress ? Maui.Theme.highlightColor : ( control.hovered ? Maui.Theme.hoverColor : "transparent")
294
295 radius: control.radius
296 }
297}
298
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.