Cockatrice 2026-01-14-Development-2.11.0-beta.46
A cross-platform 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 if (s == "Mana Cost")
95 return MANA_COST;
96 if (s == "Colors")
97 return COLOR;
98 return MAIN_TYPE; // default
99}
100} // namespace DeckListModelGroupCriteria
101
112{
113private:
115
116public:
123 DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent, int position = -1)
124 : AbstractDecklistCardNode(_parent, position), dataNode(_dataNode)
125 {
126 }
127 [[nodiscard]] int getNumber() const override
128 {
129 return dataNode->getNumber();
130 }
131 void setNumber(int _number) override
132 {
133 dataNode->setNumber(_number);
134 }
135 [[nodiscard]] QString getName() const override
136 {
137 return dataNode->getName();
138 }
139 void setName(const QString &_name) override
140 {
141 dataNode->setName(_name);
142 }
143 [[nodiscard]] QString getCardProviderId() const override
144 {
145 return dataNode->getCardProviderId();
146 }
147 void setCardProviderId(const QString &_cardProviderId) override
148 {
149 dataNode->setCardProviderId(_cardProviderId);
150 }
151 [[nodiscard]] QString getCardSetShortName() const override
152 {
153 return dataNode->getCardSetShortName();
154 }
155 void setCardSetShortName(const QString &_cardSetShortName) override
156 {
157 dataNode->setCardSetShortName(_cardSetShortName);
158 }
159 [[nodiscard]] QString getCardCollectorNumber() const override
160 {
161 return dataNode->getCardCollectorNumber();
162 }
163 void setCardCollectorNumber(const QString &_cardSetNumber) override
164 {
165 dataNode->setCardCollectorNumber(_cardSetNumber);
166 }
167 bool getFormatLegality() const override
168 {
169 return dataNode->getFormatLegality();
170 }
171 void setFormatLegality(const bool _formatLegal) override
172 {
173 dataNode->setFormatLegality(_formatLegal);
174 }
175
180 [[nodiscard]] DecklistCardNode *getDataNode() const
181 {
182 return dataNode;
183 }
184 [[nodiscard]] bool isDeckHeader() const override
185 {
186 return false;
187 }
188};
189
213class DeckListModel : public QAbstractItemModel
214{
215 Q_OBJECT
216
217public slots:
224 void rebuildTree();
225
231
232 void setActiveFormat(const QString &_format);
233
234signals:
239
244
250 void cardAddedAt(const QModelIndex &index);
251
257
262
267 void cardNodeAddedAt(const QModelIndex &index);
268
273
278
279public:
280 explicit DeckListModel(QObject *parent = nullptr);
281 explicit DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList);
282 ~DeckListModel() override;
283
288 [[nodiscard]] QModelIndex getRoot() const
289 {
290 return nodeToIndex(root);
291 }
292
295 [[nodiscard]] int rowCount(const QModelIndex &parent) const override;
296 [[nodiscard]] int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override;
297 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
298 [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
299 [[nodiscard]] QModelIndex index(int row, int column, const QModelIndex &parent) const override;
300 [[nodiscard]] QModelIndex parent(const QModelIndex &index) const override;
301 [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
302 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
303 bool removeRows(int row, int count, const QModelIndex &parent) override;
304 void sort(int column, Qt::SortOrder order) override;
306
315 [[nodiscard]] QModelIndex findCard(const QString &cardName,
316 const QString &zoneName,
317 const QString &providerId = "",
318 const QString &cardNumber = "") const;
319
328 QModelIndex addPreferredPrintingCard(const QString &cardName, const QString &zoneName, bool abAddAnyway);
329
336 QModelIndex addCard(const ExactCard &card, const QString &zoneName);
337
345 bool offsetCountAtIndex(const QModelIndex &idx, int offset);
346
352 bool removeCardAtIndex(const QModelIndex &idx);
353
357 void cleanList();
358
359 [[nodiscard]] QSharedPointer<DeckList> getDeckList() const
360 {
361 return deckList;
362 }
363 void setDeckList(const QSharedPointer<DeckList> &_deck);
364
370 void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func);
371
375 [[nodiscard]] QList<const DecklistCardNode *> getCardNodes() const;
376 [[nodiscard]] QList<const DecklistCardNode *> getCardNodesForZone(const QString &zoneName) const;
377
381 [[nodiscard]] QList<QString> getCardNames() const;
385 [[nodiscard]] QList<CardRef> getCardRefs() const;
389 [[nodiscard]] QList<QString> getZones() const;
390
391private:
392 QSharedPointer<DeckList> deckList;
396 Qt::SortOrder lastKnownOrder;
397
399 QModelIndex nodeToIndex(AbstractDecklistNode *node) const;
400 [[nodiscard]] DecklistModelCardNode *findCardNode(const QString &cardName,
401 const QString &zoneName,
402 const QString &providerId = "",
403 const QString &cardNumber = "") const;
404
411 int findSortedInsertRow(const InnerDecklistNode *parent, const CardInfoPtr &cardInfo) const;
412
418 void emitBackgroundUpdates(const QModelIndex &parent);
419
424 void emitRecursiveUpdates(const QModelIndex &index);
425
426 void sortHelper(InnerDecklistNode *node, Qt::SortOrder order);
427
428 template <typename T> T getNode(const QModelIndex &index) const
429 {
430 if (!index.isValid())
431 return dynamic_cast<T>(root);
432 return dynamic_cast<T>(static_cast<AbstractDecklistNode *>(index.internalPointer()));
433 }
434
436};
437
438#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:523
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:382
void setActiveFormat(const QString &_format)
Definition deck_list_model.cpp:622
void cleanList()
Removes all cards and resets the model.
Definition deck_list_model.cpp:629
void cardNodeAddedAt(const QModelIndex &index)
Emitted whenever a new card node is added.
QSharedPointer< DeckList > getDeckList() const
Definition deck_list_model.h:359
QVariant data(const QModelIndex &index, int role) const override
Definition deck_list_model.cpp:106
QList< QString > getCardNames() const
Gets a deduplicated list of all card names that appear in the model.
Definition deck_list_model.cpp:662
QList< const DecklistCardNode * > getCardNodes() const
Gets a list of all card nodes in the deck.
Definition deck_list_model.cpp:652
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:243
QList< const DecklistCardNode * > getCardNodesForZone(const QString &zoneName) const
Definition deck_list_model.cpp:657
T getNode(const QModelIndex &index) const
Definition deck_list_model.h:428
QModelIndex getRoot() const
Returns the root index of the model.
Definition deck_list_model.h:288
void emitRecursiveUpdates(const QModelIndex &index)
Recursively emits the dataChanged signal for the given node and all parent nodes.
Definition deck_list_model.cpp:271
void sortHelper(InnerDecklistNode *node, Qt::SortOrder order)
Definition deck_list_model.cpp:565
int rowCount(const QModelIndex &parent) const override
Definition deck_list_model.cpp:90
bool removeRows(int row, int count, const QModelIndex &parent) override
Definition deck_list_model.cpp:316
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:101
void forEachCard(const std::function< void(InnerDecklistNode *, DecklistCardNode *)> &func)
Apply a function to every card in the deck tree.
Definition deck_list_model.cpp:647
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition deck_list_model.cpp:223
int lastKnownColumn
Definition deck_list_model.h:395
QModelIndex nodeToIndex(AbstractDecklistNode *node) const
Definition deck_list_model.cpp:556
Qt::SortOrder lastKnownOrder
Definition deck_list_model.h:396
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:255
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:395
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:616
QSharedPointer< DeckList > deckList
Definition deck_list_model.h:392
void sort(int column, Qt::SortOrder order) override
Definition deck_list_model.cpp:593
QList< QString > getZones() const
Gets a list of all zone names that appear in the model.
Definition deck_list_model.cpp:683
~DeckListModel() override
Definition deck_list_model.cpp:26
void refreshCardFormatLegalities()
Definition deck_list_model.cpp:741
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:468
void setDeckList(const QSharedPointer< DeckList > &_deck)
Definition deck_list_model.cpp:637
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition deck_list_model.cpp:197
QModelIndex parent(const QModelIndex &index) const override
Definition deck_list_model.cpp:234
bool removeCardAtIndex(const QModelIndex &idx)
Removes the card node at the index.
Definition deck_list_model.cpp:501
QModelIndex addCard(const ExactCard &card, const QString &zoneName)
Adds an ExactCard to the specified zone.
Definition deck_list_model.cpp:414
DecklistModelCardNode * findCardNode(const QString &cardName, const QString &zoneName, const QString &providerId="", const QString &cardNumber="") const
Definition deck_list_model.cpp:357
InnerDecklistNode * createNodeIfNeeded(const QString &name, InnerDecklistNode *parent)
Definition deck_list_model.cpp:346
InnerDecklistNode * root
Definition deck_list_model.h:393
QList< CardRef > getCardRefs() const
Gets a deduplicated list of all CardRefs that appear in the model.
Definition deck_list_model.cpp:672
void cardNodeRemoved()
Emitted whenever a card node is removed.
DeckListModelGroupCriteria::Type activeGroupCriteria
Definition deck_list_model.h:394
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition deck_list_model.cpp:281
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:112
DecklistCardNode * dataNode
Definition deck_list_model.h:114
void setName(const QString &_name) override
Definition deck_list_model.h:139
void setCardCollectorNumber(const QString &_cardSetNumber) override
Definition deck_list_model.h:163
void setCardProviderId(const QString &_cardProviderId) override
Definition deck_list_model.h:147
DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent, int position=-1)
Constructs a model node wrapping a DecklistCardNode.
Definition deck_list_model.h:123
QString getCardSetShortName() const override
Definition deck_list_model.h:151
void setFormatLegality(const bool _formatLegal) override
Definition deck_list_model.h:171
void setNumber(int _number) override
Definition deck_list_model.h:131
QString getCardProviderId() const override
Definition deck_list_model.h:143
QString getName() const override
Definition deck_list_model.h:135
QString getCardCollectorNumber() const override
Definition deck_list_model.h:159
DecklistCardNode * getDataNode() const
Returns the underlying data node.
Definition deck_list_model.h:180
bool isDeckHeader() const override
Whether this node is the "deck header" (deck metadata).
Definition deck_list_model.h:184
int getNumber() const override
Definition deck_list_model.h:127
bool getFormatLegality() const override
Definition deck_list_model.h:167
void setCardSetShortName(const QString &_cardSetShortName) override
Definition deck_list_model.h:155
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