From 703e129f71efd08629fe3c9b11a52f10c6770b71 Mon Sep 17 00:00:00 2001 From: Noah Davis Date: Sat, 5 Mar 2022 02:38:29 -0500 Subject: [PATCH] wallpapers: Sort BackgroundListModel by title It seems like it had no sorting at all previously. Initially I thought the issue was 10 coming before 2 because it starts with 1, but it seems like reverse orders were also possible. Now it is always sorted alphabetically with numbers properly accounted for. --- wallpapers/image/backgroundlistmodel.cpp | 32 ++++++++++++++++++------ wallpapers/image/backgroundlistmodel.h | 1 + 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/wallpapers/image/backgroundlistmodel.cpp b/wallpapers/image/backgroundlistmodel.cpp index 33ba39ec7..0c990a630 100644 --- a/wallpapers/image/backgroundlistmodel.cpp +++ b/wallpapers/image/backgroundlistmodel.cpp @@ -10,6 +10,7 @@ #include "backgroundlistmodel.h" #include "debug.h" +#include #include #include #include @@ -33,6 +34,8 @@ #include +#include + QStringList BackgroundFinder::s_suffixes; QMutex BackgroundFinder::s_suffixMutex; @@ -177,6 +180,18 @@ void BackgroundListModel::processPaths(const QStringList &paths) if (!newPackages.isEmpty()) { m_packages.append(newPackages); + QCollator collator; + // Make sure 2 comes before 10 + collator.setNumericMode(true); + // Behave like Dolphin with natural sorting enabled + collator.setCaseSensitivity(Qt::CaseInsensitive); + const auto compare = [this, &collator](const KPackage::Package &a, const KPackage::Package &b){ + const QString aDisplay = displayStringForPackage(a); + const QString bDisplay = displayStringForPackage(b); + // Checking if less than zero makes ascending order (A-Z) + return collator.compare(aDisplay, bDisplay) < 0; + }; + std::stable_sort(m_packages.begin(), m_packages.end(), compare); } endResetModel(); Q_EMIT countChanged(); @@ -283,6 +298,15 @@ void BackgroundListModel::sizeFound(const QString &path, const QSize &s) } } +QString BackgroundListModel::displayStringForPackage(const KPackage::Package &package) const +{ + QString title = package.metadata().isValid() ? package.metadata().name() : QString(); + if (title.isEmpty()) { + return QFileInfo(package.filePath("preferred")).completeBaseName(); + } + return title; +} + QVariant BackgroundListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { @@ -300,13 +324,7 @@ QVariant BackgroundListModel::data(const QModelIndex &index, int role) const switch (role) { case Qt::DisplayRole: { - QString title = b.metadata().isValid() ? b.metadata().name() : QString(); - - if (title.isEmpty()) { - return QFileInfo(b.filePath("preferred")).completeBaseName(); - } - - return title; + return displayStringForPackage(b); } case ScreenshotRole: { diff --git a/wallpapers/image/backgroundlistmodel.h b/wallpapers/image/backgroundlistmodel.h index f10f938b1..b80431438 100644 --- a/wallpapers/image/backgroundlistmodel.h +++ b/wallpapers/image/backgroundlistmodel.h @@ -98,6 +98,7 @@ protected: private: QSize bestSize(const KPackage::Package &package) const; + QString displayStringForPackage(const KPackage::Package &package) const; QSet m_removableWallpapers; QHash m_sizeCache;