mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 09:05:10 -07:00
[DeckList] Deduplicate undo/redo state switching (#7302)
undo() and redo() were mirror images that differed only in which stack was the source. Both now delegate to a single restoreAndSwap(source, target, deck) helper, so the save-current- state, apply-memento and signal-emission logic lives in one place. Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
87443d58f7
commit
2a3a8982a6
2 changed files with 23 additions and 26 deletions
|
|
@ -14,42 +14,32 @@ void DeckListHistoryManager::clear()
|
||||||
emit undoRedoStateChanged();
|
emit undoRedoStateChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckListHistoryManager::undo(DeckList *deck)
|
void DeckListHistoryManager::restoreAndSwap(QStack<DeckListMemento> &source,
|
||||||
|
QStack<DeckListMemento> &target,
|
||||||
|
DeckList *deck)
|
||||||
{
|
{
|
||||||
if (undoStack.isEmpty()) {
|
if (source.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Peek at the memento we are going to restore
|
// The reason is read before the source is popped.
|
||||||
const DeckListMemento &mementoToRestore = undoStack.top();
|
const QString reason = source.top().getReason();
|
||||||
|
|
||||||
// Save current state for redo
|
// Save the current state so the opposite direction can return to it.
|
||||||
DeckListMemento currentState = deck->createMemento(mementoToRestore.getReason());
|
target.push(deck->createMemento(reason));
|
||||||
redoStack.push(currentState);
|
|
||||||
|
|
||||||
// Pop the last state from undo stack and restore it
|
// Apply the state we are moving to.
|
||||||
DeckListMemento memento = undoStack.pop();
|
deck->restoreMemento(source.pop());
|
||||||
deck->restoreMemento(memento);
|
|
||||||
|
|
||||||
emit undoRedoStateChanged();
|
emit undoRedoStateChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DeckListHistoryManager::undo(DeckList *deck)
|
||||||
|
{
|
||||||
|
restoreAndSwap(undoStack, redoStack, deck);
|
||||||
|
}
|
||||||
|
|
||||||
void DeckListHistoryManager::redo(DeckList *deck)
|
void DeckListHistoryManager::redo(DeckList *deck)
|
||||||
{
|
{
|
||||||
if (redoStack.isEmpty()) {
|
restoreAndSwap(redoStack, undoStack, deck);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Peek at the memento we are going to restore
|
|
||||||
const DeckListMemento &mementoToRestore = redoStack.top();
|
|
||||||
|
|
||||||
// Save current state for undo
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,13 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* @brief Moves one state from @p source to @p target, applying it to @p deck.
|
||||||
|
*
|
||||||
|
* Used by both undo (undoStack -> redoStack) and redo (redoStack -> undoStack).
|
||||||
|
*/
|
||||||
|
void restoreAndSwap(QStack<DeckListMemento> &source, QStack<DeckListMemento> &target, DeckList *deck);
|
||||||
|
|
||||||
QStack<DeckListMemento> undoStack;
|
QStack<DeckListMemento> undoStack;
|
||||||
QStack<DeckListMemento> redoStack;
|
QStack<DeckListMemento> redoStack;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue