mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-20 05:13:30 -07:00
Merge pull request #3230 from Chocobo1/rss_sort_v320
Sort labels in RSS Downloader dialog
This commit is contained in:
commit
81912736ed
3 changed files with 99 additions and 6 deletions
|
@ -598,7 +598,7 @@ QString misc::toQString(time_t t, Qt::DateFormat f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef DISABLE_GUI
|
#ifndef DISABLE_GUI
|
||||||
bool misc::naturalSort(QString left, QString right, bool &result) // uses lessThan comparison
|
bool misc::naturalSort(const QString &left, const QString &right, bool &result) // uses lessThan comparison
|
||||||
{ // Return value indicates if functions was successful
|
{ // Return value indicates if functions was successful
|
||||||
// result argument will contain actual comparison result if function was successful
|
// result argument will contain actual comparison result if function was successful
|
||||||
int posL = 0;
|
int posL = 0;
|
||||||
|
@ -657,6 +657,83 @@ bool misc::naturalSort(QString left, QString right, bool &result) // uses less
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
misc::NaturalCompare::NaturalCompare()
|
||||||
|
{
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
|
||||||
|
#if defined(Q_OS_WIN)
|
||||||
|
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7
|
||||||
|
if(SysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7)
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
m_collator.setNumericMode(true);
|
||||||
|
m_collator.setCaseSensitivity(Qt::CaseInsensitive);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool misc::NaturalCompare::operator()(const QString &l, const QString &r)
|
||||||
|
{
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
|
||||||
|
#if defined(Q_OS_WIN)
|
||||||
|
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7
|
||||||
|
if(SysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7)
|
||||||
|
return lessThan(l, r);
|
||||||
|
#endif
|
||||||
|
return (m_collator.compare(l, r) < 0);
|
||||||
|
#else
|
||||||
|
return lessThan(l, r);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool misc::NaturalCompare::lessThan(const QString &left, const QString &right)
|
||||||
|
{
|
||||||
|
// Return value `false` indicates `right` should go before `left`, otherwise, after
|
||||||
|
int posL = 0;
|
||||||
|
int posR = 0;
|
||||||
|
while (true) {
|
||||||
|
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
|
||||||
|
|
||||||
|
QChar leftChar = left[posL].toLower();
|
||||||
|
QChar rightChar = right[posR].toLower();
|
||||||
|
if (leftChar == rightChar)
|
||||||
|
; // compare next character
|
||||||
|
else if (leftChar.isDigit() && rightChar.isDigit())
|
||||||
|
break; // Both are digits, break this loop and compare numbers
|
||||||
|
else
|
||||||
|
return leftChar < rightChar;
|
||||||
|
|
||||||
|
++posL;
|
||||||
|
++posR;
|
||||||
|
}
|
||||||
|
|
||||||
|
int startL = posL;
|
||||||
|
while ((posL < left.size()) && left[posL].isDigit())
|
||||||
|
++posL;
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
|
||||||
|
int numL = left.midRef(startL, posL - startL).toInt();
|
||||||
|
#else
|
||||||
|
int numL = left.mid(startL, posL - startL).toInt();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int startR = posR;
|
||||||
|
while ((posR < right.size()) && right[posR].isDigit())
|
||||||
|
++posR;
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
|
||||||
|
int numR = right.midRef(startR, posR - startR).toInt();
|
||||||
|
#else
|
||||||
|
int numR = right.mid(startR, posR - startR).toInt();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (numL != numR)
|
||||||
|
return (numL < numR);
|
||||||
|
|
||||||
|
// Strings + digits do match and we haven't hit string end
|
||||||
|
// Do another round
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// to send numbers instead of strings with suffixes
|
// to send numbers instead of strings with suffixes
|
||||||
|
|
|
@ -41,6 +41,9 @@
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#ifndef DISABLE_GUI
|
#ifndef DISABLE_GUI
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
|
||||||
|
#include <QCollator>
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <libtorrent/version.hpp>
|
#include <libtorrent/version.hpp>
|
||||||
|
@ -106,7 +109,19 @@ namespace misc
|
||||||
QString accurateDoubleToString(const double &n, const int &precision);
|
QString accurateDoubleToString(const double &n, const int &precision);
|
||||||
|
|
||||||
#ifndef DISABLE_GUI
|
#ifndef DISABLE_GUI
|
||||||
bool naturalSort(QString left, QString right, bool& result);
|
bool naturalSort(const QString &left, const QString &right, bool &result);
|
||||||
|
|
||||||
|
class NaturalCompare
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NaturalCompare();
|
||||||
|
bool operator()(const QString &l, const QString &r);
|
||||||
|
bool lessThan(const QString &left, const QString &right);
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
|
||||||
|
private:
|
||||||
|
QCollator m_collator;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Implements constant-time comparison to protect against timing attacks
|
// Implements constant-time comparison to protect against timing attacks
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
#include "iconprovider.h"
|
#include "iconprovider.h"
|
||||||
#include "autoexpandabledialog.h"
|
#include "autoexpandabledialog.h"
|
||||||
#include "fs_utils.h"
|
#include "fs_utils.h"
|
||||||
|
#include "misc.h"
|
||||||
|
|
||||||
AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& manager, QWidget *parent) :
|
AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& manager, QWidget *parent) :
|
||||||
QDialog(parent),
|
QDialog(parent),
|
||||||
|
@ -310,10 +311,10 @@ RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const
|
||||||
void AutomatedRssDownloader::initLabelCombobox()
|
void AutomatedRssDownloader::initLabelCombobox()
|
||||||
{
|
{
|
||||||
// Load custom labels
|
// Load custom labels
|
||||||
const QStringList customLabels = Preferences::instance()->getTorrentLabels();
|
QStringList customLabels = Preferences::instance()->getTorrentLabels();
|
||||||
foreach (const QString& label, customLabels) {
|
std::sort(customLabels.begin(), customLabels.end(), misc::NaturalCompare());
|
||||||
ui->comboLabel->addItem(label);
|
foreach (const QString& l, customLabels)
|
||||||
}
|
ui->comboLabel->addItem(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AutomatedRssDownloader::saveEditedRule()
|
void AutomatedRssDownloader::saveEditedRule()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue