Browse Source

Simplify natural sort classes interface

Now the comparison function/class should be constructed before usage.
This change also make it easier to plug in into various containers which
will require a compare function type (such as std::set).
adaptive-webui-19844
Chocobo1 4 years ago
parent
commit
a64bb1a990
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 4
      src/app/application.cpp
  2. 2
      src/base/CMakeLists.txt
  3. 2
      src/base/base.pri
  4. 7
      src/base/bittorrent/torrentcreatorthread.cpp
  5. 96
      src/base/utils/compare.cpp
  6. 88
      src/base/utils/compare.h
  7. 133
      src/base/utils/string.cpp
  8. 11
      src/base/utils/string.h
  9. 4
      src/gui/addnewtorrentdialog.cpp
  10. 7
      src/gui/categoryfilterproxymodel.cpp
  11. 4
      src/gui/categoryfilterproxymodel.h
  12. 6
      src/gui/properties/peerlistsortmodel.cpp
  13. 4
      src/gui/properties/peerlistsortmodel.h
  14. 3
      src/gui/rss/automatedrssdownloader.cpp
  15. 6
      src/gui/search/searchsortmodel.cpp
  16. 4
      src/gui/search/searchsortmodel.h
  17. 6
      src/gui/tagfilterproxymodel.cpp
  18. 4
      src/gui/tagfilterproxymodel.h
  19. 3
      src/gui/torrentcontentfiltermodel.cpp
  20. 2
      src/gui/torrentcontentfiltermodel.h
  21. 5
      src/gui/transferlistfilterswidget.cpp
  22. 3
      src/gui/transferlistsortmodel.cpp
  23. 3
      src/gui/transferlistsortmodel.h
  24. 5
      src/gui/transferlistwidget.cpp

4
src/app/application.cpp

@ -78,9 +78,9 @@
#include "base/scanfoldersmodel.h" #include "base/scanfoldersmodel.h"
#include "base/search/searchpluginmanager.h" #include "base/search/searchpluginmanager.h"
#include "base/settingsstorage.h" #include "base/settingsstorage.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "base/utils/string.h"
#include "base/version.h" #include "base/version.h"
#include "applicationinstancemanager.h" #include "applicationinstancemanager.h"
#include "filelogger.h" #include "filelogger.h"
@ -352,7 +352,7 @@ void Application::runExternalProgram(const BitTorrent::Torrent *torrent) const
case u'G': case u'G':
{ {
QStringList tags = torrent->tags().values(); QStringList tags = torrent->tags().values();
std::sort(tags.begin(), tags.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(tags.begin(), tags.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
program.replace(i, 2, tags.join(',')); program.replace(i, 2, tags.join(','));
} }
break; break;

2
src/base/CMakeLists.txt

@ -79,6 +79,7 @@ add_library(qbt_base STATIC
types.h types.h
unicodestrings.h unicodestrings.h
utils/bytearray.h utils/bytearray.h
utils/compare.h
utils/foreignapps.h utils/foreignapps.h
utils/fs.h utils/fs.h
utils/gzip.h utils/gzip.h
@ -154,6 +155,7 @@ add_library(qbt_base STATIC
torrentfileguard.cpp torrentfileguard.cpp
torrentfilter.cpp torrentfilter.cpp
utils/bytearray.cpp utils/bytearray.cpp
utils/compare.cpp
utils/foreignapps.cpp utils/foreignapps.cpp
utils/fs.cpp utils/fs.cpp
utils/gzip.cpp utils/gzip.cpp

2
src/base/base.pri

@ -79,6 +79,7 @@ HEADERS += \
$$PWD/types.h \ $$PWD/types.h \
$$PWD/unicodestrings.h \ $$PWD/unicodestrings.h \
$$PWD/utils/bytearray.h \ $$PWD/utils/bytearray.h \
$$PWD/utils/compare.h \
$$PWD/utils/foreignapps.h \ $$PWD/utils/foreignapps.h \
$$PWD/utils/fs.h \ $$PWD/utils/fs.h \
$$PWD/utils/gzip.h \ $$PWD/utils/gzip.h \
@ -154,6 +155,7 @@ SOURCES += \
$$PWD/torrentfileguard.cpp \ $$PWD/torrentfileguard.cpp \
$$PWD/torrentfilter.cpp \ $$PWD/torrentfilter.cpp \
$$PWD/utils/bytearray.cpp \ $$PWD/utils/bytearray.cpp \
$$PWD/utils/compare.cpp \
$$PWD/utils/foreignapps.cpp \ $$PWD/utils/foreignapps.cpp \
$$PWD/utils/fs.cpp \ $$PWD/utils/fs.cpp \
$$PWD/utils/gzip.cpp \ $$PWD/utils/gzip.cpp \

7
src/base/bittorrent/torrentcreatorthread.cpp

@ -42,9 +42,9 @@
#include "base/exceptions.h" #include "base/exceptions.h"
#include "base/global.h" #include "base/global.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/io.h" #include "base/utils/io.h"
#include "base/utils/string.h"
#include "base/version.h" #include "base/version.h"
#include "ltunderlyingtype.h" #include "ltunderlyingtype.h"
@ -105,6 +105,7 @@ void TorrentCreatorThread::run()
try try
{ {
const QString parentPath = Utils::Fs::branchPath(m_params.inputPath) + '/'; const QString parentPath = Utils::Fs::branchPath(m_params.inputPath) + '/';
const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> naturalLessThan {};
// Adding files to the torrent // Adding files to the torrent
lt::file_storage fs; lt::file_storage fs;
@ -123,7 +124,7 @@ void TorrentCreatorThread::run()
dirIter.next(); dirIter.next();
dirs += dirIter.filePath(); dirs += dirIter.filePath();
} }
std::sort(dirs.begin(), dirs.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(dirs.begin(), dirs.end(), naturalLessThan);
QStringList fileNames; QStringList fileNames;
QHash<QString, qint64> fileSizeMap; QHash<QString, qint64> fileSizeMap;
@ -142,7 +143,7 @@ void TorrentCreatorThread::run()
fileSizeMap[relFilePath] = fileIter.fileInfo().size(); fileSizeMap[relFilePath] = fileIter.fileInfo().size();
} }
std::sort(tmpNames.begin(), tmpNames.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(tmpNames.begin(), tmpNames.end(), naturalLessThan);
fileNames += tmpNames; fileNames += tmpNames;
} }

96
src/base/utils/compare.cpp

@ -0,0 +1,96 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "compare.h"
#include <QChar>
#include <QString>
#ifndef QBT_USE_QCOLLATOR
int Utils::Compare::naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity)
{
// Return value <0: `left` is smaller than `right`
// Return value >0: `left` is greater than `right`
// Return value =0: both strings are equal
int posL = 0;
int posR = 0;
while (true)
{
if ((posL == left.size()) || (posR == right.size()))
return (left.size() - right.size()); // when a shorter string is another string's prefix, shorter string place before longer string
const QChar leftChar = (caseSensitivity == Qt::CaseSensitive) ? left[posL] : left[posL].toLower();
const QChar rightChar = (caseSensitivity == Qt::CaseSensitive) ? right[posR] : right[posR].toLower();
// Compare only non-digits.
// Numbers should be compared as a whole
// otherwise the string->int conversion can yield a wrong value
if ((leftChar == rightChar) && !leftChar.isDigit())
{
// compare next character
++posL;
++posR;
}
else if (leftChar.isDigit() && rightChar.isDigit())
{
// Both are digits, compare the numbers
const auto numberView = [](const QString &str, int &pos) -> QStringRef
{
const int start = pos;
while ((pos < str.size()) && str[pos].isDigit())
++pos;
return str.midRef(start, (pos - start));
};
const QStringRef numViewL = numberView(left, posL);
const QStringRef numViewR = numberView(right, posR);
if (numViewL.length() != numViewR.length())
return (numViewL.length() - numViewR.length());
// both string/view has the same length
for (int i = 0; i < numViewL.length(); ++i)
{
const QChar numL = numViewL[i];
const QChar numR = numViewR[i];
if (numL != numR)
return (numL.unicode() - numR.unicode());
}
// String + digits do match and we haven't hit the end of both strings
// then continue to consume the remainings
}
else
{
return (leftChar.unicode() - rightChar.unicode());
}
}
}
#endif

88
src/base/utils/compare.h

@ -0,0 +1,88 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#pragma once
#include <Qt>
#include <QtGlobal>
#ifndef Q_OS_WIN
#define QBT_USE_QCOLLATOR
#include <QCollator>
#endif
class QString;
namespace Utils::Compare
{
#ifdef QBT_USE_QCOLLATOR
template <Qt::CaseSensitivity caseSensitivity>
class NaturalCompare
{
public:
NaturalCompare()
{
m_collator.setNumericMode(true);
m_collator.setCaseSensitivity(caseSensitivity);
}
int operator()(const QString &left, const QString &right) const
{
return m_collator.compare(left, right);
}
private:
QCollator m_collator;
};
#else
int naturalCompare(const QString &left, const QString &right, Qt::CaseSensitivity caseSensitivity);
template <Qt::CaseSensitivity caseSensitivity>
class NaturalCompare
{
public:
int operator()(const QString &left, const QString &right) const
{
return naturalCompare(left, right, caseSensitivity);
}
};
#endif
template <Qt::CaseSensitivity caseSensitivity>
class NaturalLessThan
{
public:
bool operator()(const QString &left, const QString &right) const
{
return (m_comparator(left, right) < 0);
}
private:
NaturalCompare<caseSensitivity> m_comparator;
};
}

133
src/base/utils/string.cpp

@ -31,9 +31,7 @@
#include <cmath> #include <cmath>
#include <QCollator>
#include <QLocale> #include <QLocale>
#include <QtGlobal>
#include <QVector> #include <QVector>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@ -42,137 +40,6 @@
#include <QRegExp> #include <QRegExp>
#endif #endif
#if defined(Q_OS_MACOS) || defined(__MINGW32__)
#define QBT_USES_QTHREADSTORAGE
#include <QThreadStorage>
#endif
namespace
{
class NaturalCompare
{
public:
explicit NaturalCompare(const Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
: m_caseSensitivity(caseSensitivity)
{
#ifdef Q_OS_WIN
// Without ICU library, QCollator uses the native API on Windows 7+. But that API
// sorts older versions of μTorrent differently than the newer ones because the
// 'μ' character is encoded differently and the native API can't cope with that.
// So default to using our custom natural sorting algorithm instead.
// See #5238 and #5240
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on an OS older than Win7
#else
m_collator.setNumericMode(true);
m_collator.setCaseSensitivity(caseSensitivity);
#endif
}
int operator()(const QString &left, const QString &right) const
{
#ifdef Q_OS_WIN
return compare(left, right);
#else
return m_collator.compare(left, right);
#endif
}
private:
int compare(const QString &left, const QString &right) const
{
// Return value <0: `left` is smaller than `right`
// Return value >0: `left` is greater than `right`
// Return value =0: both strings are equal
int posL = 0;
int posR = 0;
while (true)
{
if ((posL == left.size()) || (posR == right.size()))
return (left.size() - right.size()); // when a shorter string is another string's prefix, shorter string place before longer string
const QChar leftChar = (m_caseSensitivity == Qt::CaseSensitive) ? left[posL] : left[posL].toLower();
const QChar rightChar = (m_caseSensitivity == Qt::CaseSensitive) ? right[posR] : right[posR].toLower();
// Compare only non-digits.
// Numbers should be compared as a whole
// otherwise the string->int conversion can yield a wrong value
if ((leftChar == rightChar) && !leftChar.isDigit())
{
// compare next character
++posL;
++posR;
}
else if (leftChar.isDigit() && rightChar.isDigit())
{
// Both are digits, compare the numbers
const auto numberView = [](const QString &str, int &pos) -> QStringRef
{
const int start = pos;
while ((pos < str.size()) && str[pos].isDigit())
++pos;
return str.midRef(start, (pos - start));
};
const QStringRef numViewL = numberView(left, posL);
const QStringRef numViewR = numberView(right, posR);
if (numViewL.length() != numViewR.length())
return (numViewL.length() - numViewR.length());
// both string/view has the same length
for (int i = 0; i < numViewL.length(); ++i)
{
const QChar numL = numViewL[i];
const QChar numR = numViewR[i];
if (numL != numR)
return (numL.unicode() - numR.unicode());
}
// String + digits do match and we haven't hit the end of both strings
// then continue to consume the remainings
}
else
{
return (leftChar.unicode() - rightChar.unicode());
}
}
}
QCollator m_collator;
const Qt::CaseSensitivity m_caseSensitivity;
};
}
int Utils::String::naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity)
{
// provide a single `NaturalCompare` instance for easy use
// https://doc.qt.io/qt-5/threads-reentrancy.html
if (caseSensitivity == Qt::CaseSensitive)
{
#ifdef QBT_USES_QTHREADSTORAGE
static QThreadStorage<NaturalCompare> nCmp;
if (!nCmp.hasLocalData())
nCmp.setLocalData(NaturalCompare(Qt::CaseSensitive));
return (nCmp.localData())(left, right);
#else
thread_local NaturalCompare nCmp(Qt::CaseSensitive);
return nCmp(left, right);
#endif
}
#ifdef QBT_USES_QTHREADSTORAGE
static QThreadStorage<NaturalCompare> nCmp;
if (!nCmp.hasLocalData())
nCmp.setLocalData(NaturalCompare(Qt::CaseInsensitive));
return (nCmp.localData())(left, right);
#else
thread_local NaturalCompare nCmp(Qt::CaseInsensitive);
return nCmp(left, right);
#endif
}
// to send numbers instead of strings with suffixes // to send numbers instead of strings with suffixes
QString Utils::String::fromDouble(const double n, const int precision) QString Utils::String::fromDouble(const double n, const int precision)
{ {

11
src/base/utils/string.h

@ -41,15 +41,6 @@ class QStringRef;
namespace Utils::String namespace Utils::String
{ {
QString fromDouble(double n, int precision);
int naturalCompare(const QString &left, const QString &right, const Qt::CaseSensitivity caseSensitivity);
template <Qt::CaseSensitivity caseSensitivity>
bool naturalLessThan(const QString &left, const QString &right)
{
return (naturalCompare(left, right, caseSensitivity) < 0);
}
QString wildcardToRegexPattern(const QString &pattern); QString wildcardToRegexPattern(const QString &pattern);
template <typename T> template <typename T>
@ -72,6 +63,8 @@ namespace Utils::String
QString join(const QVector<QStringRef> &strings, const QString &separator); QString join(const QVector<QStringRef> &strings, const QString &separator);
QString fromDouble(double n, int precision);
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0> template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
QString fromEnum(const T &value) QString fromEnum(const T &value)
{ {

4
src/gui/addnewtorrentdialog.cpp

@ -48,9 +48,9 @@
#include "base/net/downloadmanager.h" #include "base/net/downloadmanager.h"
#include "base/settingsstorage.h" #include "base/settingsstorage.h"
#include "base/torrentfileguard.h" #include "base/torrentfileguard.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "base/utils/string.h"
#include "autoexpandabledialog.h" #include "autoexpandabledialog.h"
#include "properties/proplistdelegate.h" #include "properties/proplistdelegate.h"
#include "raisedmessagebox.h" #include "raisedmessagebox.h"
@ -129,7 +129,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
// Load categories // Load categories
QStringList categories = session->categories().keys(); QStringList categories = session->categories().keys();
std::sort(categories.begin(), categories.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
auto defaultCategory = settings()->loadValue<QString>(KEY_DEFAULTCATEGORY); auto defaultCategory = settings()->loadValue<QString>(KEY_DEFAULTCATEGORY);
if (!m_torrentParams.category.isEmpty()) if (!m_torrentParams.category.isEmpty())

7
src/gui/categoryfilterproxymodel.cpp

@ -28,7 +28,6 @@
#include "categoryfilterproxymodel.h" #include "categoryfilterproxymodel.h"
#include "base/utils/string.h"
#include "categoryfiltermodel.h" #include "categoryfiltermodel.h"
CategoryFilterProxyModel::CategoryFilterProxyModel(QObject *parent) CategoryFilterProxyModel::CategoryFilterProxyModel(QObject *parent)
@ -51,9 +50,5 @@ bool CategoryFilterProxyModel::lessThan(const QModelIndex &left, const QModelInd
// "All" and "Uncategorized" must be left in place // "All" and "Uncategorized" must be left in place
if (CategoryFilterModel::isSpecialItem(left) || CategoryFilterModel::isSpecialItem(right)) if (CategoryFilterModel::isSpecialItem(left) || CategoryFilterModel::isSpecialItem(right))
return (left < right); return (left < right);
return m_naturalLessThan(left.data().toString(), right.data().toString());
int result = Utils::String::naturalCompare(left.data().toString(), right.data().toString()
, Qt::CaseInsensitive);
return (result < 0);
} }

4
src/gui/categoryfilterproxymodel.h

@ -30,6 +30,8 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "base/utils/compare.h"
class QString; class QString;
class CategoryFilterProxyModel final : public QSortFilterProxyModel class CategoryFilterProxyModel final : public QSortFilterProxyModel
@ -47,4 +49,6 @@ protected:
private: private:
// we added another overload of index(), hence this using directive: // we added another overload of index(), hence this using directive:
using QSortFilterProxyModel::index; using QSortFilterProxyModel::index;
Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> m_naturalLessThan;
}; };

6
src/gui/properties/peerlistsortmodel.cpp

@ -28,7 +28,6 @@
#include "peerlistsortmodel.h" #include "peerlistsortmodel.h"
#include "base/utils/string.h"
#include "peerlistwidget.h" #include "peerlistwidget.h"
PeerListSortModel::PeerListSortModel(QObject *parent) PeerListSortModel::PeerListSortModel(QObject *parent)
@ -43,11 +42,10 @@ bool PeerListSortModel::lessThan(const QModelIndex &left, const QModelIndex &rig
{ {
case PeerListWidget::IP: case PeerListWidget::IP:
case PeerListWidget::CLIENT: case PeerListWidget::CLIENT:
{ {
const QString strL = left.data(UnderlyingDataRole).toString(); const QString strL = left.data(UnderlyingDataRole).toString();
const QString strR = right.data(UnderlyingDataRole).toString(); const QString strR = right.data(UnderlyingDataRole).toString();
const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive); return m_naturalLessThan(strL, strR);
return (result < 0);
} }
break; break;
default: default:

4
src/gui/properties/peerlistsortmodel.h

@ -30,6 +30,8 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "base/utils/compare.h"
class PeerListSortModel final : public QSortFilterProxyModel class PeerListSortModel final : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
@ -45,4 +47,6 @@ public:
private: private:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> m_naturalLessThan;
}; };

3
src/gui/rss/automatedrssdownloader.cpp

@ -47,6 +47,7 @@
#include "base/rss/rss_feed.h" #include "base/rss/rss_feed.h"
#include "base/rss/rss_folder.h" #include "base/rss/rss_folder.h"
#include "base/rss/rss_session.h" #include "base/rss/rss_session.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/string.h" #include "base/utils/string.h"
#include "gui/autoexpandabledialog.h" #include "gui/autoexpandabledialog.h"
@ -330,7 +331,7 @@ void AutomatedRssDownloader::initCategoryCombobox()
{ {
// Load torrent categories // Load torrent categories
QStringList categories = BitTorrent::Session::instance()->categories().keys(); QStringList categories = BitTorrent::Session::instance()->categories().keys();
std::sort(categories.begin(), categories.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
m_ui->comboCategory->addItem(""); m_ui->comboCategory->addItem("");
m_ui->comboCategory->addItems(categories); m_ui->comboCategory->addItems(categories);
} }

6
src/gui/search/searchsortmodel.cpp

@ -29,7 +29,6 @@
#include "searchsortmodel.h" #include "searchsortmodel.h"
#include "base/global.h" #include "base/global.h"
#include "base/utils/string.h"
SearchSortModel::SearchSortModel(QObject *parent) SearchSortModel::SearchSortModel(QObject *parent)
: base(parent) : base(parent)
@ -118,11 +117,10 @@ bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right
{ {
case NAME: case NAME:
case ENGINE_URL: case ENGINE_URL:
{ {
const QString strL = left.data().toString(); const QString strL = left.data().toString();
const QString strR = right.data().toString(); const QString strR = right.data().toString();
const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive); return m_naturalLessThan(strL, strR);
return (result < 0);
} }
break; break;
default: default:

4
src/gui/search/searchsortmodel.h

@ -31,6 +31,8 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QStringList> #include <QStringList>
#include "base/utils/compare.h"
class SearchSortModel final : public QSortFilterProxyModel class SearchSortModel final : public QSortFilterProxyModel
{ {
using base = QSortFilterProxyModel; using base = QSortFilterProxyModel;
@ -94,4 +96,6 @@ private:
int m_minSeeds, m_maxSeeds; int m_minSeeds, m_maxSeeds;
int m_minLeeches, m_maxLeeches; int m_minLeeches, m_maxLeeches;
qint64 m_minSize, m_maxSize; qint64 m_minSize, m_maxSize;
Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> m_naturalLessThan;
}; };

6
src/gui/tagfilterproxymodel.cpp

@ -28,7 +28,6 @@
#include "tagfilterproxymodel.h" #include "tagfilterproxymodel.h"
#include "base/utils/string.h"
#include "tagfiltermodel.h" #include "tagfiltermodel.h"
TagFilterProxyModel::TagFilterProxyModel(QObject *parent) TagFilterProxyModel::TagFilterProxyModel(QObject *parent)
@ -51,8 +50,5 @@ bool TagFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &r
// "All" and "Untagged" must be left in place // "All" and "Untagged" must be left in place
if (TagFilterModel::isSpecialItem(left) || TagFilterModel::isSpecialItem(right)) if (TagFilterModel::isSpecialItem(left) || TagFilterModel::isSpecialItem(right))
return (left < right); return (left < right);
return m_naturalLessThan(left.data().toString(), right.data().toString());
int result = Utils::String::naturalCompare(left.data().toString(), right.data().toString()
, Qt::CaseInsensitive);
return (result < 0);
} }

4
src/gui/tagfilterproxymodel.h

@ -30,6 +30,8 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "base/utils/compare.h"
class QString; class QString;
class TagFilterProxyModel final : public QSortFilterProxyModel class TagFilterProxyModel final : public QSortFilterProxyModel
@ -47,4 +49,6 @@ protected:
private: private:
// we added another overload of index(), hence this using directive: // we added another overload of index(), hence this using directive:
using QSortFilterProxyModel::index; using QSortFilterProxyModel::index;
Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> m_naturalLessThan;
}; };

3
src/gui/torrentcontentfiltermodel.cpp

@ -28,7 +28,6 @@
#include "torrentcontentfiltermodel.h" #include "torrentcontentfiltermodel.h"
#include "base/utils/string.h"
#include "torrentcontentmodel.h" #include "torrentcontentmodel.h"
TorrentContentFilterModel::TorrentContentFilterModel(QObject *parent) TorrentContentFilterModel::TorrentContentFilterModel(QObject *parent)
@ -94,7 +93,7 @@ bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIn
{ {
const QString strL = left.data().toString(); const QString strL = left.data().toString();
const QString strR = right.data().toString(); const QString strR = right.data().toString();
return Utils::String::naturalLessThan<Qt::CaseInsensitive>(strL, strR); return m_naturalLessThan(strL, strR);
} }
if ((leftType == TorrentContentModelItem::FolderType) && (sortOrder() == Qt::AscendingOrder)) if ((leftType == TorrentContentModelItem::FolderType) && (sortOrder() == Qt::AscendingOrder))
{ {

2
src/gui/torrentcontentfiltermodel.h

@ -30,6 +30,7 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "base/utils/compare.h"
#include "torrentcontentmodelitem.h" #include "torrentcontentmodelitem.h"
class TorrentContentModel; class TorrentContentModel;
@ -61,4 +62,5 @@ private:
bool hasFiltered(const QModelIndex &folder) const; bool hasFiltered(const QModelIndex &folder) const;
TorrentContentModel *m_model; TorrentContentModel *m_model;
Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> m_naturalLessThan;
}; };

5
src/gui/transferlistfilterswidget.cpp

@ -47,8 +47,8 @@
#include "base/net/downloadmanager.h" #include "base/net/downloadmanager.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/torrentfilter.h" #include "base/torrentfilter.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/string.h"
#include "categoryfilterwidget.h" #include "categoryfilterwidget.h"
#include "tagfilterwidget.h" #include "tagfilterwidget.h"
#include "transferlistwidget.h" #include "transferlistwidget.h"
@ -366,10 +366,11 @@ void TrackerFiltersList::addItem(const QString &tracker, const BitTorrent::Torre
} }
Q_ASSERT(count() >= 4); Q_ASSERT(count() >= 4);
const Utils::Compare::NaturalLessThan<Qt::CaseSensitive> naturalLessThan {};
int insPos = count(); int insPos = count();
for (int i = 4; i < count(); ++i) for (int i = 4; i < count(); ++i)
{ {
if (Utils::String::naturalLessThan<Qt::CaseSensitive>(host, item(i)->text())) if (naturalLessThan(host, item(i)->text()))
{ {
insPos = i; insPos = i;
break; break;

3
src/gui/transferlistsortmodel.cpp

@ -34,7 +34,6 @@
#include "base/bittorrent/infohash.h" #include "base/bittorrent/infohash.h"
#include "base/bittorrent/torrent.h" #include "base/bittorrent/torrent.h"
#include "base/utils/string.h"
#include "transferlistmodel.h" #include "transferlistmodel.h"
namespace namespace
@ -143,7 +142,7 @@ int TransferListSortModel::compare(const QModelIndex &left, const QModelIndex &r
case TransferListModel::TR_SAVE_PATH: case TransferListModel::TR_SAVE_PATH:
case TransferListModel::TR_TAGS: case TransferListModel::TR_TAGS:
case TransferListModel::TR_TRACKER: case TransferListModel::TR_TRACKER:
return Utils::String::naturalCompare(leftValue.toString(), rightValue.toString(), Qt::CaseInsensitive); return m_naturalCompare(leftValue.toString(), rightValue.toString());
case TransferListModel::TR_AMOUNT_DOWNLOADED: case TransferListModel::TR_AMOUNT_DOWNLOADED:
case TransferListModel::TR_AMOUNT_DOWNLOADED_SESSION: case TransferListModel::TR_AMOUNT_DOWNLOADED_SESSION:

3
src/gui/transferlistsortmodel.h

@ -32,6 +32,7 @@
#include "base/settingvalue.h" #include "base/settingvalue.h"
#include "base/torrentfilter.h" #include "base/torrentfilter.h"
#include "base/utils/compare.h"
namespace BitTorrent namespace BitTorrent
{ {
@ -64,4 +65,6 @@ private:
TorrentFilter m_filter; TorrentFilter m_filter;
mutable CachedSettingValue<int> m_subSortColumn; mutable CachedSettingValue<int> m_subSortColumn;
mutable int m_lastSortColumn = -1; mutable int m_lastSortColumn = -1;
Utils::Compare::NaturalCompare<Qt::CaseInsensitive> m_naturalCompare;
}; };

5
src/gui/transferlistwidget.cpp

@ -52,6 +52,7 @@
#include "base/logger.h" #include "base/logger.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/torrentfilter.h" #include "base/torrentfilter.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "base/utils/string.h" #include "base/utils/string.h"
@ -963,7 +964,7 @@ void TransferListWidget::displayListMenu(const QPoint &)
// Category Menu // Category Menu
QStringList categories = BitTorrent::Session::instance()->categories().keys(); QStringList categories = BitTorrent::Session::instance()->categories().keys();
std::sort(categories.begin(), categories.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
QMenu *categoryMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon("view-categories"), tr("Category")); QMenu *categoryMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon("view-categories"), tr("Category"));
@ -988,7 +989,7 @@ void TransferListWidget::displayListMenu(const QPoint &)
// Tag Menu // Tag Menu
QStringList tags(BitTorrent::Session::instance()->tags().values()); QStringList tags(BitTorrent::Session::instance()->tags().values());
std::sort(tags.begin(), tags.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>); std::sort(tags.begin(), tags.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
QMenu *tagsMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon("view-categories"), tr("Tags")); QMenu *tagsMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon("view-categories"), tr("Tags"));

Loading…
Cancel
Save