new undo system

Also enables undoing when working with node input arrays
This commit is contained in:
itsmattkc
2021-01-29 16:10:34 +11:00
parent 4e5db91c17
commit ad4b83a098
69 changed files with 1218 additions and 837 deletions
+9 -9
View File
@@ -118,13 +118,13 @@ void NodeView::DeleteSelected()
return;
}
QUndoCommand* command = new QUndoCommand();
MultiUndoCommand* command = new MultiUndoCommand();
{
QVector<NodeViewEdge *> selected_edges = scene_.GetSelectedEdges();
foreach (NodeViewEdge* edge, selected_edges) {
new NodeEdgeRemoveCommand(edge->output(), edge->input(), edge->element(), command);
command->add_child(new NodeEdgeRemoveCommand(edge->output(), edge->input(), edge->element()));
}
}
@@ -141,7 +141,7 @@ void NodeView::DeleteSelected()
if (!selected_nodes.isEmpty()) {
foreach (Node* node, selected_nodes) {
new NodeRemoveAndDisconnectCommand(node, command);
command->add_child(new NodeRemoveAndDisconnectCommand(node));
}
}
}
@@ -292,7 +292,7 @@ void NodeView::Paste()
return;
}
QUndoCommand* command = new QUndoCommand();
MultiUndoCommand* command = new MultiUndoCommand();
QVector<Node*> pasted_nodes = PasteNodesFromClipboard(graph_, command);
@@ -315,7 +315,7 @@ void NodeView::Duplicate()
return;
}
QUndoCommand* command = new QUndoCommand();
MultiUndoCommand* command = new MultiUndoCommand();
QVector<Node*> duplicated_nodes = Node::CopyDependencyGraph(selected, command);
@@ -536,14 +536,14 @@ void NodeView::mouseReleaseEvent(QMouseEvent *event)
if (drop_edge_) {
// We have everything we need to place the node in between
QUndoCommand* command = new QUndoCommand();
MultiUndoCommand* command = new MultiUndoCommand();
// Remove old edge
new NodeEdgeRemoveCommand(drop_edge_->output(), drop_edge_->input(), drop_edge_->element(), command);
command->add_child(new NodeEdgeRemoveCommand(drop_edge_->output(), drop_edge_->input(), drop_edge_->element()));
// Place new edges
new NodeEdgeAddCommand(drop_edge_->output(), drop_input_, -1, command);
new NodeEdgeAddCommand(dropping_node, drop_edge_->input(), drop_edge_->element(), command);
command->add_child(new NodeEdgeAddCommand(drop_edge_->output(), drop_input_, -1));
command->add_child(new NodeEdgeAddCommand(dropping_node, drop_edge_->input(), drop_edge_->element()));
Core::instance()->undo_stack()->push(command);
}
-1
View File
@@ -24,7 +24,6 @@
#include <QFontMetrics>
#include <QGraphicsRectItem>
#include <QLinearGradient>
#include <QUndoCommand>
#include <QWidget>
#include "node/node.h"
+35 -19
View File
@@ -25,22 +25,41 @@
namespace olive {
NodeEdgeAddCommand::NodeEdgeAddCommand(Node *output, NodeInput *input, int element, QUndoCommand *parent) :
UndoCommand(parent),
NodeEdgeAddCommand::NodeEdgeAddCommand(Node *output, NodeInput *input, int element) :
output_(output),
input_(input),
element_(element)
element_(element),
remove_command_(nullptr)
{
}
void NodeEdgeAddCommand::redo_internal()
NodeEdgeAddCommand::~NodeEdgeAddCommand()
{
delete remove_command_;
}
void NodeEdgeAddCommand::redo()
{
if (input_->IsConnected(element_)) {
if (!remove_command_) {
remove_command_ = new NodeEdgeRemoveCommand(input_->GetConnectedNode(element_),
input_,
element_);
}
remove_command_->redo();
}
Node::ConnectEdge(output_, input_, element_);
}
void NodeEdgeAddCommand::undo_internal()
void NodeEdgeAddCommand::undo()
{
Node::DisconnectEdge(output_, input_, element_);
if (remove_command_) {
remove_command_->undo();
}
}
Project *NodeEdgeAddCommand::GetRelevantProject() const
@@ -48,20 +67,19 @@ Project *NodeEdgeAddCommand::GetRelevantProject() const
return output_->parent()->project();
}
NodeEdgeRemoveCommand::NodeEdgeRemoveCommand(Node *output, NodeInput *input, int element, QUndoCommand *parent) :
UndoCommand(parent),
NodeEdgeRemoveCommand::NodeEdgeRemoveCommand(Node *output, NodeInput *input, int element) :
output_(output),
input_(input),
element_(element)
{
}
void NodeEdgeRemoveCommand::redo_internal()
void NodeEdgeRemoveCommand::redo()
{
Node::DisconnectEdge(output_, input_, element_);
}
void NodeEdgeRemoveCommand::undo_internal()
void NodeEdgeRemoveCommand::undo()
{
Node::ConnectEdge(output_, input_, element_);
}
@@ -71,8 +89,7 @@ Project *NodeEdgeRemoveCommand::GetRelevantProject() const
return output_->parent()->project();
}
NodeAddCommand::NodeAddCommand(NodeGraph *graph, Node *node, QUndoCommand *parent) :
UndoCommand(parent),
NodeAddCommand::NodeAddCommand(NodeGraph *graph, Node *node) :
graph_(graph),
node_(node)
{
@@ -80,12 +97,12 @@ NodeAddCommand::NodeAddCommand(NodeGraph *graph, Node *node, QUndoCommand *paren
node_->setParent(&memory_manager_);
}
void NodeAddCommand::redo_internal()
void NodeAddCommand::redo()
{
node_->setParent(graph_);
}
void NodeAddCommand::undo_internal()
void NodeAddCommand::undo()
{
node_->setParent(&memory_manager_);
}
@@ -95,8 +112,7 @@ Project *NodeAddCommand::GetRelevantProject() const
return graph_->project();
}
NodeCopyInputsCommand::NodeCopyInputsCommand(Node *src, Node *dest, bool include_connections, QUndoCommand *parent) :
QUndoCommand(parent),
NodeCopyInputsCommand::NodeCopyInputsCommand(Node *src, Node *dest, bool include_connections) :
src_(src),
dest_(dest),
include_connections_(include_connections)
@@ -110,21 +126,21 @@ void NodeCopyInputsCommand::redo()
void NodeRemoveAndDisconnectCommand::prep()
{
command_ = new QUndoCommand();
command_ = new MultiUndoCommand();
// If this is a block, remove all links
if (node_->HasLinks()) {
new NodeUnlinkAllCommand(node_, command_);
command_->add_child(new NodeUnlinkAllCommand(node_));
}
// Disconnect everything
foreach (const Node::InputConnection& conn, node_->edges()) {
new NodeEdgeRemoveCommand(node_, conn.input, conn.element, command_);
command_->add_child(new NodeEdgeRemoveCommand(node_, conn.input, conn.element));
}
foreach (NodeInput* input, node_->inputs()) {
for (auto it=input->edges().cbegin(); it!=input->edges().cend(); it++) {
new NodeEdgeRemoveCommand(it->second, input, it->first, command_);
command_->add_child(new NodeEdgeRemoveCommand(it->second, input, it->first));
}
}
}
+45 -53
View File
@@ -21,8 +21,6 @@
#ifndef NODEVIEWUNDO_H
#define NODEVIEWUNDO_H
#include <QUndoCommand>
#include "node/graph.h"
#include "node/node.h"
#include "undo/undocommand.h"
@@ -30,19 +28,18 @@
namespace olive {
/**
* @brief An undoable command for connecting two NodeParams together
* @brief An undoable command for disconnecting two NodeParams
*
* Can be considered a QUndoCommand wrapper for NodeParam::ConnectEdge()/
* Can be considered a UndoCommand wrapper for NodeParam::DisonnectEdge()/
*/
class NodeEdgeAddCommand : public UndoCommand {
class NodeEdgeRemoveCommand : public UndoCommand {
public:
NodeEdgeAddCommand(Node* output, NodeInput* input, int element, QUndoCommand* parent = nullptr);
NodeEdgeRemoveCommand(Node* output, NodeInput* input, int element);
virtual Project* GetRelevantProject() const override;
protected:
virtual void redo_internal() override;
virtual void undo_internal() override;
virtual void redo() override;
virtual void undo() override;
private:
Node* output_;
@@ -52,36 +49,38 @@ private:
};
/**
* @brief An undoable command for disconnecting two NodeParams
* @brief An undoable command for connecting two NodeParams together
*
* Can be considered a QUndoCommand wrapper for NodeParam::DisonnectEdge()/
* Can be considered a UndoCommand wrapper for NodeParam::ConnectEdge()/
*/
class NodeEdgeRemoveCommand : public UndoCommand {
class NodeEdgeAddCommand : public UndoCommand {
public:
NodeEdgeRemoveCommand(Node* output, NodeInput* input, int element, QUndoCommand* parent = nullptr);
NodeEdgeAddCommand(Node* output, NodeInput* input, int element);
virtual ~NodeEdgeAddCommand() override;
virtual Project* GetRelevantProject() const override;
protected:
virtual void redo_internal() override;
virtual void undo_internal() override;
virtual void redo() override;
virtual void undo() override;
private:
Node* output_;
NodeInput* input_;
int element_;
NodeEdgeRemoveCommand* remove_command_;
};
class NodeAddCommand : public UndoCommand {
public:
NodeAddCommand(NodeGraph* graph, Node* node, QUndoCommand* parent = nullptr);
NodeAddCommand(NodeGraph* graph, Node* node);
virtual Project* GetRelevantProject() const override;
protected:
virtual void redo_internal() override;
virtual void undo_internal() override;
virtual void redo() override;
virtual void undo() override;
private:
QObject memory_manager_;
@@ -92,8 +91,7 @@ private:
class NodeRemoveAndDisconnectCommand : public UndoCommand {
public:
NodeRemoveAndDisconnectCommand(Node* node, QUndoCommand* parent = nullptr) :
UndoCommand(parent),
NodeRemoveAndDisconnectCommand(Node* node) :
node_(node),
graph_(nullptr),
command_(nullptr),
@@ -115,8 +113,7 @@ public:
}
}
protected:
virtual void redo_internal() override
virtual void redo() override
{
if (!prepped_) {
prep();
@@ -129,7 +126,7 @@ protected:
node_->setParent(&memory_manager_);
}
virtual void undo_internal() override
virtual void undo() override
{
node_->setParent(graph_);
graph_ = nullptr;
@@ -145,7 +142,7 @@ private:
Node* node_;
NodeGraph* graph_;
QUndoCommand* command_;
MultiUndoCommand* command_;
bool prepped_;
@@ -153,8 +150,7 @@ private:
class NodeRemoveWithExclusiveDependenciesAndDisconnect : public UndoCommand {
public:
NodeRemoveWithExclusiveDependenciesAndDisconnect(Node* node, QUndoCommand* parent = nullptr) :
UndoCommand(parent),
NodeRemoveWithExclusiveDependenciesAndDisconnect(Node* node) :
node_(node),
command_(nullptr),
prepped_(false)
@@ -175,8 +171,7 @@ public:
}
}
protected:
virtual void redo_internal() override
virtual void redo() override
{
if (!prepped_) {
prep();
@@ -186,7 +181,7 @@ protected:
command_->redo();
}
virtual void undo_internal() override
virtual void undo() override
{
command_->undo();
}
@@ -194,33 +189,35 @@ protected:
private:
void prep()
{
command_ = new QUndoCommand();
command_ = new MultiUndoCommand();
new NodeRemoveAndDisconnectCommand(node_, command_);
command_->add_child(new NodeRemoveAndDisconnectCommand(node_));
// Remove exclusive dependencies
QVector<Node*> deps = node_->GetExclusiveDependencies();
foreach (Node* d, deps) {
new NodeRemoveAndDisconnectCommand(d, command_);
command_->add_child(new NodeRemoveAndDisconnectCommand(d));
}
}
Node* node_;
QUndoCommand* command_;
MultiUndoCommand* command_;
bool prepped_;
};
class NodeCopyInputsCommand : public QUndoCommand {
class NodeCopyInputsCommand : public UndoCommand {
public:
NodeCopyInputsCommand(Node* src,
Node* dest,
bool include_connections,
QUndoCommand* parent = nullptr);
bool include_connections);
protected:
virtual void redo() override;
virtual void undo() override {}
virtual Project* GetRelevantProject() const override {return nullptr;}
private:
Node* src_;
@@ -232,8 +229,7 @@ private:
class NodeLinkCommand : public UndoCommand {
public:
NodeLinkCommand(Node* a, Node* b, bool link, QUndoCommand* parent = nullptr) :
UndoCommand(parent),
NodeLinkCommand(Node* a, Node* b, bool link) :
a_(a),
b_(b),
link_(link)
@@ -245,8 +241,7 @@ public:
return a_->parent()->project();
}
protected:
virtual void redo_internal() override
virtual void redo() override
{
if (link_) {
done_ = Node::Link(a_, b_);
@@ -255,7 +250,7 @@ protected:
}
}
virtual void undo_internal() override
virtual void undo() override
{
if (done_) {
if (link_) {
@@ -276,8 +271,7 @@ private:
class NodeUnlinkAllCommand : public UndoCommand {
public:
NodeUnlinkAllCommand(Node* node, QUndoCommand* parent = nullptr) :
UndoCommand(parent),
NodeUnlinkAllCommand(Node* node) :
node_(node)
{
}
@@ -287,8 +281,7 @@ public:
return node_->parent()->project();
}
protected:
virtual void redo_internal() override
virtual void redo() override
{
unlinked_ = node_->links();
@@ -297,7 +290,7 @@ protected:
}
}
virtual void undo_internal() override
virtual void undo() override
{
foreach (Node* link, unlinked_) {
Node::Link(node_, link);
@@ -313,16 +306,15 @@ private:
};
class NodeLinkManyCommand : public UndoCommand {
class NodeLinkManyCommand : public MultiUndoCommand {
public:
NodeLinkManyCommand(const QVector<Node*> nodes, bool link, QUndoCommand* parent = nullptr) :
UndoCommand(parent),
NodeLinkManyCommand(const QVector<Node*> nodes, bool link) :
nodes_(nodes)
{
foreach (Node* a, nodes_) {
foreach (Node* b, nodes_) {
if (a != b) {
new NodeLinkCommand(a, b, link, this);
add_child(new NodeLinkCommand(a, b, link));
}
}
}