Cockatrice 2026-01-14-Development-2.11.0-beta.46
A cross-platform virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
DeckStateManager Class Reference

This class centralizes the management of the state of the deck in the deck editor tab. It is responsible for owning and managing the DeckListModel, underlying DeckList, load info, and edit history. More...

#include <deck_state_manager.h>

Inheritance diagram for DeckStateManager:
[legend]
Collaboration diagram for DeckStateManager:
[legend]

Public Slots

void requestHistorySave (const QString &reason)

Signals

void cardModified ()
void uniqueCardsChanged ()
void metadataModified ()
void deckModified ()
void historyChanged ()
void deckReplaced ()
void isModifiedChanged (bool isModified)
void focusIndexChanged (QModelIndex index, bool preserveWidgetFocus)

Public Member Functions

 DeckStateManager (QObject *parent=nullptr)
DeckListHistoryManagergetHistoryManager () const
DeckListModelgetModel () const
 Gets the underlying DeckListModel. You should generally refrain modifying the returned model directly. However, it's fine (and intended) to perform queries on the returned model.
const DeckListgetDeckList () const
 Gets a view of the current deck.
LoadedDeck toLoadedDeck () const
 Creates a LoadedDeck containing the contents of the current deck and the current LoadInfo.
DeckList::Metadata const & getMetadata () const
 Gets a view of the metadata in the DeckList.
QString getSimpleDeckName () const
 Gets the deck's simplified name.
QString getDeckHash () const
 Gets the deck hash.
bool isModified () const
 Checks if the deck has been modified since it was last saved.
void setModified (bool state)
 Sets the new isModified state, emitting a signal if the state changed. This class will automatically update its isModified state, but you may need to set it manually to handle, for example, saving.
bool isBlankNewDeck () const
 Checks if the deck state is as if it was a new deck.
void replaceDeck (const LoadedDeck &deck)
 Overwrites the current deck with a new deck, resetting all history.
void clearDeck ()
 Resets the deck to a blank new deck, resetting all history.
void setLastLoadInfo (const LoadedDeck::LoadInfo &loadInfo)
 Sets the lastLoadInfo.
bool modifyDeck (const QString &reason, const std::function< bool(DeckListModel *)> &operation)
 Modifies the cards in the deck, in a wrapped operation that is saved to the history.
QModelIndex modifyDeck (const QString &reason, const std::function< QModelIndex(DeckListModel *)> &operation)
 Modifies the cards in the deck, in a wrapped operation that is saved to the history.
QModelIndex addCard (const ExactCard &card, const QString &zoneName)
 Adds the given card to the given zone. Saves the operation to history if successful.
QModelIndex decrementCard (const ExactCard &card, const QString &zoneName)
 Removes 1 copy of the given card from the given zone. Saves the operation to history if successful.
bool swapCardAtIndex (const QModelIndex &idx)
 Swaps one copy of the card at the given index between the maindeck and sideboard. No-ops if index is invalid or not a card node. Saves the operation to history if successful.
bool removeCardAtIndex (const QModelIndex &idx)
 Removes all copies of the card at the given index. No-ops if index is invalid or not a card node. Saves the operation to history if successful.
bool incrementCountAtIndex (const QModelIndex &idx)
 Increments the number of copies of the card at the given index by 1. No-ops if index is invalid or not a card node. Saves the operation to history if successful.
bool decrementCountAtIndex (const QModelIndex &idx)
 Decrements the number of copies of the card at the given index by 1. No-ops if index is invalid or not a card node. Saves the operation to history if successful.
void undo (int steps=1)
void redo (int steps=1)
Metadata setters

These methods set the metadata. Will no-op if the new value is the same as the current value. Saves the operation to history if successful.

void setName (const QString &name)
void setComments (const QString &comments)
void setBannerCard (const CardRef &bannerCard)
void setTags (const QStringList &tags)
void setFormat (const QString &format)

Private Member Functions

bool offsetCountAtIndex (const QModelIndex &idx, int offset)
void doCardModified ()
 Handles updating state and emitting signals whenever the cards are modified.
void doMetadataModified ()
 Handles updating state and emitting signals whenever the metadata is modified.

Private Attributes

LoadedDeck::LoadInfo lastLoadInfo
QSharedPointer< DeckListdeckList
DeckListModeldeckListModel
DeckListHistoryManagerhistoryManager
bool modified = false

Detailed Description

This class centralizes the management of the state of the deck in the deck editor tab. It is responsible for owning and managing the DeckListModel, underlying DeckList, load info, and edit history.

Although this class provides getters for the underlying DeckListModel, you should generally refrain from directly modifying the returned model. Outside modifications to the deck state should be done through and the metadata setters. Those methods ensure that the history is recorded and correct signals are emitted.

Constructor & Destructor Documentation

◆ DeckStateManager()

DeckStateManager::DeckStateManager ( QObject * parent = nullptr)
explicit
Here is the call graph for this function:

Member Function Documentation

◆ addCard()

QModelIndex DeckStateManager::addCard ( const ExactCard & card,
const QString & zoneName )

Adds the given card to the given zone. Saves the operation to history if successful.

Parameters
cardThe card to add
zoneNameThe zone to add the card to
Returns
The index of the added card
Here is the call graph for this function:

◆ cardModified

void DeckStateManager::cardModified ( )
signal

A modification has been made to the cards in the deck

Here is the caller graph for this function:

◆ clearDeck()

void DeckStateManager::clearDeck ( )

Resets the deck to a blank new deck, resetting all history.

Here is the call graph for this function:

◆ deckModified

void DeckStateManager::deckModified ( )
signal

A modification has been made to the cards or metadata in the deck

Here is the caller graph for this function:

◆ deckReplaced

void DeckStateManager::deckReplaced ( )
signal

The deck has been completely changed.

Here is the caller graph for this function:

◆ decrementCard()

QModelIndex DeckStateManager::decrementCard ( const ExactCard & card,
const QString & zoneName )

Removes 1 copy of the given card from the given zone. Saves the operation to history if successful.

Parameters
cardThe card to remove
zoneNameThe zone to remove the card from
Returns
The index of the removed card. Will be invalid if the last copy was removed.
Here is the call graph for this function:

◆ decrementCountAtIndex()

bool DeckStateManager::decrementCountAtIndex ( const QModelIndex & idx)

Decrements the number of copies of the card at the given index by 1. No-ops if index is invalid or not a card node. Saves the operation to history if successful.

Parameters
idxThe model index
Returns
Whether the operation was successfully performed
Here is the call graph for this function:

◆ doCardModified()

void DeckStateManager::doCardModified ( )
private

Handles updating state and emitting signals whenever the cards are modified.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doMetadataModified()

void DeckStateManager::doMetadataModified ( )
private

Handles updating state and emitting signals whenever the metadata is modified.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ focusIndexChanged

void DeckStateManager::focusIndexChanged ( QModelIndex index,
bool preserveWidgetFocus )
signal

The selected card on any views connected to this deck should be changed to this index.

Parameters
indexThe model index
preserveWidgetFocusWhether to keep the widget focus unchanged
Here is the caller graph for this function:

◆ getDeckHash()

QString DeckStateManager::getDeckHash ( ) const

Gets the deck hash.

◆ getDeckList()

const DeckList & DeckStateManager::getDeckList ( ) const

Gets a view of the current deck.

Here is the caller graph for this function:

◆ getHistoryManager()

DeckListHistoryManager * DeckStateManager::getHistoryManager ( ) const
inline

Gets the underlying HistoryManager.

Returns
The DeckListHistoryManager instance

◆ getMetadata()

DeckList::Metadata const & DeckStateManager::getMetadata ( ) const

Gets a view of the metadata in the DeckList.

◆ getModel()

DeckListModel * DeckStateManager::getModel ( ) const
inline

Gets the underlying DeckListModel. You should generally refrain modifying the returned model directly. However, it's fine (and intended) to perform queries on the returned model.

Returns
The DeckListModel instance

◆ getSimpleDeckName()

QString DeckStateManager::getSimpleDeckName ( ) const

Gets the deck's simplified name.

◆ historyChanged

void DeckStateManager::historyChanged ( )
signal

The history has been greatly changed and needs to be reloaded.

Here is the caller graph for this function:

◆ incrementCountAtIndex()

bool DeckStateManager::incrementCountAtIndex ( const QModelIndex & idx)

Increments the number of copies of the card at the given index by 1. No-ops if index is invalid or not a card node. Saves the operation to history if successful.

Parameters
idxThe model index
Returns
Whether the operation was successfully performed
Here is the call graph for this function:

◆ isBlankNewDeck()

bool DeckStateManager::isBlankNewDeck ( ) const

Checks if the deck state is as if it was a new deck.

Here is the call graph for this function:

◆ isModified()

bool DeckStateManager::isModified ( ) const

Checks if the deck has been modified since it was last saved.

Here is the caller graph for this function:

◆ isModifiedChanged

void DeckStateManager::isModifiedChanged ( bool isModified)
signal

The isModified state of the deck has changed

Parameters
isModifiedthe new state
Here is the call graph for this function:
Here is the caller graph for this function:

◆ metadataModified

void DeckStateManager::metadataModified ( )
signal

A modification has been made to the metadata in the deck

Here is the caller graph for this function:

◆ modifyDeck() [1/2]

bool DeckStateManager::modifyDeck ( const QString & reason,
const std::function< bool(DeckListModel *)> & operation )

Modifies the cards in the deck, in a wrapped operation that is saved to the history.

The operation is a function that accepts a DeckListModel that it operates upon, and returns a bool.

This method will pass the underlying DeckListModel into the operation function. The function can call methods on the model to modify the deck. The function should return a bool to indicate success/failure.

If the operation returns true, the state of the deck before the operation is ran is saved to the history, and the isModified state is updated. If the operation returns false, the history and isModified state is not updated.

Note that even if the operation fails, any modifications to the model will already have been made. It's recommended for the operation to always return true if any modification has already been made to the model, as not doing that may cause the state to become desynced.

Parameters
reasonThe reason to display in the history
operationThe modification operation.
Returns
The bool returned from the operation
Here is the call graph for this function:
Here is the caller graph for this function:

◆ modifyDeck() [2/2]

QModelIndex DeckStateManager::modifyDeck ( const QString & reason,
const std::function< QModelIndex(DeckListModel *)> & operation )

Modifies the cards in the deck, in a wrapped operation that is saved to the history.

The operation is a function that accepts a DeckListModel that it operates upon, and returns a QModelIndex. If the index is invalid, then the operation is considered to be a failure.

See the other for more info about the behavior of this method. reason The reason to display in the history operation The modification operation. The QModelIndex returned from the operation

Here is the call graph for this function:

◆ offsetCountAtIndex()

bool DeckStateManager::offsetCountAtIndex ( const QModelIndex & idx,
int offset )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ redo()

void DeckStateManager::redo ( int steps = 1)

Redoes n steps of the history, setting the decklist state and updating the current step in the historyManager.

Parameters
stepsNumber of steps to redo.

◆ removeCardAtIndex()

bool DeckStateManager::removeCardAtIndex ( const QModelIndex & idx)

Removes all copies of the card at the given index. No-ops if index is invalid or not a card node. Saves the operation to history if successful.

Parameters
idxThe model index
Returns
Whether the operation was successfully performed
Here is the call graph for this function:

◆ replaceDeck()

void DeckStateManager::replaceDeck ( const LoadedDeck & deck)

Overwrites the current deck with a new deck, resetting all history.

Parameters
deckThe new deck.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ requestHistorySave

void DeckStateManager::requestHistorySave ( const QString & reason)
slot

Saves the current decklist state to history.

Parameters
reasonThe reason that is shown in the history.
Here is the caller graph for this function:

◆ setBannerCard()

void DeckStateManager::setBannerCard ( const CardRef & bannerCard)
Here is the call graph for this function:

◆ setComments()

void DeckStateManager::setComments ( const QString & comments)
Here is the call graph for this function:

◆ setFormat()

void DeckStateManager::setFormat ( const QString & format)
Here is the call graph for this function:

◆ setLastLoadInfo()

void DeckStateManager::setLastLoadInfo ( const LoadedDeck::LoadInfo & loadInfo)
inline

Sets the lastLoadInfo.

Parameters
loadInfoThe lastLoadInfo

◆ setModified()

void DeckStateManager::setModified ( bool state)

Sets the new isModified state, emitting a signal if the state changed. This class will automatically update its isModified state, but you may need to set it manually to handle, for example, saving.

Parameters
stateThe state
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setName()

void DeckStateManager::setName ( const QString & name)
Here is the call graph for this function:

◆ setTags()

void DeckStateManager::setTags ( const QStringList & tags)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ swapCardAtIndex()

bool DeckStateManager::swapCardAtIndex ( const QModelIndex & idx)

Swaps one copy of the card at the given index between the maindeck and sideboard. No-ops if index is invalid or not a card node. Saves the operation to history if successful.

Parameters
idxThe model index
Returns
Whether the operation was successfully performed
Here is the call graph for this function:

◆ toLoadedDeck()

LoadedDeck DeckStateManager::toLoadedDeck ( ) const

Creates a LoadedDeck containing the contents of the current deck and the current LoadInfo.

Returns
A new LoadedDeck instance.
Here is the call graph for this function:

◆ undo()

void DeckStateManager::undo ( int steps = 1)

Undoes n steps of the history, setting the decklist state and updating the current step in the historyManager.

Parameters
stepsNumber of steps to undo.

◆ uniqueCardsChanged

void DeckStateManager::uniqueCardsChanged ( )
signal

A card that wasn't previously in the deck was added to the deck, or the last copy of a card was removed from the deck.

Here is the caller graph for this function:

Member Data Documentation

◆ deckList

QSharedPointer<DeckList> DeckStateManager::deckList
private

◆ deckListModel

DeckListModel* DeckStateManager::deckListModel
private

◆ historyManager

DeckListHistoryManager* DeckStateManager::historyManager
private

◆ lastLoadInfo

LoadedDeck::LoadInfo DeckStateManager::lastLoadInfo
private

◆ modified

bool DeckStateManager::modified = false
private

The documentation for this class was generated from the following files: