X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/04e825d022c77baad3345269da7a210e95274f07..6bfa0ccfc701df5af5f043fce3168b3840858212:/src/kitemviews/kfileitemmodel.cpp diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index d30d9e5be..eb7b16461 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -114,6 +114,7 @@ KFileItemModel::~KFileItemModel() { qDeleteAll(m_itemData); qDeleteAll(m_filteredItems.values()); + qDeleteAll(m_pendingItemsToInsert); } void KFileItemModel::loadDirectory(const KUrl& url) @@ -124,8 +125,10 @@ void KFileItemModel::loadDirectory(const KUrl& url) void KFileItemModel::refreshDirectory(const KUrl& url) { // Refresh all expanded directories first (Bug 295300) - foreach (const KUrl& expandedUrl, m_expandedDirs) { - m_dirLister->openUrl(expandedUrl, KDirLister::Reload); + QHashIterator expandedDirs(m_expandedDirs); + while (expandedDirs.hasNext()) { + expandedDirs.next(); + m_dirLister->openUrl(expandedDirs.value(), KDirLister::Reload); } m_dirLister->openUrl(url, KDirLister::Reload); @@ -167,7 +170,7 @@ bool KFileItemModel::setData(int index, const QHash& value QHashIterator it(values); while (it.hasNext()) { it.next(); - const QByteArray role = it.key(); + const QByteArray role = sharedValue(it.key()); const QVariant value = it.value(); if (currentValues[role] != value) { @@ -189,8 +192,30 @@ bool KFileItemModel::setData(int index, const QHash& value emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); - if (changedRoles.contains(sortRole())) { - m_resortAllItemsTimer->start(); + // Trigger a resorting if the item's correct position has changed. Note + // that this can happen even if the sort role has not changed at all + // because the file name can be used as a fallback. + if (changedRoles.contains(sortRole()) || changedRoles.contains(roleForType(NameRole))) { + // Compare the changed item with its neighbors to see + // if an expensive resorting is needed at all. + const ItemData* changedItem = m_itemData.at(index); + const ItemData* previousItem = (index == 0) ? 0 : m_itemData.at(index - 1); + const ItemData* nextItem = (index == m_itemData.count() - 1) ? 0 : m_itemData.at(index + 1); + + if ((previousItem && lessThan(changedItem, previousItem)) + || (nextItem && lessThan(nextItem, changedItem))) { + m_resortAllItemsTimer->start(); + } else if (groupedSorting() && changedRoles.contains(sortRole())) { + // The position is still correct, but the groups might have changed + // if the changed item is either the first or the last item in a + // group. + // In principle, we could try to find out if the item really is the + // first or last one in its group and then update the groups + // (possibly with a delayed timer to make sure that we don't + // re-calculate the groups very often if items are updated one by + // one), but starting m_resortAllItemsTimer is easier. + m_resortAllItemsTimer->start(); + } } return true; @@ -249,7 +274,7 @@ QMimeData* KFileItemModel::createMimeData(const QSet& indexes) const const int index = it.next(); const KFileItem item = fileItem(index); if (!item.isNull()) { - urls << item.url(); + urls << item.targetUrl(); bool isLocal; mostLocalUrls << item.mostLocalUrl(isLocal); @@ -425,18 +450,19 @@ bool KFileItemModel::setExpanded(int index, bool expanded) } QHash values; - values.insert("isExpanded", expanded); + values.insert(sharedValue("isExpanded"), expanded); if (!setData(index, values)) { return false; } const KFileItem item = m_itemData.at(index)->item; const KUrl url = item.url(); + const KUrl targetUrl = item.targetUrl(); if (expanded) { - m_expandedDirs.insert(url); + m_expandedDirs.insert(targetUrl, url); m_dirLister->openUrl(url, KDirLister::Keep); } else { - m_expandedDirs.remove(url); + m_expandedDirs.remove(targetUrl); m_dirLister->stop(url); removeFilteredChildren(KFileItemList() << item); @@ -478,7 +504,7 @@ int KFileItemModel::expandedParentsCount(int index) const QSet KFileItemModel::expandedDirectories() const { - return m_expandedDirs; + return m_expandedDirs.values().toSet(); } void KFileItemModel::restoreExpandedDirectories(const QSet& urls) @@ -644,11 +670,11 @@ void KFileItemModel::onSortRoleChanged(const QByteArray& current, const QByteArr Q_UNUSED(previous); m_sortRole = typeForRole(current); -#ifdef KFILEITEMMODEL_DEBUG if (!m_requestRole[m_sortRole]) { - kWarning() << "The sort-role has been changed to a role that has not been received yet"; + QSet newRoles = m_roles; + newRoles << current; + setRoles(newRoles); } -#endif resortAllItems(); } @@ -685,7 +711,6 @@ void KFileItemModel::resortAllItems() oldUrls.append(itemData->item.url()); } - m_groups.clear(); m_items.clear(); // Resort the items @@ -694,20 +719,45 @@ void KFileItemModel::resortAllItems() m_items.insert(m_itemData.at(i)->item.url(), i); } - // Determine the indexes that have been moved - QList movedToIndexes; - movedToIndexes.reserve(itemCount); - for (int i = 0; i < itemCount; i++) { - const int newIndex = m_items.value(oldUrls.at(i).url()); - movedToIndexes.append(newIndex); + // Determine the first index that has been moved. + int firstMovedIndex = 0; + while (firstMovedIndex < itemCount + && firstMovedIndex == m_items.value(oldUrls.at(firstMovedIndex))) { + ++firstMovedIndex; } - // 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); + const bool itemsHaveMoved = firstMovedIndex < itemCount; + if (itemsHaveMoved) { + m_groups.clear(); + + int lastMovedIndex = itemCount - 1; + while (lastMovedIndex > firstMovedIndex + && lastMovedIndex == m_items.value(oldUrls.at(lastMovedIndex))) { + --lastMovedIndex; + } + + Q_ASSERT(firstMovedIndex <= lastMovedIndex); + + // Create a list movedToIndexes, which has the property that + // movedToIndexes[i] is the new index of the item with the old index + // firstMovedIndex + i. + const int movedItemsCount = lastMovedIndex - firstMovedIndex + 1; + QList movedToIndexes; + movedToIndexes.reserve(movedItemsCount); + for (int i = firstMovedIndex; i <= lastMovedIndex; ++i) { + const int newIndex = m_items.value(oldUrls.at(i)); + movedToIndexes.append(newIndex); + } + + emit itemsMoved(KItemRange(firstMovedIndex, movedItemsCount), movedToIndexes); + } else if (groupedSorting()) { + // The groups might have changed even if the order of the items has not. + const QList > oldGroups = m_groups; + m_groups.clear(); + if (groups() != oldGroups) { + emit groupsChanged(); + } + } #ifdef KFILEITEMMODEL_DEBUG kDebug() << "[TIME] Resorting of" << itemCount << "items:" << timer.elapsed(); @@ -755,14 +805,15 @@ void KFileItemModel::slotItemsAdded(const KUrl& directoryUrl, const KFileItemLis { Q_ASSERT(!items.isEmpty()); - KUrl parentUrl = directoryUrl; - parentUrl.adjustPath(KUrl::RemoveTrailingSlash); + KUrl parentUrl; + if (m_expandedDirs.contains(directoryUrl)) { + parentUrl = m_expandedDirs.value(directoryUrl); + } else { + parentUrl = directoryUrl; + parentUrl.adjustPath(KUrl::RemoveTrailingSlash); + } if (m_requestRole[ExpandedParentsCountRole]) { - // 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 @@ -776,6 +827,12 @@ void KFileItemModel::slotItemsAdded(const KUrl& directoryUrl, const KFileItemLis return; } + if (directoryUrl != directory()) { + // 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(); + } + // 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. @@ -846,12 +903,12 @@ void KFileItemModel::slotRefreshItems(const QList >& kDebug() << "Refreshing" << items.count() << "items"; #endif - m_groups.clear(); - // Get the indexes of all items that have been refreshed QList indexes; indexes.reserve(items.count()); + QSet changedRoles; + QListIterator > it(items); while (it.hasNext()) { const QPair& itemPair = it.next(); @@ -864,9 +921,14 @@ void KFileItemModel::slotRefreshItems(const QList >& // Keep old values as long as possible if they could not retrieved synchronously yet. // The update of the values will be done asynchronously by KFileItemModelRolesUpdater. QHashIterator it(retrieveData(newItem, m_itemData.at(index)->parent)); + QHash& values = m_itemData[index]->values; while (it.hasNext()) { it.next(); - m_itemData[index]->values.insert(it.key(), it.value()); + const QByteArray& role = it.key(); + if (values.value(role) != it.value()) { + values.insert(role, it.value()); + changedRoles.insert(role); + } } m_items.remove(oldItem.url()); @@ -907,9 +969,11 @@ void KFileItemModel::slotRefreshItems(const QList >& itemRangeList.append(KItemRange(rangeIndex, rangeCount)); } - emit itemsChanged(itemRangeList, m_roles); + emit itemsChanged(itemRangeList, changedRoles); - resortAllItems(); + if (changedRoles.contains(sortRole())) { + resortAllItems(); + } } void KFileItemModel::slotClear() @@ -924,6 +988,8 @@ void KFileItemModel::slotClear() m_maximumUpdateIntervalTimer->stop(); m_resortAllItemsTimer->stop(); + + qDeleteAll(m_pendingItemsToInsert); m_pendingItemsToInsert.clear(); const int removedCount = m_itemData.count(); @@ -1252,28 +1318,23 @@ QHash KFileItemModel::retrieveData(const KFileItem& item, // KFileItem::iconName() can be very expensive if the MIME-type is unknown // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater. QHash data; - data.insert("url", item.url()); + data.insert(sharedValue("url"), item.url()); const bool isDir = item.isDir(); - if (m_requestRole[IsDirRole]) { - data.insert("isDir", isDir); + if (m_requestRole[IsDirRole] && isDir) { + data.insert(sharedValue("isDir"), true); } - if (m_requestRole[IsLinkRole]) { - const bool isLink = item.isLink(); - data.insert("isLink", isLink); + if (m_requestRole[IsLinkRole] && item.isLink()) { + data.insert(sharedValue("isLink"), true); } if (m_requestRole[NameRole]) { - data.insert("text", item.text()); + data.insert(sharedValue("text"), item.text()); } - if (m_requestRole[SizeRole]) { - if (isDir) { - data.insert("size", QVariant()); - } else { - data.insert("size", item.size()); - } + if (m_requestRole[SizeRole] && !isDir) { + data.insert(sharedValue("size"), item.size()); } if (m_requestRole[DateRole]) { @@ -1281,19 +1342,19 @@ QHash KFileItemModel::retrieveData(const KFileItem& item, // having several thousands of items. Instead the formatting of the // date-time will be done on-demand by the view when the date will be shown. const KDateTime dateTime = item.time(KFileItem::ModificationTime); - data.insert("date", dateTime.dateTime()); + data.insert(sharedValue("date"), dateTime.dateTime()); } if (m_requestRole[PermissionsRole]) { - data.insert("permissions", item.permissionsString()); + data.insert(sharedValue("permissions"), item.permissionsString()); } if (m_requestRole[OwnerRole]) { - data.insert("owner", item.user()); + data.insert(sharedValue("owner"), item.user()); } if (m_requestRole[GroupRole]) { - data.insert("group", item.group()); + data.insert(sharedValue("group"), item.group()); } if (m_requestRole[DestinationRole]) { @@ -1301,7 +1362,7 @@ QHash KFileItemModel::retrieveData(const KFileItem& item, if (destination.isEmpty()) { destination = QLatin1String("-"); } - data.insert("destination", destination); + data.insert(sharedValue("destination"), destination); } if (m_requestRole[PathRole]) { @@ -1324,27 +1385,25 @@ QHash KFileItemModel::retrieveData(const KFileItem& item, const int index = path.lastIndexOf(item.text()); path = path.mid(0, index - 1); - data.insert("path", path); + data.insert(sharedValue("path"), path); } - if (m_requestRole[IsExpandableRole]) { - data.insert("isExpandable", item.isDir() && item.url() == item.targetUrl()); + if (m_requestRole[IsExpandableRole] && isDir) { + data.insert(sharedValue("isExpandable"), true); } if (m_requestRole[ExpandedParentsCountRole]) { - int level = 0; if (parent) { - level = parent->values["expandedParentsCount"].toInt() + 1; + const int level = parent->values["expandedParentsCount"].toInt() + 1; + data.insert(sharedValue("expandedParentsCount"), level); } - - data.insert("expandedParentsCount", level); } if (item.isMimeTypeKnown()) { - data.insert("iconName", item.iconName()); + data.insert(sharedValue("iconName"), item.iconName()); if (m_requestRole[TypeRole]) { - data.insert("type", item.mimeComment()); + data.insert(sharedValue("type"), item.mimeComment()); } } @@ -1961,6 +2020,19 @@ void KFileItemModel::determineMimeTypes(const KFileItemList& items, int timeout) } } +QByteArray KFileItemModel::sharedValue(const QByteArray& value) +{ + static QSet pool; + const QSet::const_iterator it = pool.constFind(value); + + if (it != pool.constEnd()) { + return *it; + } else { + pool.insert(value); + return value; + } +} + bool KFileItemModel::isConsistent() const { if (m_items.count() != m_itemData.count()) {