[TabDeckEditor] Create class to centralize deck state (#6459)

* create new file

* use QSharedPointer in DeckListModel

* [TabDeckEditor] Create class to centralize deck state

* delete method

* update docs
This commit is contained in:
RickyRister 2025-12-31 08:54:47 -08:00 committed by GitHub
parent 0085015ebe
commit b2dd8eed3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 933 additions and 577 deletions

View file

@ -5,14 +5,15 @@
DeckListModel::DeckListModel(QObject *parent)
: QAbstractItemModel(parent), lastKnownColumn(1), lastKnownOrder(Qt::AscendingOrder)
{
// This class will leak the decklist object. We cannot safely delete it in the dtor because the deckList field is a
// non-owning pointer and another deckList might have been assigned to it.
// `DeckListModel::cleanList` also leaks for the same reason.
// TODO: fix the leak
deckList = new DeckList;
deckList = QSharedPointer<DeckList>(new DeckList());
root = new InnerDecklistNode;
}
DeckListModel::DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList) : DeckListModel(parent)
{
setDeckList(deckList);
}
DeckListModel::~DeckListModel()
{
delete root;
@ -586,13 +587,13 @@ void DeckListModel::setActiveFormat(const QString &_format)
void DeckListModel::cleanList()
{
setDeckList(new DeckList);
setDeckList(QSharedPointer<DeckList>(new DeckList()));
}
/**
* @param _deck The deck.
*/
void DeckListModel::setDeckList(DeckList *_deck)
void DeckListModel::setDeckList(const QSharedPointer<DeckList> &_deck)
{
if (deckList != _deck) {
deckList = _deck;

View file

@ -245,6 +245,7 @@ signals:
public:
explicit DeckListModel(QObject *parent = nullptr);
explicit DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList);
~DeckListModel() override;
/**
@ -314,11 +315,12 @@ public:
* @brief Removes all cards and resets the model.
*/
void cleanList();
[[nodiscard]] DeckList *getDeckList() const
[[nodiscard]] QSharedPointer<DeckList> getDeckList() const
{
return deckList;
}
void setDeckList(DeckList *_deck);
void setDeckList(const QSharedPointer<DeckList> &_deck);
/**
* @brief Apply a function to every card in the deck tree.
@ -351,8 +353,8 @@ public:
[[nodiscard]] QList<QString> getZones() const;
private:
DeckList *deckList; /**< Pointer to the deck loader providing the underlying data. */
InnerDecklistNode *root; /**< Root node of the model tree. */
QSharedPointer<DeckList> deckList; /**< Pointer to the decklist providing the underlying data. */
InnerDecklistNode *root; /**< Root node of the model tree. */
DeckListModelGroupCriteria::Type activeGroupCriteria = DeckListModelGroupCriteria::MAIN_TYPE;
int lastKnownColumn; /**< Last column used for sorting. */
Qt::SortOrder lastKnownOrder; /**< Last known sort order. */