[DeckEditor] Deck List History Manager. (#6340)

* [DeckEditor] Deck List History Manager.

Took 23 minutes

Took 17 minutes

* Add icons.

Took 2 minutes


Took 3 seconds

* Small fixes.

Took 12 minutes

* Style lint.

Took 48 seconds

* tr() things.

Took 5 minutes

* Add tooltips for buttons.

Took 3 minutes

* Add explanation label to history.

Took 3 minutes

* Refactor to .cpp, delegate undo/redo to manager, don't return memento

Took 8 minutes

* Clear history when setting deck.

Took 6 minutes

* Move to value based stacks.

Took 52 seconds

* Default constructor.

Took 31 seconds

Took 3 minutes

Took 4 minutes

Took 2 minutes

* Have it listen to deck editor additions.

Took 18 minutes

* Don't connect buttons *and* actions.

Took 2 minutes

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-11-20 14:54:32 +01:00 committed by GitHub
parent c46f6d1178
commit 846f16ddaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 612 additions and 41 deletions

View file

@ -3,8 +3,12 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(HEADERS
libcockatrice/deck_list/abstract_deck_list_card_node.h libcockatrice/deck_list/abstract_deck_list_node.h
libcockatrice/deck_list/deck_list.h libcockatrice/deck_list/deck_list_card_node.h
libcockatrice/deck_list/abstract_deck_list_card_node.h
libcockatrice/deck_list/abstract_deck_list_node.h
libcockatrice/deck_list/deck_list.h
libcockatrice/deck_list/deck_list_card_node.h
libcockatrice/deck_list/deck_list_history_manager.h
libcockatrice/deck_list/deck_list_memento.h
libcockatrice/deck_list/inner_deck_list_node.h
)
@ -16,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)

View file

@ -2,6 +2,7 @@
#include "abstract_deck_list_node.h"
#include "deck_list_card_node.h"
#include "deck_list_memento.h"
#include "inner_deck_list_node.h"
#include <QCryptographicHash>
@ -719,4 +720,15 @@ void DeckList::forEachCard(const std::function<void(InnerDecklistNode *, Decklis
func(node, card);
}
}
}
}
DeckListMemento DeckList::createMemento(const QString &reason) const
{
return DeckListMemento(writeToString_Native(), reason);
}
void DeckList::restoreMemento(const DeckListMemento &m)
{
cleanList();
loadFromString_Native(m.getMemento());
}

View file

@ -10,6 +10,7 @@
#ifndef DECKLIST_H
#define DECKLIST_H
#include "deck_list_memento.h"
#include "inner_deck_list_node.h"
#include <QMap>
@ -317,6 +318,8 @@ public:
* @param func Function taking (zone node, card node).
*/
void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func) const;
DeckListMemento createMemento(const QString &reason) const;
void restoreMemento(const DeckListMemento &m);
};
#endif

View file

@ -0,0 +1,53 @@
#include "deck_list_history_manager.h"
void DeckListHistoryManager::save(const DeckListMemento &memento)
{
undoStack.push(memento);
redoStack.clear();
emit undoRedoStateChanged();
}
void DeckListHistoryManager::clear()
{
undoStack.clear();
redoStack.clear();
emit undoRedoStateChanged();
}
void DeckListHistoryManager::undo(DeckList *deck)
{
if (undoStack.isEmpty())
return;
// Peek at the memento we are going to restore
const DeckListMemento &mementoToRestore = undoStack.top();
// Save current state for redo
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
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

@ -0,0 +1,54 @@
#ifndef COCKATRICE_DECK_LIST_HISTORY_MANAGER_H
#define COCKATRICE_DECK_LIST_HISTORY_MANAGER_H
#include "deck_list.h"
#include "deck_list_memento.h"
#include <QObject>
#include <QStack>
class DeckListHistoryManager : public QObject
{
Q_OBJECT
signals:
void undoRedoStateChanged();
public:
explicit DeckListHistoryManager(QObject *parent = nullptr) : QObject(parent)
{
}
void save(const DeckListMemento &memento);
void clear();
bool canUndo() const
{
return !undoStack.isEmpty();
}
bool canRedo() const
{
return !redoStack.isEmpty();
}
void undo(DeckList *deck);
void redo(DeckList *deck);
QStack<DeckListMemento> getRedoStack() const
{
return redoStack;
}
QStack<DeckListMemento> getUndoStack() const
{
return undoStack;
}
private:
QStack<DeckListMemento> undoStack;
QStack<DeckListMemento> redoStack;
};
#endif // COCKATRICE_DECK_LIST_HISTORY_MANAGER_H

View file

@ -0,0 +1,28 @@
#ifndef COCKATRICE_DECK_LIST_MEMENTO_H
#define COCKATRICE_DECK_LIST_MEMENTO_H
#include <QString>
class DeckListMemento
{
public:
DeckListMemento() = default;
explicit DeckListMemento(const QString &memento, const QString &reason = QString())
: memento(memento), reason(reason)
{
}
QString getMemento() const
{
return memento;
}
QString getReason() const
{
return reason;
}
private:
QString memento;
QString reason;
};
#endif // COCKATRICE_DECK_LIST_MEMENTO_H