]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Fix small problems with enums. Sort by type works like a charm :)
authorRafael Fernández López <ereslibre@kde.org>
Mon, 18 Jun 2007 10:33:28 +0000 (10:33 +0000)
committerRafael Fernández López <ereslibre@kde.org>
Mon, 18 Jun 2007 10:33:28 +0000 (10:33 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=677029

src/dolphinitemcategorizer.cpp
src/dolphinsortfilterproxymodel.cpp

index 990f2a3b610c564409154bc5f0be58aa282d4c26..0f55881c43183e6e0725e69fca1919622b97b9f9 100644 (file)
@@ -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<const KDirModel*>(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;
index 363140fc53aa56c0ab3765c6036c7d738806fa1d..04294b9c26f6b46ed336e9a509e763f880ce8c59 100644 (file)
@@ -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<KDirModel*>(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.