diff --git a/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp b/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp index 907027fa3..e574ddf6c 100644 --- a/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp +++ b/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp @@ -107,7 +107,7 @@ void DeckListHistoryManagerWidget::doUndo() return; } - deckListModel->getDeckList()->restoreMemento(historyManager->undo(deckListModel->getDeckList())); + historyManager->undo(deckListModel->getDeckList()); deckListModel->rebuildTree(); emit deckListModel->layoutChanged(); emit requestDisplayWidgetSync(); @@ -121,7 +121,7 @@ void DeckListHistoryManagerWidget::doRedo() return; } - deckListModel->getDeckList()->restoreMemento(historyManager->redo(deckListModel->getDeckList())); + historyManager->redo(deckListModel->getDeckList()); deckListModel->rebuildTree(); emit deckListModel->layoutChanged(); @@ -144,13 +144,13 @@ void DeckListHistoryManagerWidget::onListClicked(QListWidgetItem *item) const auto redoStack = historyManager->getRedoStack(); int steps = redoStack.size() - index; for (int i = 0; i < steps; ++i) { - deckListModel->getDeckList()->restoreMemento(historyManager->redo(deckListModel->getDeckList())); + historyManager->redo(deckListModel->getDeckList()); } } else if (mode == "undo") { const auto undoStack = historyManager->getUndoStack(); int steps = undoStack.size() - 1 - index; for (int i = 0; i < steps + 1; ++i) { - deckListModel->getDeckList()->restoreMemento(historyManager->undo(deckListModel->getDeckList())); + historyManager->undo(deckListModel->getDeckList()); } } diff --git a/libcockatrice_deck_list/CMakeLists.txt b/libcockatrice_deck_list/CMakeLists.txt index b76c12ec3..d9b27354b 100644 --- a/libcockatrice_deck_list/CMakeLists.txt +++ b/libcockatrice_deck_list/CMakeLists.txt @@ -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) diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp new file mode 100644 index 000000000..fe44d0eb8 --- /dev/null +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp @@ -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(); +} \ No newline at end of file diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h index d8d0b473f..ca4a7e8dc 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h @@ -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 getRedoStack() const {