Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui] Paste nodes at the center of the Graph Editor when it does not contain the mouse #1788

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 19 additions & 6 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 @@ -89,21 +88,35 @@ 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]
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