implemented deleting nodes in the nodeview (undoable)
This commit is contained in:
@@ -20,7 +20,9 @@
|
||||
|
||||
#include "nodeview.h"
|
||||
|
||||
#include "nodeviewundo.h"
|
||||
#include "node/factory.h"
|
||||
#include "undo/undostack.h"
|
||||
|
||||
NodeView::NodeView(QWidget *parent) :
|
||||
QGraphicsView(parent),
|
||||
@@ -119,6 +121,30 @@ NodeViewEdge *NodeView::EdgeToUIObject(NodeEdgePtr n)
|
||||
return EdgeToUIObject(&scene_, n);
|
||||
}
|
||||
|
||||
void NodeView::DeleteSelected()
|
||||
{
|
||||
if (!graph_) {
|
||||
return;
|
||||
}
|
||||
|
||||
QList<QGraphicsItem*> selected = scene_.selectedItems();
|
||||
QList<Node*> selected_nodes;
|
||||
|
||||
foreach (QGraphicsItem* item, selected) {
|
||||
NodeViewItem* node_item = dynamic_cast<NodeViewItem*>(item);
|
||||
|
||||
if (node_item) {
|
||||
selected_nodes.append(node_item->node());
|
||||
}
|
||||
}
|
||||
|
||||
if (selected_nodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
olive::undo_stack.push(new NodeRemoveCommand(graph_, selected_nodes));
|
||||
}
|
||||
|
||||
void NodeView::AddNode(Node* node)
|
||||
{
|
||||
NodeViewItem* item = new NodeViewItem();
|
||||
@@ -218,6 +244,6 @@ void NodeView::CreateNodeSlot(QAction *action)
|
||||
Node* new_node = NodeFactory::CreateFromMenuAction(action);
|
||||
|
||||
if (new_node) {
|
||||
graph_->AddNode(new_node);
|
||||
olive::undo_stack.push(new NodeAddCommand(graph_, new_node));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +76,11 @@ public:
|
||||
*/
|
||||
NodeViewEdge* EdgeToUIObject(NodeEdgePtr n);
|
||||
|
||||
/**
|
||||
* @brief Delete selected nodes from graph (user-friendly/undoable)
|
||||
*/
|
||||
void DeleteSelected();
|
||||
|
||||
signals:
|
||||
/**
|
||||
* @brief Signal emitted when the selected nodes have changed
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <QUndoCommand>
|
||||
|
||||
#include "node/graph.h"
|
||||
#include "node/node.h"
|
||||
|
||||
/**
|
||||
@@ -45,4 +46,33 @@ private:
|
||||
bool done_;
|
||||
};
|
||||
|
||||
class NodeAddCommand : public QUndoCommand {
|
||||
public:
|
||||
NodeAddCommand(NodeGraph* graph, Node* node, QUndoCommand* parent = nullptr);
|
||||
|
||||
virtual void redo() override;
|
||||
virtual void undo() override;
|
||||
|
||||
private:
|
||||
QObject memory_manager_;
|
||||
|
||||
NodeGraph* graph_;
|
||||
Node* node_;
|
||||
};
|
||||
|
||||
class NodeRemoveCommand : public QUndoCommand {
|
||||
public:
|
||||
NodeRemoveCommand(NodeGraph* graph, const QList<Node*>& nodes, QUndoCommand* parent = nullptr);
|
||||
|
||||
virtual void redo() override;
|
||||
virtual void undo() override;
|
||||
|
||||
private:
|
||||
QObject memory_manager_;
|
||||
|
||||
NodeGraph* graph_;
|
||||
QList<Node*> nodes_;
|
||||
QList<NodeEdgePtr> edges_;
|
||||
};
|
||||
|
||||
#endif // NODEVIEWUNDO_H
|
||||
|
||||
Reference in New Issue
Block a user