From 7e5ecfe8bcdf138292572a07386acbff1d491ffc Mon Sep 17 00:00:00 2001 From: Samuel Ghinet Date: Thu, 1 Dec 2022 22:05:55 +0200 Subject: [PATCH] Fix glitch in AssetsView when dragging files onto it Moved the DropArea from the AssetDelegate into the AssetsView, so that moving the cursor through the delegates no longer denies and then permits dragging. This glitch happened because the DropArea was inside the delegate, while the TreeView also has rowSpacing, which are areas that do not belong to the delegates, and for which you don't normally have drag & drop support. Task-number: QDS-8232 Change-Id: If49a384f25bb870105448156f436e048479e880c Reviewed-by: Miikka Heikkinen Reviewed-by: Thomas Hartmann --- .../itemLibraryQmlSources/AssetDelegate.qml | 46 +++------- .../itemLibraryQmlSources/AssetsView.qml | 88 ++++++++++++++++++- 2 files changed, 96 insertions(+), 38 deletions(-) diff --git a/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetDelegate.qml b/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetDelegate.qml index a28becff06b..7bfec10aa32 100644 --- a/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetDelegate.qml +++ b/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetDelegate.qml @@ -12,7 +12,7 @@ TreeViewDelegate { required property Item assetsRoot property bool hasChildWithDropHover: false - property bool isHoveringDrop: false + property bool isHighlighted: false readonly property string suffix: model.fileName.substr(-4) readonly property bool isFont: root.suffix === ".ttf" || root.suffix === ".otf" readonly property bool isEffect: root.suffix === ".qep" @@ -74,7 +74,7 @@ TreeViewDelegate { id: bg color: { - if (root.__isDirectory && (root.isHoveringDrop || root.hasChildWithDropHover)) + if (root.__isDirectory && (root.isHighlighted || root.hasChildWithDropHover)) return StudioTheme.Values.themeInteraction if (!root.__isDirectory && root.assetsView.selectedAssets[root.__itemPath]) @@ -120,40 +120,6 @@ TreeViewDelegate { } } - DropArea { - id: treeDropArea - - enabled: true - anchors.fill: parent - - onEntered: (drag) => { - root.assetsRoot.updateDropExtFiles(drag) - root.isHoveringDrop = drag.accepted && root.assetsRoot.dropSimpleExtFiles.length > 0 - if (root.isHoveringDrop) - root.assetsView.startDropHoverOver(root.__currentRow) - } - - onDropped: (drag) => { - root.isHoveringDrop = false - root.assetsView.endDropHover(root.__currentRow) - - let dirPath = root.__isDirectory - ? model.filePath - : assetsModel.parentDirPath(model.filePath); - - rootView.emitExtFilesDrop(root.assetsRoot.dropSimpleExtFiles, - root.assetsRoot.dropComplexExtFiles, - dirPath) - } - - onExited: { - if (root.isHoveringDrop) { - root.isHoveringDrop = false - root.assetsView.endDropHover(root.__currentRow) - } - } - } - MouseArea { id: mouseArea @@ -247,6 +213,14 @@ TreeViewDelegate { } } // MouseArea + function getDirPath() + { + if (root.__isDirectory) + return model.filePath + else + return assetsModel.parentDirPath(model.filePath) + } + function __openContextMenuForCurrentRow() { let modelIndex = assetsModel.indexForPath(model.filePath) diff --git a/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsView.qml b/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsView.qml index 782cca5ebc6..dcffab432be 100644 --- a/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsView.qml +++ b/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsView.qml @@ -250,8 +250,12 @@ TreeView { function startDropHoverOver(row) { let index = root.__modelIndex(row) - if (assetsModel.isDirectory(index)) + if (assetsModel.isDirectory(index)) { + let item = root.__getDelegateItemForIndex(index) + if (item) + item.isHighlighted = true return + } let parentItem = root.__getDelegateParentForIndex(index) if (parentItem) @@ -261,8 +265,12 @@ TreeView { function endDropHover(row) { let index = root.__modelIndex(row) - if (assetsModel.isDirectory(index)) + if (assetsModel.isDirectory(index)) { + let item = root.__getDelegateItemForIndex(index) + if (item) + item.isHighlighted = false return + } let parentItem = root.__getDelegateParentForIndex(index) if (parentItem) @@ -292,6 +300,12 @@ TreeView { return root.itemAtCell(parentCell) } + function __getDelegateItemForIndex(index) + { + let cell = root.cellAtIndex(index) + return root.itemAtCell(cell) + } + function __modelIndex(row) { // The modelIndex() function exists since 6.3. In Qt 6.3, this modelIndex() function was a @@ -303,6 +317,76 @@ TreeView { return root.modelIndex(row, 0) } + DropArea { + id: dropArea + enabled: true + anchors.fill: parent + + property bool __isHoveringDrop: false + property int __rowHoveringOver: -1 + + function __rowAndItem(drag) + { + let pos = dropArea.mapToItem(root, drag.x, drag.y) + let cell = root.cellAtPos(pos.x, pos.y, true) + let item = root.itemAtCell(cell) + + return [cell.y, item] + } + + onEntered: (drag) => { + root.assetsRoot.updateDropExtFiles(drag) + + let [row, item] = dropArea.__rowAndItem(drag) + dropArea.__isHoveringDrop = drag.accepted && root.assetsRoot.dropSimpleExtFiles.length > 0 + + if (item && dropArea.__isHoveringDrop) + root.startDropHoverOver(row) + + dropArea.__rowHoveringOver = row + } + + onDropped: (drag) => { + let [row, item] = dropArea.__rowAndItem(drag) + + if (item) { + root.endDropHover(row) + + let dirPath = item.getDirPath() + + rootView.emitExtFilesDrop(root.assetsRoot.dropSimpleExtFiles, + root.assetsRoot.dropComplexExtFiles, + dirPath) + } + + dropArea.__isHoveringDrop = false + dropArea.__rowHoveringOver = -1 + } + + onPositionChanged: (drag) => { + let [row, item] = dropArea.__rowAndItem(drag) + + if (dropArea.__rowHoveringOver !== row && dropArea.__rowHoveringOver > -1) { + root.endDropHover(dropArea.__rowHoveringOver) + + if (item) + root.startDropHoverOver(row) + } + + dropArea.__rowHoveringOver = row + } + + onExited: { + if (!dropArea.__isHoveringDrop || dropArea.__rowHoveringOver === -1) + return + + root.endDropHover(dropArea.__rowHoveringOver) + + dropArea.__isHoveringDrop = false + dropArea.__rowHoveringOver = -1 + } + } + delegate: AssetDelegate { assetsView: root assetsRoot: root.assetsRoot