mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-11 21:04:07 -07:00
[BannerCard] Try to restore by providerId
Took 27 minutes Took 41 seconds
This commit is contained in:
parent
8788a7aada
commit
137328dc10
1 changed files with 42 additions and 28 deletions
|
|
@ -272,8 +272,9 @@ void DeckEditorDeckDockWidget::updateHash()
|
||||||
|
|
||||||
void DeckEditorDeckDockWidget::updateBannerCardComboBox()
|
void DeckEditorDeckDockWidget::updateBannerCardComboBox()
|
||||||
{
|
{
|
||||||
// Store the current text of the combo box
|
// Store current banner card identity
|
||||||
QString currentText = bannerCardComboBox->currentText();
|
QString wantedName = deckModel->getDeckList()->getBannerCard().name;
|
||||||
|
QString wantedProvider = deckModel->getDeckList()->getBannerCard().providerId;
|
||||||
|
|
||||||
// Block signals temporarily
|
// Block signals temporarily
|
||||||
bool wasBlocked = bannerCardComboBox->blockSignals(true);
|
bool wasBlocked = bannerCardComboBox->blockSignals(true);
|
||||||
|
|
@ -281,49 +282,62 @@ void DeckEditorDeckDockWidget::updateBannerCardComboBox()
|
||||||
// Clear the existing items in the combo box
|
// Clear the existing items in the combo box
|
||||||
bannerCardComboBox->clear();
|
bannerCardComboBox->clear();
|
||||||
|
|
||||||
// Prepare the new items with deduplication
|
// Collect unique (name, providerId) pairs
|
||||||
QSet<QPair<QString, QString>> bannerCardSet;
|
QSet<QPair<QString, QString>> bannerCardSet;
|
||||||
QList<DecklistCardNode *> cardsInDeck = deckModel->getDeckList()->getCardNodes();
|
QList<DecklistCardNode *> cardsInDeck = deckModel->getDeckList()->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
if (!CardDatabaseManager::query()->getCard(currentCard->toCardRef()))
|
||||||
if (CardDatabaseManager::query()->getCard(currentCard->toCardRef())) {
|
continue;
|
||||||
bannerCardSet.insert({currentCard->getName(), currentCard->getCardProviderId()});
|
|
||||||
}
|
// 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();
|
QList<QPair<QString, QString>> pairList = bannerCardSet.values();
|
||||||
|
|
||||||
// Sort QList by the first() element of the QPair
|
// 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) {
|
std::sort(pairList.begin(), pairList.end(),
|
||||||
return a.first.toLower() < b.first.toLower();
|
[](const auto &a, const auto &b) { return a.first.toLower() < b.first.toLower(); });
|
||||||
});
|
|
||||||
|
|
||||||
|
// Add to combo box
|
||||||
for (const auto &pair : pairList) {
|
for (const auto &pair : pairList) {
|
||||||
bannerCardComboBox->addItem(pair.first, QVariant::fromValue(pair));
|
bannerCardComboBox->addItem(pair.first, QVariant::fromValue(pair));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to restore the previous selection by finding the currentText
|
// Try restoring by provider ID first (strongest match)
|
||||||
int restoredIndex = bannerCardComboBox->findText(currentText);
|
int restoreIndex = -1;
|
||||||
if (restoredIndex != -1) {
|
for (int i = 0; i < bannerCardComboBox->count(); ++i) {
|
||||||
bannerCardComboBox->setCurrentIndex(restoredIndex);
|
auto pair = bannerCardComboBox->itemData(i).value<QPair<QString, QString>>();
|
||||||
if (deckModel->getDeckList()->getBannerCard().providerId !=
|
if (pair.second == wantedProvider) {
|
||||||
bannerCardComboBox->currentData().value<QPair<QString, QString>>().second) {
|
restoreIndex = i;
|
||||||
setBannerCard(restoredIndex);
|
break;
|
||||||
}
|
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore the previous signal blocking state
|
// If provider ID not found, try matching name
|
||||||
|
if (restoreIndex == -1) {
|
||||||
|
for (int i = 0; i < bannerCardComboBox->count(); ++i) {
|
||||||
|
auto pair = bannerCardComboBox->itemData(i).value<QPair<QString, QString>>();
|
||||||
|
if (pair.first == wantedName) {
|
||||||
|
restoreIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle results
|
||||||
|
if (restoreIndex != -1) {
|
||||||
|
bannerCardComboBox->setCurrentIndex(restoreIndex);
|
||||||
|
setBannerCard(restoreIndex);
|
||||||
|
} else {
|
||||||
|
// Add a placeholder "-" and set it as the current selection
|
||||||
|
bannerCardComboBox->insertItem(0, "-");
|
||||||
|
bannerCardComboBox->setCurrentIndex(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore signal state
|
||||||
bannerCardComboBox->blockSignals(wasBlocked);
|
bannerCardComboBox->blockSignals(wasBlocked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue