]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodel.cpp
Fix style-issues in items when not using Oxygen
[dolphin.git] / src / kitemviews / kfileitemmodel.cpp
index 8e1943b4b4f211e4cf592460177630505249d2d7..3fd40e9d453c35c2949f2182fba2062655430322 100644 (file)
@@ -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).
@@ -367,6 +367,7 @@ void KFileItemModel::setRoles(const QSet<QByteArray>& roles)
         }
     }
 
+    m_groups.clear();
     resetRoles();
 
     QSetIterator<QByteArray> it(roles);
@@ -394,7 +395,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 +446,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 +463,6 @@ void KFileItemModel::restoreExpandedUrls(const QSet<KUrl>& urls)
 
 void KFileItemModel::setExpanded(const QSet<KUrl>& urls)
 {
-
     const KDirLister* dirLister = m_dirLister.data();
     if (!dirLister) {
         return;
@@ -607,20 +607,19 @@ void KFileItemModel::resortAllItems()
     }
     
     // Determine the indexes that have been moved
-    bool emitItemsMoved = false;
     QList<int> movedToIndexes;
     movedToIndexes.reserve(itemCount);
     for (int i = 0; i < itemCount; i++) {
         const int newIndex = m_items.value(oldUrls.at(i).url());
         movedToIndexes.append(newIndex);
-        if (!emitItemsMoved && newIndex != i) {
-            emitItemsMoved = true;
-        }
     }   
 
-    if (emitItemsMoved) {
-        emit itemsMoved(KItemRange(0, itemCount), movedToIndexes);
-    }
+    // Don't check whether items have really been moved and always emit a
+    // itemsMoved() signal after resorting: In case of grouped items
+    // the groups might change even if the items themselves don't change their
+    // position. Let the receiver of the signal decide whether a check for moved
+    // items makes sense.
+    emit itemsMoved(KItemRange(0, itemCount), movedToIndexes);
     
 #ifdef KFILEITEMMODEL_DEBUG
     kDebug() << "[TIME] Resorting of" << itemCount << "items:" << timer.elapsed();
@@ -674,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;
         }
     }
 
@@ -1081,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);
@@ -1141,24 +1157,33 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
     }
 
     if (m_requestRole[PathRole]) {
+        QString path;
         if (item.url().protocol() == QLatin1String("trash")) {
-            const KIO::UDSEntry udsEntry = item.entry();
-            data.insert("path", udsEntry.stringValue(KIO::UDSEntry::UDS_EXTRA));
+            path = item.entry().stringValue(KIO::UDSEntry::UDS_EXTRA);
         } else {
-            data.insert("path", item.localPath());
+            path = item.localPath();
         }
+
+        const int index = path.lastIndexOf(item.text());
+        path = path.mid(0, index - 1);
+        data.insert("path", path);
     }
 
     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 == 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;
@@ -1506,16 +1531,16 @@ int KFileItemModel::expansionLevelsCompare(const ItemData* a, const ItemData* b)
 
     // Compare the items of the parents that represent the first
     // different path after the common path.
-    const KUrl parentUrlA(pathA.left(index) + subPathA);
-    const KUrl parentUrlB(pathB.left(index) + subPathB);
+    const QString parentPathA = pathA.left(index) + subPathA;
+    const QString parentPathB = pathB.left(index) + subPathB;
 
     const ItemData* parentA = a;
-    while (parentA && parentA->item.url() != parentUrlA) {
+    while (parentA && parentA->item.url().path() != parentPathA) {
         parentA = parentA->parent;
     }
 
     const ItemData* parentB = b;
-    while (parentB && parentB->item.url() != parentUrlB) {
+    while (parentB && parentB->item.url().path() != parentPathB) {
         parentB = parentB->parent;
     }
 
@@ -1820,12 +1845,12 @@ QList<QPair<int, QVariant> > KFileItemModel::ratingRoleGroups() const
     const int maxIndex = count() - 1;
     QList<QPair<int, QVariant> > groups;
 
-    int groupValue;
+    int groupValue = -1;
     for (int i = 0; i <= maxIndex; ++i) {
         if (isChildItem(i)) {
             continue;
         }
-        const int newGroupValue = m_itemData.at(i)->values.value("rating").toInt();
+        const int newGroupValue = m_itemData.at(i)->values.value("rating", 0).toInt();
         if (newGroupValue != groupValue) {
             groupValue = newGroupValue;
             groups.append(QPair<int, QVariant>(i, newGroupValue));
@@ -1842,15 +1867,17 @@ QList<QPair<int, QVariant> > KFileItemModel::genericStringRoleGroups(const QByte
     const int maxIndex = count() - 1;
     QList<QPair<int, QVariant> > groups;
 
+    bool isFirstGroupValue = true;
     QString groupValue;
     for (int i = 0; i <= maxIndex; ++i) {
         if (isChildItem(i)) {
             continue;
         }
         const QString newGroupValue = m_itemData.at(i)->values.value(role).toString();
-        if (newGroupValue != groupValue) {
+        if (newGroupValue != groupValue || isFirstGroupValue) {
             groupValue = newGroupValue;
             groups.append(QPair<int, QVariant>(i, newGroupValue));
+            isFirstGroupValue = false;
         }
     }