implemented deleting nodes in the nodeview (undoable)

This commit is contained in:
itsmattkc
2019-12-13 04:12:00 +11:00
parent 186973ff20
commit 18bf126574
9 changed files with 140 additions and 2 deletions
+61
View File
@@ -64,3 +64,64 @@ void NodeEdgeRemoveCommand::undo()
NodeParam::ConnectEdge(output_, input_);
done_ = false;
}
NodeAddCommand::NodeAddCommand(NodeGraph *graph, Node *node, QUndoCommand *parent) :
QUndoCommand(parent),
graph_(graph),
node_(node)
{
// Ensures that when this command is destroyed, if redo() hasn't been called, the node will be destroyed too
node->setParent(&memory_manager_);
}
void NodeAddCommand::redo()
{
graph_->AddNode(node_);
}
void NodeAddCommand::undo()
{
graph_->TakeNode(node_, &memory_manager_);
}
NodeRemoveCommand::NodeRemoveCommand(NodeGraph *graph, const QList<Node *> &nodes, QUndoCommand *parent) :
QUndoCommand(parent),
graph_(graph),
nodes_(nodes)
{
}
void NodeRemoveCommand::redo()
{
// Cache edges for undoing
foreach (Node* n, nodes_) {
foreach (NodeParam* param, n->parameters()) {
foreach (NodeEdgePtr edge, param->edges()) {
// Ensures the same edge isn't added twice (prevents double connecting when undoing)
if (!edges_.contains(edge)) {
edges_.append(edge);
}
}
}
}
// Take nodes from graph (TakeNode() will automatically disconnect edges)
foreach (Node* n, nodes_) {
graph_->TakeNode(n, &memory_manager_);
}
}
void NodeRemoveCommand::undo()
{
// Re-add nodes to graph
foreach (Node* n, nodes_) {
graph_->AddNode(n);
}
// Re-connect edges
foreach (NodeEdgePtr edge, edges_) {
NodeParam::ConnectEdge(edge->output(), edge->input());
}
edges_.clear();
}