mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-11 04:43:56 -07:00
Move to value based stacks.
Took 52 seconds
This commit is contained in:
parent
d4afa0e330
commit
7540fd6632
6 changed files with 28 additions and 31 deletions
|
|
@ -73,7 +73,7 @@ void DeckListHistoryManagerWidget::refreshList()
|
|||
// Fill redo section first (oldest redo at top, newest redo closest to divider)
|
||||
const auto redoStack = historyManager->getRedoStack();
|
||||
for (int i = 0; i < redoStack.size(); ++i) { // iterate forward
|
||||
auto item = new QListWidgetItem(tr("[redo] ") + redoStack[i]->getReason(), historyList);
|
||||
auto item = new QListWidgetItem(tr("[redo] ") + redoStack[i].getReason(), historyList);
|
||||
item->setData(Qt::UserRole, QVariant("redo"));
|
||||
item->setData(Qt::UserRole + 1, i); // index in redo stack
|
||||
item->setForeground(Qt::gray);
|
||||
|
|
@ -90,7 +90,7 @@ void DeckListHistoryManagerWidget::refreshList()
|
|||
// Fill undo section
|
||||
const auto undoStack = historyManager->getUndoStack();
|
||||
for (int i = undoStack.size() - 1; i >= 0; --i) {
|
||||
auto item = new QListWidgetItem(tr("[undo] ") + undoStack[i]->getReason(), historyList);
|
||||
auto item = new QListWidgetItem(tr("[undo] ") + undoStack[i].getReason(), historyList);
|
||||
item->setData(Qt::UserRole, QVariant("undo"));
|
||||
item->setData(Qt::UserRole + 1, i); // index in undo stack
|
||||
historyList->addItem(item);
|
||||
|
|
|
|||
|
|
@ -722,13 +722,13 @@ void DeckList::forEachCard(const std::function<void(InnerDecklistNode *, Decklis
|
|||
}
|
||||
}
|
||||
|
||||
DeckListMemento *DeckList::createMemento(QString reason) const
|
||||
DeckListMemento DeckList::createMemento(const QString &reason) const
|
||||
{
|
||||
return new DeckListMemento(writeToString_Native(), reason);
|
||||
return DeckListMemento(writeToString_Native(), reason);
|
||||
}
|
||||
|
||||
void DeckList::restoreMemento(const DeckListMemento *m)
|
||||
void DeckList::restoreMemento(const DeckListMemento &m)
|
||||
{
|
||||
cleanList();
|
||||
loadFromString_Native(m->getMemento());
|
||||
loadFromString_Native(m.getMemento());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -318,8 +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);
|
||||
DeckListMemento createMemento(const QString &reason) const;
|
||||
void restoreMemento(const DeckListMemento &m);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "deck_list_history_manager.h"
|
||||
|
||||
void DeckListHistoryManager::save(DeckListMemento *memento)
|
||||
void DeckListHistoryManager::save(const DeckListMemento &memento)
|
||||
{
|
||||
undoStack.push(memento);
|
||||
redoStack.clear();
|
||||
|
|
@ -11,25 +11,23 @@ void DeckListHistoryManager::clear()
|
|||
{
|
||||
undoStack.clear();
|
||||
redoStack.clear();
|
||||
|
||||
emit undoRedoStateChanged();
|
||||
}
|
||||
|
||||
void DeckListHistoryManager::undo(DeckList *deck)
|
||||
{
|
||||
if (undoStack.isEmpty()) {
|
||||
if (undoStack.isEmpty())
|
||||
return;
|
||||
}
|
||||
|
||||
// Peek at the memento we are going to restore
|
||||
DeckListMemento *mementoToRestore = undoStack.top();
|
||||
const 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());
|
||||
// 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();
|
||||
DeckListMemento memento = undoStack.pop();
|
||||
deck->restoreMemento(memento);
|
||||
|
||||
emit undoRedoStateChanged();
|
||||
|
|
@ -37,20 +35,19 @@ void DeckListHistoryManager::undo(DeckList *deck)
|
|||
|
||||
void DeckListHistoryManager::redo(DeckList *deck)
|
||||
{
|
||||
if (redoStack.isEmpty()) {
|
||||
if (redoStack.isEmpty())
|
||||
return;
|
||||
}
|
||||
|
||||
// Peek at the memento we are going to restore
|
||||
DeckListMemento *mementoToRestore = redoStack.top();
|
||||
const 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());
|
||||
// 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();
|
||||
DeckListMemento memento = redoStack.pop();
|
||||
deck->restoreMemento(memento);
|
||||
|
||||
emit undoRedoStateChanged();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
#include "deck_list_memento.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
#include <QStack>
|
||||
|
||||
class DeckListHistoryManager : public QObject
|
||||
|
|
@ -20,7 +19,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
void save(DeckListMemento *memento);
|
||||
void save(const DeckListMemento &memento);
|
||||
|
||||
void clear();
|
||||
|
||||
|
|
@ -28,6 +27,7 @@ public:
|
|||
{
|
||||
return !undoStack.isEmpty();
|
||||
}
|
||||
|
||||
bool canRedo() const
|
||||
{
|
||||
return !redoStack.isEmpty();
|
||||
|
|
@ -37,19 +37,18 @@ public:
|
|||
|
||||
void redo(DeckList *deck);
|
||||
|
||||
QStack<DeckListMemento *> getRedoStack() const
|
||||
QStack<DeckListMemento> getRedoStack() const
|
||||
{
|
||||
return redoStack;
|
||||
}
|
||||
|
||||
QStack<DeckListMemento *> getUndoStack() const
|
||||
QStack<DeckListMemento> getUndoStack() const
|
||||
{
|
||||
return undoStack;
|
||||
}
|
||||
|
||||
private:
|
||||
QStack<DeckListMemento *> undoStack;
|
||||
QStack<DeckListMemento *> redoStack;
|
||||
QStack<DeckListMemento> undoStack;
|
||||
QStack<DeckListMemento> redoStack;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_DECK_LIST_HISTORY_MANAGER_H
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@
|
|||
class DeckListMemento
|
||||
{
|
||||
public:
|
||||
explicit DeckListMemento(QString memento, QString reason = QString()) : memento(memento), reason(reason)
|
||||
explicit DeckListMemento(const QString &memento, const QString &reason = QString())
|
||||
: memento(memento), reason(reason)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue