From 643a7a8d19458ff3577b83baa658001c528472cd Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Sun, 22 Jul 2007 19:28:47 +0000 Subject: [PATCH] Use KDirSortFilterProxyModel::naturalCompare() to prevent code duplication. DolphinSortFilterProxyModel::lessThan() still contains similar code like KDirSortFilterProxyModel::lessThan(), but we will discuss this with Rafael in August (it's open yet whether KSortFilterProxyModel should be derived from KDirSortFilterProxyModel or not...). CCMAIL: ereslibre@gmail.com svn path=/trunk/KDE/kdebase/apps/; revision=691028 --- src/dolphinsortfilterproxymodel.cpp | 112 ++-------------------------- src/dolphinsortfilterproxymodel.h | 13 +++- 2 files changed, 18 insertions(+), 107 deletions(-) diff --git a/src/dolphinsortfilterproxymodel.cpp b/src/dolphinsortfilterproxymodel.cpp index 592b0aa1c..5ae0f012a 100644 --- a/src/dolphinsortfilterproxymodel.cpp +++ b/src/dolphinsortfilterproxymodel.cpp @@ -196,6 +196,12 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const { + // TODO: There is some code duplication of this method inside + // kdelibs/kfile/kdirsortfilterproxymodel.cpp. Possible solution: + // - get rid of KSortFilterProxyModel and derive from KDirSortFilterProxyModel + // - adapt DolphinSortFilterProxyModel::lessThan() to use + // KSortFilterProxyModel::lessThan() if possible + KDirModel* dirModel = static_cast(sourceModel()); const KFileItem* leftFileItem = dirModel->itemForIndex(left); @@ -426,8 +432,9 @@ QString DolphinSortFilterProxyModel::tagsForIndex(const QModelIndex& index) tagsString += ", "; } - if (!tagsString.isEmpty()) + if (!tagsString.isEmpty()) { tagsString.resize(tagsString.size() - 2); + } } return tagsString; @@ -436,107 +443,4 @@ QString DolphinSortFilterProxyModel::tagsForIndex(const QModelIndex& index) #endif } -int DolphinSortFilterProxyModel::naturalCompare(const QString& a, - const QString& b) -{ - // This method chops the input a and b into pieces of - // digits and non-digits (a1.05 becomes a | 1 | . | 05) - // and compares these pieces of a and b to each other - // (first with first, second with second, ...). - // - // This is based on the natural sort order code code by Martin Pool - // http://sourcefrog.net/projects/natsort/ - // Martin Pool agreed to license this under LGPL or GPL. - - const QChar* currA = a.unicode(); // iterator over a - const QChar* currB = b.unicode(); // iterator over b - - if (currA == currB) { - return 0; - } - - const QChar* begSeqA = currA; // beginning of a new character sequence of a - const QChar* begSeqB = currB; - - while (!currA->isNull() && !currB->isNull()) { - // find sequence of characters ending at the first non-character - while (!currA->isNull() && !currA->isDigit()) { - ++currA; - } - - while (!currB->isNull() && !currB->isDigit()) { - ++currB; - } - - // compare these sequences - const QString subA(begSeqA, currA - begSeqA); - const QString subB(begSeqB, currB - begSeqB); - const int cmp = QString::localeAwareCompare(subA, subB); - if (cmp != 0) { - return cmp; - } - - if (currA->isNull() || currB->isNull()) { - break; - } - - // now some digits follow... - if ((*currA == '0') || (*currB == '0')) { - // one digit-sequence starts with 0 -> assume we are in a fraction part - // do left aligned comparison (numbers are considered left aligned) - while (1) { - if (!currA->isDigit() && !currB->isDigit()) { - break; - } else if (!currA->isDigit()) { - return -1; - } else if (!currB->isDigit()) { - return + 1; - } else if (*currA < *currB) { - return -1; - } else if (*currA > *currB) { - return + 1; - } - ++currA; - ++currB; - } - } else { - // No digit-sequence starts with 0 -> assume we are looking at some integer - // do right aligned comparison. - // - // The longest run of digits wins. That aside, the greatest - // value wins, but we can't know that it will until we've scanned - // both numbers to know that they have the same magnitude. - - int weight = 0; - while (1) { - if (!currA->isDigit() && !currB->isDigit()) { - if (weight != 0) { - return weight; - } - break; - } else if (!currA->isDigit()) { - return -1; - } else if (!currB->isDigit()) { - return + 1; - } else if ((*currA < *currB) && (weight == 0)) { - weight = -1; - } else if ((*currA > *currB) && (weight == 0)) { - weight = + 1; - } - ++currA; - ++currB; - } - } - - begSeqA = currA; - begSeqB = currB; - } - - if (currA->isNull() && currB->isNull()) { - return 0; - } - - return currA->isNull() ? -1 : + 1; -} - #include "dolphinsortfilterproxymodel.moc" diff --git a/src/dolphinsortfilterproxymodel.h b/src/dolphinsortfilterproxymodel.h index 56ee77dba..537c6aa1c 100644 --- a/src/dolphinsortfilterproxymodel.h +++ b/src/dolphinsortfilterproxymodel.h @@ -20,8 +20,9 @@ #ifndef DOLPHINSORTFILTERPROXYMODEL_H #define DOLPHINSORTFILTERPROXYMODEL_H -#include #include +#include +#include #include /** @@ -75,6 +76,7 @@ public: */ static DolphinView::Sorting sortingForColumn(int column); + /** @see KSortFilterProxyModel::lessThanGeneralPurpose() */ virtual bool lessThanGeneralPurpose(const QModelIndex &left, const QModelIndex &right) const; @@ -83,6 +85,8 @@ protected: const QModelIndex& right) const; private: + inline int naturalCompare(const QString& a, const QString& b) const; + /** * Returns the rating for the item with the index \a index. 0 is * returned if no item could be found. @@ -95,8 +99,6 @@ private: */ static QString tagsForIndex(const QModelIndex& index); - static int naturalCompare(const QString& a, const QString& b); - private: DolphinView::Sorting m_sorting; Qt::SortOrder m_sortOrder; @@ -114,4 +116,9 @@ Qt::SortOrder DolphinSortFilterProxyModel::sortOrder() const return m_sortOrder; } +int DolphinSortFilterProxyModel::naturalCompare(const QString& a, const QString& b) const +{ + return KDirSortFilterProxyModel::naturalCompare(a, b); +} + #endif -- 2.47.3