feat(editor): undo/redo action buttons reflect state

This commit is contained in:
maelstrom 2025-06-23 17:46:52 +02:00
parent b5fc91eea0
commit cef5db5b2d
3 changed files with 22 additions and 1 deletions

View file

@ -103,6 +103,15 @@ MainWindow::MainWindow(QWidget *parent)
ui->propertiesView->init(); ui->propertiesView->init();
ui->mdiArea->setTabsClosable(true); 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) { void MainWindow::closeEvent(QCloseEvent* evt) {

View file

@ -10,6 +10,7 @@ void UndoHistory::PushState(UndoState state) {
undoHistory.erase(undoHistory.begin(), undoHistory.begin()+maxBufferSize-(int)undoHistory.size()-1); undoHistory.erase(undoHistory.begin(), undoHistory.begin()+maxBufferSize-(int)undoHistory.size()-1);
undoHistory.push_back(state); undoHistory.push_back(state);
undoStateListener(!undoHistory.empty(), !redoHistory.empty());
} }
void UndoHistory::Undo() { void UndoHistory::Undo() {
@ -37,6 +38,7 @@ void UndoHistory::Undo() {
} }
processingUndo = false; processingUndo = false;
undoStateListener(!undoHistory.empty(), !redoHistory.empty());
} }
void UndoHistory::Redo() { void UndoHistory::Redo() {
@ -64,4 +66,9 @@ void UndoHistory::Redo() {
} }
processingUndo = false; processingUndo = false;
} undoStateListener(!undoHistory.empty(), !redoHistory.empty());
}
void UndoHistory::SetUndoStateListener(UndoStateChangedListener listener) {
undoStateListener = listener;
}

View file

@ -38,16 +38,21 @@ struct UndoStateSelectionChanged {
typedef std::variant<UndoStatePropertyChanged, UndoStateInstanceCreated, UndoStateInstanceRemoved, UndoStateInstanceReparented, UndoStateSelectionChanged> UndoStateChange; typedef std::variant<UndoStatePropertyChanged, UndoStateInstanceCreated, UndoStateInstanceRemoved, UndoStateInstanceReparented, UndoStateSelectionChanged> UndoStateChange;
typedef std::vector<UndoStateChange> UndoState; typedef std::vector<UndoStateChange> UndoState;
typedef std::function<void(bool canUndo, bool canRedo)> UndoStateChangedListener;
class UndoHistory { class UndoHistory {
// Ignore PushState requests // Ignore PushState requests
bool processingUndo = false; bool processingUndo = false;
std::deque<UndoState> undoHistory; std::deque<UndoState> undoHistory;
std::stack<UndoState> redoHistory; std::stack<UndoState> redoHistory;
UndoStateChangedListener undoStateListener;
public: public:
int maxBufferSize = 100; int maxBufferSize = 100;
void PushState(UndoState); void PushState(UndoState);
void Undo(); void Undo();
void Redo(); void Redo();
void SetUndoStateListener(UndoStateChangedListener listener);
}; };