mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 00:55:09 -07:00
[CardSearchModel] Match English and localized names in Both mode
Card names are stored in both English and localized forms, so search for matches in both during the 'Both' search mode instead of checking only the localized name.
This commit is contained in:
parent
0967644640
commit
a640ada8cc
2 changed files with 45 additions and 23 deletions
|
|
@ -66,36 +66,30 @@ void CardSearchModel::updateSearchResults(const QString &query)
|
|||
}
|
||||
|
||||
// The completer suggestions match against the same languages the card
|
||||
// search uses, so typing a localized name finds the card.
|
||||
QString matchName = card->getName();
|
||||
if (searchLanguage.mode != SearchLanguageMode::English && !searchLanguage.isEnglishOnly()) {
|
||||
matchName = card->getLocalizedName(searchLanguage.language);
|
||||
}
|
||||
const QString lowerName = matchName.toLower();
|
||||
if (!lowerName.contains(lowerQuery)) {
|
||||
continue;
|
||||
}
|
||||
// search uses, so typing a localized name finds the card. In Both mode
|
||||
// either language can match.
|
||||
for (const QString &matchName : searchableNames(card)) {
|
||||
const QString lowerName = matchName.toLower();
|
||||
if (!lowerName.contains(lowerQuery)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int distance = levenshteinDistance(lowerQuery, lowerName);
|
||||
const int distance = levenshteinDistance(lowerQuery, lowerName);
|
||||
|
||||
if (lowerName.startsWith(lowerQuery)) {
|
||||
prefixMatches.append({card, distance});
|
||||
} else {
|
||||
containsMatches.append({card, distance});
|
||||
if (lowerName.startsWith(lowerQuery)) {
|
||||
prefixMatches.append({card, distance});
|
||||
} else {
|
||||
containsMatches.append({card, distance});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
auto sortByDistanceThenLength = [this](const SearchResult &a, const SearchResult &b) {
|
||||
QString nameA = a.card->getName();
|
||||
QString nameB = b.card->getName();
|
||||
if (searchLanguage.mode != SearchLanguageMode::English && !searchLanguage.isEnglishOnly()) {
|
||||
nameA = a.card->getLocalizedName(searchLanguage.language);
|
||||
nameB = b.card->getLocalizedName(searchLanguage.language);
|
||||
}
|
||||
if (a.distance != b.distance) {
|
||||
return a.distance < b.distance;
|
||||
}
|
||||
return nameA.size() < nameB.size();
|
||||
return sortableName(a.card).size() < sortableName(b.card).size();
|
||||
};
|
||||
|
||||
std::sort(prefixMatches.begin(), prefixMatches.end(), sortByDistanceThenLength);
|
||||
|
|
@ -113,3 +107,25 @@ void CardSearchModel::updateSearchResults(const QString &query)
|
|||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QStringList CardSearchModel::searchableNames(const CardInfoPtr &card) const
|
||||
{
|
||||
if (searchLanguage.isEnglishOnly()) {
|
||||
return {card->getName()};
|
||||
}
|
||||
|
||||
const QString localizedName = card->getLocalizedName(searchLanguage.language);
|
||||
if (searchLanguage.mode == SearchLanguageMode::Selected) {
|
||||
return {localizedName};
|
||||
}
|
||||
|
||||
return {card->getName(), localizedName};
|
||||
}
|
||||
|
||||
QString CardSearchModel::sortableName(const CardInfoPtr &card) const
|
||||
{
|
||||
if (searchLanguage.isEnglishOnly()) {
|
||||
return card->getName();
|
||||
}
|
||||
return card->getLocalizedName(searchLanguage.language);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,6 +42,12 @@ private:
|
|||
int distance;
|
||||
};
|
||||
|
||||
/** @brief The names a card is searched by with the current search language. */
|
||||
[[nodiscard]] QStringList searchableNames(const CardInfoPtr &card) const;
|
||||
|
||||
/** @brief The name used to break distance ties when sorting suggestions. */
|
||||
[[nodiscard]] QString sortableName(const CardInfoPtr &card) const;
|
||||
|
||||
CardDatabaseDisplayModel *sourceModel;
|
||||
QList<SearchResult> searchResults;
|
||||
CardSearchLanguage searchLanguage;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue