Skip to content

Commit

Permalink
[ui] Paste nodes at the center of GraphEditor with the Edit > Paste menu
Browse files Browse the repository at this point in the history
When using the Edit > Paste menu, 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.
  • Loading branch information
cbentejac committed Sep 30, 2022
1 parent 243c278 commit 09a63f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
19 changes: 17 additions & 2 deletions meshroom/ui/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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 = []
Expand Down
27 changes: 20 additions & 7 deletions meshroom/ui/qml/GraphEditor/GraphEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Item {
clip: true

SystemPalette { id: activePalette }
property point pastePosition

/// Get node delegate for the given node object
function nodeDelegate(node)
Expand Down Expand Up @@ -87,23 +86,37 @@ Item {
}

/// Paste content of clipboard to graph editor and create new node if valid
function pasteNodes()
function pasteNodes(position = undefined)
{
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 (position === undefined) {
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 = position
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]
uigraph.selectNodes(nodes)
}
}

/// 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()
Expand Down
2 changes: 1 addition & 1 deletion meshroom/ui/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ ApplicationWindow {

property string tooltip: "Paste the clipboard content to the project if it contains valid nodes"
text: "Paste Node(s)"
onTriggered: graphEditor.pasteNodes()
onTriggered: graphEditor.pasteNodes(graphEditor.getCenterPosition())
}

Action {
Expand Down

0 comments on commit 09a63f8

Please sign in to comment.