fixed all undos biatch

This commit is contained in:
itsmattkc
2018-07-16 00:05:40 +01:00
parent a614803b6c
commit 4d53cb41c1
7 changed files with 202 additions and 15 deletions
+41 -1
View File
@@ -1,5 +1,7 @@
#include "labelslider.h"
#include "project/undo.h"
#include <QMouseEvent>
#include <QInputDialog>
#include <QApplication>
@@ -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);
}
}
+15 -1
View File
@@ -2,6 +2,7 @@
#define LABELSLIDER_H
#include <QLabel>
#include <QUndoCommand>
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
+27
View File
@@ -13,10 +13,23 @@
#include <QDebug>
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);
}
}
+14
View File
@@ -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