mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-13 14:02:15 -07:00
[DeckEditor] Deck List History Manager.
Took 23 minutes Took 17 minutes
This commit is contained in:
parent
ab5d6db8a2
commit
7ebaf40c98
12 changed files with 440 additions and 19 deletions
|
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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(QString reason) const
|
||||
{
|
||||
return new DeckListMemento(writeToString_Native(), reason);
|
||||
}
|
||||
|
||||
void DeckList::restoreMemento(const DeckListMemento *m)
|
||||
{
|
||||
cleanList();
|
||||
loadFromString_Native(m->getMemento());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(QString reason) const;
|
||||
void restoreMemento(const DeckListMemento *m);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,96 @@
|
|||
#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 <QSharedPointer>
|
||||
#include <QStack>
|
||||
|
||||
class DeckListHistoryManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
signals:
|
||||
void undoRedoStateChanged();
|
||||
|
||||
public:
|
||||
explicit DeckListHistoryManager(QObject *parent = nullptr) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void save(DeckListMemento *memento)
|
||||
{
|
||||
undoStack.push(memento);
|
||||
redoStack.clear();
|
||||
emit undoRedoStateChanged();
|
||||
}
|
||||
|
||||
bool canUndo() const
|
||||
{
|
||||
return !undoStack.isEmpty();
|
||||
}
|
||||
bool canRedo() const
|
||||
{
|
||||
return !redoStack.isEmpty();
|
||||
}
|
||||
|
||||
DeckListMemento *undo(DeckList *deck)
|
||||
{
|
||||
if (undoStack.isEmpty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 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();
|
||||
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
|
||||
{
|
||||
return redoStack;
|
||||
}
|
||||
|
||||
QStack<DeckListMemento *> getUndoStack() const
|
||||
{
|
||||
return undoStack;
|
||||
}
|
||||
|
||||
private:
|
||||
QStack<DeckListMemento *> undoStack;
|
||||
QStack<DeckListMemento *> redoStack;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_DECK_LIST_HISTORY_MANAGER_H
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef COCKATRICE_DECK_LIST_MEMENTO_H
|
||||
#define COCKATRICE_DECK_LIST_MEMENTO_H
|
||||
#include <QString>
|
||||
|
||||
class DeckListMemento
|
||||
{
|
||||
public:
|
||||
explicit DeckListMemento(QString memento, 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue