mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-11 12:54:10 -07:00
Refactor to .cpp, delegate undo/redo to manager, don't return memento
Took 8 minutes
This commit is contained in:
parent
a4fd752ddf
commit
126b338d3e
4 changed files with 62 additions and 53 deletions
|
|
@ -107,7 +107,7 @@ void DeckListHistoryManagerWidget::doUndo()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
deckListModel->getDeckList()->restoreMemento(historyManager->undo(deckListModel->getDeckList()));
|
historyManager->undo(deckListModel->getDeckList());
|
||||||
deckListModel->rebuildTree();
|
deckListModel->rebuildTree();
|
||||||
emit deckListModel->layoutChanged();
|
emit deckListModel->layoutChanged();
|
||||||
emit requestDisplayWidgetSync();
|
emit requestDisplayWidgetSync();
|
||||||
|
|
@ -121,7 +121,7 @@ void DeckListHistoryManagerWidget::doRedo()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
deckListModel->getDeckList()->restoreMemento(historyManager->redo(deckListModel->getDeckList()));
|
historyManager->redo(deckListModel->getDeckList());
|
||||||
deckListModel->rebuildTree();
|
deckListModel->rebuildTree();
|
||||||
|
|
||||||
emit deckListModel->layoutChanged();
|
emit deckListModel->layoutChanged();
|
||||||
|
|
@ -144,13 +144,13 @@ void DeckListHistoryManagerWidget::onListClicked(QListWidgetItem *item)
|
||||||
const auto redoStack = historyManager->getRedoStack();
|
const auto redoStack = historyManager->getRedoStack();
|
||||||
int steps = redoStack.size() - index;
|
int steps = redoStack.size() - index;
|
||||||
for (int i = 0; i < steps; ++i) {
|
for (int i = 0; i < steps; ++i) {
|
||||||
deckListModel->getDeckList()->restoreMemento(historyManager->redo(deckListModel->getDeckList()));
|
historyManager->redo(deckListModel->getDeckList());
|
||||||
}
|
}
|
||||||
} else if (mode == "undo") {
|
} else if (mode == "undo") {
|
||||||
const auto undoStack = historyManager->getUndoStack();
|
const auto undoStack = historyManager->getUndoStack();
|
||||||
int steps = undoStack.size() - 1 - index;
|
int steps = undoStack.size() - 1 - index;
|
||||||
for (int i = 0; i < steps + 1; ++i) {
|
for (int i = 0; i < steps + 1; ++i) {
|
||||||
deckListModel->getDeckList()->restoreMemento(historyManager->undo(deckListModel->getDeckList()));
|
historyManager->undo(deckListModel->getDeckList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,13 @@ endif()
|
||||||
|
|
||||||
add_library(
|
add_library(
|
||||||
libcockatrice_deck_list STATIC
|
libcockatrice_deck_list STATIC
|
||||||
${MOC_SOURCES} libcockatrice/deck_list/abstract_deck_list_card_node.cpp
|
${MOC_SOURCES}
|
||||||
libcockatrice/deck_list/abstract_deck_list_node.cpp libcockatrice/deck_list/deck_list.cpp
|
libcockatrice/deck_list/abstract_deck_list_card_node.cpp
|
||||||
libcockatrice/deck_list/deck_list_card_node.cpp libcockatrice/deck_list/inner_deck_list_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)
|
add_dependencies(libcockatrice_deck_list libcockatrice_protocol)
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
@ -20,12 +20,7 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void save(DeckListMemento *memento)
|
void save(DeckListMemento *memento);
|
||||||
{
|
|
||||||
undoStack.push(memento);
|
|
||||||
redoStack.clear();
|
|
||||||
emit undoRedoStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool canUndo() const
|
bool canUndo() const
|
||||||
{
|
{
|
||||||
|
|
@ -36,47 +31,9 @@ public:
|
||||||
return !redoStack.isEmpty();
|
return !redoStack.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
DeckListMemento *undo(DeckList *deck)
|
void undo(DeckList *deck);
|
||||||
{
|
|
||||||
if (undoStack.isEmpty()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Peek at the memento we are going to restore
|
void redo(DeckList *deck);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
QStack<DeckListMemento *> getRedoStack() const
|
QStack<DeckListMemento *> getRedoStack() const
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue