moved timeline undoable commands to using the new NodeAddCommand

Previous iteration used some "magic code" that added clips automatically to the
timeline. This was functional but ultimately outside of the undo commands'
control meaning nodes could be infinitely added and abandoned. This makes the
add process part of the undo command which means it's all undoable as the user
would expect.
This commit is contained in:
itsmattkc
2019-12-14 00:04:57 +11:00
parent 18bf126574
commit 323718bc11
21 changed files with 120 additions and 114 deletions
-27
View File
@@ -49,18 +49,6 @@ void NodeGraph::AddNode(Node *node)
emit NodeAdded(node);
}
void NodeGraph::AddNodeWithDependencies(Node *node)
{
// Add node and its connected nodes to graph
AddNode(node);
// Add all of Block's dependencies
QList<Node*> node_dependencies = node->GetDependencies();
foreach (Node* dep, node_dependencies) {
AddNode(dep);
}
}
void NodeGraph::TakeNode(Node *node, QObject* new_parent)
{
if (!ContainsNode(node)) {
@@ -84,21 +72,6 @@ void NodeGraph::TakeNode(Node *node, QObject* new_parent)
emit NodeRemoved(node);
}
QList<Node *> NodeGraph::TakeNodeWithItsDependencies(Node *node, QObject *new_parent)
{
if (!ContainsNode(node)) {
return QList<Node*>();
}
QList<Node*> deps = node->GetExclusiveDependencies();
foreach (Node* d, deps) {
TakeNode(d, new_parent);
}
return deps;
}
const QList<Node *> &NodeGraph::nodes()
{
return node_children_;
-20
View File
@@ -50,31 +50,11 @@ public:
*/
void AddNode(Node* node);
/**
* @brief Adds a node to this graph and all nodes connected to its inputs
*
* Adds the Node to the graph and runs through its inputs adding all of its dependencies (and all of their
* dependencies and so forth). The graph takes ownershi of all Nodes added through this process.
*/
void AddNodeWithDependencies(Node* node);
/**
* @brief Removes a Node from the graph BUT doesn't destroy it. Ownership is passed to `new_parent`.
*/
void TakeNode(Node* node, QObject* new_parent = nullptr);
/**
* @brief Removes a Node from the graph and its dependencies (ONLY if the dependencies are exclusive to this Node).
*
* Returns a list of all Nodes that were removed in this process (except the Node used as a parameter)
*
* Only dependencies that are exclusively dependencies of this Node are removed. If a dependency Node is also
* used as the dependency of another Node, it is not removed and not returned in the list.
*
* Ownership of all Nodes is passed to `new_parent`.
*/
QList<Node*> TakeNodeWithItsDependencies(Node* node, QObject* new_parent = nullptr);
/**
* @brief Retrieve a complete list of the nodes belonging to this graph
*/
+10
View File
@@ -468,6 +468,16 @@ QVariant Node::InputValueFromTable(NodeInput *input, const NodeValueTable &table
return table.Get(find_data_type);
}
const QPointF &Node::GetPosition()
{
return position_;
}
void Node::SetPosition(const QPointF &pos)
{
position_ = pos;
}
void Node::AddInput(NodeInput *input)
{
AddParameter(input);
+10
View File
@@ -24,6 +24,7 @@
#include <QCryptographicHash>
#include <QMutex>
#include <QObject>
#include <QPointF>
#include "common/rational.h"
#include "node/dependency.h"
@@ -270,6 +271,10 @@ public:
virtual QVariant InputValueFromTable(NodeInput* input, const NodeValueTable& table) const;
const QPointF& GetPosition();
void SetPosition(const QPointF& pos);
protected:
void AddInput(NodeInput* input);
@@ -339,6 +344,11 @@ private:
*/
NodeOutput* output_;
/**
* @brief UI position for NodeViews
*/
QPointF position_;
private slots:
void InputChanged(rational start, rational end);
+5 -22
View File
@@ -231,17 +231,19 @@ void TrackOutput::PrependBlock(Block *block)
void TrackOutput::InsertBlockAtIndex(Block *block, int index)
{
AddBlockToGraph(block);
BlockInvalidateCache();
block_input_->InsertAt(index);
NodeParam::ConnectEdge(block->output(),
block_input_->At(index));
UnblockInvalidateCache();
InvalidateCache(block->in(), track_length());
}
void TrackOutput::AppendBlock(Block *block)
{
AddBlockToGraph(block);
BlockInvalidateCache();
int last_index = block_input_->GetSize();
@@ -255,13 +257,6 @@ void TrackOutput::AppendBlock(Block *block)
InvalidateCache(block->in(), track_length());
}
void TrackOutput::AddBlockToGraph(Block *block)
{
// Find the parent graph
NodeGraph* graph = static_cast<NodeGraph*>(parent());
graph->AddNodeWithDependencies(block);
}
void TrackOutput::BlockInvalidateCache()
{
block_invalidate_cache_stack_++;
@@ -272,14 +267,6 @@ void TrackOutput::UnblockInvalidateCache()
block_invalidate_cache_stack_--;
}
void TrackOutput::RemoveBlock(Block *block)
{
GapBlock* gap = new GapBlock();
gap->set_length(block->length());
ReplaceBlock(block, gap);
}
void TrackOutput::RippleRemoveBlock(Block *block)
{
BlockInvalidateCache();
@@ -293,8 +280,6 @@ void TrackOutput::RippleRemoveBlock(Block *block)
UnblockInvalidateCache();
InvalidateCache(remove_in, track_length());
// FIXME: Should there be removing the Blocks from the graph?
}
void TrackOutput::ReplaceBlock(Block *old, Block *replace)
@@ -303,8 +288,6 @@ void TrackOutput::ReplaceBlock(Block *old, Block *replace)
BlockInvalidateCache();
AddBlockToGraph(replace);
int index_of_old_block = block_cache_.indexOf(old);
NodeParam::DisconnectEdge(old->output(),
-12
View File
@@ -98,11 +98,6 @@ public:
*/
void AppendBlock(Block* block);
/**
* @brief Removes a Block and places a Gap in its place
*/
void RemoveBlock(Block* block);
/**
* @brief Removes a Block pushing all subsequent Blocks earlier to take up the space
*/
@@ -119,13 +114,6 @@ public:
void UnblockInvalidateCache();
/**
* @brief Adds a Block to the parent graph so it can be connected to other Nodes
*
* Also runs through Node's dependencies (the Nodes whose outputs are connected to this Node's inputs)
*/
void AddBlockToGraph(Block* block);
static TrackOutput* TrackFromBlock(Block* block);
const rational& track_length() const;
+1 -4
View File
@@ -116,10 +116,7 @@ NodeEdgePtr NodeParam::ConnectEdge(NodeOutput *output, NodeInput *input, bool lo
}
// Ensure both nodes are in the same graph
if (output->parentNode()->parent() != input->parentNode()->parent()) {
qWarning() << "Tried to connect two nodes that aren't part of the same graph";
return nullptr;
}
Q_ASSERT(output->parentNode()->parent() == input->parentNode()->parent());
NodeEdgePtr edge = std::make_shared<NodeEdge>(output, input);
+12 -2
View File
@@ -47,6 +47,7 @@ NodeViewItem::NodeViewItem(QGraphicsItem *parent) :
// Set flags for this widget
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
//
// We use font metrics to set all the UI measurements for DPI-awareness
@@ -77,6 +78,8 @@ void NodeViewItem::SetNode(Node *n)
{
node_ = n;
setPos(node_->GetPosition());
update();
}
@@ -256,8 +259,6 @@ void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
}
}
#include "node/block/block.h"
void NodeViewItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// We override standard mouse behavior in some cases. In these cases, we don't want the standard "move" and "release"
@@ -470,3 +471,12 @@ void NodeViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QGraphicsRectItem::mouseReleaseEvent(event);
}
}
QVariant NodeViewItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionHasChanged && node_) {
node_->SetPosition(value.toPointF());
}
return QGraphicsItem::itemChange(change, value);
}
+2
View File
@@ -82,6 +82,8 @@ protected:
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override;
private:
/**
* @brief Get the relative position to draw text for a parameter at a certain index
+2 -2
View File
@@ -70,8 +70,8 @@ NodeAddCommand::NodeAddCommand(NodeGraph *graph, Node *node, QUndoCommand *paren
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_);
// Ensures that when this command is destroyed, if redo() is never called again, the node will be destroyed too
node_->setParent(&memory_manager_);
}
void NodeAddCommand::redo()
+4 -1
View File
@@ -5,6 +5,7 @@
#include "node/graph.h"
#include "node/node.h"
#include "nodeviewitem.h"
/**
* @brief An undoable commnd for connecting two NodeParams together
@@ -62,7 +63,9 @@ private:
class NodeRemoveCommand : public QUndoCommand {
public:
NodeRemoveCommand(NodeGraph* graph, const QList<Node*>& nodes, QUndoCommand* parent = nullptr);
NodeRemoveCommand(NodeGraph* graph,
const QList<Node*>& nodes,
QUndoCommand* parent = nullptr);
virtual void redo() override;
virtual void undo() override;
+20 -2
View File
@@ -8,6 +8,7 @@
#include "common/timecodefunctions.h"
#include "tool/tool.h"
#include "trackview/trackview.h"
#include "widget/nodeview/nodeviewundo.h"
TimelineWidget::TimelineWidget(QWidget *parent) :
QWidget(parent),
@@ -382,6 +383,18 @@ void TimelineWidget::SplitAtPlayhead()
}
}
void TimelineWidget::DeleteSelected()
{
QList<TimelineViewBlockItem *> list = GetSelectedBlocks();
// No-op if nothing is selected
if (list.isEmpty()) {
return;
}
}
QList<TimelineViewBlockItem *> TimelineWidget::GetSelectedBlocks()
{
QList<TimelineViewBlockItem *> list;
@@ -440,14 +453,19 @@ void TimelineWidget::RippleEditTo(olive::timeline::MovementMode mode, bool inser
rational ripple_length = out_ripple - in_ripple;
foreach (TrackOutput* track, timeline_node_->Tracks()) {
GapBlock* gap = nullptr;
if (insert_gaps) {
gap = new GapBlock();
gap->set_length(ripple_length);
new NodeAddCommand(static_cast<NodeGraph*>(track->parent()), gap, command);
}
TrackRippleRemoveAreaCommand* ripple_command = new TrackRippleRemoveAreaCommand(track,
in_ripple,
out_ripple,
command);
if (insert_gaps) {
GapBlock* gap = new GapBlock();
gap->set_length(ripple_length);
ripple_command->SetInsert(gap);
}
}
@@ -51,6 +51,8 @@ public:
void SplitAtPlayhead();
void DeleteSelected();
QList<TimelineViewBlockItem*> GetSelectedBlocks();
public slots:
+14 -4
View File
@@ -1,6 +1,7 @@
#include "widget/timelinewidget/timelinewidget.h"
#include "core.h"
#include "widget/nodeview/nodeviewundo.h"
TimelineWidget::AddTool::AddTool(TimelineWidget *parent) :
Tool(parent),
@@ -46,12 +47,21 @@ void TimelineWidget::AddTool::MouseRelease(TimelineViewMouseEvent *event)
if (ghost_) {
if (!ghost_->AdjustedLength().isNull()) {
QUndoCommand* command = new QUndoCommand();
ClipBlock* clip = new ClipBlock();
clip->set_length(ghost_->AdjustedLength());
olive::undo_stack.push(new TrackPlaceBlockCommand(parent()->timeline_node_->track_list(track.type()),
track.index(),
clip,
ghost_->GetAdjustedIn()));
new NodeAddCommand(static_cast<NodeGraph*>(parent()->timeline_node_->parent()),
clip,
command);
new TrackPlaceBlockCommand(parent()->timeline_node_->track_list(track.type()),
track.index(),
clip,
ghost_->GetAdjustedIn(),
command);
olive::undo_stack.push(command);
}
parent()->ClearGhosts();
+9 -3
View File
@@ -30,6 +30,7 @@
#include "node/color/opacity/opacity.h"
#include "node/input/media/audio/audio.h"
#include "node/input/media/video/video.h"
#include "widget/nodeview/nodeviewundo.h"
TrackType TrackTypeFromStreamType(Stream::Type stream_type)
{
@@ -207,6 +208,7 @@ void TimelineWidget::ImportTool::DragDrop(TimelineViewMouseEvent *event)
{
if (parent()->HasGhosts()) {
QUndoCommand* command = new QUndoCommand();
NodeGraph* dst_graph = static_cast<NodeGraph*>(parent()->timeline_node_->parent());
QVector<Block*> block_items(parent()->ghost_items_.size());
@@ -218,6 +220,7 @@ void TimelineWidget::ImportTool::DragDrop(TimelineViewMouseEvent *event)
ClipBlock* clip = new ClipBlock();
clip->set_length(ghost->Length());
clip->set_block_name(footage_stream->footage()->name());
new NodeAddCommand(dst_graph, clip, command);
switch (footage_stream->type()) {
case Stream::kVideo:
@@ -225,10 +228,12 @@ void TimelineWidget::ImportTool::DragDrop(TimelineViewMouseEvent *event)
{
VideoInput* video_input = new VideoInput();
video_input->SetFootage(footage_stream);
NodeParam::ConnectEdge(video_input->output(), clip->texture_input());
new NodeAddCommand(dst_graph, video_input, command);
new NodeEdgeAddCommand(video_input->output(), clip->texture_input(), command);
TransformDistort* transform = new TransformDistort();
NodeParam::ConnectEdge(transform->output(), video_input->matrix_input());
new NodeAddCommand(dst_graph, transform, command);
new NodeEdgeAddCommand(transform->output(), video_input->matrix_input(), command);
//OpacityNode* opacity = new OpacityNode();
//NodeParam::ConnectEdge(opacity->texture_output(), clip->texture_input());
@@ -239,7 +244,8 @@ void TimelineWidget::ImportTool::DragDrop(TimelineViewMouseEvent *event)
{
AudioInput* audio_input = new AudioInput();
audio_input->SetFootage(footage_stream);
NodeParam::ConnectEdge(audio_input->output(), clip->texture_input());
new NodeAddCommand(dst_graph, audio_input, command);
new NodeEdgeAddCommand(audio_input->output(), clip->texture_input(), command);
break;
}
default:
+5 -5
View File
@@ -30,6 +30,7 @@
#include "config/config.h"
#include "core.h"
#include "node/block/gap/gap.h"
#include "widget/nodeview/nodeviewundo.h"
TimelineWidget::PointerTool::PointerTool(TimelineWidget *parent) :
Tool(parent),
@@ -158,10 +159,6 @@ void TimelineWidget::PointerTool::MouseReleaseInternal(TimelineViewMouseEvent *e
{
Q_UNUSED(event)
// We create a QObject on the stack so that when we allocate objects on the heap, they aren't parent-less and will
// get cleaned up if they aren't re-parented by the attached NodeGraph
QObject block_memory_manager;
QUndoCommand* command = new QUndoCommand();
// Since all the ghosts will be leaving their old position in some way, we replace all of them with gaps here so the
@@ -171,9 +168,12 @@ void TimelineWidget::PointerTool::MouseReleaseInternal(TimelineViewMouseEvent *e
// Replace old Block with a new Gap
GapBlock* gap = new GapBlock();
gap->setParent(&block_memory_manager);
gap->set_length(b->length());
new NodeAddCommand(static_cast<NodeGraph*>(b->parent()),
gap,
command);
new TrackReplaceBlockCommand(parent()->GetTrackFromReference(ghost->Track()),
b,
gap,
+6 -1
View File
@@ -21,6 +21,7 @@
#include "widget/timelinewidget/timelinewidget.h"
#include "node/block/gap/gap.h"
#include "widget/nodeview/nodeviewundo.h"
TimelineWidget::RippleTool::RippleTool(TimelineWidget* parent) :
PointerTool(parent)
@@ -48,13 +49,15 @@ void TimelineWidget::RippleTool::MouseReleaseInternal(TimelineViewMouseEvent *ev
if (ghost->AdjustedLength() > 0) {
GapBlock* gap = new GapBlock();
gap->set_length(ghost->AdjustedLength());
new NodeAddCommand(static_cast<NodeGraph*>(parent()->timeline_node_->parent()), gap, command);
Block* block_to_append_gap_to = Node::ValueToPtr<Block>(ghost->data(TimelineViewGhostItem::kReferenceBlock));
new TrackInsertBlockBetweenBlocksCommand(parent()->GetTrackFromReference(ghost->Track()),
gap,
block_to_append_gap_to,
block_to_append_gap_to->next());
block_to_append_gap_to->next(),
command);
}
} else {
// This was a Block that already existed
@@ -68,6 +71,8 @@ void TimelineWidget::RippleTool::MouseReleaseInternal(TimelineViewMouseEvent *ev
} else {
// Assumed the Block was a Gap and it was reduced to zero length, remove it here
new TrackRippleRemoveBlockCommand(parent()->GetTrackFromReference(ghost->Track()), b, command);
new NodeRemoveCommand(static_cast<NodeGraph*>(b->parent()), {b}, command);
}
}
}
@@ -21,6 +21,7 @@
#include "widget/timelinewidget/timelinewidget.h"
#include "node/block/gap/gap.h"
#include "widget/nodeview/nodeviewundo.h"
TimelineWidget::RollingTool::RollingTool(TimelineWidget* parent) :
PointerTool(parent)
@@ -43,6 +44,9 @@ void TimelineWidget::RollingTool::MouseReleaseInternal(TimelineViewMouseEvent *e
// We'll need to insert a gap here, so we'll do a Place command instead
GapBlock* gap = new GapBlock();
gap->set_length(ghost->Length());
new NodeAddCommand(static_cast<NodeGraph*>(b->parent()),
gap,
command);
new TrackReplaceBlockCommand(parent()->GetTrackFromReference(ghost->Track()), b, gap, command);
}
+2
View File
@@ -21,6 +21,7 @@
#include "widget/timelinewidget/timelinewidget.h"
#include "node/block/gap/gap.h"
#include "widget/nodeview/nodeviewundo.h"
TimelineWidget::SlideTool::SlideTool(TimelineWidget* parent) :
PointerTool(parent)
@@ -46,6 +47,7 @@ void TimelineWidget::SlideTool::MouseReleaseInternal(TimelineViewMouseEvent *eve
} else if (ghost->mode() == olive::timeline::kMove && b->previous() == nullptr) {
GapBlock* gap = new GapBlock();
gap->set_length(ghost->InAdjustment());
new NodeAddCommand(static_cast<NodeGraph*>(b->parent()), gap, command);
new TrackPrependBlockCommand(parent()->GetTrackFromReference(ghost->Track()), gap, command);
}
}
@@ -94,6 +94,9 @@ void TimelineWidget::TransitionTool::MouseRelease(TimelineViewMouseEvent *event)
QUndoCommand* command = new QUndoCommand();
// Place transition in place
new NodeAddCommand(static_cast<NodeGraph*>(parent()->timeline_node_->parent()),
transition,
command);
new TrackPlaceBlockCommand(parent()->timeline_node_->track_list(track.type()),
track.index(),
transition,
+9 -9
View File
@@ -183,7 +183,7 @@ void TrackRippleRemoveAreaCommand::redo()
splice_original_length_ = splice_->length();
splice_->set_length(out_ - splice_->in());
track_->AddBlockToGraph(copy);
static_cast<NodeGraph*>(track_->parent())->AddNode(copy);
Node::CopyInputs(splice_, copy);
track_->InsertBlockAfter(copy, splice_);
@@ -213,6 +213,8 @@ void TrackRippleRemoveAreaCommand::redo()
// Remove all blocks that are flagged for removal
foreach (Block* remove_block, removed_blocks_) {
track_->RippleRemoveBlock(remove_block);
// FIXME: Delete blocks from graph and restore them in undo
}
// If we picked up a block to trim the out point of
@@ -221,13 +223,12 @@ void TrackRippleRemoveAreaCommand::redo()
}
// If we were given a block to insert, insert it here
if (insert_ != nullptr) {
track_->AddBlockToGraph(insert_);
if (trim_out_ == nullptr) {
if (insert_) {
qDebug() << "Insert is" << insert_ << ", parent is" << insert_->parent();
if (!trim_out_) {
// This is the start of the Sequence
track_->PrependBlock(insert_);
} else if (trim_in_ == nullptr) {
} else if (!trim_in_) {
// This is the end of the Sequence
track_->AppendBlock(insert_);
} else {
@@ -291,7 +292,6 @@ TrackPlaceBlockCommand::TrackPlaceBlockCommand(TrackList *timeline, int track, B
gap_(nullptr)
{
insert_ = block;
insert_->setParent(&memory_manager_);
}
void TrackPlaceBlockCommand::redo()
@@ -315,6 +315,7 @@ void TrackPlaceBlockCommand::redo()
// If so, insert a gap here
gap_ = new GapBlock();
gap_->set_length(in_ - track_->track_length());
static_cast<NodeGraph*>(track_->parent())->AddNode(gap_);
track_->AppendBlock(gap_);
}
@@ -331,7 +332,6 @@ void TrackPlaceBlockCommand::undo()
{
if (append_) {
track_->RippleRemoveBlock(insert_);
TakeNodeFromParentGraph(insert_, &memory_manager_);
if (gap_ != nullptr) {
track_->RippleRemoveBlock(gap_);
@@ -365,7 +365,7 @@ void BlockSplitCommand::redo()
block_->set_length(new_length_);
track_->AddBlockToGraph(new_block_);
static_cast<NodeGraph*>(block_->parent())->AddNode(new_block_);
Node::CopyInputs(block_, new_block_);
// Will re-parent new_block_ to the track's graph