NOTE to dolphin devs: please, consider using 'left.column()' instead of 'sortRole()' in lessThan methods, and 'sorting()' instead of 'sortRole()' in other places.
Didn't make switch by myself because it needs nepomk testing (shouldn't be too hard though).
svn path=/trunk/KDE/kdebase/apps/; revision=692517
dolphincolumnview.cpp
dolphinitemcategorizer.cpp
kcategorizedview.cpp
- ksortfilterproxymodel.cpp
kitemcategorizer.cpp
dolphinsettings.cpp
viewproperties.cpp
};
DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
- KSortFilterProxyModel(parent),
+ KDirSortFilterProxyModel(parent),
m_sorting(DolphinView::SortByName),
m_sortOrder(Qt::AscendingOrder)
{
- setDynamicSortFilter(true);
-
- // sort by the user visible string for now
- setSortRole(DolphinView::SortByName);
- setSortCaseSensitivity(Qt::CaseInsensitive);
- sort(KDirModel::Name, Qt::AscendingOrder);
}
DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
m_sorting = sortingForColumn(column);
m_sortOrder = sortOrder;
setSortRole(m_sorting);
- KSortFilterProxyModel::sort(column, sortOrder);
-}
-
-bool DolphinSortFilterProxyModel::hasChildren(const QModelIndex& parent) const
-{
- const QModelIndex sourceParent = mapToSource(parent);
- return sourceModel()->hasChildren(sourceParent);
-}
-
-bool DolphinSortFilterProxyModel::canFetchMore(const QModelIndex& parent) const
-{
- const QModelIndex sourceParent = mapToSource(parent);
- return sourceModel()->canFetchMore(sourceParent);
+ emit sortingRoleChanged();
+ KDirSortFilterProxyModel::sort(column, sortOrder);
}
DolphinView::Sorting DolphinSortFilterProxyModel::sortingForColumn(int column)
const KFileItem* leftFileItem = dirModel->itemForIndex(left);
const KFileItem* rightFileItem = dirModel->itemForIndex(right);
+ //FIXME left.column() should be used instead!
switch (sortRole()) {
case DolphinView::SortByName: {
QString leftFileName(leftFileItem->name());
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
-
+#ifdef HAVE_NEPOMUK
KDirModel* dirModel = static_cast<KDirModel*>(sourceModel());
const KFileItem* leftFileItem = dirModel->itemForIndex(left);
const KFileItem* rightFileItem = dirModel->itemForIndex(right);
- // If we are sorting by rating, folders and files are citizens of the same
- // class. Same if we are sorting by tags.
-#ifdef HAVE_NEPOMUK
- if ((sortRole() != DolphinView::SortByRating) &&
- (sortRole() != DolphinView::SortByTags))
- {
-#endif
- // On our priority, folders go above regular files.
- if (leftFileItem->isDir() && !rightFileItem->isDir()) {
- return true;
- } else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
- return false;
- }
-#ifdef HAVE_NEPOMUK
- }
-#endif
// Hidden elements go before visible ones, if they both are
// folders or files.
return false;
}
+ //FIXME left.column() should be used instead!
switch (sortRole()) {
- case DolphinView::SortByName: {
- // So we are in the same priority, what counts now is their names.
- const QVariant leftData = dirModel->data(left, KDirModel::Name);
- const QVariant rightData = dirModel->data(right, KDirModel::Name);
- const QString leftValueString(leftData.toString());
- const QString rightValueString(rightData.toString());
-
- return sortCaseSensitivity() ?
- (naturalCompare(leftValueString, rightValueString) < 0) :
- (naturalCompare(leftValueString.toLower(), rightValueString.toLower()) < 0);
- }
-
- case DolphinView::SortBySize: {
- // If we have two folders, what we have to measure is the number of
- // items that contains each other
- if (leftFileItem->isDir() && rightFileItem->isDir()) {
- QVariant leftValue = dirModel->data(left, KDirModel::ChildCountRole);
- int leftCount = leftValue.type() == QVariant::Int ? leftValue.toInt() : KDirModel::ChildCountUnknown;
-
- QVariant rightValue = dirModel->data(right, KDirModel::ChildCountRole);
- int rightCount = rightValue.type() == QVariant::Int ? rightValue.toInt() : KDirModel::ChildCountUnknown;
-
- // In the case they two have the same child items, we sort them by
- // their names. So we have always everything ordered. We also check
- // if we are taking in count their cases.
- if (leftCount == rightCount) {
- return sortCaseSensitivity() ? (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- // If they had different number of items, we sort them depending
- // on how many items had each other.
- return leftCount < rightCount;
- }
-
- // If what we are measuring is two files and they have the same size,
- // sort them by their file names.
- if (leftFileItem->size() == rightFileItem->size()) {
- return sortCaseSensitivity() ? (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- // If their sizes are different, sort them by their sizes, as expected.
- return leftFileItem->size() < rightFileItem->size();
- }
-
- case DolphinView::SortByDate: {
- KDateTime leftTime, rightTime;
- leftTime.setTime_t(leftFileItem->time(KFileItem::ModificationTime));
- rightTime.setTime_t(rightFileItem->time(KFileItem::ModificationTime));
-
- if (leftTime == rightTime) {
- return sortCaseSensitivity() ?
- (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- return leftTime > rightTime;
- }
-
- case DolphinView::SortByPermissions: {
- if (leftFileItem->permissionsString() == rightFileItem->permissionsString()) {
- return sortCaseSensitivity() ?
- (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- return naturalCompare(leftFileItem->permissionsString(),
- rightFileItem->permissionsString()) < 0;
- }
-
- case DolphinView::SortByOwner: {
- if (leftFileItem->user() == rightFileItem->user()) {
- return sortCaseSensitivity() ?
- (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- return naturalCompare(leftFileItem->user(), rightFileItem->user()) < 0;
- }
-
- case DolphinView::SortByGroup: {
- if (leftFileItem->group() == rightFileItem->group()) {
- return sortCaseSensitivity() ? (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- return naturalCompare(leftFileItem->group(),
- rightFileItem->group()) < 0;
- }
-
- case DolphinView::SortByType: {
- if (leftFileItem->mimetype() == rightFileItem->mimetype()) {
- return sortCaseSensitivity() ?
- (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
- (naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
- }
-
- return naturalCompare(leftFileItem->mimeComment(),
- rightFileItem->mimeComment()) < 0;
- }
-
-#ifdef HAVE_NEPOMUK
case DolphinView::SortByRating: {
const quint32 leftRating = ratingForIndex(left);
const quint32 rightRating = ratingForIndex(right);
return naturalCompare(leftTags, rightTags) < 0;
}
-#endif
}
+#endif
- // We have set a SortRole and trust the ProxyModel to do
- // the right thing for now.
- return QSortFilterProxyModel::lessThan(left, right);
+ return KDirSortFilterProxyModel::lessThan(left, right);
}
quint32 DolphinSortFilterProxyModel::ratingForIndex(const QModelIndex& index)
return tagsString;
#else
+ Q_UNUSED(index);
return QString();
#endif
}
#include <dolphinview.h>
#include <kdirsortfilterproxymodel.h>
-#include <ksortfilterproxymodel.h>
#include <libdolphin_export.h>
/**
*
* It is assured that directories are always sorted before files.
*/
-class LIBDOLPHINPRIVATE_EXPORT DolphinSortFilterProxyModel : public KSortFilterProxyModel
+class LIBDOLPHINPRIVATE_EXPORT DolphinSortFilterProxyModel : public KDirSortFilterProxyModel
{
Q_OBJECT
virtual void sort(int column,
Qt::SortOrder order = Qt::AscendingOrder);
- /** Reimplemented from QAbstractItemModel. Returns true for directories. */
- virtual bool hasChildren(const QModelIndex& parent = QModelIndex()) const;
-
- /** Reimplemented from QAbstractItemModel. Returns true for empty directories. */
- virtual bool canFetchMore(const QModelIndex& parent) const;
-
/**
* Helper method to get the DolphinView::Sorting type for a given
* column \a column. If the column is smaller 0 or greater than the
*/
static DolphinView::Sorting sortingForColumn(int column);
- /** @see KSortFilterProxyModel::lessThanGeneralPurpose() */
+ /**
+ * This method is essential on the categorized view.
+ * It will does a "basic" sorting, just for finding out categories,
+ * and their order. Then over those elements DISORDERED on categories,
+ * the lessThan method will be applied for each category.
+ *
+ * The easy explanation is that not always folders go first. That will depend.
+ * Imagine we sort by Rating. Categories will be created by 10 stars,
+ * 9 stars, 8 stars... but a category with only a file with rating 10
+ * will go before a category with a folder with rating 8.
+ * That's the main reason, and that's lessThanGeneralPurpose() method.
+ * That will go category by category creating sets of elements...
+ */
virtual bool lessThanGeneralPurpose(const QModelIndex &left,
const QModelIndex &right) const;
+ /**
+ * Then for each set of elements lessThanCategoryPurpose() will be applied,
+ * because for each category we wan't first folders and bla bla bla...
+ * That's the main reason of that method existence.
+ *
+ * For that reason, is not that clear that we want ALWAYS folders first.
+ * On each category, yes, that's true. But that's not true always,
+ * as I have pointed out on the example before.
+ */
+ bool lessThanCategoryPurpose(const QModelIndex &left,
+ const QModelIndex &right) const
+ {
+ //when we sort inside 1 category its the usual lessThan()
+ //from KDirSortFilterProxyModel(+nepomuk)
+ return lessThan(left,right);
+ }
+
+signals:
+ void sortingRoleChanged();
+
protected:
virtual bool lessThan(const QModelIndex& left,
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.
static QString tagsForIndex(const QModelIndex& index);
private:
- DolphinView::Sorting m_sorting;
- Qt::SortOrder m_sortOrder;
+ DolphinView::Sorting m_sorting:16;
+ Qt::SortOrder m_sortOrder:16;
friend class DolphinItemCategorizer;
};
+inline
DolphinView::Sorting DolphinSortFilterProxyModel::sorting() const
{
return m_sorting;
}
+inline
Qt::SortOrder DolphinSortFilterProxyModel::sortOrder() const
{
return m_sortOrder;
}
-int DolphinSortFilterProxyModel::naturalCompare(const QString& a, const QString& b) const
-{
- return KDirSortFilterProxyModel::naturalCompare(a, b);
-}
-
#endif
#include <kstyle.h>
#include "kitemcategorizer.h"
-#include "ksortfilterproxymodel.h"
+#include "dolphinsortfilterproxymodel.h"
class LessThan
{
CategoryPurpose
};
- inline LessThan(const KSortFilterProxyModel *proxyModel,
+ inline LessThan(const DolphinSortFilterProxyModel *proxyModel,
Purpose purpose)
: proxyModel(proxyModel)
, purpose(purpose)
}
private:
- const KSortFilterProxyModel *proxyModel;
+ const DolphinSortFilterProxyModel *proxyModel;
const Purpose purpose;
};
QListView::setModel(model);
- d->proxyModel = dynamic_cast<KSortFilterProxyModel*>(model);
+ d->proxyModel = dynamic_cast<DolphinSortFilterProxyModel*>(model);
if (d->proxyModel)
{
#ifndef KCATEGORIZEDVIEW_P_H
#define KCATEGORIZEDVIEW_P_H
-class KSortFilterProxyModel;
+class DolphinSortFilterProxyModel;
/**
* @internal
QRect lastSelectionRect;
// Attributes for speed reasons
- KSortFilterProxyModel *proxyModel;
+ DolphinSortFilterProxyModel *proxyModel;
QModelIndexList sourceModelIndexList; // in source model
};
+++ /dev/null
-/**
- * This file is part of the KDE project
- * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library 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
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include "ksortfilterproxymodel.h"
-
-KSortFilterProxyModel::KSortFilterProxyModel(QObject *parent)
- : QSortFilterProxyModel(parent)
-{
-}
-
-KSortFilterProxyModel::~KSortFilterProxyModel()
-{
-}
-
-void KSortFilterProxyModel::sort(int column, Qt::SortOrder order)
-{
- QSortFilterProxyModel::sort(column, order);
-
- m_sortOrder = order;
-
- emit sortingRoleChanged();
-}
-
-Qt::SortOrder KSortFilterProxyModel::sortOrder() const
-{
- return m_sortOrder;
-}
-
-bool KSortFilterProxyModel::lessThanCategoryPurpose(const QModelIndex &left,
- const QModelIndex &right) const
-{
- return lessThan(left, right);
-}
-
-#include "ksortfilterproxymodel.moc"
+++ /dev/null
-/**
- * This file is part of the KDE project
- * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library 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
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#ifndef KSORTFILTERPROXYMODEL_H
-#define KSORTFILTERPROXYMODEL_H
-
-#include <QtGui/QSortFilterProxyModel>
-
-#include <libdolphin_export.h>
-
-/**
- * @internal
- *
- * This class is meant to be used with KListView class
- *
- * @see KListView
- */
-class LIBDOLPHINPRIVATE_EXPORT KSortFilterProxyModel
- : public QSortFilterProxyModel
-{
- Q_OBJECT
-
-public:
- KSortFilterProxyModel(QObject *parent = 0);
- ~KSortFilterProxyModel();
-
- virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
-
- Qt::SortOrder sortOrder() const;
-
- virtual bool lessThanGeneralPurpose(const QModelIndex &left,
- const QModelIndex &right) const = 0;
-
- virtual bool lessThanCategoryPurpose(const QModelIndex &left,
- const QModelIndex &right) const;
-
-Q_SIGNALS:
- void sortingRoleChanged();
-
-private:
- Qt::SortOrder m_sortOrder;
-};
-
-#endif // KSORTFILTERPROXYMODEL_H