[DeckListModel] remove more access to underlying decklist for iteration (#6436)

* [DeckListModel] remove more access to underlying decklist for iteration

* remove one last direct iteration of decklist
This commit is contained in:
RickyRister 2025-12-21 16:19:57 -08:00 committed by GitHub
parent a0f977e80c
commit c12f4e9d2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 35 additions and 10 deletions

View file

@ -386,15 +386,15 @@ void DeckEditorDeckDockWidget::updateBannerCardComboBox()
// Collect unique (name, providerId) pairs // Collect unique (name, providerId) pairs
QSet<QPair<QString, QString>> bannerCardSet; QSet<QPair<QString, QString>> bannerCardSet;
QList<const DecklistCardNode *> cardsInDeck = deckModel->getDeckList()->getCardNodes(); QList<CardRef> cardsInDeck = deckModel->getCardRefs();
for (auto currentCard : cardsInDeck) { for (auto cardRef : cardsInDeck) {
if (!CardDatabaseManager::query()->getCard(currentCard->toCardRef())) { if (!CardDatabaseManager::query()->getCard(cardRef)) {
continue; continue;
} }
// Insert one entry per distinct card, ignore copies // Insert one entry per distinct card, ignore copies
bannerCardSet.insert({currentCard->getName(), currentCard->getCardProviderId()}); bannerCardSet.insert({cardRef.name, cardRef.providerId});
} }
// Convert to sorted list // Convert to sorted list

View file

@ -178,7 +178,7 @@ void DlgSelectSetForCards::actOK()
void DlgSelectSetForCards::actClear() void DlgSelectSetForCards::actClear()
{ {
emit deckAboutToBeModified(tr("Cleared all printing information.")); emit deckAboutToBeModified(tr("Cleared all printing information."));
model->getDeckList()->forEachCard(CardNodeFunction::ClearPrintingData()); model->forEachCard(CardNodeFunction::ClearPrintingData());
emit deckModified(); emit deckModified();
accept(); accept();
} }
@ -186,8 +186,8 @@ void DlgSelectSetForCards::actClear()
void DlgSelectSetForCards::actSetAllToPreferred() void DlgSelectSetForCards::actSetAllToPreferred()
{ {
emit deckAboutToBeModified(tr("Set all printings to preferred.")); emit deckAboutToBeModified(tr("Set all printings to preferred."));
model->getDeckList()->forEachCard(CardNodeFunction::ClearPrintingData()); model->forEachCard(CardNodeFunction::ClearPrintingData());
model->getDeckList()->forEachCard(CardNodeFunction::SetProviderIdToPreferred()); model->forEachCard(CardNodeFunction::SetProviderIdToPreferred());
emit deckModified(); emit deckModified();
accept(); accept();
} }

View file

@ -70,9 +70,7 @@ ArchidektApiResponseDeckDisplayWidget::ArchidektApiResponseDeckDisplayWidget(QWi
connect(model, &DeckListModel::modelReset, this, &ArchidektApiResponseDeckDisplayWidget::decklistModelReset); connect(model, &DeckListModel::modelReset, this, &ArchidektApiResponseDeckDisplayWidget::decklistModelReset);
model->getDeckList()->loadFromStream_Plain(deckStream, false); model->getDeckList()->loadFromStream_Plain(deckStream, false);
model->getDeckList()->forEachCard(CardNodeFunction::ResolveProviderId()); model->forEachCard(CardNodeFunction::ResolveProviderId());
model->rebuildTree();
retranslateUi(); retranslateUi();
} }

View file

@ -561,6 +561,11 @@ void DeckListModel::setDeckList(DeckList *_deck)
rebuildTree(); rebuildTree();
} }
void DeckListModel::forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func)
{
deckList->forEachCard(func);
}
static QList<ExactCard> cardNodesToExactCards(QList<const DecklistCardNode *> nodes) static QList<ExactCard> cardNodesToExactCards(QList<const DecklistCardNode *> nodes)
{ {
QList<ExactCard> cards; QList<ExactCard> cards;
@ -600,6 +605,17 @@ QList<QString> DeckListModel::getCardNames() const
return names; return names;
} }
QList<CardRef> DeckListModel::getCardRefs() const
{
auto nodes = deckList->getCardNodes();
QList<CardRef> cardRefs;
std::transform(nodes.cbegin(), nodes.cend(), std::back_inserter(cardRefs),
[](auto node) { return node->toCardRef(); });
return cardRefs;
}
QList<QString> DeckListModel::getZones() const QList<QString> DeckListModel::getZones() const
{ {
auto zoneNodes = deckList->getZoneNodes(); auto zoneNodes = deckList->getZoneNodes();

View file

@ -309,6 +309,13 @@ public:
} }
void setDeckList(DeckList *_deck); void setDeckList(DeckList *_deck);
/**
* @brief Apply a function to every card in the deck tree.
*
* @param func Function taking (zone node, card node).
*/
void forEachCard(const std::function<void(InnerDecklistNode *, DecklistCardNode *)> &func);
/** /**
* @brief Creates a list consisting of the entries of the model mapped into ExactCards (with each entry looked up * @brief Creates a list consisting of the entries of the model mapped into ExactCards (with each entry looked up
* in the card database). * in the card database).
@ -323,6 +330,10 @@ public:
* @brief Gets a deduplicated list of all card names that appear in the model * @brief Gets a deduplicated list of all card names that appear in the model
*/ */
[[nodiscard]] QList<QString> getCardNames() const; [[nodiscard]] QList<QString> getCardNames() const;
/**
* @brief Gets a deduplicated list of all CardRefs that appear in the model
*/
[[nodiscard]] QList<CardRef> getCardRefs() const;
/** /**
* @brief Gets a list of all zone names that appear in the model * @brief Gets a list of all zone names that appear in the model
*/ */