Commit 80a0d665 authored by Peter Penz's avatar Peter Penz
Browse files

Implement sorting by "tags". Same performance problems occur as when using...

Implement sorting by "tags". Same performance problems occur as when using sorting by "rating", but we must get a feeling first how we use the Nepomuk API before thinking about caching...

svn path=/trunk/KDE/kdebase/apps/; revision=678957
parent 73f074ac
......@@ -21,6 +21,7 @@
#include "dolphinitemcategorizer.h"
#include "dolphinview.h"
#include "dolphinsortfilterproxymodel.h"
#ifdef HAVE_NEPOMUK
#include <config-nepomuk.h>
......@@ -34,7 +35,8 @@
#include <klocale.h>
#include <kurl.h>
#include <QtGui/QSortFilterProxyModel>
#include <QList>
#include <QSortFilterProxyModel>
DolphinItemCategorizer::DolphinItemCategorizer() :
KItemCategorizer()
......@@ -164,19 +166,19 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
#ifdef HAVE_NEPOMUK
case DolphinView::SortByRating: {
KFileItem* item = dirModel->itemForIndex(index);
if (item != 0) {
const Nepomuk::Resource resource(item->url().url(), Nepomuk::NFO::File());
const quint32 rating = resource.rating();
if (!rating)
retString = i18n("Not yet rated");
else
retString = i18np("1 star", "%1 stars", rating);
const quint32 rating = DolphinSortFilterProxyModel::ratingForIndex(index);
if (rating) {
retString = i18np("1 star", "%1 stars", rating);
} else {
retString = i18n("Not yet rated");
}
break;
}
case DolphinView::SortByTags:
case DolphinView::SortByTags: {
retString = DolphinSortFilterProxyModel::tagsForIndex(index);
break;
}
#endif
}
......
......@@ -26,11 +26,13 @@
#include <config-nepomuk.h>
#include <nepomuk/global.h>
#include <nepomuk/resource.h>
#include <nepomuk/tag.h>
#endif
#include <kdirmodel.h>
#include <kfileitem.h>
#include <kdatetime.h>
#include <klocale.h>
static DolphinView::Sorting sortingTypeTable[] =
{
......@@ -173,6 +175,9 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left
const quint32 rightRating = ratingForIndex(right);
return leftRating > rightRating;
}
case DolphinView::SortByTags: {
return naturalCompare(tagsForIndex(left), tagsForIndex(right)) < 0;
}
#endif
default:
break;
......@@ -331,6 +336,10 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
return leftRating > rightRating;
}
case DolphinView::SortByTags: {
return naturalCompare(tagsForIndex(left), tagsForIndex(right)) < 0;
}
#endif
}
......@@ -339,12 +348,12 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
return QSortFilterProxyModel::lessThan(left, right);
}
quint32 DolphinSortFilterProxyModel::ratingForIndex(const QModelIndex& index) const
quint32 DolphinSortFilterProxyModel::ratingForIndex(const QModelIndex& index)
{
#ifdef HAVE_NEPOMUK
quint32 rating = 0;
const KDirModel* dirModel = static_cast<const KDirModel*>(sourceModel());
const KDirModel* dirModel = static_cast<const KDirModel*>(index.model());
KFileItem* item = dirModel->itemForIndex(index);
if (item != 0) {
const Nepomuk::Resource resource(item->url().url(), Nepomuk::NFO::File());
......@@ -357,6 +366,38 @@ quint32 DolphinSortFilterProxyModel::ratingForIndex(const QModelIndex& index) co
#endif
}
QString DolphinSortFilterProxyModel::tagsForIndex(const QModelIndex& index)
{
#ifdef HAVE_NEPOMUK
QString tagsString;
const KDirModel* dirModel = static_cast<const KDirModel*>(index.model());
KFileItem* item = dirModel->itemForIndex(index);
if (item != 0) {
const Nepomuk::Resource resource(item->url().url(), Nepomuk::NFO::File());
const QList<Nepomuk::Tag> tags = resource.tags();
QStringList stringList;
foreach (const Nepomuk::Tag& tag, tags) {
stringList.append(tag.label());
}
stringList.sort();
foreach (const QString& str, stringList) {
tagsString += str;
tagsString += ' ';
}
}
if (tagsString.isEmpty()) {
tagsString = i18n("(no tags)");
}
return tagsString;
#else
return QString();
#endif
}
int DolphinSortFilterProxyModel::naturalCompare(const QString& a,
const QString& b)
{
......
......@@ -87,13 +87,21 @@ private:
* Returns the rating for the item with the index \a index. 0 is
* returned if no item could be found.
*/
quint32 ratingForIndex(const QModelIndex& index) const;
static quint32 ratingForIndex(const QModelIndex& index);
/**
* Returns the tags for the item with the index \a index. If no
* tag is applied, a predefined string will be returned.
*/
static QString tagsForIndex(const QModelIndex& index);
static int naturalCompare(const QString& a, const QString& b);
private:
DolphinView::Sorting m_sorting;
Qt::SortOrder m_sortOrder;
friend class DolphinItemCategorizer;
};
DolphinView::Sorting DolphinSortFilterProxyModel::sorting() const
......
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