From cef5db5b2d38d5cc28489af4cd1891974183a702 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Mon, 23 Jun 2025 17:46:52 +0200 Subject: [PATCH] feat(editor): undo/redo action buttons reflect state --- editor/mainwindow.cpp | 9 +++++++++ editor/undohistory.cpp | 9 ++++++++- editor/undohistory.h | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 3a47a7f..179b188 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -103,6 +103,15 @@ MainWindow::MainWindow(QWidget *parent) ui->propertiesView->init(); ui->mdiArea->setTabsClosable(true); + + // Undo/Redo + ui->actionUndo->setEnabled(false); + ui->actionRedo->setEnabled(false); + + undoManager.SetUndoStateListener([&](bool canUndo, bool canRedo) { + ui->actionUndo->setEnabled(canUndo); + ui->actionRedo->setEnabled(canRedo); + }); } void MainWindow::closeEvent(QCloseEvent* evt) { diff --git a/editor/undohistory.cpp b/editor/undohistory.cpp index 17923c6..f49b721 100644 --- a/editor/undohistory.cpp +++ b/editor/undohistory.cpp @@ -10,6 +10,7 @@ void UndoHistory::PushState(UndoState state) { undoHistory.erase(undoHistory.begin(), undoHistory.begin()+maxBufferSize-(int)undoHistory.size()-1); undoHistory.push_back(state); + undoStateListener(!undoHistory.empty(), !redoHistory.empty()); } void UndoHistory::Undo() { @@ -37,6 +38,7 @@ void UndoHistory::Undo() { } processingUndo = false; + undoStateListener(!undoHistory.empty(), !redoHistory.empty()); } void UndoHistory::Redo() { @@ -64,4 +66,9 @@ void UndoHistory::Redo() { } processingUndo = false; -} \ No newline at end of file + undoStateListener(!undoHistory.empty(), !redoHistory.empty()); +} + +void UndoHistory::SetUndoStateListener(UndoStateChangedListener listener) { + undoStateListener = listener; +} diff --git a/editor/undohistory.h b/editor/undohistory.h index bace6d8..b4cc5fc 100644 --- a/editor/undohistory.h +++ b/editor/undohistory.h @@ -38,16 +38,21 @@ struct UndoStateSelectionChanged { typedef std::variant UndoStateChange; typedef std::vector UndoState; +typedef std::function UndoStateChangedListener; class UndoHistory { // Ignore PushState requests bool processingUndo = false; std::deque undoHistory; std::stack redoHistory; + + UndoStateChangedListener undoStateListener; public: int maxBufferSize = 100; void PushState(UndoState); void Undo(); void Redo(); + + void SetUndoStateListener(UndoStateChangedListener listener); }; \ No newline at end of file