Undo and Redo command
This commit is contained in:
parent
57d77cf7cf
commit
0cd4f95865
21
Undo.h
Normal file
21
Undo.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef UNDO_H
|
||||||
|
#define UNDO_H
|
||||||
|
|
||||||
|
#include <QUndoCommand>
|
||||||
|
#include <QLineEdit>
|
||||||
|
|
||||||
|
class LineEditCommand : public QUndoCommand {
|
||||||
|
public:
|
||||||
|
LineEditCommand(QLineEdit* edit, const QString& oldText, const QString& newText)
|
||||||
|
: m_edit(edit), m_oldText(oldText), m_newText(newText) {}
|
||||||
|
|
||||||
|
void undo() override { m_edit->setText(m_oldText); }
|
||||||
|
void redo() override { m_edit->setText(m_newText); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
QLineEdit* m_edit;
|
||||||
|
QString m_oldText;
|
||||||
|
QString m_newText;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // UNDO_H
|
||||||
@ -1,15 +1,49 @@
|
|||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
#include "Undo.h"
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent)
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
, ui(new Ui::MainWindow)
|
, ui(new Ui::MainWindow)
|
||||||
|
, undoStack(new QUndoStack(this))
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
|
||||||
|
connect(ui->titleEdit, &QLineEdit::editingFinished, this, [this]() {
|
||||||
|
static QString previousText;
|
||||||
|
QString currentText = ui->titleEdit->text();
|
||||||
|
if(previousText != currentText) {
|
||||||
|
undoStack->push(new LineEditCommand(ui->titleEdit, previousText, currentText));
|
||||||
|
previousText = currentText;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(ui->locEdit, &QLineEdit::editingFinished, this, [this]() {
|
||||||
|
static QString previousText;
|
||||||
|
QString currentText = ui->locEdit->text();
|
||||||
|
if(previousText != currentText) {
|
||||||
|
undoStack->push(new LineEditCommand(ui->locEdit, previousText, currentText));
|
||||||
|
previousText = currentText;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(ui->imagePath, &QLineEdit::editingFinished, this, [this]() {
|
||||||
|
static QString previousText;
|
||||||
|
QString currentText = ui->imagePath->text();
|
||||||
|
if(previousText != currentText) {
|
||||||
|
undoStack->push(new LineEditCommand(ui->imagePath, previousText, currentText));
|
||||||
|
previousText = currentText;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(ui->actionEditUndo, &QAction::triggered, undoStack, &QUndoStack::undo);
|
||||||
|
connect(ui->actionEditRedo, &QAction::triggered, undoStack, &QUndoStack::redo);
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
@ -136,4 +170,3 @@ void MainWindow::on_actionopenFile_triggered()
|
|||||||
{
|
{
|
||||||
this->loadPath();
|
this->loadPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
#define MAINWINDOW_H
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include <QUndoStack>
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -35,6 +35,7 @@ private slots:
|
|||||||
|
|
||||||
void on_actionopenFile_triggered();
|
void on_actionopenFile_triggered();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
QString currentFile;
|
QString currentFile;
|
||||||
@ -42,5 +43,6 @@ private:
|
|||||||
QList<Path*> path;
|
QList<Path*> path;
|
||||||
Path* currentPath;
|
Path* currentPath;
|
||||||
void loadImage(QString fileName);
|
void loadImage(QString fileName);
|
||||||
|
QUndoStack *undoStack;
|
||||||
};
|
};
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|||||||
@ -15,6 +15,7 @@ SOURCES += \
|
|||||||
path.cpp \
|
path.cpp \
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
Undo.h \
|
||||||
mainwindow.h \
|
mainwindow.h \
|
||||||
step.h \
|
step.h \
|
||||||
path.h \
|
path.h \
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user