From 021770a424a82d5199ce3dbedb215c81a51e1b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Candice=20Bent=C3=A9jac?= Date: Fri, 30 Sep 2022 15:43:42 +0200 Subject: [PATCH] [ui] Center pasted nodes in GraphEditor when it does not contain the mouse When using the Edit > Paste menu, or when pressing Ctrl+V while the Graph Editor has the focus but the mouse is not contained in it, there is no current mouse position in the GraphEditor so the position that is provided to the "pasteNodes" function is the last known mouse position, which is oftentimes on the border of the GraphEditor. This commit automatically sets the mouse's position to the center of the GraphEditor, and "builds" the zone containing the pasted nodes around it. --- meshroom/ui/graph.py | 19 ++++++++++++++-- meshroom/ui/qml/GraphEditor/GraphEditor.qml | 25 ++++++++++++++++----- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/meshroom/ui/graph.py b/meshroom/ui/graph.py index 16c58dacbf..0abf173b45 100644 --- a/meshroom/ui/graph.py +++ b/meshroom/ui/graph.py @@ -789,8 +789,8 @@ def getSelectedNodesContent(self): return json.dumps(selection, indent=4) return '' - @Slot(str, QPoint, result="QVariantList") - def pasteNodes(self, clipboardContent, position=None): + @Slot(str, QPoint, bool, result="QVariantList") + def pasteNodes(self, clipboardContent, position=None, centerPosition=False): """ Parse the content of the clipboard to see whether it contains valid node descriptions. If that is the case, the nodes described @@ -810,6 +810,8 @@ def pasteNodes(self, clipboardContent, position=None): clipboardContent (str): the string contained in the clipboard, that may or may not contain valid node information position (QPoint): the position of the mouse in the Graph Editor when the function was called + centerPosition (bool): whether the provided position is not the top-left corner of the pasting + zone, but its center Returns: list: the list of Node objects that were pasted and added to the graph @@ -844,7 +846,9 @@ def pasteNodes(self, clipboardContent, position=None): # to the first node within that zone. firstNodePos = None minX = 0 + maxX = 0 minY = 0 + maxY = 0 for key in sorted(d): nodeType = d[key].get("nodeType", None) if not nodeType: @@ -855,12 +859,18 @@ def pasteNodes(self, clipboardContent, position=None): if not firstNodePos: firstNodePos = pos minX = pos[0] + maxX = pos[0] minY = pos[1] + maxY = pos[1] else: if minX > pos[0]: minX = pos[0] + if maxX < pos[0]: + maxX = pos[0] if minY > pos[1]: minY = pos[1] + if maxY < pos[1]: + maxY = pos[1] # Ensure there will not be an error if no node has a specified position if not firstNodePos: @@ -869,6 +879,11 @@ def pasteNodes(self, clipboardContent, position=None): # Position of the first node within the zone position = Position(position.x + firstNodePos[0] - minX, position.y + firstNodePos[1] - minY) + if centerPosition: # Center the zone around the mouse's position (mouse's position might be artificial) + maxX = maxX + self.layout.nodeWidth # maxX and maxY are the position of the furthest node's top-left corner + maxY = maxY + self.layout.nodeHeight # We want the position of the furthest node's bottom-right corner + position = Position(position.x - ((maxX - minX) / 2), position.y - ((maxY - minY) / 2)) + finalPosition = None prevPosition = None positions = [] diff --git a/meshroom/ui/qml/GraphEditor/GraphEditor.qml b/meshroom/ui/qml/GraphEditor/GraphEditor.qml index c2c1c34b1f..a1f79b2413 100755 --- a/meshroom/ui/qml/GraphEditor/GraphEditor.qml +++ b/meshroom/ui/qml/GraphEditor/GraphEditor.qml @@ -41,7 +41,6 @@ Item { clip: true SystemPalette { id: activePalette } - property point pastePosition /// Get node delegate for the given node object function nodeDelegate(node) @@ -89,14 +88,22 @@ Item { /// Paste content of clipboard to graph editor and create new node if valid function pasteNodes() { - if (uigraph.hoveredNode != null) { - var node = nodeDelegate(uigraph.hoveredNode) - root.pastePosition = Qt.point(node.mousePosition.x + node.x, node.mousePosition.y + node.y) + var finalPosition = undefined + var centerPosition = false + if (mouseArea.containsMouse) { + if (uigraph.hoveredNode != null) { + var node = nodeDelegate(uigraph.hoveredNode) + finalPosition = Qt.point(node.mousePosition.x + node.x, node.mousePosition.y + node.y) + } else { + finalPosition = mapToItem(draggable, mouseArea.mouseX, mouseArea.mouseY) + } } else { - root.pastePosition = mapToItem(draggable, mouseArea.mouseX, mouseArea.mouseY) + finalPosition = getCenterPosition() + centerPosition = true } + var copiedContent = Clipboard.getText() - var nodes = uigraph.pasteNodes(copiedContent, root.pastePosition) + var nodes = uigraph.pasteNodes(copiedContent, finalPosition, centerPosition) if (nodes.length > 0) { uigraph.clearNodeSelection() uigraph.selectedNode = nodes[0] @@ -104,6 +111,12 @@ Item { } } + /// Get the coordinates of the point at the center of the GraphEditor + function getCenterPosition() + { + return mapToItem(draggable, mouseArea.width / 2, mouseArea.height / 2) + } + Keys.onPressed: { if (event.key === Qt.Key_F) fit()