Browse Source

Replace `naturalSort()` with `naturalCompare()`.

adaptive-webui-19844
Chocobo1 9 years ago
parent
commit
d65ff87148
  1. 79
      src/base/utils/string.cpp
  2. 6
      src/base/utils/string.h
  3. 2
      src/gui/addnewtorrentdialog.cpp
  4. 21
      src/gui/properties/peerlistsortmodel.h
  5. 2
      src/gui/rss/automatedrssdownloader.cpp
  6. 19
      src/gui/search/searchsortmodel.cpp
  7. 26
      src/gui/torrentcontentfiltermodel.cpp
  8. 28
      src/gui/transferlistfilterswidget.cpp
  9. 36
      src/gui/transferlistsortmodel.cpp

79
src/base/utils/string.cpp

@ -44,68 +44,6 @@ std::string Utils::String::toStdString(const QString &str)
return std::string(utf8.constData(), utf8.length()); return std::string(utf8.constData(), utf8.length());
} }
// uses lessThan comparison
bool Utils::String::naturalSort(const QString &left, const QString &right, bool &result)
{
// Return value indicates if functions was successful
// result argument will contain actual comparison result if function was successful
int posL = 0;
int posR = 0;
do {
forever {
if (posL == left.size() || posR == right.size())
return false; // No data
QChar leftChar = left.at(posL);
QChar rightChar = right.at(posR);
bool leftCharIsDigit = leftChar.isDigit();
bool rightCharIsDigit = rightChar.isDigit();
if (leftCharIsDigit != rightCharIsDigit)
return false; // Digit positions mismatch
if (leftCharIsDigit)
break; // Both are digit, break this loop and compare numbers
if (leftChar != rightChar)
return false; // Strings' subsets before digit do not match
++posL;
++posR;
}
QString temp;
while (posL < left.size()) {
if (left.at(posL).isDigit())
temp += left.at(posL);
else
break;
posL++;
}
int numL = temp.toInt();
temp.clear();
while (posR < right.size()) {
if (right.at(posR).isDigit())
temp += right.at(posR);
else
break;
posR++;
}
int numR = temp.toInt();
if (numL != numR) {
result = (numL < numR);
return true;
}
// Strings + digits do match and we haven't hit string end
// Do another round
} while (true);
return false;
}
Utils::String::NaturalCompare::NaturalCompare() Utils::String::NaturalCompare::NaturalCompare()
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
@ -119,23 +57,25 @@ Utils::String::NaturalCompare::NaturalCompare()
#endif #endif
} }
bool Utils::String::NaturalCompare::operator()(const QString &l, const QString &r) bool Utils::String::NaturalCompare::operator()(const QString &left, const QString &right)
{ {
// case-insensitive comparison
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7 // Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7
if(QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7) if(QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7)
return lessThan(l, r); return lessThan(left, right);
#endif #endif
return (m_collator.compare(l, r) < 0); return (m_collator.compare(left, right) < 0);
#else #else
return lessThan(l, r); return lessThan(left, right);
#endif #endif
} }
bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString &right) bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString &right)
{ {
// Return value `false` indicates `right` should go before `left`, otherwise, after // Return value `false` indicates `right` should go before `left`, otherwise, after
// case-insensitive comparison
int posL = 0; int posL = 0;
int posR = 0; int posR = 0;
while (true) { while (true) {
@ -183,6 +123,13 @@ bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString
return false; return false;
} }
bool Utils::String::naturalCompare(const QString &left, const QString &right)
{
// provide a single `NaturalCompare` instance for easy use
static NaturalCompare nCmp; // this is thread-safe in C++11 (stated in spec 6.7.4)
return nCmp(left, right);
}
// to send numbers instead of strings with suffixes // to send numbers instead of strings with suffixes
QString Utils::String::fromDouble(double n, int precision) QString Utils::String::fromDouble(double n, int precision)
{ {

6
src/base/utils/string.h

@ -51,19 +51,19 @@ namespace Utils
// Taken from https://crackstation.net/hashing-security.htm // Taken from https://crackstation.net/hashing-security.htm
bool slowEquals(const QByteArray &a, const QByteArray &b); bool slowEquals(const QByteArray &a, const QByteArray &b);
bool naturalSort(const QString &left, const QString &right, bool &result);
class NaturalCompare class NaturalCompare
{ {
public: public:
NaturalCompare(); NaturalCompare();
bool operator()(const QString &l, const QString &r); bool operator()(const QString &left, const QString &right);
bool lessThan(const QString &left, const QString &right); bool lessThan(const QString &left, const QString &right);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
private: private:
QCollator m_collator; QCollator m_collator;
#endif #endif
}; };
bool naturalCompare(const QString &left, const QString &right);
} }
} }

2
src/gui/addnewtorrentdialog.cpp

@ -96,7 +96,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(QWidget *parent)
// Load categories // Load categories
QStringList categories = session->categories(); QStringList categories = session->categories();
std::sort(categories.begin(), categories.end(), Utils::String::NaturalCompare()); std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare);
QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString(); QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString();
if (!defaultCategory.isEmpty()) if (!defaultCategory.isEmpty())

21
src/gui/properties/peerlistsortmodel.h

@ -43,20 +43,15 @@ public:
protected: protected:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const { bool lessThan(const QModelIndex &left, const QModelIndex &right) const {
if (sortColumn() == PeerListDelegate::IP || sortColumn() == PeerListDelegate::CLIENT) { switch (sortColumn()) {
QVariant vL = sourceModel()->data(left); case PeerListDelegate::IP:
QVariant vR = sourceModel()->data(right); case PeerListDelegate::CLIENT: {
if (!(vL.isValid() && vR.isValid())) QString vL = left.data().toString();
return QSortFilterProxyModel::lessThan(left, right); QString vR = right.data().toString();
Q_ASSERT(vL.isValid()); return Utils::String::naturalCompare(vL, vR);
Q_ASSERT(vR.isValid());
bool res = false;
if (Utils::String::naturalSort(vL.toString(), vR.toString(), res))
return res;
return QSortFilterProxyModel::lessThan(left, right);
} }
};
return QSortFilterProxyModel::lessThan(left, right); return QSortFilterProxyModel::lessThan(left, right);
} }
}; };

2
src/gui/rss/automatedrssdownloader.cpp

@ -314,7 +314,7 @@ void AutomatedRssDownloader::initCategoryCombobox()
{ {
// Load torrent categories // Load torrent categories
QStringList categories = BitTorrent::Session::instance()->categories(); QStringList categories = BitTorrent::Session::instance()->categories();
std::sort(categories.begin(), categories.end(), Utils::String::NaturalCompare()); std::sort(categories.begin(), categories.end(), Utils::String::naturalCompare);
ui->comboCategory->addItems(categories); ui->comboCategory->addItems(categories);
} }

19
src/gui/search/searchsortmodel.cpp

@ -107,20 +107,17 @@ qint64 SearchSortModel::maxSize() const
bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{ {
if ((sortColumn() == NAME) || (sortColumn() == ENGINE_URL)) { switch (sortColumn()) {
QVariant vL = sourceModel()->data(left); case NAME:
QVariant vR = sourceModel()->data(right); case ENGINE_URL: {
if (!(vL.isValid() && vR.isValid())) QString vL = left.data().toString();
return base::lessThan(left, right); QString vR = right.data().toString();
Q_ASSERT(vL.isValid()); return Utils::String::naturalCompare(vL, vR);
Q_ASSERT(vR.isValid());
bool res = false;
if (Utils::String::naturalSort(vL.toString(), vR.toString(), res))
return res;
} }
default:
return base::lessThan(left, right); return base::lessThan(left, right);
};
} }
bool SearchSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool SearchSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const

26
src/gui/torrentcontentfiltermodel.cpp

@ -82,28 +82,22 @@ bool TorrentContentFilterModel::filterAcceptsRow(int source_row, const QModelInd
} }
bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const { bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
if (sortColumn() == NAME) { switch (sortColumn()) {
QVariant vL = sourceModel()->data(left); case NAME: { // PropColumn::NAME
QVariant vR = sourceModel()->data(right); QString vL = left.data().toString();
if (!(vL.isValid() && vR.isValid())) QString vR = right.data().toString();
return QSortFilterProxyModel::lessThan(left, right); TorrentContentModelItem::ItemType leftType = m_model->itemType(m_model->index(left.row(), 0, left.parent()));
Q_ASSERT(vL.isValid()); TorrentContentModelItem::ItemType rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent()));
Q_ASSERT(vR.isValid());
TorrentContentModelItem::ItemType leftType, rightType; if (leftType == rightType)
leftType = m_model->itemType(m_model->index(left.row(), 0, left.parent())); return Utils::String::naturalCompare(vL, vR);
rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent()));
if (leftType == rightType) {
bool res = false;
if (Utils::String::naturalSort(vL.toString(), vR.toString(), res))
return res;
return QSortFilterProxyModel::lessThan(left, right);
}
else if (leftType == TorrentContentModelItem::FolderType && sortOrder() == Qt::AscendingOrder) else if (leftType == TorrentContentModelItem::FolderType && sortOrder() == Qt::AscendingOrder)
return true; return true;
else else
return false; return false;
} }
};
return QSortFilterProxyModel::lessThan(left, right); return QSortFilterProxyModel::lessThan(left, right);
} }

28
src/gui/transferlistfilterswidget.cpp

@ -237,17 +237,14 @@ void CategoryFiltersList::addItem(const QString &category, bool hasTorrent)
if (exists) return; if (exists) return;
Q_ASSERT(count() >= 2); Q_ASSERT(count() >= 2);
int insPos = count();
for (int i = 2; i < count(); ++i) { for (int i = 2; i < count(); ++i) {
bool less = false; if (Utils::String::naturalCompare(category, item(i)->text())) {
if (!(Utils::String::naturalSort(category, item(i)->text(), less))) insPos = i;
less = (category.localeAwareCompare(item(i)->text()) < 0); break;
if (less) {
insertItem(i, categoryItem);
updateGeometry();
return;
} }
} }
QListWidget::addItem(categoryItem); QListWidget::insertItem(insPos, categoryItem);
updateGeometry(); updateGeometry();
} }
@ -512,17 +509,14 @@ void TrackerFiltersList::addItem(const QString &tracker, const QString &hash)
} }
Q_ASSERT(count() >= 4); Q_ASSERT(count() >= 4);
for (int i = 4; i<count(); ++i) { int insPos = count();
bool less = false; for (int i = 4; i < count(); ++i) {
if (!(Utils::String::naturalSort(host, item(i)->text(), less))) if (Utils::String::naturalCompare(host, item(i)->text())) {
less = (host.localeAwareCompare(item(i)->text()) < 0); insPos = i;
if (less) { break;
insertItem(i, trackerItem);
updateGeometry();
return;
} }
} }
QListWidget::addItem(trackerItem); QListWidget::insertItem(insPos, trackerItem);
updateGeometry(); updateGeometry();
} }

36
src/gui/transferlistsortmodel.cpp

@ -73,21 +73,19 @@ void TransferListSortModel::disableTrackerFilter()
bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{ {
const int column = sortColumn(); switch (sortColumn()) {
case TorrentModel::TR_NAME: {
if (column == TorrentModel::TR_NAME) {
QVariant vL = left.data(); QVariant vL = left.data();
QVariant vR = right.data(); QVariant vR = right.data();
if (!vL.isValid() || !vR.isValid() || (vL == vR)) if (!vL.isValid() || !vR.isValid() || (vL == vR))
return lowerPositionThan(left, right); return lowerPositionThan(left, right);
bool res = false; return Utils::String::naturalCompare(vL.toString(), vR.toString());
if (Utils::String::naturalSort(vL.toString(), vR.toString(), res))
return res;
return QSortFilterProxyModel::lessThan(left, right);
} }
else if (column == TorrentModel::TR_ADD_DATE || column == TorrentModel::TR_SEED_DATE || column == TorrentModel::TR_SEEN_COMPLETE_DATE) {
case TorrentModel::TR_ADD_DATE:
case TorrentModel::TR_SEED_DATE:
case TorrentModel::TR_SEEN_COMPLETE_DATE: {
QDateTime vL = left.data().toDateTime(); QDateTime vL = left.data().toDateTime();
QDateTime vR = right.data().toDateTime(); QDateTime vR = right.data().toDateTime();
@ -97,10 +95,13 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return vL < vR; return vL < vR;
} }
else if (column == TorrentModel::TR_PRIORITY) {
case TorrentModel::TR_PRIORITY: {
return lowerPositionThan(left, right); return lowerPositionThan(left, right);
} }
else if (column == TorrentModel::TR_PEERS || column == TorrentModel::TR_SEEDS) {
case TorrentModel::TR_SEEDS:
case TorrentModel::TR_PEERS: {
int left_active = left.data().toInt(); int left_active = left.data().toInt();
int left_total = left.data(Qt::UserRole).toInt(); int left_total = left.data(Qt::UserRole).toInt();
int right_active = right.data().toInt(); int right_active = right.data().toInt();
@ -116,7 +117,8 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return (left_active < right_active); return (left_active < right_active);
} }
} }
else if (column == TorrentModel::TR_ETA) {
case TorrentModel::TR_ETA: {
TorrentModel *model = qobject_cast<TorrentModel *>(sourceModel()); TorrentModel *model = qobject_cast<TorrentModel *>(sourceModel());
const int prioL = model->data(model->index(left.row(), TorrentModel::TR_PRIORITY)).toInt(); const int prioL = model->data(model->index(left.row(), TorrentModel::TR_PRIORITY)).toInt();
const int prioR = model->data(model->index(right.row(), TorrentModel::TR_PRIORITY)).toInt(); const int prioR = model->data(model->index(right.row(), TorrentModel::TR_PRIORITY)).toInt();
@ -164,7 +166,8 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return !invalidL; return !invalidL;
} }
} }
else if (column == TorrentModel::TR_LAST_ACTIVITY) {
case TorrentModel::TR_LAST_ACTIVITY: {
const qlonglong vL = left.data().toLongLong(); const qlonglong vL = left.data().toLongLong();
const qlonglong vR = right.data().toLongLong(); const qlonglong vR = right.data().toLongLong();
@ -173,7 +176,8 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return vL < vR; return vL < vR;
} }
else if (column == TorrentModel::TR_RATIO_LIMIT) {
case TorrentModel::TR_RATIO_LIMIT: {
const qreal vL = left.data().toDouble(); const qreal vL = left.data().toDouble();
const qreal vR = right.data().toDouble(); const qreal vR = right.data().toDouble();
@ -183,10 +187,12 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
return vL < vR; return vL < vR;
} }
default: {
if (left.data() == right.data()) if (left.data() == right.data())
return lowerPositionThan(left, right); return lowerPositionThan(left, right);
return QSortFilterProxyModel::lessThan(left, right); return QSortFilterProxyModel::lessThan(left, right);
}
};
} }
bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const

Loading…
Cancel
Save