Cockatrice 2026-10-19-Development-3.1.2-beta.2
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
7
10#include <QAbstractItemModel>
11#include <QList>
12#include <QModelIndex>
13#include <QSharedPointer>
14#include <QString>
15#include <QStringList>
16#include <functional>
17#include <qnamespace.h>
18#include <qtmetamacros.h>
19
21class DeckList;
22class ExactCard;
23class QObject;
24
31namespace DeckRoles
32{
39enum
40{
41 IsCardRole = Qt::UserRole + 1,
45};
46} // namespace DeckRoles
47
55{
60enum
61{
67};
68} // namespace DeckListModelColumns
69
77{
88static inline QString toString(Type t)
89{
90 switch (t) {
91 case MAIN_TYPE:
92 return "Main Type";
93 case MANA_COST:
94 return "Mana Cost";
95 case COLOR:
96 return "Colors";
97 }
98 return {};
99}
100
101static inline Type fromString(const QString &s)
102{
103 if (s == "Main Type") {
104 return MAIN_TYPE;
105 }
106 if (s == "Mana Cost") {
107 return MANA_COST;
108 }
109 if (s == "Colors") {
110 return COLOR;
111 }
112 return MAIN_TYPE; // default
113}
114} // namespace DeckListModelGroupCriteria
115
126{
127private:
129
130public:
137 DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent, int position = -1)
138 : AbstractDecklistCardNode(_parent, position), dataNode(_dataNode)
139 {
140 }
141 [[nodiscard]] int getNumber() const override
142 {
143 return dataNode->getNumber();
144 }
145 void setNumber(int _number) override
146 {
147 dataNode->setNumber(_number);
148 }
149 [[nodiscard]] QString getName() const override
150 {
151 return dataNode->getName();
152 }
153 void setName(const QString &_name) override
154 {
155 dataNode->setName(_name);
156 }
157 [[nodiscard]] QString getCardProviderId() const override
158 {
159 return dataNode->getCardProviderId();
160 }
161 void setCardProviderId(const QString &_cardProviderId) override
162 {
163 dataNode->setCardProviderId(_cardProviderId);
164 }
165 [[nodiscard]] QString getCardSetShortName() const override
166 {
168 }
169 void setCardSetShortName(const QString &_cardSetShortName) override
170 {
171 dataNode->setCardSetShortName(_cardSetShortName);
172 }
173 [[nodiscard]] QString getCardCollectorNumber() const override
174 {
176 }
177 void setCardCollectorNumber(const QString &_cardSetNumber) override
178 {
179 dataNode->setCardCollectorNumber(_cardSetNumber);
180 }
181 bool getFormatLegality() const override
182 {
183 return dataNode->getFormatLegality();
184 }
185 void setFormatLegality(const bool _formatLegal) override
186 {
187 dataNode->setFormatLegality(_formatLegal);
188 }
189
194 [[nodiscard]] DecklistCardNode *getDataNode() const
195 {
196 return dataNode;
197 }
198 [[nodiscard]] bool isDeckHeader() const override
199 {
200 return false;
201 }
202};
203
227class DeckListModel : public QAbstractItemModel
228{
229 Q_OBJECT
230
231public slots:
238 void rebuildTree();
239
245
246 void setActiveFormat(const QString &_format);
247
248signals:
253
258
264 void cardAddedAt(const QModelIndex &index);
265
271
276
281 void cardNodeAddedAt(const QModelIndex &index);
282
287
292
293public:
294 explicit DeckListModel(QObject *parent = nullptr);
295 explicit DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList);
296 ~DeckListModel() override;
297
306 void setDisplayLanguage(const QString &lang);
307
312 [[nodiscard]] QModelIndex getRoot() const
313 {
314 return nodeToIndex(root);
315 }
316
319 [[nodiscard]] int rowCount(const QModelIndex &parent) const override;
320 [[nodiscard]] int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override;
321 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
322 [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
323 [[nodiscard]] QModelIndex index(int row, int column, const QModelIndex &parent) const override;
324 [[nodiscard]] QModelIndex parent(const QModelIndex &index) const override;
325 [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
326 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
327 bool removeRows(int row, int count, const QModelIndex &parent) override;
328 void sort(int column, Qt::SortOrder order) override;
330
339 [[nodiscard]] QModelIndex findCard(const QString &cardName,
340 const QString &zoneName,
341 const QString &providerId = "",
342 const QString &cardNumber = "") const;
343
352 QModelIndex addPreferredPrintingCard(const QString &cardName, const QString &zoneName, bool abAddAnyway);
353
360 QModelIndex addCard(const ExactCard &card, const QString &zoneName);
361
369 bool offsetCountAtIndex(const QModelIndex &idx, int offset);
370
376 bool removeCardAtIndex(const QModelIndex &idx);
377
381 void cleanList();
382
384 {
385 return deckList;
386 }
387 void setDeckList(const QSharedPointer<DeckList> &_deck);
388
394 void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func);
395
399 [[nodiscard]] QList<const DecklistCardNode *> getCardNodes() const;
400 [[nodiscard]] QList<const DecklistCardNode *> getCardNodesForZone(const QString &zoneName) const;
401
405 [[nodiscard]] QList<QString> getCardNames() const;
409 [[nodiscard]] QList<CardRef> getCardRefs() const;
413 [[nodiscard]] QList<QString> getZones() const;
414
421 [[nodiscard]] QStringList getCustomZoneNames(const QString &boardZoneName) const;
422
423private:
428 Qt::SortOrder lastKnownOrder;
429 QString displayLang = "en";
430
432 QModelIndex nodeToIndex(AbstractDecklistNode *node) const;
433 [[nodiscard]] DecklistModelCardNode *findCardNode(const QString &cardName,
434 const QString &zoneName,
435 const QString &providerId = "",
436 const QString &cardNumber = "") const;
437
444 int findSortedInsertRow(const InnerDecklistNode *parent, const CardInfoPtr &cardInfo) const;
445
451 void emitBackgroundUpdates(const QModelIndex &parent);
452
457 void emitRecursiveUpdates(const QModelIndex &index);
458
459 void sortHelper(InnerDecklistNode *node, Qt::SortOrder order);
460 void sortShadowTree(InnerDecklistNode *node, Qt::SortOrder order);
461
462 template <typename T> T getNode(const QModelIndex &index) const
463 {
464 if (!index.isValid()) {
465 return dynamic_cast<T>(root);
466 }
467 return dynamic_cast<T>(static_cast<AbstractDecklistNode *>(index.internalPointer()));
468 }
469
471};
472
473#endif
Defines the AbstractDecklistCardNode base class, which adds card-specific behavior on top of Abstract...
QSharedPointer< CardInfo > CardInfoPtr
Definition card_info.cpp:15
AbstractDecklistCardNode(InnerDecklistNode *_parent=nullptr, int position=-1)
Construct a new AbstractDecklistCardNode.
Definition abstract_deck_list_card_node.h:60
Base class for all nodes in the deck list tree.
Definition abstract_deck_list_node.h:75
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:687
void rebuildTree()
Rebuilds the model tree from the underlying node tree.
Definition deck_list_model.cpp:86
void sortShadowTree(InnerDecklistNode *node, Qt::SortOrder order)
Sorts a freshly built shadow subtree without emitting model signals.
Definition deck_list_model.cpp:739
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:474
void setActiveFormat(const QString &_format)
Definition deck_list_model.cpp:808
void cleanList()
Removes all cards and resets the model.
Definition deck_list_model.cpp:815
void cardNodeAddedAt(const QModelIndex &index)
Emitted whenever a new card node is added.
QSharedPointer< DeckList > getDeckList() const
Definition deck_list_model.h:383
QVariant data(const QModelIndex &index, int role) const override
Definition deck_list_model.cpp:151
QList< QString > getCardNames() const
Gets a deduplicated list of all card names that appear in the model.
Definition deck_list_model.cpp:847
QList< const DecklistCardNode * > getCardNodes() const
Gets a list of all card nodes in the deck.
Definition deck_list_model.cpp:837
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:302
QList< const DecklistCardNode * > getCardNodesForZone(const QString &zoneName) const
Definition deck_list_model.cpp:842
T getNode(const QModelIndex &index) const
Definition deck_list_model.h:462
QModelIndex getRoot() const
Returns the root index of the model.
Definition deck_list_model.h:312
void setDisplayLanguage(const QString &lang)
Selects the language code for localized card names in the display role.
Definition deck_list_model.cpp:52
QStringList getCustomZoneNames(const QString &boardZoneName) const
Gets the names of the custom zones nested under the given board zone.
Definition deck_list_model.cpp:879
void emitRecursiveUpdates(const QModelIndex &index)
Recursively emits the dataChanged signal for the given node and all parent nodes.
Definition deck_list_model.cpp:331
void sortHelper(InnerDecklistNode *node, Qt::SortOrder order)
Definition deck_list_model.cpp:751
int rowCount(const QModelIndex &parent) const override
Definition deck_list_model.cpp:135
bool removeRows(int row, int count, const QModelIndex &parent) override
Definition deck_list_model.cpp:376
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:146
void forEachCard(const std::function< void(InnerDecklistNode *, DecklistCardNode *)> &func)
Apply a function to every card in the deck tree.
Definition deck_list_model.cpp:832
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition deck_list_model.cpp:282
int lastKnownColumn
Definition deck_list_model.h:427
QModelIndex nodeToIndex(AbstractDecklistNode *node) const
Definition deck_list_model.cpp:722
Qt::SortOrder lastKnownOrder
Definition deck_list_model.h:428
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:314
DeckListModel(QObject *parent=nullptr)
Definition deck_list_model.cpp:26
QModelIndex addPreferredPrintingCard(const QString &cardName, const QString &zoneName, bool abAddAnyway)
Adds a card using the preferred printing if available.
Definition deck_list_model.cpp:487
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:802
QSharedPointer< DeckList > deckList
Definition deck_list_model.h:424
void sort(int column, Qt::SortOrder order) override
Definition deck_list_model.cpp:779
QList< QString > getZones() const
Gets a list of all zone names that appear in the model.
Definition deck_list_model.cpp:868
void refreshCardFormatLegalities()
Definition deck_list_model.cpp:952
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:632
void setDeckList(const QSharedPointer< DeckList > &_deck)
Definition deck_list_model.cpp:823
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition deck_list_model.cpp:256
QModelIndex parent(const QModelIndex &index) const override
Definition deck_list_model.cpp:293
bool removeCardAtIndex(const QModelIndex &idx)
Removes the card node at the index.
Definition deck_list_model.cpp:665
QModelIndex addCard(const ExactCard &card, const QString &zoneName)
Adds an ExactCard to the specified zone.
Definition deck_list_model.cpp:506
DecklistModelCardNode * findCardNode(const QString &cardName, const QString &zoneName, const QString &providerId="", const QString &cardNumber="") const
Definition deck_list_model.cpp:429
InnerDecklistNode * createNodeIfNeeded(const QString &name, InnerDecklistNode *parent)
Definition deck_list_model.cpp:417
InnerDecklistNode * root
Definition deck_list_model.h:425
QList< CardRef > getCardRefs() const
Gets a deduplicated list of all CardRefs that appear in the model.
Definition deck_list_model.cpp:857
void cardNodeRemoved()
Emitted whenever a card node is removed.
QString displayLang
Definition deck_list_model.h:429
DeckListModelGroupCriteria::Type activeGroupCriteria
Definition deck_list_model.h:426
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition deck_list_model.cpp:341
Represents a complete deck, including metadata, zones, cards, and sideboard plans.
Definition deck_list.h:67
Concrete node type representing an actual card entry in the deck.
Definition deck_list_card_node.h:52
void setNumber(int _number) override
Definition deck_list_card_node.h:107
QString getCardProviderId() const override
Definition deck_list_card_node.h:125
void setCardCollectorNumber(const QString &_cardSetNumber) override
Definition deck_list_card_node.h:155
QString getCardSetShortName() const override
Definition deck_list_card_node.h:137
void setCardProviderId(const QString &_providerId) override
Definition deck_list_card_node.h:131
QString getCardCollectorNumber() const override
Definition deck_list_card_node.h:149
void setFormatLegality(const bool _formatLegal) override
Definition deck_list_card_node.h:167
QString getName() const override
Definition deck_list_card_node.h:113
void setCardSetShortName(const QString &_cardSetShortName) override
Definition deck_list_card_node.h:143
int getNumber() const override
Definition deck_list_card_node.h:101
bool getFormatLegality() const override
Definition deck_list_card_node.h:161
void setName(const QString &_name) override
Definition deck_list_card_node.h:119
Adapter node that wraps a DecklistCardNode for use in the DeckListModel tree.
Definition deck_list_model.h:126
DecklistCardNode * dataNode
Definition deck_list_model.h:128
void setName(const QString &_name) override
Definition deck_list_model.h:153
void setCardCollectorNumber(const QString &_cardSetNumber) override
Definition deck_list_model.h:177
void setCardProviderId(const QString &_cardProviderId) override
Definition deck_list_model.h:161
DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent, int position=-1)
Constructs a model node wrapping a DecklistCardNode.
Definition deck_list_model.h:137
QString getCardSetShortName() const override
Definition deck_list_model.h:165
void setFormatLegality(const bool _formatLegal) override
Definition deck_list_model.h:185
void setNumber(int _number) override
Definition deck_list_model.h:145
QString getCardProviderId() const override
Definition deck_list_model.h:157
QString getName() const override
Definition deck_list_model.h:149
QString getCardCollectorNumber() const override
Definition deck_list_model.h:173
DecklistCardNode * getDataNode() const
Returns the underlying data node.
Definition deck_list_model.h:194
bool isDeckHeader() const override
Whether this node is the "deck header" (deck metadata).
Definition deck_list_model.h:198
int getNumber() const override
Definition deck_list_model.h:141
bool getFormatLegality() const override
Definition deck_list_model.h:181
void setCardSetShortName(const QString &_cardSetShortName) override
Definition deck_list_model.h:169
Represents a specific card instance, defined by its CardInfo and a particular printing.
Definition exact_card.h:24
Represents a container node in the deck list hierarchy (zones and groupings).
Definition inner_deck_list_node.h:73
Definition remote_connection_controller.h:20
Definition arrow_registry.h:10
Defines the DecklistCardNode class, representing a single card entry in the deck list tree.
Defines the InnerDecklistNode class, which represents structural nodes (zones and groups) in the deck...
Column indices for the DeckListModel.
@ CARD_AMOUNT
Definition deck_list_model.h:62
@ CARD_SET
Definition deck_list_model.h:64
@ CARD_PROVIDER_ID
Definition deck_list_model.h:66
@ CARD_COLLECTOR_NUMBER
Definition deck_list_model.h:65
@ CARD_NAME
Definition deck_list_model.h:63
Specifies criteria used to group cards in the DeckListModel.
static QString toString(Type t)
Definition deck_list_model.h:88
static Type fromString(const QString &s)
Definition deck_list_model.h:101
Type
Definition deck_list_model.h:83
@ MAIN_TYPE
Definition deck_list_model.h:84
@ COLOR
Definition deck_list_model.h:86
@ MANA_COST
Definition deck_list_model.h:85
Custom model roles used by the DeckListModel for data retrieval.
@ IsLegalRole
Definition deck_list_model.h:43
@ DepthRole
Definition deck_list_model.h:42
@ IsCustomZoneRole
Definition deck_list_model.h:44
@ IsCardRole
Definition deck_list_model.h:41