Refactor to .cpp, delegate undo/redo to manager, don't return memento

Took 8 minutes
This commit is contained in:
Lukas Brübach 2025-11-19 23:11:11 +01:00
parent a4fd752ddf
commit 126b338d3e
4 changed files with 62 additions and 53 deletions

View file

@ -20,9 +20,13 @@ endif()
add_library(
libcockatrice_deck_list STATIC
${MOC_SOURCES} libcockatrice/deck_list/abstract_deck_list_card_node.cpp
libcockatrice/deck_list/abstract_deck_list_node.cpp libcockatrice/deck_list/deck_list.cpp
libcockatrice/deck_list/deck_list_card_node.cpp libcockatrice/deck_list/inner_deck_list_node.cpp
${MOC_SOURCES}
libcockatrice/deck_list/abstract_deck_list_card_node.cpp
libcockatrice/deck_list/abstract_deck_list_node.cpp
libcockatrice/deck_list/deck_list.cpp
libcockatrice/deck_list/deck_list_card_node.cpp
libcockatrice/deck_list/deck_list_history_manager.cpp
libcockatrice/deck_list/inner_deck_list_node.cpp
)
add_dependencies(libcockatrice_deck_list libcockatrice_protocol)

View file

@ -0,0 +1,48 @@
#include "deck_list_history_manager.h"
void DeckListHistoryManager::save(DeckListMemento *memento)
{
undoStack.push(memento);
redoStack.clear();
emit undoRedoStateChanged();
}
void DeckListHistoryManager::undo(DeckList *deck)
{
if (undoStack.isEmpty()) {
return;
}
// Peek at the memento we are going to restore
DeckListMemento *mementoToRestore = undoStack.top();
// Save current state for redo using the same reason as the memento we're restoring
DeckListMemento *currentState = deck->createMemento(mementoToRestore->getReason());
redoStack.push(currentState);
// Pop the last state from undo stack and restore it
DeckListMemento *memento = undoStack.pop();
deck->restoreMemento(memento);
emit undoRedoStateChanged();
}
void DeckListHistoryManager::redo(DeckList *deck)
{
if (redoStack.isEmpty()) {
return;
}
// Peek at the memento we are going to restore
DeckListMemento *mementoToRestore = redoStack.top();
// Save current state for undo using the same reason as the memento we're restoring
DeckListMemento *currentState = deck->createMemento(mementoToRestore->getReason());
undoStack.push(currentState);
// Pop the next state from redo stack and restore it
DeckListMemento *memento = redoStack.pop();
deck->restoreMemento(memento);
emit undoRedoStateChanged();
}

View file

@ -20,12 +20,7 @@ public:
{
}
void save(DeckListMemento *memento)
{
undoStack.push(memento);
redoStack.clear();
emit undoRedoStateChanged();
}
void save(DeckListMemento *memento);
bool canUndo() const
{
@ -36,47 +31,9 @@ public:
return !redoStack.isEmpty();
}
DeckListMemento *undo(DeckList *deck)
{
if (undoStack.isEmpty()) {
return nullptr;
}
void undo(DeckList *deck);
// Peek at the memento we are going to restore
DeckListMemento *mementoToRestore = undoStack.top();
// Save current state for redo using the same reason as the memento we're restoring
DeckListMemento *currentState = deck->createMemento(mementoToRestore->getReason());
redoStack.push(currentState);
// Pop the last state from undo stack and restore it
DeckListMemento *memento = undoStack.pop();
deck->restoreMemento(memento);
emit undoRedoStateChanged();
return memento;
}
DeckListMemento *redo(DeckList *deck)
{
if (redoStack.isEmpty()) {
return nullptr;
}
// Peek at the memento we are going to restore
DeckListMemento *mementoToRestore = redoStack.top();
// Save current state for undo using the same reason as the memento we're restoring
DeckListMemento *currentState = deck->createMemento(mementoToRestore->getReason());
undoStack.push(currentState);
// Pop the next state from redo stack and restore it
DeckListMemento *memento = redoStack.pop();
deck->restoreMemento(memento);
emit undoRedoStateChanged();
return memento;
}
void redo(DeckList *deck);
QStack<DeckListMemento *> getRedoStack() const
{