From: Rafael Fernández López Date: Mon, 18 Jun 2007 10:33:28 +0000 (+0000) Subject: Fix small problems with enums. Sort by type works like a charm :) X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/20aedc53294376e05b79da5ea775f740b7a1aa49 Fix small problems with enums. Sort by type works like a charm :) svn path=/trunk/KDE/kdebase/apps/; revision=677029 --- diff --git a/src/dolphinitemcategorizer.cpp b/src/dolphinitemcategorizer.cpp index 990f2a3b6..0f55881c4 100644 --- a/src/dolphinitemcategorizer.cpp +++ b/src/dolphinitemcategorizer.cpp @@ -46,25 +46,11 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index, return retString; } - int indexColumn; - - switch (sortRole) - { - case DolphinView::SortByName: - indexColumn = KDirModel::Name; - break; - case DolphinView::SortBySize: - indexColumn = KDirModel::Size; - break; - default: - return retString; - } - // KDirModel checks columns to know to which role are // we talking about QModelIndex theIndex = index.model()->index(index.row(), - indexColumn, - index.parent()); + sortRole, + index.parent()); if (!theIndex.isValid()) { return retString; @@ -73,11 +59,12 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index, QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole); const KDirModel *dirModel = qobject_cast(index.model()); - KFileItem* item = dirModel->itemForIndex(index); + KFileItem *item = dirModel->itemForIndex(index); + int fileSize; switch (sortRole) { - case DolphinView::SortByName: + case KDirModel::Name: if (data.toString().size()) { if (!item->isHidden() && data.toString().at(0).isLetter()) @@ -95,19 +82,24 @@ QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index, else retString = i18n("Others"); } - break; - case DolphinView::SortBySize: - int fileSize = (item) ? item->size() : -1; - if (item && item->isDir()) { - retString = i18n("Folders"); - } else if (fileSize < 5242880) { - retString = i18nc("Size", "Small"); - } else if (fileSize < 10485760) { - retString = i18nc("Size", "Medium"); - } else { - retString = i18nc("Size", "Big"); - } - break; + break; + + case KDirModel::Size: + fileSize = (item) ? item->size() : -1; + if (item && item->isDir()) { + retString = i18n("Folders"); + } else if (fileSize < 5242880) { + retString = i18nc("Size", "Small"); + } else if (fileSize < 10485760) { + retString = i18nc("Size", "Medium"); + } else { + retString = i18nc("Size", "Big"); + } + break; + + case KDirModel::Type: + retString = item->mimeComment(); + break; } return retString; diff --git a/src/dolphinsortfilterproxymodel.cpp b/src/dolphinsortfilterproxymodel.cpp index 363140fc5..04294b9c2 100644 --- a/src/dolphinsortfilterproxymodel.cpp +++ b/src/dolphinsortfilterproxymodel.cpp @@ -88,7 +88,7 @@ void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder) m_sorting = (column >= 0) && (column < dolphinMapSize) ? dirModelColumnToDolphinView[column] : DolphinView::SortByName; - setSortRole(m_sorting); + setSortRole(dirModelColumnToDolphinView[column]); KSortFilterProxyModel::sort(column, sortOrder); } @@ -118,7 +118,7 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left KDirModel* dirModel = static_cast(sourceModel()); const KFileItem *leftFileItem = dirModel->itemForIndex(left); - const KFileItem *rightFileItem = dirModel->itemForIndex(right); + const KFileItem *rightFileItem = dirModel->itemForIndex(right); if (sortRole() == DolphinView::SortByName) // If we are sorting by name { @@ -132,7 +132,7 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left // want here to compare by a natural comparation return leftStr.toLower() < rightStr.toLower(); } - else if (sortRole() == DolphinView::SortBySize) // If we are sorting by size + else if (sortRole() == KDirModel::Size) // If we are sorting by size { // If we are sorting by size, show folders first. We will sort them // correctly later @@ -141,6 +141,11 @@ bool DolphinSortFilterProxyModel::lessThanGeneralPurpose(const QModelIndex &left return false; } + else if (sortRole() == KDirModel::Type) + { + return naturalCompare(leftFileItem->mimetype().toLower(), + rightFileItem->mimetype().toLower()) < 0; + } } bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left, @@ -154,7 +159,7 @@ bool DolphinSortFilterProxyModel::lessThan(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 + if (sortRole() == KDirModel::Name) { // If we are sorting by name if ((leftData.type() == QVariant::String) && (rightData.type() == QVariant::String)) { // Priority: hidden > folders > regular files. If an item is @@ -186,7 +191,7 @@ bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left, (naturalCompare(leftStr.toLower(), rightStr.toLower()) < 0); } } - else if (sortRole() == DolphinView::SortBySize) { // If we are sorting by size + else if (sortRole() == KDirModel::Size) { // 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()) { @@ -217,8 +222,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, DolphinView::SortByName).toString(); - const QString rightStr = dirModel->data(right, DolphinView::SortByName).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); @@ -232,8 +237,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, DolphinView::SortByName).toString(); - const QString rightStr = dirModel->data(right, DolphinView::SortByName).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); @@ -242,6 +247,20 @@ 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) + { + if (leftFileItem->mimetype() == rightFileItem->mimetype()) + { + 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 naturalCompare(leftFileItem->mimeComment(), + rightFileItem->mimeComment()) < 0; + } // We have set a SortRole and trust the ProxyModel to do // the right thing for now.