[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.
This commit is contained in:
Candice Bentéjac 2022-09-30 15:43:42 +02:00
parent 243c278bcc
commit 021770a424
2 changed files with 36 additions and 8 deletions

View file

@ -789,8 +789,8 @@ class UIGraph(QObject):
return json.dumps(selection, indent=4) return json.dumps(selection, indent=4)
return '' return ''
@Slot(str, QPoint, result="QVariantList") @Slot(str, QPoint, bool, result="QVariantList")
def pasteNodes(self, clipboardContent, position=None): def pasteNodes(self, clipboardContent, position=None, centerPosition=False):
""" """
Parse the content of the clipboard to see whether it contains Parse the content of the clipboard to see whether it contains
valid node descriptions. If that is the case, the nodes described valid node descriptions. If that is the case, the nodes described
@ -810,6 +810,8 @@ class UIGraph(QObject):
clipboardContent (str): the string contained in the clipboard, that may or may not contain valid clipboardContent (str): the string contained in the clipboard, that may or may not contain valid
node information node information
position (QPoint): the position of the mouse in the Graph Editor when the function was called 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: Returns:
list: the list of Node objects that were pasted and added to the graph list: the list of Node objects that were pasted and added to the graph
@ -844,7 +846,9 @@ class UIGraph(QObject):
# to the first node within that zone. # to the first node within that zone.
firstNodePos = None firstNodePos = None
minX = 0 minX = 0
maxX = 0
minY = 0 minY = 0
maxY = 0
for key in sorted(d): for key in sorted(d):
nodeType = d[key].get("nodeType", None) nodeType = d[key].get("nodeType", None)
if not nodeType: if not nodeType:
@ -855,12 +859,18 @@ class UIGraph(QObject):
if not firstNodePos: if not firstNodePos:
firstNodePos = pos firstNodePos = pos
minX = pos[0] minX = pos[0]
maxX = pos[0]
minY = pos[1] minY = pos[1]
maxY = pos[1]
else: else:
if minX > pos[0]: if minX > pos[0]:
minX = pos[0] minX = pos[0]
if maxX < pos[0]:
maxX = pos[0]
if minY > pos[1]: if minY > pos[1]:
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 # Ensure there will not be an error if no node has a specified position
if not firstNodePos: if not firstNodePos:
@ -869,6 +879,11 @@ class UIGraph(QObject):
# Position of the first node within the zone # Position of the first node within the zone
position = Position(position.x + firstNodePos[0] - minX, position.y + firstNodePos[1] - minY) 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 finalPosition = None
prevPosition = None prevPosition = None
positions = [] positions = []

View file

@ -41,7 +41,6 @@ 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)
@ -89,14 +88,22 @@ 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 pasteNodes() function pasteNodes()
{ {
if (uigraph.hoveredNode != null) { var finalPosition = undefined
var node = nodeDelegate(uigraph.hoveredNode) var centerPosition = false
root.pastePosition = Qt.point(node.mousePosition.x + node.x, node.mousePosition.y + node.y) 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 { } else {
root.pastePosition = mapToItem(draggable, mouseArea.mouseX, mouseArea.mouseY) finalPosition = getCenterPosition()
centerPosition = true
} }
var copiedContent = Clipboard.getText() var copiedContent = Clipboard.getText()
var nodes = uigraph.pasteNodes(copiedContent, root.pastePosition) var nodes = uigraph.pasteNodes(copiedContent, finalPosition, centerPosition)
if (nodes.length > 0) { if (nodes.length > 0) {
uigraph.clearNodeSelection() uigraph.clearNodeSelection()
uigraph.selectedNode = nodes[0] 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: { Keys.onPressed: {
if (event.key === Qt.Key_F) if (event.key === Qt.Key_F)
fit() fit()