[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(); 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();
} }

View file

@ -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;
}; };