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.
wilder-5.25
Noah Davis 4 years ago
parent e49897b8f7
commit 703e129f71
  1. 32
      wallpapers/image/backgroundlistmodel.cpp
  2. 1
      wallpapers/image/backgroundlistmodel.h

@ -10,6 +10,7 @@
#include "backgroundlistmodel.h"
#include "debug.h"
#include <QCollator>
#include <QDir>
#include <QElapsedTimer>
#include <QFile>
@ -33,6 +34,8 @@
#include <KIO/OpenFileManagerWindowJob>
#include <algorithm>
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: {

@ -98,6 +98,7 @@ protected:
private:
QSize bestSize(const KPackage::Package &package) const;
QString displayStringForPackage(const KPackage::Package &package) const;
QSet<QString> m_removableWallpapers;
QHash<QString, QSize> m_sizeCache;

Loading…
Cancel
Save