Cockatrice 2026-06-01-Development-3.1.0-beta.3
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
deck_list_model.h
Go to the documentation of this file.
1#ifndef DECKLISTMODEL_H
2#define DECKLISTMODEL_H
3
6#include <QAbstractItemModel>
7#include <QList>
10
11class CardDatabase;
12class QPrinter;
13class QTextCursor;
14
21namespace DeckRoles
22{
29enum
30{
31 IsCardRole = Qt::UserRole + 1,
34};
35} // namespace DeckRoles
36
44{
49enum
50{
56};
57} // namespace DeckListModelColumns
58
66{
77static inline QString toString(Type t)
78{
79 switch (t) {
80 case MAIN_TYPE:
81 return "Main Type";
82 case MANA_COST:
83 return "Mana Cost";
84 case COLOR:
85 return "Colors";
86 }
87 return {};
88}
89
90static inline Type fromString(const QString &s)
91{
92 if (s == "Main Type") {
93 return MAIN_TYPE;
94 }
95 if (s == "Mana Cost") {
96 return MANA_COST;
97 }
98 if (s == "Colors") {
99 return COLOR;
100 }
101 return MAIN_TYPE; // default
102}
103} // namespace DeckListModelGroupCriteria
104
115{
116private:
118
119public:
126 DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent, int position = -1)
127 : AbstractDecklistCardNode(_parent, position), dataNode(_dataNode)
128 {
129 }
130 [[nodiscard]] int getNumber() const override
131 {
132 return dataNode->getNumber();
133 }
134 void setNumber(int _number) override
135 {
136 dataNode->setNumber(_number);
137 }
138 [[nodiscard]] QString getName() const override
139 {
140 return dataNode->getName();
141 }
142 void setName(const QString &_name) override
143 {
144 dataNode->setName(_name);
145 }
146 [[nodiscard]] QString getCardProviderId() const override
147 {
148 return dataNode->getCardProviderId();
149 }
150 void setCardProviderId(const QString &_cardProviderId) override
151 {
152 dataNode->setCardProviderId(_cardProviderId);
153 }
154 [[nodiscard]] QString getCardSetShortName() const override
155 {
156 return dataNode->getCardSetShortName();
157 }
158 void setCardSetShortName(const QString &_cardSetShortName) override
159 {
160 dataNode->setCardSetShortName(_cardSetShortName);
161 }
162 [[nodiscard]] QString getCardCollectorNumber() const override
163 {
164 return dataNode->getCardCollectorNumber();
165 }
166 void setCardCollectorNumber(const QString &_cardSetNumber) override
167 {
168 dataNode->setCardCollectorNumber(_cardSetNumber);
169 }
170 bool getFormatLegality() const override
171 {
172 return dataNode->getFormatLegality();
173 }
174 void setFormatLegality(const bool _formatLegal) override
175 {
176 dataNode->setFormatLegality(_formatLegal);
177 }
178
183 [[nodiscard]] DecklistCardNode *getDataNode() const
184 {
185 return dataNode;
186 }
187 [[nodiscard]] bool isDeckHeader() const override
188 {
189 return false;
190 }
191};
192
216class DeckListModel : public QAbstractItemModel
217{
218 Q_OBJECT
219
220public slots:
227 void rebuildTree();
228
234
235 void setActiveFormat(const QString &_format);
236
237signals:
242
247
253 void cardAddedAt(const QModelIndex &index);
254
260
265
270 void cardNodeAddedAt(const QModelIndex &index);
271
276
281
282public:
283 explicit DeckListModel(QObject *parent = nullptr);
284 explicit DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList);
285 ~DeckListModel() override;
286
291 [[nodiscard]] QModelIndex getRoot() const
292 {
293 return nodeToIndex(root);
294 }
295
298 [[nodiscard]] int rowCount(const QModelIndex &parent) const override;
299 [[nodiscard]] int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override;
300 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
301 [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
302 [[nodiscard]] QModelIndex index(int row, int column, const QModelIndex &parent) const override;
303 [[nodiscard]] QModelIndex parent(const QModelIndex &index) const override;
304 [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
305 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
306 bool removeRows(int row, int count, const QModelIndex &parent) override;
307 void sort(int column, Qt::SortOrder order) override;
309
318 [[nodiscard]] QModelIndex findCard(const QString &cardName,
319 const QString &zoneName,
320 const QString &providerId = "",
321 const QString &cardNumber = "") const;
322
331 QModelIndex addPreferredPrintingCard(const QString &cardName, const QString &zoneName, bool abAddAnyway);
332
339 QModelIndex addCard(const ExactCard &card, const QString &zoneName);
340
348 bool offsetCountAtIndex(const QModelIndex &idx, int offset);
349
355 bool removeCardAtIndex(const QModelIndex &idx);
356
360 void cleanList();
361
362 [[nodiscard]] QSharedPointer<DeckList> getDeckList() const
363 {
364 return deckList;
365 }
366 void setDeckList(const QSharedPointer<DeckList> &_deck);
367
373 void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func);
374
378 [[nodiscard]] QList<const DecklistCardNode *> getCardNodes() const;
379 [[nodiscard]] QList<const DecklistCardNode *> getCardNodesForZone(const QString &zoneName) const;
380
384 [[nodiscard]] QList<QString> getCardNames() const;
388 [[nodiscard]] QList<CardRef> getCardRefs() const;
392 [[nodiscard]] QList<QString> getZones() const;
393
394private:
395 QSharedPointer<DeckList> deckList;
399 Qt::SortOrder lastKnownOrder;
400
402 QModelIndex nodeToIndex(AbstractDecklistNode *node) const;
403 [[nodiscard]] DecklistModelCardNode *findCardNode(const QString &cardName,
404 const QString &zoneName,
405 const QString &providerId = "",
406 const QString &cardNumber = "") const;
407
414 int findSortedInsertRow(const InnerDecklistNode *parent, const CardInfoPtr &cardInfo) const;
415
421 void emitBackgroundUpdates(const QModelIndex &parent);
422
427 void emitRecursiveUpdates(const QModelIndex &index);
428
429 void sortHelper(InnerDecklistNode *node, Qt::SortOrder order);
430
431 template <typename T> T getNode(const QModelIndex &index) const
432 {
433 if (!index.isValid()) {
434 return dynamic_cast<T>(root);
435 }
436 return dynamic_cast<T>(static_cast<AbstractDecklistNode *>(index.internalPointer()));
437 }
438
440};
441
442#endif
Defines the AbstractDecklistCardNode base class, which adds card-specific behavior on top of Abstract...
QSharedPointer< CardInfo > CardInfoPtr
Definition card_info.cpp:20
AbstractDecklistCardNode(InnerDecklistNode *_parent=nullptr, int position=-1)
Construct a new AbstractDecklistCardNode.
Definition abstract_deck_list_card_node.h:56
Base class for all nodes in the deck list tree.
Definition abstract_deck_list_node.h:73
Core in-memory container for card and set data.
Definition card_database.h:29
void cardAddedAt(const QModelIndex &index)
Emitted whenever a card is added to the deck, regardless of whether it's an entirely new card or an e...
int findSortedInsertRow(const InnerDecklistNode *parent, const CardInfoPtr &cardInfo) const
Determines the sorted insertion row for a card.
Definition deck_list_model.cpp:535
void rebuildTree()
Rebuilds the model tree from the underlying node tree.
Definition deck_list_model.cpp:55
QModelIndex findCard(const QString &cardName, const QString &zoneName, const QString &providerId="", const QString &cardNumber="") const
Finds a card by name, zone, and optional identifiers.
Definition deck_list_model.cpp:388
void setActiveFormat(const QString &_format)
Definition deck_list_model.cpp:636
void cleanList()
Removes all cards and resets the model.
Definition deck_list_model.cpp:643
void cardNodeAddedAt(const QModelIndex &index)
Emitted whenever a new card node is added.
QSharedPointer< DeckList > getDeckList() const
Definition deck_list_model.h:362
QVariant data(const QModelIndex &index, int role) const override
Definition deck_list_model.cpp:108
QList< QString > getCardNames() const
Gets a deduplicated list of all card names that appear in the model.
Definition deck_list_model.cpp:675
QList< const DecklistCardNode * > getCardNodes() const
Gets a list of all card nodes in the deck.
Definition deck_list_model.cpp:665
void deckHashChanged()
Emitted whenever the deck hash changes due to modifications in the model.
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition deck_list_model.cpp:245
QList< const DecklistCardNode * > getCardNodesForZone(const QString &zoneName) const
Definition deck_list_model.cpp:670
T getNode(const QModelIndex &index) const
Definition deck_list_model.h:431
QModelIndex getRoot() const
Returns the root index of the model.
Definition deck_list_model.h:291
void emitRecursiveUpdates(const QModelIndex &index)
Recursively emits the dataChanged signal for the given node and all parent nodes.
Definition deck_list_model.cpp:274
void sortHelper(InnerDecklistNode *node, Qt::SortOrder order)
Definition deck_list_model.cpp:579
int rowCount(const QModelIndex &parent) const override
Definition deck_list_model.cpp:92
bool removeRows(int row, int count, const QModelIndex &parent) override
Definition deck_list_model.cpp:319
void cardsChanged()
Emitted whenever the cards in the deck changes. This includes when the deck is replaced.
void cardRemoved()
Emitted whenever a card is removed from the deck, regardless of whether a card node was removed or an...
int columnCount(const QModelIndex &=QModelIndex()) const override
Definition deck_list_model.cpp:103
void forEachCard(const std::function< void(InnerDecklistNode *, DecklistCardNode *)> &func)
Apply a function to every card in the deck tree.
Definition deck_list_model.cpp:660
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition deck_list_model.cpp:225
int lastKnownColumn
Definition deck_list_model.h:398
QModelIndex nodeToIndex(AbstractDecklistNode *node) const
Definition deck_list_model.cpp:570
Qt::SortOrder lastKnownOrder
Definition deck_list_model.h:399
void deckReplaced()
Emitted whenever the deck in the model has been replaced with a new one.
void emitBackgroundUpdates(const QModelIndex &parent)
Recursively emits the dataChanged signal with role as Qt::BackgroundRole for all indices that are chi...
Definition deck_list_model.cpp:257
DeckListModel(QObject *parent=nullptr)
Definition deck_list_model.cpp:5
QModelIndex addPreferredPrintingCard(const QString &cardName, const QString &zoneName, bool abAddAnyway)
Adds a card using the preferred printing if available.
Definition deck_list_model.cpp:401
void cardNodesChanged()
Emitted whenever a card node is added or removed. This includes when the deck is replaced.
void setActiveGroupCriteria(DeckListModelGroupCriteria::Type newCriteria)
Sets the criteria used to group cards in the model.
Definition deck_list_model.cpp:630
QSharedPointer< DeckList > deckList
Definition deck_list_model.h:395
void sort(int column, Qt::SortOrder order) override
Definition deck_list_model.cpp:607
QList< QString > getZones() const
Gets a list of all zone names that appear in the model.
Definition deck_list_model.cpp:696
~DeckListModel() override
Definition deck_list_model.cpp:26
void refreshCardFormatLegalities()
Definition deck_list_model.cpp:771
bool offsetCountAtIndex(const QModelIndex &idx, int offset)
Changes the amount field in the card node at the index by the amount. Removes the node if it causes t...
Definition deck_list_model.cpp:480
void setDeckList(const QSharedPointer< DeckList > &_deck)
Definition deck_list_model.cpp:651
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition deck_list_model.cpp:199
QModelIndex parent(const QModelIndex &index) const override
Definition deck_list_model.cpp:236
bool removeCardAtIndex(const QModelIndex &idx)
Removes the card node at the index.
Definition deck_list_model.cpp:513
QModelIndex addCard(const ExactCard &card, const QString &zoneName)
Adds an ExactCard to the specified zone.
Definition deck_list_model.cpp:420
DecklistModelCardNode * findCardNode(const QString &cardName, const QString &zoneName, const QString &providerId="", const QString &cardNumber="") const
Definition deck_list_model.cpp:363
InnerDecklistNode * createNodeIfNeeded(const QString &name, InnerDecklistNode *parent)
Definition deck_list_model.cpp:352
InnerDecklistNode * root
Definition deck_list_model.h:396
QList< CardRef > getCardRefs() const
Gets a deduplicated list of all CardRefs that appear in the model.
Definition deck_list_model.cpp:685
void cardNodeRemoved()
Emitted whenever a card node is removed.
DeckListModelGroupCriteria::Type activeGroupCriteria
Definition deck_list_model.h:397
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition deck_list_model.cpp:284
Concrete node type representing an actual card entry in the deck.
Definition deck_list_card_node.h:48
Adapter node that wraps a DecklistCardNode for use in the DeckListModel tree.
Definition deck_list_model.h:115
DecklistCardNode * dataNode
Definition deck_list_model.h:117
void setName(const QString &_name) override
Definition deck_list_model.h:142
void setCardCollectorNumber(const QString &_cardSetNumber) override
Definition deck_list_model.h:166
void setCardProviderId(const QString &_cardProviderId) override
Definition deck_list_model.h:150
DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent, int position=-1)
Constructs a model node wrapping a DecklistCardNode.
Definition deck_list_model.h:126
QString getCardSetShortName() const override
Definition deck_list_model.h:154
void setFormatLegality(const bool _formatLegal) override
Definition deck_list_model.h:174
void setNumber(int _number) override
Definition deck_list_model.h:134
QString getCardProviderId() const override
Definition deck_list_model.h:146
QString getName() const override
Definition deck_list_model.h:138
QString getCardCollectorNumber() const override
Definition deck_list_model.h:162
DecklistCardNode * getDataNode() const
Returns the underlying data node.
Definition deck_list_model.h:183
bool isDeckHeader() const override
Whether this node is the "deck header" (deck metadata).
Definition deck_list_model.h:187
int getNumber() const override
Definition deck_list_model.h:130
bool getFormatLegality() const override
Definition deck_list_model.h:170
void setCardSetShortName(const QString &_cardSetShortName) override
Definition deck_list_model.h:158
Represents a specific card instance, defined by its CardInfo and a particular printing.
Definition exact_card.h:19
Represents a container node in the deck list hierarchy (zones and groupings).
Definition inner_deck_list_node.h:62
Defines the DeckList class, which manages a full deck structure including cards, zones,...
Defines the DecklistCardNode class, representing a single card entry in the deck list tree.
Column indices for the DeckListModel.
@ CARD_AMOUNT
Definition deck_list_model.h:51
@ CARD_SET
Definition deck_list_model.h:53
@ CARD_PROVIDER_ID
Definition deck_list_model.h:55
@ CARD_COLLECTOR_NUMBER
Definition deck_list_model.h:54
@ CARD_NAME
Definition deck_list_model.h:52
Specifies criteria used to group cards in the DeckListModel.
static QString toString(Type t)
Definition deck_list_model.h:77
static Type fromString(const QString &s)
Definition deck_list_model.h:90
Type
Definition deck_list_model.h:72
@ MAIN_TYPE
Definition deck_list_model.h:73
@ COLOR
Definition deck_list_model.h:75
@ MANA_COST
Definition deck_list_model.h:74
Custom model roles used by the DeckListModel for data retrieval.
@ IsLegalRole
Definition deck_list_model.h:33
@ DepthRole
Definition deck_list_model.h:32
@ IsCardRole
Definition deck_list_model.h:31