[ui] Paste a node on the mouse's position

When creating a node with a "paste" operation, place the node
on the mouse's position in the graph instead of default position (0,0).

If the mouse is placed on an existing node, the pasted node will be
placed on the mouse's position plus an offset so that the pasted node
does not directly overlap with the existing node.
This commit is contained in:
Candice Bentéjac 2022-08-19 11:02:42 +02:00
parent ddda62a652
commit 5b65866e49
4 changed files with 14 additions and 8 deletions

View file

@ -396,11 +396,11 @@ class Graph(BaseObject):
return duplicates return duplicates
def pasteNode(self, nodeType, **kwargs): def pasteNode(self, nodeType, position, **kwargs):
name = self._createUniqueNodeName(nodeType) name = self._createUniqueNodeName(nodeType)
node = None node = None
with GraphModification(self): with GraphModification(self):
node = Node(nodeType, **kwargs) node = Node(nodeType, position=position, **kwargs)
self._addNode(node, name) self._addNode(node, name)
self._applyExpr() self._applyExpr()
return node return node

View file

@ -199,14 +199,15 @@ class PasteNodeCommand(GraphCommand):
""" """
Handle node pasting in a Graph. Handle node pasting in a Graph.
""" """
def __init__(self, graph, nodeType, parent=None, **kwargs): def __init__(self, graph, nodeType, position=None, parent=None, **kwargs):
super(PasteNodeCommand, self).__init__(graph, parent) super(PasteNodeCommand, self).__init__(graph, parent)
self.nodeType = nodeType self.nodeType = nodeType
self.position = position
self.nodeName = None self.nodeName = None
self.kwargs = kwargs self.kwargs = kwargs
def redoImpl(self): def redoImpl(self):
node = self.graph.pasteNode(self.nodeType, **self.kwargs) node = self.graph.pasteNode(self.nodeType, self.position, **self.kwargs)
self.nodeName = node.name self.nodeName = node.name
self.setText("Paste Node {}".format(self.nodeName)) self.setText("Paste Node {}".format(self.nodeName))
return node return node

View file

@ -766,8 +766,8 @@ class UIGraph(QObject):
return json.dumps(node, indent=4) return json.dumps(node, indent=4)
return '' return ''
@Slot(str) @Slot(str, QPoint)
def pasteNode(self, clipboardContent): def pasteNode(self, clipboardContent, position=None):
""" """
Parse the content of the clipboard to see whether it contains Parse the content of the clipboard to see whether it contains
a valid node description. If that is the case, the node described a valid node description. If that is the case, the node described
@ -802,7 +802,10 @@ class UIGraph(QObject):
attributes.update(d.get("inputs", {})) attributes.update(d.get("inputs", {}))
attributes.update(d.get("outputs", {})) attributes.update(d.get("outputs", {}))
self.push(commands.PasteNodeCommand(self._graph, nodeType, **attributes)) if isinstance(position, QPoint):
position = Position(position.x(), position.y())
self.push(commands.PasteNodeCommand(self._graph, nodeType, position=position, **attributes))
undoStack = Property(QObject, lambda self: self._undoStack, constant=True) undoStack = Property(QObject, lambda self: self._undoStack, constant=True)
graphChanged = Signal() graphChanged = Signal()

View file

@ -41,6 +41,7 @@ Item {
clip: true clip: true
SystemPalette { id: activePalette } SystemPalette { id: activePalette }
property point pastePosition
/// Get node delegate for the given node object /// Get node delegate for the given node object
function nodeDelegate(node) function nodeDelegate(node)
@ -88,8 +89,9 @@ Item {
/// Paste content of clipboard to graph editor and create new node if valid /// Paste content of clipboard to graph editor and create new node if valid
function pasteNode() function pasteNode()
{ {
root.pastePosition = mapToItem(draggable, mouseArea.mouseX, mouseArea.mouseY)
var copiedContent = Clipboard.getText() var copiedContent = Clipboard.getText()
uigraph.pasteNode(copiedContent) uigraph.pasteNode(copiedContent, root.pastePosition)
} }
Keys.onPressed: { Keys.onPressed: {