diff --git a/cockatrice/src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp b/cockatrice/src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp index 18b05f96b..56bd16f0a 100644 --- a/cockatrice/src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp +++ b/cockatrice/src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp @@ -13,6 +13,30 @@ #include #include +static int findRestoreIndex(const CardRef &wanted, const QComboBox *combo) +{ + // Try providerId + name (strongest match) + if (!wanted.providerId.isEmpty()) { + for (int i = 0; i < combo->count(); ++i) { + auto pair = combo->itemData(i).value>(); + if (pair.second == wanted.providerId && pair.first == wanted.name) { + return i; + } + } + } + + // Try name only + for (int i = 0; i < combo->count(); ++i) { + auto pair = combo->itemData(i).value>(); + if (pair.first == wanted.name) { + return i; + } + } + + // Not found + return -1; +} + DeckEditorDeckDockWidget::DeckEditorDeckDockWidget(AbstractTabDeckEditor *parent) : QDockWidget(parent), deckEditor(parent) { @@ -306,8 +330,8 @@ void DeckEditorDeckDockWidget::updateHash() void DeckEditorDeckDockWidget::updateBannerCardComboBox() { - // Store the current text of the combo box - QString currentText = bannerCardComboBox->currentText(); + // Store current banner card identity + CardRef wanted = deckModel->getDeckList()->getBannerCard(); // Block signals temporarily bool wasBlocked = bannerCardComboBox->blockSignals(true); @@ -315,49 +339,45 @@ void DeckEditorDeckDockWidget::updateBannerCardComboBox() // Clear the existing items in the combo box bannerCardComboBox->clear(); - // Prepare the new items with deduplication + // Collect unique (name, providerId) pairs QSet> bannerCardSet; QList cardsInDeck = deckModel->getDeckList()->getCardNodes(); for (auto currentCard : cardsInDeck) { - for (int k = 0; k < currentCard->getNumber(); ++k) { - if (CardDatabaseManager::query()->getCard(currentCard->toCardRef())) { - bannerCardSet.insert({currentCard->getName(), currentCard->getCardProviderId()}); - } + if (!CardDatabaseManager::query()->getCard(currentCard->toCardRef())) { + continue; } + + // Insert one entry per distinct card, ignore copies + bannerCardSet.insert({currentCard->getName(), currentCard->getCardProviderId()}); } + // Convert to sorted list QList> pairList = bannerCardSet.values(); // Sort QList by the first() element of the QPair - std::sort(pairList.begin(), pairList.end(), [](const QPair &a, const QPair &b) { - return a.first.toLower() < b.first.toLower(); - }); + std::sort(pairList.begin(), pairList.end(), + [](const auto &a, const auto &b) { return a.first.toLower() < b.first.toLower(); }); + // Add to combo box for (const auto &pair : pairList) { bannerCardComboBox->addItem(pair.first, QVariant::fromValue(pair)); } - // Try to restore the previous selection by finding the currentText - int restoredIndex = bannerCardComboBox->findText(currentText); - if (restoredIndex != -1) { - bannerCardComboBox->setCurrentIndex(restoredIndex); - if (deckModel->getDeckList()->getBannerCard().providerId != - bannerCardComboBox->currentData().value>().second) { - setBannerCard(restoredIndex); - } + // Try to find an index with a matching card + int restoreIndex = findRestoreIndex(wanted, bannerCardComboBox); + + // Handle results + if (restoreIndex != -1) { + bannerCardComboBox->setCurrentIndex(restoreIndex); + setBannerCard(restoreIndex); } else { // Add a placeholder "-" and set it as the current selection - int bannerIndex = bannerCardComboBox->findText(deckModel->getDeckList()->getBannerCard().name); - if (bannerIndex != -1) { - bannerCardComboBox->setCurrentIndex(bannerIndex); - } else { - bannerCardComboBox->insertItem(0, "-"); - bannerCardComboBox->setCurrentIndex(0); - } + bannerCardComboBox->insertItem(0, "-"); + bannerCardComboBox->setCurrentIndex(0); } - // Restore the previous signal blocking state + // Restore signal state bannerCardComboBox->blockSignals(wasBlocked); }