]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodel.cpp
Ensure that all children of a collapsed folder are removed
[dolphin.git] / src / kitemviews / kfileitemmodel.cpp
index a0f9305cb03264ea66d77c41a3495eb03c181a5a..34a97de4c01e4ebff9549a8ef43093e77fac3d2a 100644 (file)
@@ -486,6 +486,18 @@ bool KFileItemModel::setExpanded(int index, bool expanded)
             m_urlsToExpand.insert(url);
         }
     } else {
+        // Note that there might be (indirect) children of the folder which is to be collapsed in
+        // m_pendingItemsToInsert. To prevent that they will be inserted into the model later,
+        // possibly without a parent, which might result in a crash, we insert all pending items
+        // right now. All new items which would be without a parent will then be removed.
+        dispatchPendingItemsToInsert();
+
+        // Check if the index of the collapsed folder has changed. If that is the case, then items
+        // were inserted before the collapsed folder, and its index needs to be updated.
+        if (m_itemData.at(index)->item != item) {
+            index = this->index(item);
+        }
+
         m_expandedDirs.remove(targetUrl);
         m_dirLister->stop(url);