Add sortable Identity Attack Armour Health columns and rename P/T to ATK/HP

This commit is contained in:
Christian Daniel Jensen 2026-11-03 13:13:01 -07:00
parent c69c0854fb
commit ea0ff5c8df

View file

@ -70,11 +70,18 @@ bool CardDatabaseDisplayModel::lessThan(const QModelIndex &left, const QModelInd
if (isRightType && (!isLeftType || rightString.size() == cardName.size())) { if (isRightType && (!isLeftType || rightString.size() == cardName.size())) {
return false; return false;
} }
} else if (right.column() == CardDatabaseModel::PTColumn && left.column() == CardDatabaseModel::PTColumn) { } else if (left.column() == right.column() &&
// The comparator runs O(n log n) times while the view is sorted, so (left.column() == CardDatabaseModel::AttackColumn ||
// splitting every power/toughness string into a QStringList allocates left.column() == CardDatabaseModel::ArmourColumn ||
// a lot for data that has exactly one separator. Work on views and left.column() == CardDatabaseModel::HealthColumn)) {
// only split the two halves out when both strings are well formed. // Compare numeric card statistics as numbers: 9 sorts before 10.
// Values containing expressions retain the existing numeric-prefix
// comparator instead of being forced into an integer type.
return lessThanNumerically(leftString, rightString) < 0;
} else if (right.column() == CardDatabaseModel::AtkHpColumn &&
left.column() == CardDatabaseModel::AtkHpColumn) {
// ATK/HP sorts by Attack first, then Health, both numerically.
// Operate on views to avoid allocations during large database sorts.
const QStringView leftPT = leftString; const QStringView leftPT = leftString;
const QStringView rightPT = rightString; const QStringView rightPT = rightString;
const qsizetype leftSlash = leftPT.indexOf(u'/'); const qsizetype leftSlash = leftPT.indexOf(u'/');
@ -83,11 +90,11 @@ bool CardDatabaseDisplayModel::lessThan(const QModelIndex &left, const QModelInd
const bool rightIsWellFormed = rightSlash >= 0 && rightPT.indexOf(u'/', rightSlash + 1) < 0; const bool rightIsWellFormed = rightSlash >= 0 && rightPT.indexOf(u'/', rightSlash + 1) < 0;
if (leftIsWellFormed && rightIsWellFormed) { if (leftIsWellFormed && rightIsWellFormed) {
const int powerComparison = lessThanNumerically(leftPT.left(leftSlash), rightPT.left(rightSlash)); const int attackComparison = lessThanNumerically(leftPT.left(leftSlash), rightPT.left(rightSlash));
if (powerComparison != 0) { if (attackComparison != 0) {
return powerComparison < 0; return attackComparison < 0;
} }
// power equal, check toughness // Attack equal, compare Health
return lessThanNumerically(leftPT.mid(leftSlash + 1), rightPT.mid(rightSlash + 1)) < 0; return lessThanNumerically(leftPT.mid(leftSlash + 1), rightPT.mid(rightSlash + 1)) < 0;
} }
} }