]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodel.cpp
Introduce "isExpandable" role
[dolphin.git] / src / kitemviews / kfileitemmodel.cpp
index d81a985c73605061317f0ecf2c0f7886465edae8..baf168ddf1df3050903aec365a2a13bffd172701 100644 (file)
@@ -28,7 +28,7 @@
 #include <QMimeData>
 #include <QTimer>
 
-#define KFILEITEMMODEL_DEBUG
+// #define KFILEITEMMODEL_DEBUG
 
 KFileItemModel::KFileItemModel(KDirLister* dirLister, QObject* parent) :
     KItemModelBase("name", parent),
@@ -49,7 +49,7 @@ KFileItemModel::KFileItemModel(KDirLister* dirLister, QObject* parent) :
     m_pendingItemsToInsert(),
     m_pendingEmitLoadingCompleted(false),
     m_groups(),
-    m_rootExpansionLevel(-1),
+    m_rootExpansionLevel(UninitializedRootExpansionLevel),
     m_expandedUrls(),
     m_urlsToExpand()
 {
@@ -358,8 +358,8 @@ void KFileItemModel::setRoles(const QSet<QByteArray>& roles)
     m_roles = roles;
 
     if (count() > 0) {
-        const bool supportedExpanding = m_requestRole[IsExpandedRole] && m_requestRole[ExpansionLevelRole];
-        const bool willSupportExpanding = roles.contains("isExpanded") && roles.contains("expansionLevel");
+        const bool supportedExpanding = m_requestRole[ExpansionLevelRole];
+        const bool willSupportExpanding = roles.contains("expansionLevel");
         if (supportedExpanding && !willSupportExpanding) {
             // No expanding is supported anymore. Take care to delete all items that have an expansion level
             // that is not 0 (and hence are part of an expanded item).
@@ -394,7 +394,7 @@ QSet<QByteArray> KFileItemModel::roles() const
 
 bool KFileItemModel::setExpanded(int index, bool expanded)
 {
-    if (isExpanded(index) == expanded || index < 0 || index >= count()) {
+    if (!isExpandable(index) || isExpanded(index) == expanded) {
         return false;
     }
 
@@ -445,7 +445,7 @@ bool KFileItemModel::isExpanded(int index) const
 bool KFileItemModel::isExpandable(int index) const
 {
     if (index >= 0 && index < count()) {
-        return m_itemData.at(index)->item.isDir();
+        return m_itemData.at(index)->values.value("isExpandable").toBool();
     }
     return false;
 }
@@ -462,7 +462,6 @@ void KFileItemModel::restoreExpandedUrls(const QSet<KUrl>& urls)
 
 void KFileItemModel::setExpanded(const QSet<KUrl>& urls)
 {
-
     const KDirLister* dirLister = m_dirLister.data();
     if (!dirLister) {
         return;
@@ -512,8 +511,12 @@ void KFileItemModel::setNameFilter(const QString& nameFilter)
 
         foreach (ItemData* itemData, m_itemData) {
             if (!m_filter.matches(itemData->item)) {
-                newFilteredItems.append(itemData->item);
-                m_filteredItems.insert(itemData->item);
+                // Only filter non-expanded items as child items may never
+                // exist without a parent item
+                if (!itemData->values.value("isExpanded").toBool()) {
+                    newFilteredItems.append(itemData->item);
+                    m_filteredItems.insert(itemData->item);
+                }
             }
         }
 
@@ -670,19 +673,35 @@ void KFileItemModel::slotCanceled()
 
 void KFileItemModel::slotNewItems(const KFileItemList& items)
 {
+    Q_ASSERT(!items.isEmpty());
+
     if (m_requestRole[ExpansionLevelRole] && m_rootExpansionLevel >= 0) {
-        // If the expanding of items is enabled in the model, it might be
-        // possible that the call dirLister->openUrl(url, KDirLister::Keep) in
-        // KFileItemModel::setExpanded() results in emitting of the same items
-        // twice due to the Keep-parameter. This case happens if an item gets
-        // expanded, collapsed and expanded again before the items could be loaded
-        // for the first expansion.
-        foreach (const KFileItem& item, items) {
-            const int index = m_items.value(item.url(), -1);
-            if (index >= 0) {
-                // The items are already part of the model.
-                return;
-            }
+        // To be able to compare whether the new items may be inserted as children
+        // of a parent item the pending items must be added to the model first.
+        dispatchPendingItemsToInsert();
+
+        KFileItem item = items.first();
+
+        // If the expanding of items is enabled, the call
+        // dirLister->openUrl(url, KDirLister::Keep) in KFileItemModel::setExpanded()
+        // might result in emitting the same items twice due to the Keep-parameter.
+        // This case happens if an item gets expanded, collapsed and expanded again
+        // before the items could be loaded for the first expansion.
+        const int index = m_items.value(item.url(), -1);
+        if (index >= 0) {
+            // The items are already part of the model.
+            return;
+        }
+
+        // KDirLister keeps the children of items that got expanded once even if
+        // they got collapsed again with KFileItemModel::setExpanded(false). So it must be
+        // checked whether the parent for new items is still expanded.
+        KUrl parentUrl = item.url().upUrl();
+        parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
+        const int parentIndex = m_items.value(parentUrl, -1);
+        if (parentIndex >= 0 && !m_itemData[parentIndex]->values.value("isExpanded").toBool()) {
+            // The parent is not expanded.
+            return;
         }
     }
 
@@ -715,13 +734,21 @@ void KFileItemModel::slotItemsDeleted(const KFileItemList& items)
 {
     dispatchPendingItemsToInsert();
 
-    if (!m_filteredItems.isEmpty()) {
+    KFileItemList itemsToRemove = items;
+    if (m_requestRole[ExpansionLevelRole] && m_rootExpansionLevel >= 0) {
+        // Assure that removing a parent item also results in removing all children
         foreach (const KFileItem& item, items) {
+            itemsToRemove.append(childItems(item));
+        }
+    }
+
+    if (!m_filteredItems.isEmpty()) {
+        foreach (const KFileItem& item, itemsToRemove) {
             m_filteredItems.remove(item);
         }
     }
 
-    removeItems(items);
+    removeItems(itemsToRemove);
 }
 
 void KFileItemModel::slotRefreshItems(const QList<QPair<KFileItem, KFileItem> >& items)
@@ -803,7 +830,7 @@ void KFileItemModel::slotClear()
     m_resortAllItemsTimer->stop();
     m_pendingItemsToInsert.clear();
 
-    m_rootExpansionLevel = -1;
+    m_rootExpansionLevel = UninitializedRootExpansionLevel;
 
     const int removedCount = m_itemData.count();
     if (removedCount > 0) {
@@ -986,7 +1013,7 @@ void KFileItemModel::removeItems(const KFileItemList& items)
     }
 
     if (count() <= 0) {
-        m_rootExpansionLevel = -1;
+        m_rootExpansionLevel = UninitializedRootExpansionLevel;
     }
 
     itemRanges << KItemRange(removedAtIndex, removedCount);
@@ -1002,6 +1029,21 @@ QList<KFileItemModel::ItemData*> KFileItemModel::createItemDataList(const KFileI
         ItemData* itemData = new ItemData();
         itemData->item = item;
         itemData->values = retrieveData(item);
+        itemData->parent = 0;
+
+        const bool determineParent = m_requestRole[ExpansionLevelRole]
+                                     && itemData->values["expansionLevel"].toInt() > 0;
+        if (determineParent) {
+            KUrl parentUrl = item.url().upUrl();
+            parentUrl.adjustPath(KUrl::RemoveTrailingSlash);
+            const int parentIndex = m_items.value(parentUrl, -1);
+            if (parentIndex >= 0) {
+                itemData->parent = m_itemData.at(parentIndex);
+            } else {
+                kWarning() << "Parent item not found for" << item.url();
+            }
+        }
+
         itemDataList.append(itemData);
     }
  
@@ -1025,7 +1067,7 @@ void KFileItemModel::removeExpandedItems()
     Q_ASSERT(m_rootExpansionLevel >= 0);
     removeItems(expandedItems);
 
-    m_rootExpansionLevel = -1;
+    m_rootExpansionLevel = UninitializedRootExpansionLevel;
     m_expandedUrls.clear();
 }
 
@@ -1054,6 +1096,7 @@ KFileItemModel::Role KFileItemModel::roleIndex(const QByteArray& role) const
         rolesHash.insert("rating", RatingRole);
         rolesHash.insert("isDir", IsDirRole);
         rolesHash.insert("isExpanded", IsExpandedRole);
+        rolesHash.insert("isExpandable", IsExpandableRole);
         rolesHash.insert("expansionLevel", ExpansionLevelRole);
     }
     return rolesHash.value(role, NoRole);
@@ -1114,25 +1157,49 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
     }
 
     if (m_requestRole[PathRole]) {
-        data.insert("path", item.localPath());
+        if (item.url().protocol() == QLatin1String("trash")) {
+            const KIO::UDSEntry udsEntry = item.entry();
+            data.insert("path", udsEntry.stringValue(KIO::UDSEntry::UDS_EXTRA));
+        } else {
+            data.insert("path", item.localPath());
+        }
     }
 
     if (m_requestRole[IsExpandedRole]) {
         data.insert("isExpanded", false);
     }
 
+    if (m_requestRole[IsExpandableRole]) {
+        data.insert("isExpandable", item.isDir() && item.url() == item.targetUrl());
+    }
+
     if (m_requestRole[ExpansionLevelRole]) {
-        if (m_rootExpansionLevel < 0 && m_dirLister.data()) {
-            const QString rootDir = m_dirLister.data()->url().directory(KUrl::AppendTrailingSlash);
-            m_rootExpansionLevel = rootDir.count('/');
-            if (m_rootExpansionLevel == 1) {
-                // Special case: The root is already reached and no parent is available
-                --m_rootExpansionLevel;
+        if (m_rootExpansionLevel == UninitializedRootExpansionLevel && m_dirLister.data()) {
+            const KUrl rootUrl = m_dirLister.data()->url();
+            const QString protocol = rootUrl.protocol();
+            const bool forceRootExpansionLevel = (protocol == QLatin1String("trash") ||
+                                                  protocol == QLatin1String("nepomuk") ||
+                                                  protocol == QLatin1String("remote") ||
+                                                  protocol.contains(QLatin1String("search")));
+            if (forceRootExpansionLevel) {
+                m_rootExpansionLevel = ForceRootExpansionLevel;
+            } else {
+                const QString rootDir = rootUrl.directory(KUrl::AppendTrailingSlash);
+                m_rootExpansionLevel = rootDir.count('/');
+                if (m_rootExpansionLevel == 1) {
+                    // Special case: The root is already reached and no parent is available
+                    --m_rootExpansionLevel;
+                }
             }
         }
-        const QString dir = item.url().directory(KUrl::AppendTrailingSlash);
-        const int level = dir.count('/') - m_rootExpansionLevel - 1;
-        data.insert("expansionLevel", level);
+
+        if (m_rootExpansionLevel == ForceRootExpansionLevel) {
+            data.insert("expansionLevel", -1);
+        } else {
+            const QString dir = item.url().directory(KUrl::AppendTrailingSlash);
+            const int level = dir.count('/') - m_rootExpansionLevel - 1;
+            data.insert("expansionLevel", level);
+        }
     }
 
     if (item.isMimeTypeKnown()) {
@@ -1148,13 +1215,10 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
 
 bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
 {
-    const KFileItem& itemA = a->item;
-    const KFileItem& itemB = b->item;
-    
     int result = 0;
 
     if (m_rootExpansionLevel >= 0) {
-        result = expansionLevelsCompare(itemA, itemB);
+        result = expansionLevelsCompare(a, b);
         if (result != 0) {
             // The items have parents with different expansion levels
             return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
@@ -1162,8 +1226,8 @@ bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
     }
 
     if (m_sortFoldersFirst || m_sortRole == SizeRole) {
-        const bool isDirA = itemA.isDir();
-        const bool isDirB = itemB.isDir();
+        const bool isDirA = a->item.isDir();
+        const bool isDirB = b->item.isDir();
         if (isDirA && !isDirB) {
             return true;
         } else if (!isDirA && isDirB) {
@@ -1415,10 +1479,10 @@ int KFileItemModel::stringCompare(const QString& a, const QString& b) const
                             : QString::compare(a, b, Qt::CaseSensitive);
 }
 
-int KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& b) const
+int KFileItemModel::expansionLevelsCompare(const ItemData* a, const ItemData* b) const
 {
-    const KUrl urlA = a.url();
-    const KUrl urlB = b.url();
+    const KUrl urlA = a->item.url();
+    const KUrl urlB = b->item.url();
     if (urlA.directory() == urlB.directory()) {
         // Both items have the same directory as parent
         return 0;
@@ -1451,61 +1515,36 @@ int KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem&
     // Determine the first sub-path after the common path and
     // check whether it represents a directory or already a file
     bool isDirA = true;
-    const QString subPathA = subPath(a, pathA, index, &isDirA);
+    const QString subPathA = subPath(a->item, pathA, index, &isDirA);
     bool isDirB = true;
-    const QString subPathB = subPath(b, pathB, index, &isDirB);
+    const QString subPathB = subPath(b->item, pathB, index, &isDirB);
 
     if (isDirA && !isDirB) {
-        return -1;
+        return (sortOrder() == Qt::AscendingOrder) ? -1 : +1;
     } else if (!isDirA && isDirB) {
-        return +1;
-    }
-
-    if (m_sortRole == NameRole) {
-        // Performance optimization for sorting by names: Just call
-        // stringCompare() directly to prevent the potentially slow
-        // path to get the ItemData for parents.
-        return stringCompare(subPathA, subPathB);
+        return (sortOrder() == Qt::AscendingOrder) ? +1 : -1;
     }
 
-    // Compare the item-data the parents that represent the first
+    // Compare the items of the parents that represent the first
     // different path after the common path.
-
-    // TODO: The following implementation is very hacky but works. Issues with
-    // this approach:
-    // 1. m_items is accessed, although the sorting in might also work on
-    //    a non-member variables. This happens in insertItems() but it does
-    //    not really matter as in this case only a presorting is done.
-    // 2. It is very slow in theory as it introduces a O(n*n) runtime complexity
-    //    in combination with lessThan(). Practically the code is still fast
-    //    enough for thousands of items but this must be fixed.
-    //
-    // Proposal: Extend the internal structure ItemData by a member
-    // 'ItemData* parent and access it here.
-
     const KUrl parentUrlA(pathA.left(index) + subPathA);
     const KUrl parentUrlB(pathB.left(index) + subPathB);
 
-    const ItemData* parentItemDataA = 0;
-    foreach (const ItemData* itemData, m_itemData) {
-        if (itemData->item.url() == parentUrlA) {
-            parentItemDataA = itemData;
-            break;
-        }
+    const ItemData* parentA = a;
+    while (parentA && parentA->item.url() != parentUrlA) {
+        parentA = parentA->parent;
     }
 
-    const ItemData* parentItemDataB = 0;
-    foreach (const ItemData* itemData, m_itemData) {
-        if (itemData->item.url() == parentUrlB) {
-            parentItemDataB = itemData;
-            break;
-        }
+    const ItemData* parentB = b;
+    while (parentB && parentB->item.url() != parentUrlB) {
+        parentB = parentB->parent;
     }
 
-    if (parentItemDataA && parentItemDataB) {
-        return sortRoleCompare(parentItemDataA, parentItemDataB);
+    if (parentA && parentB) {
+        return sortRoleCompare(parentA, parentB);
     }
 
+    kWarning() << "Child items without parent detected:" << a->item.url() << b->item.url();
     return QString::compare(urlA.url(), urlB.url(), Qt::CaseSensitive);
 }
 
@@ -1546,7 +1585,7 @@ QList<QPair<int, QVariant> > KFileItemModel::nameRoleGroups() const
         // Use the first character of the name as group indication
         QChar newFirstChar = name.at(0).toUpper();
         if (newFirstChar == QLatin1Char('~') && name.length() > 1) {
-            newFirstChar = name.at(1);
+            newFirstChar = name.at(1).toUpper();
         }
 
         if (firstChar != newFirstChar) {
@@ -1839,4 +1878,21 @@ QList<QPair<int, QVariant> > KFileItemModel::genericStringRoleGroups(const QByte
     return groups;
 }
 
+KFileItemList KFileItemModel::childItems(const KFileItem& item) const
+{
+    KFileItemList items;
+
+    int index = m_items.value(item.url(), -1);
+    if (index >= 0) {
+        const int parentLevel = m_itemData.at(index)->values.value("expansionLevel").toInt();
+        ++index;
+        while (index < m_itemData.count() && m_itemData.at(index)->values.value("expansionLevel").toInt() > parentLevel) {
+            items.append(m_itemData.at(index)->item);
+            ++index;
+        }
+    }
+
+    return items;
+}
+
 #include "kfileitemmodel.moc"