nodeview: combined paste/duplicate code

Code cleanup. Doesn't fix bug involved with pasting or duplicating.
This commit is contained in:
itsmattkc
2021-07-23 17:47:08 -07:00
parent d0e7258301
commit a83be3a253
2 changed files with 32 additions and 32 deletions
+30 -32
View File
@@ -353,42 +353,12 @@ void NodeView::CopySelected(bool cut)
void NodeView::Paste()
{
if (!graph_) {
return;
}
paste_command_ = new MultiUndoCommand();
QVector<Node*> pasted_nodes = PasteNodesFromClipboard(graph_, paste_command_);
if (!pasted_nodes.isEmpty()) {
paste_command_->add_child(new NodeViewAttachNodesToCursor(this, pasted_nodes));
}
paste_command_->redo();
PasteNodesInternal();
}
void NodeView::Duplicate()
{
if (!graph_) {
return;
}
QVector<Node*> selected = scene_.GetSelectedNodes();
if (selected.isEmpty()) {
return;
}
paste_command_ = new MultiUndoCommand();
QVector<Node*> duplicated_nodes = Node::CopyDependencyGraph(selected, paste_command_);
if (!duplicated_nodes.isEmpty()) {
paste_command_->add_child(new NodeViewAttachNodesToCursor(this, duplicated_nodes));
}
paste_command_->redo();
PasteNodesInternal(scene_.GetSelectedNodes());
}
void NodeView::SetColorLabel(int index)
@@ -1596,6 +1566,34 @@ NodeViewItem *NodeView::UpdateNodeItem(Node *node, bool ignore_own_context)
return item;
}
void NodeView::PasteNodesInternal(const QVector<Node *> &duplicate_nodes)
{
// If no graph, do nothing
if (!graph_) {
return;
}
paste_command_ = new MultiUndoCommand();
// If duplicating nodes, duplicate, otherwise paste
QVector<Node*> new_nodes;
if (duplicate_nodes.isEmpty()) {
new_nodes = PasteNodesFromClipboard(graph_, paste_command_);
} else {
new_nodes = Node::CopyDependencyGraph(duplicate_nodes, paste_command_);
}
// If no nodes were retrieved, do nothing
if (new_nodes.isEmpty()) {
return;
}
// Attach nodes to cursor
paste_command_->add_child(new NodeViewAttachNodesToCursor(this, new_nodes));
paste_command_->redo();
}
NodeView::NodeViewAttachNodesToCursor::NodeViewAttachNodesToCursor(NodeView *view, const QVector<Node *> &nodes) :
view_(view),
nodes_(nodes)
+2
View File
@@ -142,6 +142,8 @@ private:
NodeViewItem *UpdateNodeItem(Node *node, bool ignore_own_context = false);
void PasteNodesInternal(const QVector<Node*> &duplicate_nodes = QVector<Node *>());
class NodeViewAttachNodesToCursor : public UndoCommand
{
public: