Commit d40be8ab authored by Peter Penz's avatar Peter Penz
Browse files

Use KDirSortFilterProxyModel::naturalCompare() to prevent code duplication....

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
parent 9b8f05ff
......@@ -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<KDirModel*>(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"
......@@ -20,8 +20,9 @@
#ifndef DOLPHINSORTFILTERPROXYMODEL_H
#define DOLPHINSORTFILTERPROXYMODEL_H
#include <ksortfilterproxymodel.h>
#include <dolphinview.h>
#include <kdirsortfilterproxymodel.h>
#include <ksortfilterproxymodel.h>
#include <libdolphin_export.h>
/**
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment