[BannerCard] Try to restore by providerId (#6341)

* [BannerCard] Try to restore by providerId

Took 27 minutes

Took 41 seconds

* Style lint.

Took 2 minutes

* Don't look up by providerId if it's empty.

Took 8 minutes

* Add extra name guard to providerId clause.

Took 4 minutes

* Update cockatrice/src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp

Co-authored-by: RickyRister <42636155+RickyRister@users.noreply.github.com>

* Update cockatrice/src/interface/widgets/deck_editor/deck_editor_deck_dock_widget.cpp

Co-authored-by: RickyRister <42636155+RickyRister@users.noreply.github.com>

* Adjust to comments.

Took 11 minutes

* Extract to helper function.

Took 3 minutes

* Make helper static.

Took 5 minutes

* Remove const qualifier.

Took 3 minutes

* Finally.

Took 5 minutes

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
Co-authored-by: RickyRister <42636155+RickyRister@users.noreply.github.com>
This commit is contained in:
BruebachL
2025-11-20 15:44:35 +01:00
committed by GitHub
parent 846f16ddaa
commit 73591d5d0f

View File

@@ -13,6 +13,30 @@
#include <libcockatrice/card/database/card_database_manager.h>
#include <libcockatrice/utility/trice_limits.h>
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<QPair<QString, QString>>();
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<QPair<QString, QString>>();
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<QPair<QString, QString>> bannerCardSet;
QList<DecklistCardNode *> 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<QPair<QString, QString>> pairList = bannerCardSet.values();
// Sort QList by the first() element of the QPair
std::sort(pairList.begin(), pairList.end(), [](const QPair<QString, QString> &a, const QPair<QString, QString> &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<QPair<QString, QString>>().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);
}