From 902ed1c84b48b50847bd850a473c15488d80d72d Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Thu, 11 Dec 2025 16:01:48 +0100 Subject: [PATCH] Added a undo view and react to change of the cleanness of the undo stack. --- mainwindow.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++---- mainwindow.h | 11 +++++++++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 36afc26..fc036d5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "../../ApplicationConfig.h" #include "Dialogs/newitemdialog.h" @@ -20,6 +21,7 @@ MainWindow::MainWindow(QWidget* parent) ui->setupUi(this); m_core = std::make_unique(); + setWindowTitle(windowTitle() + " [*]"); /// application icon const QString iconString = "://feature.png"; @@ -173,9 +175,33 @@ void MainWindow::deleteSelectedtItems() { } } +void MainWindow::onCleanStateChanged(bool clean) { + qInfo() << "Slot onCleanStateChanged triggered: clean:" << clean; + setWindowModified(!clean); + if (!clean) { + ui->statusbar->clearMessage(); + } +} + +void MainWindow::onShowUndoViewToggled(bool checked) { + // qInfo() << "Slot onShowUndoViewToggled toggled: checked:" << checked; + // m_undoView->setVisible(checked); + + /// workaround until m_showUndoViewAction is checkable + qInfo() << "Slot onShowUndoViewToggled triggered, toggleing undo view..."; + Q_UNUSED(checked); + if (m_modelUndoView->isVisible()) { + m_modelUndoView->setVisible(false); + + } else { + m_modelUndoView->setVisible(true); + } +} + void MainWindow::createActions() { // TODO add generic menu actions (file/new, edit/cut, ...) createFileActions(); + createUndoActions(); createEditActions(); } @@ -233,11 +259,15 @@ void MainWindow::createFileActions() { ui->menu_File->addAction(m_exitAct.get()); } -void MainWindow::createEditActions() { - m_modelUndoStack = m_core->getModUndoStack(); +void MainWindow::createUndoActions() { + if (!m_core) { + qCritical() << "No core instance set, aborting..."; + return; + } + m_modelUndoStack = m_core->getModelUndoStack(); + + connect(m_modelUndoStack, &QUndoStack::cleanChanged, this, &MainWindow::onCleanStateChanged); - connect(m_modelUndoStack.get(), SIGNAL(cleanChanged(bool)), this, - SLOT(onCleanStateChanged(bool))); m_undoAct.reset(m_modelUndoStack->createUndoAction(this, tr("&Undo"))); m_undoAct->setShortcuts(QKeySequence::Undo); m_undoAct->setStatusTip(tr("Undo the last operation")); @@ -248,6 +278,26 @@ void MainWindow::createEditActions() { m_redoAct->setStatusTip(tr("Redo the last operation")); ui->menu_Edit->addAction(m_redoAct.get()); + m_modelUndoView = make_unique(m_modelUndoStack); + m_modelUndoView->setWindowTitle(tr("Undo list")); + m_modelUndoView->setAttribute(Qt::WA_QuitOnClose, false); + m_modelUndoView->setMinimumSize(450, 600); + m_showModelUndoViewAct = make_unique(tr("Show undo/redo window"), this); + m_showModelUndoViewAct->setStatusTip(tr("Opens a window displaying the items on the undo stack")); + + // TODO showUndoViewAction should be checkable + // m_showUndoViewAction->setCheckable(true); + // connect(m_showUndoViewAction, &QAction::toggled, this, &MainWindow::onShowUndoViewToggled); + connect(m_showModelUndoViewAct.get(), &QAction::triggered, this, + &MainWindow::onShowUndoViewToggled); + + // TODO ? add a keyboard short cut? + // m_showUndoViewAction->setShortcuts(QKeySequence::Copy); + + ui->menu_View->addAction(m_showModelUndoViewAct.get()); +} + +void MainWindow::createEditActions() { m_cutAct = make_unique(tr("Cu&t"), this); m_cutAct->setShortcuts(QKeySequence::Cut); m_cutAct->setStatusTip( diff --git a/mainwindow.h b/mainwindow.h index f49bd80..4d720a1 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -10,6 +10,8 @@ class TableModel; QT_BEGIN_NAMESPACE class QUndoStack; + +class QUndoView; namespace Ui { class MainWindow; } @@ -46,13 +48,17 @@ class MainWindow : public QMainWindow { void openNewItemDialog(); void deleteSelectedtItems(); + void onCleanStateChanged(bool clean); + void onShowUndoViewToggled(bool checked); + private: Ui::MainWindow* ui; // GenericCore* m_core; unique_ptr m_core; shared_ptr m_tableModel; - shared_ptr m_modelUndoStack; + QUndoStack* m_modelUndoStack; + unique_ptr m_modelUndoView; /// File actions unique_ptr m_newFileAct; @@ -72,6 +78,8 @@ class MainWindow : public QMainWindow { unique_ptr m_openEditItemDialogAct; unique_ptr m_deleteItemAct; unique_ptr m_findItemAct; + /// View actions + unique_ptr m_showModelUndoViewAct; /// Dialogs unique_ptr m_newItemDialog; @@ -79,6 +87,7 @@ class MainWindow : public QMainWindow { /// Setup functions void createActions(); void createFileActions(); + void createUndoActions(); void createEditActions(); void createHelpMenu(); void createGuiDialogs();