diff --git a/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp b/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp index e574ddf6c..8167dad96 100644 --- a/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp +++ b/cockatrice/src/interface/widgets/deck_editor/deck_list_history_manager_widget.cpp @@ -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); diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.cpp b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.cpp index f4f1eb086..e26e55e08 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.cpp +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.cpp @@ -722,13 +722,13 @@ void DeckList::forEachCard(const std::functiongetMemento()); + loadFromString_Native(m.getMemento()); } diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.h b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.h index b5e1e7cdb..1be673fcf 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.h +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list.h @@ -318,8 +318,8 @@ public: * @param func Function taking (zone node, card node). */ void forEachCard(const std::function &func) const; - DeckListMemento *createMemento(QString reason) const; - void restoreMemento(const DeckListMemento *m); + DeckListMemento createMemento(const QString &reason) const; + void restoreMemento(const DeckListMemento &m); }; #endif diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp index 7b640370b..83c9cc0bb 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.cpp @@ -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(); -} \ No newline at end of file +} diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h index bc18461ff..f30c0affe 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_history_manager.h @@ -5,7 +5,6 @@ #include "deck_list_memento.h" #include -#include #include 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 getRedoStack() const + QStack getRedoStack() const { return redoStack; } - - QStack getUndoStack() const + QStack getUndoStack() const { return undoStack; } private: - QStack undoStack; - QStack redoStack; + QStack undoStack; + QStack redoStack; }; #endif // COCKATRICE_DECK_LIST_HISTORY_MANAGER_H diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_memento.h b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_memento.h index e8daa2d11..c9ee794c6 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_memento.h +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_memento.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) { }