Merge pull request #7665 from jordan-woyak/game-list-natural-sort

Qt Game List: Sort game titles "naturally". e.g. 10 comes after 9.
This commit is contained in:
Pierre Bourdon 2019-01-01 13:15:00 +01:00 committed by GitHub
commit cddb83fd06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -89,6 +89,8 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
if (role == Qt::DisplayRole || role == Qt::InitialSortOrderRole)
{
QString name = QString::fromStdString(game.GetName(m_title_database));
// Add disc numbers > 1 to title if not present.
const int disc_nr = game.GetDiscNumber() + 1;
if (disc_nr > 1)
{
@ -97,6 +99,21 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const
name.append(tr(" (Disc %1)").arg(disc_nr));
}
}
// For natural sorting, pad all numbers to the same length.
if (Qt::InitialSortOrderRole == role)
{
constexpr int MAX_NUMBER_LENGTH = 10;
QRegExp rx(QStringLiteral("\\d+"));
int pos = 0;
while ((pos = rx.indexIn(name, pos)) != -1)
{
name.replace(pos, rx.matchedLength(), rx.cap().rightJustified(MAX_NUMBER_LENGTH));
pos += MAX_NUMBER_LENGTH;
}
}
return name;
}
break;