[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:
BruebachL 2026-09-18 11:58:29 +02:00 committed by GitHub
parent 87443d58f7
commit 2a3a8982a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 26 deletions

View file

@ -14,42 +14,32 @@ void DeckListHistoryManager::clear()
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;
}
// Peek at the memento we are going to restore
const DeckListMemento &mementoToRestore = undoStack.top();
// The reason is read before the source is popped.
const QString reason = source.top().getReason();
// Save current state for redo
DeckListMemento currentState = deck->createMemento(mementoToRestore.getReason());
redoStack.push(currentState);
// Save the current state so the opposite direction can return to it.
target.push(deck->createMemento(reason));
// Pop the last state from undo stack and restore it
DeckListMemento memento = undoStack.pop();
deck->restoreMemento(memento);
// Apply the state we are moving to.
deck->restoreMemento(source.pop());
emit undoRedoStateChanged();
}
void DeckListHistoryManager::undo(DeckList *deck)
{
restoreAndSwap(undoStack, redoStack, deck);
}
void DeckListHistoryManager::redo(DeckList *deck)
{
if (redoStack.isEmpty()) {
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();
restoreAndSwap(redoStack, undoStack, deck);
}

View file

@ -47,6 +47,13 @@ public:
}
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> redoStack;
};