Warning, /maui/mauikit/src/controls.6/ItemDelegate.qml is written in an unsupported language. File is not indexed.
0001 /*
0002 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
0003 *
0004 * This program is free software; you can redistribute it and/or modify
0005 * it under the terms of the GNU Library General Public License as
0006 * published by the Free Software Foundation; either version 2, or
0007 * (at your option) any later version.
0008 *
0009 * This program is distributed in the hope that it will be useful,
0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012 * GNU General Public License for more details
0013 *
0014 * You should have received a copy of the GNU Library General Public
0015 * License along with this program; if not, write to the
0016 * Free Software Foundation, Inc.,
0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018 */
0019
0020 import QtQuick
0021 import QtQuick.Controls
0022
0023 import org.mauikit.controls 1.3 as Maui
0024
0025 /**
0026 * @inherit QtQuick.Controls.Control
0027 * @brief ItemDelegate is the base for the MauiKit delegate controls.
0028 * It is radically different from QQC2 ItemDelegate.
0029 *
0030 * <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>
0031 *
0032 * 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.
0033 *
0034 * Some of the controls that inherit from this are the GridBrowserDelegate adn ListBrowserDelegate.
0035 *
0036 * @section features Features
0037 *
0038 * 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.:
0039 *
0040 * @code
0041 * ItemDelegate
0042 * {
0043 * draggable: true
0044 *
0045 * Drag.keys: ["text/uri-list"]
0046 * 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.
0047 * }
0048 * @endcode
0049 *
0050 * 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.
0051 *
0052 * @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`.
0053 * @see pressAndHold
0054 */
0055 Control
0056 {
0057 id: control
0058
0059 hoverEnabled: !Maui.Handy.isMobile
0060
0061 padding: 0
0062
0063 focus: true
0064
0065 ToolTip.delay: 1000
0066 ToolTip.timeout: 5000
0067 ToolTip.visible: control.hovered && control.tooltipText
0068 ToolTip.text: control.tooltipText
0069
0070 /**
0071 * @brief The text used for the tool-tip, revealed when the item is hovered with the mouse cursor.
0072 * This type of text usually presents to the user with some extra information about the item.
0073 */
0074 property string tooltipText
0075
0076
0077 /**
0078 * @brief The children items of this item will be place by default inside an Item.
0079 * Ideally there is only one single child element. The children elements need to be positioned manually, using either anchors or coordinates and explicit sizes.
0080 * @code
0081 * Maui.ItemDelegate
0082 * {
0083 * Rectangle //the single child element
0084 * {
0085 * anchors.fill: parent
0086 * color: "pink"
0087 * }
0088 * }
0089 * @endcode
0090 *
0091 * @property list<QtObject> ItemDelegate::content
0092 */
0093 default property alias content : _content.data
0094
0095 /**
0096 * @brief An alias to the MouseArea handling the press events.
0097 * @note See Qt documentation on the MouseArea for more information.
0098 * @property MouseArea ItemDelegate::mouseArea
0099 */
0100 readonly property alias mouseArea : _mouseArea
0101
0102 /**
0103 * @brief Whether the item should respond to a drag event after have been pressed for a long time.
0104 * 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.
0105 * By default this is set to `false`.
0106 */
0107 property bool draggable: false
0108
0109 /**
0110 * @brief Whether the item should be styled in a "selected/checked" state.
0111 * This is an alias to the `highlighted` property.
0112 * @property bool ItemDelegate::isCurrentItem
0113 */
0114 property alias isCurrentItem : control.highlighted
0115
0116 /**
0117 * @brief Whether the item is currently being pressed.
0118 * This is an alias to `mouseArea.containsPress` property.
0119 * @property bool ItemDelegate::containsPress
0120 */
0121 property alias containsPress: _mouseArea.containsPress
0122
0123 /**
0124 * @brief Whether the item is being highlighted. A visual clue will be use to indicate the highlighted state.
0125 * By default this is set to `false`.
0126 */
0127 property bool highlighted: false
0128
0129 /**
0130 * @brief The border radius of the background.
0131 * @By default this is set to `Style.radiusV`, which picks the system preference for the radius of rounded elements corners.
0132 */
0133 property int radius: Maui.Style.radiusV
0134
0135 /**
0136 * @brief Whether the item should be styled as a flat element. A flat element usually does not have any selected state or background.
0137 * By default this property is set to `!Handy.isMobile"
0138 * @see Handy::isMobile
0139 */
0140 property bool flat : !Maui.Handy.isMobile
0141
0142 /**
0143 * @brief Emitted when the item has been pressed.
0144 * @param mouse The object with the event information.
0145 */
0146 signal pressed(var mouse)
0147
0148 /**
0149 * @brief Emitted when the item has been pressed and hold for a few seconds.
0150 * @param mouse The object with the event information.
0151 */
0152 signal pressAndHold(var mouse)
0153
0154 /**
0155 * @brief Emitted when the item has been clicked - this means that the item has been pressed and then released.
0156 * @param mouse The object with the event information.
0157 */
0158 signal clicked(var mouse)
0159
0160 /**
0161 * @brief Emitted when the item has been right clicked. Usually with a mouse device.
0162 * @param mouse The object with the event information.
0163 */
0164 signal rightClicked(var mouse)
0165
0166 /**
0167 * @brief Emitted when the item has been double clicked in a short period of time.
0168 * @param mouse The object with the event information.
0169 */
0170 signal doubleClicked(var mouse)
0171
0172 Drag.active: mouseArea.drag.active && control.draggable
0173 Drag.dragType: Drag.Automatic
0174 // Drag.supportedActions: Qt.MoveAction
0175 Drag.hotSpot.x: control.width / 2
0176 Drag.hotSpot.y: control.height / 2
0177
0178 contentItem: MouseArea
0179 {
0180 id: _mouseArea
0181
0182 propagateComposedEvents: false
0183 acceptedButtons: Qt.RightButton | Qt.LeftButton
0184
0185 property bool pressAndHoldIgnored : false
0186
0187 onClicked: (mouse) =>
0188 {
0189 if(mouse.button === Qt.RightButton)
0190 {
0191 control.rightClicked(mouse)
0192 }
0193 else
0194 {
0195 control.clicked(mouse)
0196 }
0197 }
0198
0199 onDoubleClicked: (mouse) =>
0200 {
0201 control.doubleClicked(mouse)
0202 }
0203
0204 onPressed: (mouse) =>
0205 {
0206 if(control.draggable && mouse.source !== Qt.MouseEventSynthesizedByQt)
0207 {
0208 drag.target = _content
0209 control.grabToImage(function(result)
0210 {
0211 control.Drag.imageSource = result.url
0212 })
0213 }else
0214 {
0215 // drag.target = null
0216 }
0217 //
0218 _mouseArea.pressAndHoldIgnored = false
0219 control.pressed(mouse)
0220 }
0221
0222 onReleased : (mouse) =>
0223 {
0224 _content.x = 0
0225 _content.y = 0
0226 // if(control.draggable)
0227 // {
0228 // drag.target = null
0229 // }
0230 console.log("DROPPING DRAG", _mouseArea.pressAndHoldIgnored)
0231
0232 if(!mouseArea.drag.active && _mouseArea.pressAndHoldIgnored)
0233 {
0234 control.pressAndHold(mouse)
0235 _mouseArea.pressAndHoldIgnored = false
0236 }
0237 }
0238
0239 onPressAndHold : (mouse) =>
0240 {
0241 xAnim.running = control.draggable || mouse.source === Qt.MouseEventSynthesizedByQt
0242
0243 _mouseArea.pressAndHoldIgnored = true
0244
0245 if(control.draggable && mouse.source === Qt.MouseEventSynthesizedByQt)
0246 {
0247 drag.target = _content
0248 console.log("GETTING DRAG", _mouseArea.pressAndHoldIgnored)
0249 control.grabToImage(function(result)
0250 {
0251 control.Drag.imageSource = result.url
0252 })
0253
0254 }else
0255 {
0256 // drag.target = null
0257 control.pressAndHold(mouse)
0258 }
0259 }
0260
0261 onPositionChanged: (mouse) =>
0262 {
0263 if(control.draggable)
0264 {
0265 console.log("MOVING DRAG", _mouseArea.pressAndHoldIgnored)
0266 _mouseArea.pressAndHoldIgnored = false
0267 mouse.accepted = true
0268 }
0269 }
0270
0271 Item
0272 {
0273 id: _content
0274
0275 height: parent.height
0276 width: parent.width
0277
0278 SequentialAnimation on y
0279 {
0280 id: xAnim
0281 // Animations on properties start running by default
0282 running: false
0283 loops: 3
0284 NumberAnimation { from: 0; to: -10; duration: 200; easing.type: Easing.InBack }
0285 NumberAnimation { from: -10; to: 0; duration: 200; easing.type: Easing.OutBack }
0286 PauseAnimation { duration: 50 } // This puts a bit of time between the loop
0287 }
0288 }
0289 }
0290
0291 background: Rectangle
0292 {
0293 color: control.isCurrentItem || control.containsPress ? Maui.Theme.highlightColor : ( control.hovered ? Maui.Theme.hoverColor : "transparent")
0294
0295 radius: control.radius
0296 }
0297 }
0298