From 0cd4f958650f801ac969903d49fdc5b06aba174f Mon Sep 17 00:00:00 2001 From: T'JAMPENS QUENTIN p2406187 Date: Thu, 19 Jun 2025 19:29:12 +0200 Subject: [PATCH] Undo and Redo command --- Undo.h | 21 +++++++++++++++++++++ mainwindow.cpp | 37 +++++++++++++++++++++++++++++++++++-- mainwindow.h | 4 +++- sae201.pro | 1 + 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 Undo.h diff --git a/Undo.h b/Undo.h new file mode 100644 index 0000000..e4ad16d --- /dev/null +++ b/Undo.h @@ -0,0 +1,21 @@ +#ifndef UNDO_H +#define UNDO_H + +#include +#include + +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 diff --git a/mainwindow.cpp b/mainwindow.cpp index 66811b9..5daf812 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,15 +1,49 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "path.h" - +#include "Undo.h" #include #include +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) + , undoStack(new QUndoStack(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() @@ -136,4 +170,3 @@ void MainWindow::on_actionopenFile_triggered() { this->loadPath(); } - diff --git a/mainwindow.h b/mainwindow.h index 618fba0..93e248f 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,7 +2,7 @@ #define MAINWINDOW_H #include - +#include #include "path.h" QT_BEGIN_NAMESPACE @@ -35,6 +35,7 @@ private slots: void on_actionopenFile_triggered(); + private: Ui::MainWindow *ui; QString currentFile; @@ -42,5 +43,6 @@ private: QList path; Path* currentPath; void loadImage(QString fileName); + QUndoStack *undoStack; }; #endif // MAINWINDOW_H diff --git a/sae201.pro b/sae201.pro index 8cb9d3f..04a7b14 100644 --- a/sae201.pro +++ b/sae201.pro @@ -15,6 +15,7 @@ SOURCES += \ path.cpp \ HEADERS += \ + Undo.h \ mainwindow.h \ step.h \ path.h \