Commit 552894c8 authored by Rafael Fernández López's avatar Rafael Fernández López
Browse files

Sort by date finished. Add speed improvement when comparing strings. enum adaptations.

CCMAIL: faure@kde.org

svn path=/trunk/KDE/kdebase/apps/; revision=677083
parent 1cf98f71
......@@ -24,6 +24,7 @@
#include <klocale.h>
#include <kdirmodel.h>
#include <kdatetime.h>
#include <QtGui/QSortFilterProxyModel>
......@@ -46,10 +47,39 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
return retString;
}
int column;
switch (sortRole)
{
case DolphinView::SortByName: // KDirModel::Name
column = KDirModel::Name;
break;
case DolphinView::SortBySize: // KDirModel::Size
column = KDirModel::Size;
break;
case DolphinView::SortByDate: // KDirModel::ModifiedTime
column = KDirModel::ModifiedTime;
break;
case DolphinView::SortByPermissions: // KDirModel::Permissions
column = KDirModel::Permissions;
break;
case DolphinView::SortByOwner: // KDirModel::Owner
column = KDirModel::Owner;
break;
case DolphinView::SortByGroup: // KDirModel::Group
column = KDirModel::Group;
break;
case DolphinView::SortByType: // KDirModel::Type
column = KDirModel::Type;
break;
default:
column = KDirModel::Name;
}
// KDirModel checks columns to know to which role are
// we talking about
QModelIndex theIndex = index.model()->index(index.row(),
sortRole,
column,
index.parent());
if (!theIndex.isValid()) {
......@@ -60,11 +90,12 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
KFileItem *item = dirModel->itemForIndex(index);
int fileSize;
int fileSize;
KDateTime modifiedTime;
switch (sortRole)
{
case KDirModel::Name:
case DolphinView::SortByName:
if (data.toString().size())
{
if (!item->isHidden() && data.toString().at(0).isLetter())
......@@ -84,7 +115,34 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
}
break;
case KDirModel::Size:
case DolphinView::SortByDate:
modifiedTime.setTime_t(item->time(KIO::UDS_MODIFICATION_TIME));
modifiedTime = modifiedTime.toLocalZone();
if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
retString = i18n("Today");
else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
retString = i18n("Yesterday");
else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
retString = i18n("Less than a week");
else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
retString = i18n("Less than a month");
else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
retString = i18n("Less than a year");
else
retString = i18n("More than a year");
break;
case DolphinView::SortByPermissions:
break;
case DolphinView::SortByOwner:
break;
case DolphinView::SortByGroup:
break;
case DolphinView::SortBySize:
fileSize = (item) ? item->size() : -1;
if (item && item->isDir()) {
retString = i18n("Folders");
......@@ -97,7 +155,7 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
}
break;
case KDirModel::Type:
case DolphinView::SortByType:
retString = item->mimeComment();
break;
}
......
......@@ -24,6 +24,7 @@
#include <kdirmodel.h>
#include <kfileitem.h>
#include <kdatetime.h>
static const int dolphinMapSize = 7;
static int dolphinViewToDirModelColumn[] =
......@@ -120,9 +121,8 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left
const KFileItem *leftFileItem = dirModel->itemForIndex(left);
const KFileItem *rightFileItem = dirModel->itemForIndex(right);
if (sortRole() == DolphinView::SortByName) // If we are sorting by name
{
const QVariant leftData = dirModel->data(left, sortRole());
if (sortRole() == DolphinView::SortByName) { // If we are sorting by name
const QVariant leftData = dirModel->data(left, sortRole());
const QVariant rightData = dirModel->data(right, sortRole());
QString leftStr = leftData.toString();
......@@ -130,10 +130,9 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left
// We don't care about case for building categories. We also don't
// want here to compare by a natural comparation
return leftStr.toLower() < rightStr.toLower();
return QString::compare(leftStr, rightStr, Qt::CaseInsensitive) < 0;
}
else if (sortRole() == KDirModel::Size) // If we are sorting by size
{
else if (sortRole() == DolphinView::SortBySize) { // If we are sorting by size
// If we are sorting by size, show folders first. We will sort them
// correctly later
if (leftFileItem->isDir() && !rightFileItem->isDir())
......@@ -141,8 +140,21 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left
return false;
}
else if (sortRole() == KDirModel::Type)
{
else if (sortRole() == DolphinView::SortByDate) {
KDateTime leftTime;
leftTime.setTime_t(leftFileItem->time(KIO::UDS_MODIFICATION_TIME));
KDateTime rightTime;
rightTime.setTime_t(rightFileItem->time(KIO::UDS_MODIFICATION_TIME));
return leftTime > rightTime;
}
else if (sortRole() == DolphinView::SortByPermissions) {
}
else if (sortRole() == DolphinView::SortByOwner) {
}
else if (sortRole() == DolphinView::SortByGroup) {
}
else if (sortRole() == DolphinView::SortByType) {
// If we are sorting by size, show folders first. We will sort them
// correctly later
if (leftFileItem->isDir() && !rightFileItem->isDir())
......@@ -160,13 +172,13 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
{
KDirModel* dirModel = static_cast<KDirModel*>(sourceModel());
QVariant leftData = dirModel->data(left, sortRole());
QVariant leftData = dirModel->data(left, sortRole());
QVariant rightData = dirModel->data(right, sortRole());
const KFileItem *leftFileItem = dirModel->itemForIndex(left);
const KFileItem *rightFileItem = dirModel->itemForIndex(right);
if (sortRole() == KDirModel::Name) { // If we are sorting by name
if (sortRole() == DolphinView::SortByName) { // If we are sorting by name
if ((leftData.type() == QVariant::String) && (rightData.type() ==
QVariant::String)) {
// Priority: hidden > folders > regular files. If an item is
......@@ -198,7 +210,7 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
(naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
}
}
else if (sortRole() == KDirModel::Size) { // If we are sorting by size
else if (sortRole() == DolphinView::SortBySize) { // If we are sorting by size
// If an item is hidden (doesn't matter if file or folder) will have
// higher preference than a non-hidden item
if (leftFileItem->isHidden() && !rightFileItem->isHidden()) {
......@@ -229,8 +241,8 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
// their names. So we have always everything ordered. We also check
// if we are taking in count their cases
if (leftCount == rightCount) {
const QString leftStr = dirModel->data(left, KDirModel::Name).toString();
const QString rightStr = dirModel->data(right, KDirModel::Name).toString();
const QString leftStr = dirModel->data(left, KDirModel::Name).toString();
const QString rightStr = dirModel->data(right, KDirModel::Name).toString();
return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
(naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
......@@ -244,8 +256,8 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
// 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()) {
const QString leftStr = dirModel->data(left, KDirModel::Name).toString();
const QString rightStr = dirModel->data(right, KDirModel::Name).toString();
const QString leftStr = dirModel->data(left, KDirModel::Name).toString();
const QString rightStr = dirModel->data(right, KDirModel::Name).toString();
return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
(naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
......@@ -254,7 +266,47 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
// If their sizes are different, sort them by their sizes, as expected
return leftFileItem->size() < rightFileItem->size();
}
else if (sortRole() == KDirModel::Type)
else if (sortRole() == DolphinView::SortByDate) {
// If an item is hidden (doesn't matter if file or folder) will have
// higher preference than a non-hidden item
if (leftFileItem->isHidden() && !rightFileItem->isHidden()) {
return true;
}
else if (!leftFileItem->isHidden() && rightFileItem->isHidden()) {
return false;
}
// On our priority, folders go above regular files
if (leftFileItem->isDir() && !rightFileItem->isDir()) {
return true;
}
else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
return false;
}
KDateTime leftTime;
leftTime.setTime_t(leftFileItem->time(KIO::UDS_MODIFICATION_TIME));
KDateTime rightTime;
rightTime.setTime_t(rightFileItem->time(KIO::UDS_MODIFICATION_TIME));
if (leftTime == rightTime)
{
const QString leftStr = dirModel->data(left, KDirModel::Name).toString();
const QString rightStr = dirModel->data(right, KDirModel::Name).toString();
return sortCaseSensitivity() ? (naturalCompare(leftStr, rightStr) < 0) :
(naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0);
}
return leftTime > rightTime;
}
else if (sortRole() == DolphinView::SortByPermissions) {
}
else if (sortRole() == DolphinView::SortByOwner) {
}
else if (sortRole() == DolphinView::SortByGroup) {
}
else if (sortRole() == DolphinView::SortByType)
{
// If an item is hidden (doesn't matter if file or folder) will have
// higher preference than a non-hidden item
......
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