From 4d53cb41c135ff2c87e0a15e98827740ee8941e7 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Mon, 16 Jul 2018 00:05:40 +0100 Subject: [PATCH] fixed all undos biatch --- panels/effectcontrols.cpp | 86 ++++++++++++++++++++++++++++++++++----- panels/effectcontrols.h | 30 +++++++++++++- project/sequence.h | 2 - ui/labelslider.cpp | 42 ++++++++++++++++++- ui/labelslider.h | 16 +++++++- ui/sourcetable.cpp | 27 ++++++++++++ ui/sourcetable.h | 14 +++++++ 7 files changed, 202 insertions(+), 15 deletions(-) diff --git a/panels/effectcontrols.cpp b/panels/effectcontrols.cpp index a7107f92f..3155df5ab 100644 --- a/panels/effectcontrols.cpp +++ b/panels/effectcontrols.cpp @@ -5,11 +5,13 @@ #include #include +#include "panels/panels.h" #include "effects/effects.h" #include "project/clip.h" #include "project/effect.h" #include "ui/collapsiblewidget.h" #include "project/sequence.h" +#include "project/undo.h" EffectControls::EffectControls(QWidget *parent) : QDockWidget(parent), @@ -26,13 +28,15 @@ EffectControls::~EffectControls() } void EffectControls::menu_select(QAction* q) { + EffectAddCommand* command = new EffectAddCommand(); for (int i=0;iget_clip(selected_clips.at(i)); if ((clip->track < 0 && video_menu) || (clip->track >= 0 && !video_menu)) { - clip->effects.append(create_effect(q->data().toInt(), clip)); + command->clips.append(clip); + command->effects.append(create_effect(q->data().toInt(), clip)); } } - reload_clips(); + undo_stack.push(command); } void EffectControls::show_menu(bool video) { @@ -117,22 +121,21 @@ void EffectControls::load_effects() { void EffectControls::delete_effects() { // load in new clips - QVector fx_to_del; + EffectDeleteCommand* command = new EffectDeleteCommand(); for (int i=0;iget_clip(selected_clips.at(i)); for (int j=0;jeffects.size();j++) { Effect* effect = c->effects.at(j); if (effect->container->selected) { - c->effects.removeAt(j); - fx_to_del.append(effect); + command->clips.append(c); + command->fx.append(j); } } } - if (fx_to_del.size() > 0) { - reload_clips(); - for (int i=0;iclips.size() > 0) { + undo_stack.push(command); + } else { + delete command; } } @@ -176,3 +179,66 @@ bool EffectControls::is_focused() { } return false; } + +EffectAddCommand::EffectAddCommand() : done(false) {} + +EffectAddCommand::~EffectAddCommand() { + if (!done) { + for (int i=0;ieffects.size();j++) { + if (c->effects.at(j) == effects.at(i)) { + c->effects.removeAt(j); + break; + } + } + } + panel_effect_controls->reload_clips(); + done = false; +} + +void EffectAddCommand::redo() { + for (int i=0;ieffects.append(effects.at(i)); + } + panel_effect_controls->reload_clips(); + done = true; +} + +EffectDeleteCommand::EffectDeleteCommand() : done(false) {} + +EffectDeleteCommand::~EffectDeleteCommand() { + if (done) { + for (int i=0;ieffects.insert(fx.at(i), deleted_objects.at(i)); + } + panel_effect_controls->reload_clips(); + done = false; +} + +void EffectDeleteCommand::redo() { + deleted_objects.clear(); + for (int i=0;ieffects.at(fx_id)); + c->effects.removeAt(fx_id); + } + panel_effect_controls->reload_clips(); + done = true; +} diff --git a/panels/effectcontrols.h b/panels/effectcontrols.h index 929e3e2b8..d893c11be 100644 --- a/panels/effectcontrols.h +++ b/panels/effectcontrols.h @@ -2,9 +2,11 @@ #define EFFECTCONTROLS_H #include +#include struct Clip; class QMenu; +class Effect; namespace Ui { class EffectControls; @@ -22,6 +24,7 @@ public: void clear_effects(bool clear_cache); void delete_effects(); bool is_focused(); + void reload_clips(); private slots: void menu_select(QAction* q); @@ -37,8 +40,33 @@ private: QVector selected_clips; void show_menu(bool video); void load_effects(); - void reload_clips(); + bool video_menu; }; +class EffectAddCommand : public QUndoCommand { +public: + EffectAddCommand(); + ~EffectAddCommand(); + void undo(); + void redo(); + QVector clips; + QVector effects; +private: + bool done; +}; + +class EffectDeleteCommand : public QUndoCommand { +public: + EffectDeleteCommand(); + ~EffectDeleteCommand(); + void undo(); + void redo(); + QVector clips; + QVector fx; +private: + bool done; + QVector deleted_objects; +}; + #endif // EFFECTCONTROLS_H diff --git a/project/sequence.h b/project/sequence.h index 566e234de..a74fce107 100644 --- a/project/sequence.h +++ b/project/sequence.h @@ -1,8 +1,6 @@ #ifndef SEQUENCE_H #define SEQUENCE_H -#define UNDO_LIMIT 100 - #include #include "project/clip.h" diff --git a/ui/labelslider.cpp b/ui/labelslider.cpp index 8a7584d41..88cad0e31 100644 --- a/ui/labelslider.cpp +++ b/ui/labelslider.cpp @@ -1,5 +1,7 @@ #include "labelslider.h" +#include "project/undo.h" + #include #include #include @@ -25,6 +27,7 @@ void LabelSlider::set_value(float v) { } else { internal_value = v; } + setText(QString::number(internal_value)); emit valueChanged(); } @@ -51,9 +54,16 @@ void LabelSlider::set_maximum_value(float v) { void LabelSlider::mousePressEvent(QMouseEvent *ev) { if (ev->modifiers() & Qt::AltModifier) { + ValueChangeCommand* vcc = new ValueChangeCommand(); + vcc->source = this; + vcc->old_val = internal_value; + vcc->new_val = default_value; + undo_stack.push(vcc); + set_value(default_value); } else { qApp->setOverrideCursor(Qt::BlankCursor); + drag_start_value = internal_value; drag_start = true; drag_start_x = cursor().pos().x(); drag_start_y = cursor().pos().y(); @@ -73,9 +83,39 @@ void LabelSlider::mouseReleaseEvent(QMouseEvent*) { qApp->restoreOverrideCursor(); drag_start = false; if (drag_proc) { + // send undo event + ValueChangeCommand* vcc = new ValueChangeCommand(); + vcc->source = this; + vcc->old_val = drag_start_value; + vcc->new_val = internal_value; + undo_stack.push(vcc); + drag_proc = false; } else { - set_value(QInputDialog::getDouble(this, "Set Value", "New value:", internal_value)); + double d = QInputDialog::getDouble(this, "Set Value", "New value:", internal_value); + if (d != internal_value) { + ValueChangeCommand* vcc = new ValueChangeCommand(); + vcc->source = this; + vcc->old_val = internal_value; + + set_value(d); + + vcc->new_val = internal_value; + undo_stack.push(vcc); + } } } } + +ValueChangeCommand::ValueChangeCommand() : done(true) {} + +void ValueChangeCommand::undo() { + source->set_value(old_val); + done = false; +} + +void ValueChangeCommand::redo() { + if (!done) { + source->set_value(new_val); + } +} diff --git a/ui/labelslider.h b/ui/labelslider.h index 21ab290de..ba14d1a48 100644 --- a/ui/labelslider.h +++ b/ui/labelslider.h @@ -2,6 +2,7 @@ #define LABELSLIDER_H #include +#include class LabelSlider : public QLabel { @@ -18,8 +19,9 @@ protected: void mouseMoveEvent(QMouseEvent *ev); void mouseReleaseEvent(QMouseEvent *ev); private: - float internal_value; float default_value; + float internal_value; + float drag_start_value; bool min_enabled; float min_value; @@ -34,4 +36,16 @@ signals: void valueChanged(); }; +class ValueChangeCommand : public QUndoCommand { +public: + ValueChangeCommand(); + LabelSlider* source; + float old_val; + float new_val; + void undo(); + void redo(); +private: + bool done; +}; + #endif // LABELSLIDER_H diff --git a/ui/sourcetable.cpp b/ui/sourcetable.cpp index 6ab1f1c46..35a2e73d0 100644 --- a/ui/sourcetable.cpp +++ b/ui/sourcetable.cpp @@ -13,10 +13,23 @@ #include SourceTable::SourceTable(QWidget* parent) : QTreeWidget(parent) { + editing_item = NULL; sortByColumn(0, Qt::AscendingOrder); rename_timer.setInterval(500); connect(&rename_timer, SIGNAL(timeout()), this, SLOT(rename_interval())); connect(this, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(item_click(QTreeWidgetItem*,int))); + connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(item_renamed(QTreeWidgetItem*))); +} + +void SourceTable::item_renamed(QTreeWidgetItem* item) { + if (editing_item == item) { + MediaRename* mr = new MediaRename(); + mr->from = editing_item_name; + mr->item = editing_item; + mr->to = editing_item->text(0); + undo_stack.push(mr); + editing_item = NULL; + } } void SourceTable::stop_rename_timer() { @@ -26,6 +39,7 @@ void SourceTable::stop_rename_timer() { void SourceTable::rename_interval() { stop_rename_timer(); if (hasFocus() && editing_item != NULL) { + editing_item_name = editing_item->text(0); editItem(editing_item, 0); } } @@ -154,3 +168,16 @@ void MediaMove::redo() { } } } + +MediaRename::MediaRename() : done(true) {} + +void MediaRename::undo() { + item->setText(0, from); + done = false; +} + +void MediaRename::redo() { + if (!done) { + item->setText(0, to); + } +} diff --git a/ui/sourcetable.h b/ui/sourcetable.h index 19f7bc22a..a2ee63442 100644 --- a/ui/sourcetable.h +++ b/ui/sourcetable.h @@ -21,10 +21,12 @@ protected: private: QTimer rename_timer; QTreeWidgetItem* editing_item; + QString editing_item_name; private slots: void rename_interval(); void item_click(QTreeWidgetItem* item, int column); void stop_rename_timer(); + void item_renamed(QTreeWidgetItem *item); }; class MediaMove : public QUndoCommand { @@ -39,4 +41,16 @@ private: SourceTable* table; }; +class MediaRename : public QUndoCommand { +public: + MediaRename(); + QTreeWidgetItem* item; + QString from; + QString to; + void undo(); + void redo(); +private: + bool done; +}; + #endif // SOURCETABLE_H