Commit 7a884193 authored by Peter Penz's avatar Peter Penz
Browse files

* coding style update to match with http://techbase.kde.org/Policies/Kdelibs_Coding_Style

* minor speed improvements by moving QString, QVariant, KDateTime, ... declarations inside inner scopes; further speed improvements should be possible, but lets do it step by step...

svn path=/trunk/KDE/kdebase/apps/; revision=677719
parent 1dc7ed71
......@@ -65,7 +65,8 @@ DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
}
DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
{}
{
}
void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
{
......@@ -118,62 +119,65 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left
{
KDirModel* dirModel = static_cast<KDirModel*>(sourceModel());
const KFileItem *leftFileItem = dirModel->itemForIndex(left);
const KFileItem *rightFileItem = dirModel->itemForIndex(right);
const KFileItem* leftFileItem = dirModel->itemForIndex(left);
const KFileItem* rightFileItem = dirModel->itemForIndex(right);
QString leftFileName, rightFileName;
KDateTime leftTime, rightTime;
switch (sortRole()) {
case DolphinView::SortByName:
leftFileName = leftFileItem->name();
rightFileName = rightFileItem->name();
leftFileName = leftFileName.at(0) == '.' ? leftFileName.mid(1) :
leftFileName;
rightFileName = rightFileName.at(0) == '.' ? rightFileName.mid(1) :
rightFileName;
// We don't care about case for building categories. We also don't
// want here to compare by a natural comparation
return naturalCompare(leftFileName, rightFileName) < 0;
case DolphinView::SortByName: {
QString leftFileName(leftFileItem->name());
if (leftFileName.at(0) == '.') {
leftFileName = leftFileName.mid(1);
}
case DolphinView::SortBySize:
// If we are sorting by size, show folders first. We will sort them
// correctly later
if (leftFileItem->isDir() && !rightFileItem->isDir())
return true;
QString rightFileName(rightFileItem->name());
if (rightFileName.at(0) == '.') {
rightFileName = rightFileName.mid(1);
}
return false;
// We don't care about case for building categories. We also don't
// want here to compare by a natural comparison.
return naturalCompare(leftFileName, rightFileName) < 0;
}
case DolphinView::SortByDate:
leftTime.setTime_t(leftFileItem->time(KIO::UDS_MODIFICATION_TIME));
rightTime.setTime_t(rightFileItem->time(KIO::UDS_MODIFICATION_TIME));
case DolphinView::SortBySize:
// If we are sorting by size, show folders first. We will sort them
// correctly later.
return leftFileItem->isDir() && !rightFileItem->isDir();
return leftTime > rightTime;
case DolphinView::SortByDate: {
KDateTime leftTime, rightTime;
leftTime.setTime_t(leftFileItem->time(KIO::UDS_MODIFICATION_TIME));
rightTime.setTime_t(rightFileItem->time(KIO::UDS_MODIFICATION_TIME));
return leftTime > rightTime;
}
case DolphinView::SortByPermissions:
return naturalCompare(leftFileItem->permissionsString(),
rightFileItem->permissionsString()) < 0;
case DolphinView::SortByPermissions: {
return naturalCompare(leftFileItem->permissionsString(),
rightFileItem->permissionsString()) < 0;
}
case DolphinView::SortByOwner:
return naturalCompare(leftFileItem->user().toLower(),
rightFileItem->user().toLower()) < 0;
case DolphinView::SortByOwner: {
return naturalCompare(leftFileItem->user().toLower(),
rightFileItem->user().toLower()) < 0;
}
case DolphinView::SortByGroup:
return naturalCompare(leftFileItem->group().toLower(),
rightFileItem->group().toLower()) < 0;
case DolphinView::SortByGroup: {
return naturalCompare(leftFileItem->group().toLower(),
rightFileItem->group().toLower()) < 0;
}
case DolphinView::SortByType:
// If we are sorting by size, show folders first. We will sort them
// correctly later
if (leftFileItem->isDir() && !rightFileItem->isDir())
return true;
else if (!leftFileItem->isDir() && rightFileItem->isDir())
return false;
case DolphinView::SortByType: {
// If we are sorting by size, show folders first. We will sort them
// correctly later
if (leftFileItem->isDir() && !rightFileItem->isDir()) {
return true;
} else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
return false;
}
return naturalCompare(leftFileItem->mimeComment().toLower(),
rightFileItem->mimeComment().toLower()) < 0;
return naturalCompare(leftFileItem->mimeComment().toLower(),
rightFileItem->mimeComment().toLower()) < 0;
}
}
}
......@@ -191,8 +195,7 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
// On our priority, folders go above regular files
if (leftFileItem->isDir() && !rightFileItem->isDir()) {
return true;
}
else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
} else if (!leftFileItem->isDir() && rightFileItem->isDir()) {
return false;
}
......@@ -200,113 +203,113 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
// folders or files
if (leftFileItem->isHidden() && !rightFileItem->isHidden()) {
return true;
}
else if (!leftFileItem->isHidden() && rightFileItem->isHidden()) {
} else if (!leftFileItem->isHidden() && rightFileItem->isHidden()) {
return false;
}
QVariant leftValue, rightValue;
QString leftValueString, rightValueString;
KDateTime leftTime, rightTime;
int leftCount, rightCount;
switch (sortRole()) {
case DolphinView::SortByName:
// So we are in the same priority, what counts now is their names
leftValueString = leftData.toString();
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()) {
leftValue = dirModel->data(left, KDirModel::ChildCountRole);
leftCount = leftValue.type() == QVariant::Int ? leftValue.toInt() : KDirModel::ChildCountUnknown;
rightValue = dirModel->data(right, KDirModel::ChildCountRole);
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);
}
case DolphinView::SortByName: {
// So we are in the same priority, what counts now is their names
QString leftValueString = leftData.toString();
QString rightValueString = rightData.toString();
// If they had different number of items, we sort them depending
// on how many items had each other
return leftCount < rightCount;
}
return sortCaseSensitivity() ? (naturalCompare(leftValueString, rightValueString) < 0) :
(naturalCompare(leftValueString.toLower(), rightValueString.toLower()) < 0);
}
// 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()) {
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);
(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();
// If they had different number of items, we sort them depending
// on how many items had each other
return leftCount < rightCount;
}
case DolphinView::SortByDate:
leftTime.setTime_t(leftFileItem->time(KIO::UDS_MODIFICATION_TIME));
rightTime.setTime_t(rightFileItem->time(KIO::UDS_MODIFICATION_TIME));
// 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 (leftTime == rightTime)
{
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();
}
return leftTime > rightTime;
case DolphinView::SortByDate: {
KDateTime leftTime, rightTime;
leftTime.setTime_t(leftFileItem->time(KIO::UDS_MODIFICATION_TIME));
rightTime.setTime_t(rightFileItem->time(KIO::UDS_MODIFICATION_TIME));
case DolphinView::SortByPermissions:
if (leftFileItem->permissionsString() == rightFileItem->permissionsString())
{
return sortCaseSensitivity() ? (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
(naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
}
if (leftTime == rightTime) {
return sortCaseSensitivity() ?
(naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
(naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
}
return naturalCompare(leftFileItem->permissionsString(),
rightFileItem->permissionsString()) < 0;
return leftTime > rightTime;
}
case DolphinView::SortByOwner:
if (leftFileItem->user() == rightFileItem->user())
{
return sortCaseSensitivity() ? (naturalCompare(leftFileItem->name(), rightFileItem->name()) < 0) :
(naturalCompare(leftFileItem->name().toLower(), rightFileItem->name().toLower()) < 0);
}
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->user(),
rightFileItem->user()) < 0;
return naturalCompare(leftFileItem->permissionsString(),
rightFileItem->permissionsString()) < 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);
}
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->group(),
rightFileItem->group()) < 0;
return naturalCompare(leftFileItem->user(), rightFileItem->user()) < 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);
}
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->mimeComment(),
rightFileItem->mimeComment()) < 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;
}
}
// We have set a SortRole and trust the ProxyModel to do
// the right thing for now.
return QSortFilterProxyModel::lessThan(left, right);
}
......
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