Merge branch 'feature/undoredo' into 'main'
Feature/undoredo See merge request p2406187/sae201!9
This commit is contained in:
commit
84217adccf
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 "ui_mainwindow.h"
|
||||
#include "path.h"
|
||||
|
||||
#include "Undo.h"
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QLineEdit>
|
||||
#include <QTimer>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
#include <QUndoStack>
|
||||
#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;
|
||||
Path* currentPath;
|
||||
void loadImage(QString fileName);
|
||||
QUndoStack *undoStack;
|
||||
};
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
@ -120,6 +120,9 @@
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="scaledContents">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QWidget" name="widget_5" native="true">
|
||||
<property name="geometry">
|
||||
|
||||
@ -15,6 +15,7 @@ SOURCES += \
|
||||
path.cpp \
|
||||
|
||||
HEADERS += \
|
||||
Undo.h \
|
||||
mainwindow.h \
|
||||
step.h \
|
||||
path.h \
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user