X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/d062256db2c0590b7628ad7dcb9e720a5ef4f4a9..756c648f62d03749fe464e6bb0b3d3595a4ced99:/src/kitemviews/kfileitemmodel.cpp diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 60f1c72ce..04e3c8ca7 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -38,22 +38,27 @@ KFileItemModel::KFileItemModel(KDirLister* dirLister, QObject* parent) : m_sortRole(NameRole), m_roles(), m_caseSensitivity(Qt::CaseInsensitive), - m_sortedItems(), + m_itemData(), m_items(), - m_data(), + m_filter(), + m_filteredItems(), m_requestRole(), m_minimumUpdateIntervalTimer(0), m_maximumUpdateIntervalTimer(0), + m_resortAllItemsTimer(0), m_pendingItemsToInsert(), m_pendingEmitLoadingCompleted(false), m_groups(), - m_rootExpansionLevel(-1), + m_rootExpansionLevel(UninitializedRootExpansionLevel), m_expandedUrls(), - m_restoredExpandedUrls() + m_urlsToExpand() { + // Apply default roles that should be determined resetRoles(); m_requestRole[NameRole] = true; m_requestRole[IsDirRole] = true; + m_roles.insert("name"); + m_roles.insert("isDir"); Q_ASSERT(dirLister); @@ -79,53 +84,72 @@ KFileItemModel::KFileItemModel(KDirLister* dirLister, QObject* parent) : m_maximumUpdateIntervalTimer->setInterval(2000); m_maximumUpdateIntervalTimer->setSingleShot(true); connect(m_maximumUpdateIntervalTimer, SIGNAL(timeout()), this, SLOT(dispatchPendingItemsToInsert())); + + // When changing the value of an item which represents the sort-role a resorting must be + // triggered. Especially in combination with KFileItemModelRolesUpdater this might be done + // for a lot of items within a quite small timeslot. To prevent expensive resortings the + // resorting is postponed until the timer has been exceeded. + m_resortAllItemsTimer = new QTimer(this); + m_resortAllItemsTimer->setInterval(500); + m_resortAllItemsTimer->setSingleShot(true); + connect(m_resortAllItemsTimer, SIGNAL(timeout()), this, SLOT(resortAllItems())); Q_ASSERT(m_minimumUpdateIntervalTimer->interval() <= m_maximumUpdateIntervalTimer->interval()); } KFileItemModel::~KFileItemModel() { + qDeleteAll(m_itemData); + m_itemData.clear(); } int KFileItemModel::count() const { - return m_data.count(); + return m_itemData.count(); } QHash KFileItemModel::data(int index) const { if (index >= 0 && index < count()) { - return m_data.at(index); + return m_itemData.at(index)->values; } return QHash(); } bool KFileItemModel::setData(int index, const QHash& values) { - if (index >= 0 && index < count()) { - QHash currentValue = m_data.at(index); + if (index < 0 || index >= count()) { + return false; + } - QSet changedRoles; - QHashIterator it(values); - while (it.hasNext()) { - it.next(); - const QByteArray role = it.key(); - const QVariant value = it.value(); + QHash currentValues = m_itemData.at(index)->values; - if (currentValue[role] != value) { - currentValue[role] = value; - changedRoles.insert(role); - } - } + // Determine which roles have been changed + QSet changedRoles; + QHashIterator it(values); + while (it.hasNext()) { + it.next(); + const QByteArray role = it.key(); + const QVariant value = it.value(); - if (!changedRoles.isEmpty()) { - m_data[index] = currentValue; - emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); + if (currentValues[role] != value) { + currentValues[role] = value; + changedRoles.insert(role); } + } - return true; + if (changedRoles.isEmpty()) { + return false; } - return false; + + m_itemData[index]->values = currentValues; + emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); + + if (changedRoles.contains(sortRole())) { + m_resortAllItemsTimer->start(); + } + + return true; } void KFileItemModel::setSortFoldersFirst(bool foldersFirst) @@ -141,6 +165,24 @@ bool KFileItemModel::sortFoldersFirst() const return m_sortFoldersFirst; } +void KFileItemModel::setShowHiddenFiles(bool show) +{ + KDirLister* dirLister = m_dirLister.data(); + if (dirLister) { + dirLister->setShowingDotFiles(show); + dirLister->emitChanges(); + if (show) { + slotCompleted(); + } + } +} + +bool KFileItemModel::showHiddenFiles() const +{ + const KDirLister* dirLister = m_dirLister.data(); + return dirLister ? dirLister->showingDotFiles() : false; +} + QMimeData* KFileItemModel::createMimeData(const QSet& indexes) const { QMimeData* data = new QMimeData(); @@ -215,6 +257,9 @@ QString KFileItemModel::roleDescription(const QByteArray& role) const case TypeRole: descr = i18nc("@item:intable", "Type"); break; case DestinationRole: descr = i18nc("@item:intable", "Destination"); break; case PathRole: descr = i18nc("@item:intable", "Path"); break; + case CommentRole: descr = i18nc("@item:intable", "Comment"); break; + case TagsRole: descr = i18nc("@item:intable", "Tags"); break; + case RatingRole: descr = i18nc("@item:intable", "Rating"); break; case NoRole: break; case IsDirRole: break; case IsExpandedRole: break; @@ -227,7 +272,7 @@ QString KFileItemModel::roleDescription(const QByteArray& role) const QList > KFileItemModel::groups() const { - if (!m_data.isEmpty() && m_groups.isEmpty()) { + if (!m_itemData.isEmpty() && m_groups.isEmpty()) { #ifdef KFILEITEMMODEL_DEBUG QElapsedTimer timer; timer.start(); @@ -242,6 +287,9 @@ QList > KFileItemModel::groups() const case TypeRole: m_groups = genericStringRoleGroups("type"); break; case DestinationRole: m_groups = genericStringRoleGroups("destination"); break; case PathRole: m_groups = genericStringRoleGroups("path"); break; + case CommentRole: m_groups = genericStringRoleGroups("comment"); break; + case TagsRole: m_groups = genericStringRoleGroups("tags"); break; + case RatingRole: m_groups = ratingRoleGroups(); break; case NoRole: break; case IsDirRole: break; case IsExpandedRole: break; @@ -260,7 +308,7 @@ QList > KFileItemModel::groups() const KFileItem KFileItemModel::fileItem(int index) const { if (index >= 0 && index < count()) { - return m_sortedItems.at(index); + return m_itemData.at(index)->item; } return KFileItem(); @@ -270,7 +318,7 @@ KFileItem KFileItemModel::fileItem(const KUrl& url) const { const int index = m_items.value(url, -1); if (index >= 0) { - return m_sortedItems.at(index); + return m_itemData.at(index)->item; } return KFileItem(); } @@ -331,7 +379,7 @@ void KFileItemModel::setRoles(const QSet& roles) // Update m_data with the changed requested roles const int maxIndex = count() - 1; for (int i = 0; i <= maxIndex; ++i) { - m_data[i] = retrieveData(m_sortedItems.at(i)); + m_itemData[i]->values = retrieveData(m_itemData.at(i)->item); } kWarning() << "TODO: Emitting itemsChanged() with no information what has changed!"; @@ -356,11 +404,11 @@ bool KFileItemModel::setExpanded(int index, bool expanded) return false; } - const KUrl url = m_sortedItems.at(index).url(); + KDirLister* dirLister = m_dirLister.data(); + const KUrl url = m_itemData.at(index)->item.url(); if (expanded) { m_expandedUrls.insert(url); - KDirLister* dirLister = m_dirLister.data(); if (dirLister) { dirLister->openUrl(url, KDirLister::Keep); return true; @@ -368,11 +416,15 @@ bool KFileItemModel::setExpanded(int index, bool expanded) } else { m_expandedUrls.remove(url); + if (dirLister) { + dirLister->stop(url); + } + KFileItemList itemsToRemove; const int expansionLevel = data(index)["expansionLevel"].toInt(); ++index; while (index < count() && data(index)["expansionLevel"].toInt() > expansionLevel) { - itemsToRemove.append(m_sortedItems.at(index)); + itemsToRemove.append(m_itemData.at(index)->item); ++index; } removeItems(itemsToRemove); @@ -385,7 +437,7 @@ bool KFileItemModel::setExpanded(int index, bool expanded) bool KFileItemModel::isExpanded(int index) const { if (index >= 0 && index < count()) { - return m_data.at(index).value("isExpanded").toBool(); + return m_itemData.at(index)->values.value("isExpanded").toBool(); } return false; } @@ -393,7 +445,7 @@ bool KFileItemModel::isExpanded(int index) const bool KFileItemModel::isExpandable(int index) const { if (index >= 0 && index < count()) { - return m_sortedItems.at(index).isDir(); + return m_itemData.at(index)->item.isDir(); } return false; } @@ -405,7 +457,92 @@ QSet KFileItemModel::expandedUrls() const void KFileItemModel::restoreExpandedUrls(const QSet& urls) { - m_restoredExpandedUrls = urls; + m_urlsToExpand = urls; +} + +void KFileItemModel::setExpanded(const QSet& urls) +{ + + const KDirLister* dirLister = m_dirLister.data(); + if (!dirLister) { + return; + } + + const int pos = dirLister->url().url().length(); + + // Assure that each sub-path of the URLs that should be + // expanded is added to m_urlsToExpand too. KDirLister + // does not care whether the parent-URL has already been + // expanded. + QSetIterator it1(urls); + while (it1.hasNext()) { + const KUrl& url = it1.next(); + + KUrl urlToExpand = dirLister->url(); + const QStringList subDirs = url.url().mid(pos).split(QDir::separator()); + for (int i = 0; i < subDirs.count(); ++i) { + urlToExpand.addPath(subDirs.at(i)); + m_urlsToExpand.insert(urlToExpand); + } + } + + // KDirLister::open() must called at least once to trigger an initial + // loading. The pending URLs that must be restored are handled + // in slotCompleted(). + QSetIterator it2(m_urlsToExpand); + while (it2.hasNext()) { + const int idx = index(it2.next()); + if (idx >= 0 && !isExpanded(idx)) { + setExpanded(idx, true); + break; + } + } +} + +void KFileItemModel::setNameFilter(const QString& nameFilter) +{ + if (m_filter.pattern() != nameFilter) { + dispatchPendingItemsToInsert(); + + m_filter.setPattern(nameFilter); + + // Check which shown items from m_itemData must get + // hidden and hence moved to m_filteredItems. + KFileItemList newFilteredItems; + + foreach (ItemData* itemData, m_itemData) { + if (!m_filter.matches(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); + } + } + } + + removeItems(newFilteredItems); + + // Check which hidden items from m_filteredItems should + // get visible again and hence removed from m_filteredItems. + KFileItemList newVisibleItems; + + QMutableSetIterator it(m_filteredItems); + while (it.hasNext()) { + const KFileItem item = it.next(); + if (m_filter.matches(item)) { + newVisibleItems.append(item); + m_filteredItems.remove(item); + } + } + + insertItems(newVisibleItems); + } +} + +QString KFileItemModel::nameFilter() const +{ + return m_filter.pattern(); } void KFileItemModel::onGroupedSortingChanged(bool current) @@ -432,12 +569,67 @@ void KFileItemModel::onSortOrderChanged(Qt::SortOrder current, Qt::SortOrder pre { Q_UNUSED(current); Q_UNUSED(previous); - resortAllItems(); + resortAllItems(); +} + +void KFileItemModel::resortAllItems() +{ + m_resortAllItemsTimer->stop(); + + const int itemCount = count(); + if (itemCount <= 0) { + return; + } + +#ifdef KFILEITEMMODEL_DEBUG + QElapsedTimer timer; + timer.start(); + kDebug() << "==========================================================="; + kDebug() << "Resorting" << itemCount << "items"; +#endif + + // Remember the order of the current URLs so + // that it can be determined which indexes have + // been moved because of the resorting. + QList oldUrls; + oldUrls.reserve(itemCount); + foreach (const ItemData* itemData, m_itemData) { + oldUrls.append(itemData->item.url()); + } + + m_groups.clear(); + m_items.clear(); + + // Resort the items + sort(m_itemData.begin(), m_itemData.end()); + for (int i = 0; i < itemCount; ++i) { + m_items.insert(m_itemData.at(i)->item.url(), i); + } + + // Determine the indexes that have been moved + bool emitItemsMoved = false; + 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); + if (!emitItemsMoved && newIndex != i) { + emitItemsMoved = true; + } + } + + if (emitItemsMoved) { + emit itemsMoved(KItemRange(0, itemCount), movedToIndexes); + } + +#ifdef KFILEITEMMODEL_DEBUG + kDebug() << "[TIME] Resorting of" << itemCount << "items:" << timer.elapsed(); +#endif } void KFileItemModel::slotCompleted() { - if (m_restoredExpandedUrls.isEmpty() && m_minimumUpdateIntervalTimer->isActive()) { + if (m_urlsToExpand.isEmpty() && m_minimumUpdateIntervalTimer->isActive()) { // dispatchPendingItems() will be called when the timer // has been expired. m_pendingEmitLoadingCompleted = true; @@ -447,25 +639,26 @@ void KFileItemModel::slotCompleted() m_pendingEmitLoadingCompleted = false; dispatchPendingItemsToInsert(); - if (!m_restoredExpandedUrls.isEmpty()) { + if (!m_urlsToExpand.isEmpty()) { // Try to find a URL that can be expanded. // Note that the parent folder must be expanded before any of its subfolders become visible. // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet // -> we expand the first visible URL we find in m_restoredExpandedUrls. - foreach(const KUrl& url, m_restoredExpandedUrls) { + foreach(const KUrl& url, m_urlsToExpand) { const int index = m_items.value(url, -1); if (index >= 0) { - // We have found an expandable URL. Expand it and return - when - // the dir lister has finished, this slot will be called again. - m_restoredExpandedUrls.remove(url); - setExpanded(index, true); - return; + m_urlsToExpand.remove(url); + if (setExpanded(index, true)) { + // The dir lister has been triggered. This slot will be called + // again after the directory has been expanded. + return; + } } } // None of the URLs in m_restoredExpandedUrls could be found in the model. This can happen // if these URLs have been deleted in the meantime. - m_restoredExpandedUrls.clear(); + m_urlsToExpand.clear(); } emit loadingCompleted(); @@ -481,7 +674,39 @@ void KFileItemModel::slotCanceled() void KFileItemModel::slotNewItems(const KFileItemList& items) { - m_pendingItemsToInsert.append(items); + 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; + } + } + } + + if (m_filter.pattern().isEmpty()) { + m_pendingItemsToInsert.append(items); + } else { + // The name-filter is active. Hide filtered items + // before inserting them into the model and remember + // the filtered items in m_filteredItems. + KFileItemList filteredItems; + foreach (const KFileItem& item, items) { + if (m_filter.matches(item)) { + filteredItems.append(item); + } else { + m_filteredItems.insert(item); + } + } + + m_pendingItemsToInsert.append(filteredItems); + } if (useMaximumUpdateInterval() && !m_maximumUpdateIntervalTimer->isActive()) { // Assure that items get dispatched if no completed() or canceled() signal is @@ -492,10 +717,14 @@ void KFileItemModel::slotNewItems(const KFileItemList& items) void KFileItemModel::slotItemsDeleted(const KFileItemList& items) { - if (!m_pendingItemsToInsert.isEmpty()) { - insertItems(m_pendingItemsToInsert); - m_pendingItemsToInsert.clear(); + dispatchPendingItemsToInsert(); + + if (!m_filteredItems.isEmpty()) { + foreach (const KFileItem& item, items) { + m_filteredItems.remove(item); + } } + removeItems(items); } @@ -515,8 +744,14 @@ void KFileItemModel::slotRefreshItems(const QList >& QListIterator > it(items); while (it.hasNext()) { const QPair& itemPair = it.next(); - const int index = m_items.value(itemPair.second.url(), -1); + const KFileItem& oldItem = itemPair.first; + const KFileItem& newItem = itemPair.second; + const int index = m_items.value(oldItem.url(), -1); if (index >= 0) { + m_itemData[index]->item = newItem; + m_itemData[index]->values = retrieveData(newItem); + m_items.remove(oldItem.url()); + m_items.insert(newItem.url(), index); indexes.append(index); } } @@ -531,9 +766,9 @@ void KFileItemModel::slotRefreshItems(const QList >& qSort(indexes); KItemRangeList itemRangeList; - int rangeIndex = 0; - int rangeCount = 1; int previousIndex = indexes.at(0); + int rangeIndex = previousIndex; + int rangeCount = 1; const int maxIndex = indexes.count() - 1; for (int i = 1; i <= maxIndex; ++i) { @@ -553,7 +788,9 @@ void KFileItemModel::slotRefreshItems(const QList >& itemRangeList.append(KItemRange(rangeIndex, rangeCount)); } - emit itemsChanged(itemRangeList, QSet()); + emit itemsChanged(itemRangeList, m_roles); + + resortAllItems(); } void KFileItemModel::slotClear() @@ -562,19 +799,21 @@ void KFileItemModel::slotClear() kDebug() << "Clearing all items"; #endif + m_filteredItems.clear(); m_groups.clear(); m_minimumUpdateIntervalTimer->stop(); m_maximumUpdateIntervalTimer->stop(); + m_resortAllItemsTimer->stop(); m_pendingItemsToInsert.clear(); - m_rootExpansionLevel = -1; + m_rootExpansionLevel = UninitializedRootExpansionLevel; - const int removedCount = m_data.count(); + const int removedCount = m_itemData.count(); if (removedCount > 0) { - m_sortedItems.clear(); + qDeleteAll(m_itemData); + m_itemData.clear(); m_items.clear(); - m_data.clear(); emit itemsRemoved(KItemRangeList() << KItemRange(0, removedCount)); } @@ -613,7 +852,7 @@ void KFileItemModel::insertItems(const KFileItemList& items) m_groups.clear(); - KFileItemList sortedItems = items; + QList sortedItems = createItemDataList(items); sort(sortedItems.begin(), sortedItems.end()); #ifdef KFILEITEMMODEL_DEBUG @@ -623,15 +862,15 @@ void KFileItemModel::insertItems(const KFileItemList& items) KItemRangeList itemRanges; int targetIndex = 0; int sourceIndex = 0; - int insertedAtIndex = -1; // Index for the current item-range - int insertedCount = 0; // Count for the current item-range - int previouslyInsertedCount = 0; // Sum of previously inserted items for all ranges + int insertedAtIndex = -1; // Index for the current item-range + int insertedCount = 0; // Count for the current item-range + int previouslyInsertedCount = 0; // Sum of previously inserted items for all ranges while (sourceIndex < sortedItems.count()) { // Find target index from m_items to insert the current item // in a sorted order const int previousTargetIndex = targetIndex; - while (targetIndex < m_sortedItems.count()) { - if (!lessThan(m_sortedItems.at(targetIndex), sortedItems.at(sourceIndex))) { + while (targetIndex < m_itemData.count()) { + if (!lessThan(m_itemData.at(targetIndex), sortedItems.at(sourceIndex))) { break; } ++targetIndex; @@ -644,11 +883,10 @@ void KFileItemModel::insertItems(const KFileItemList& items) insertedCount = 0; } - // Insert item at the position targetIndex - const KFileItem item = sortedItems.at(sourceIndex); - m_sortedItems.insert(targetIndex, item); - m_data.insert(targetIndex, retrieveData(item)); + // Insert item at the position targetIndex by transfering + // the ownership of the item-data from sortedItems to m_itemData. // m_items will be inserted after the loop (see comment below) + m_itemData.insert(targetIndex, sortedItems.at(sourceIndex)); ++insertedCount; if (insertedAtIndex < 0) { @@ -661,8 +899,9 @@ void KFileItemModel::insertItems(const KFileItemList& items) // The indexes of all m_items must be adjusted, not only the index // of the new items - for (int i = 0; i < m_sortedItems.count(); ++i) { - m_items.insert(m_sortedItems.at(i).url(), i); + const int itemDataCount = m_itemData.count(); + for (int i = 0; i < itemDataCount; ++i) { + m_items.insert(m_itemData.at(i)->item.url(), i); } itemRanges << KItemRange(insertedAtIndex, insertedCount); @@ -685,7 +924,14 @@ void KFileItemModel::removeItems(const KFileItemList& items) m_groups.clear(); - KFileItemList sortedItems = items; + QList sortedItems; + sortedItems.reserve(items.count()); + foreach (const KFileItem& item, items) { + const int index = m_items.value(item.url(), -1); + if (index >= 0) { + sortedItems.append(m_itemData.at(index)); + } + } sort(sortedItems.begin(), sortedItems.end()); QList indexesToRemove; @@ -696,15 +942,17 @@ void KFileItemModel::removeItems(const KFileItemList& items) int removedAtIndex = -1; int removedCount = 0; int targetIndex = 0; - foreach (const KFileItem& itemToRemove, sortedItems) { + foreach (const ItemData* itemData, sortedItems) { + const KFileItem& itemToRemove = itemData->item; + const int previousTargetIndex = targetIndex; - while (targetIndex < m_sortedItems.count()) { - if (m_sortedItems.at(targetIndex).url() == itemToRemove.url()) { + while (targetIndex < m_itemData.count()) { + if (m_itemData.at(targetIndex)->item.url() == itemToRemove.url()) { break; } ++targetIndex; } - if (targetIndex >= m_sortedItems.count()) { + if (targetIndex >= m_itemData.count()) { kWarning() << "Item that should be deleted has not been found!"; return; } @@ -726,76 +974,68 @@ void KFileItemModel::removeItems(const KFileItemList& items) // Delete the items for (int i = indexesToRemove.count() - 1; i >= 0; --i) { const int indexToRemove = indexesToRemove.at(i); - m_items.remove(m_sortedItems.at(indexToRemove).url()); - m_sortedItems.removeAt(indexToRemove); - m_data.removeAt(indexToRemove); + ItemData* data = m_itemData.at(indexToRemove); + + m_items.remove(data->item.url()); + + delete data; + m_itemData.removeAt(indexToRemove); } // The indexes of all m_items must be adjusted, not only the index // of the removed items - for (int i = 0; i < m_sortedItems.count(); ++i) { - m_items.insert(m_sortedItems.at(i).url(), i); + const int itemDataCount = m_itemData.count(); + for (int i = 0; i < itemDataCount; ++i) { + m_items.insert(m_itemData.at(i)->item.url(), i); } if (count() <= 0) { - m_rootExpansionLevel = -1; + m_rootExpansionLevel = UninitializedRootExpansionLevel; } itemRanges << KItemRange(removedAtIndex, removedCount); emit itemsRemoved(itemRanges); } -void KFileItemModel::resortAllItems() +QList KFileItemModel::createItemDataList(const KFileItemList& items) const { - const int itemCount = count(); - if (itemCount <= 0) { - return; - } - - const KFileItemList oldSortedItems = m_sortedItems; - const QHash oldItems = m_items; - const QList > oldData = m_data; - - m_groups.clear(); - m_items.clear(); - m_data.clear(); - - sort(m_sortedItems.begin(), m_sortedItems.end()); - int index = 0; - foreach (const KFileItem& item, m_sortedItems) { - m_items.insert(item.url(), index); - - const int oldItemIndex = oldItems.value(item.url()); - m_data.append(oldData.at(oldItemIndex)); - - ++index; - } - - bool emitItemsMoved = false; - QList movedToIndexes; - movedToIndexes.reserve(m_sortedItems.count()); - for (int i = 0; i < itemCount; i++) { - const int newIndex = m_items.value(oldSortedItems.at(i).url()); - movedToIndexes.append(newIndex); - if (!emitItemsMoved && newIndex != i) { - emitItemsMoved = true; + QList itemDataList; + itemDataList.reserve(items.count()); + + foreach (const KFileItem& item, items) { + 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(); + } } - } - if (emitItemsMoved) { - emit itemsMoved(KItemRange(0, itemCount), movedToIndexes); + itemDataList.append(itemData); } + + return itemDataList; } void KFileItemModel::removeExpandedItems() { KFileItemList expandedItems; - const int maxIndex = m_data.count() - 1; + const int maxIndex = m_itemData.count() - 1; for (int i = 0; i <= maxIndex; ++i) { - if (m_data.at(i).value("expansionLevel").toInt() > 0) { - const KFileItem fileItem = m_sortedItems.at(i); - expandedItems.append(fileItem); + const ItemData* itemData = m_itemData.at(i); + if (itemData->values.value("expansionLevel").toInt() > 0) { + expandedItems.append(itemData->item); } } @@ -804,7 +1044,7 @@ void KFileItemModel::removeExpandedItems() Q_ASSERT(m_rootExpansionLevel >= 0); removeItems(expandedItems); - m_rootExpansionLevel = -1; + m_rootExpansionLevel = UninitializedRootExpansionLevel; m_expandedUrls.clear(); } @@ -828,6 +1068,9 @@ KFileItemModel::Role KFileItemModel::roleIndex(const QByteArray& role) const rolesHash.insert("type", TypeRole); rolesHash.insert("destination", DestinationRole); rolesHash.insert("path", PathRole); + rolesHash.insert("comment", CommentRole); + rolesHash.insert("tags", TagsRole); + rolesHash.insert("rating", RatingRole); rolesHash.insert("isDir", IsDirRole); rolesHash.insert("isExpanded", IsExpandedRole); rolesHash.insert("expansionLevel", ExpansionLevelRole); @@ -836,12 +1079,13 @@ KFileItemModel::Role KFileItemModel::roleIndex(const QByteArray& role) const } QHash KFileItemModel::retrieveData(const KFileItem& item) const -{ +{ // It is important to insert only roles that are fast to retrieve. E.g. // KFileItem::iconName() can be very expensive if the MIME-type is unknown // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater. QHash data; data.insert("iconPixmap", QPixmap()); + data.insert("url", item.url()); const bool isDir = item.isDir(); if (m_requestRole[IsDirRole]) { @@ -849,7 +1093,7 @@ QHash KFileItemModel::retrieveData(const KFileItem& item) } if (m_requestRole[NameRole]) { - data.insert("name", item.name()); + data.insert("name", item.text()); } if (m_requestRole[SizeRole]) { @@ -897,16 +1141,29 @@ QHash KFileItemModel::retrieveData(const KFileItem& item) } if (m_requestRole[ExpansionLevelRole]) { - if (m_rootExpansionLevel < 0) { - KDirLister* dirLister = m_dirLister.data(); - if (dirLister) { - const QString rootDir = dirLister->url().directory(KUrl::AppendTrailingSlash); + if (m_rootExpansionLevel == UninitializedRootExpansionLevel && m_dirLister.data()) { + const KUrl rootUrl = m_dirLister.data()->url(); + const QString protocol = rootUrl.protocol(); + const bool isSearchUrl = (protocol.contains("search") || protocol == QLatin1String("nepomuk")); + if (isSearchUrl) { + 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()) { @@ -920,7 +1177,7 @@ QHash KFileItemModel::retrieveData(const KFileItem& item) return data; } -bool KFileItemModel::lessThan(const KFileItem& a, const KFileItem& b) const +bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const { int result = 0; @@ -933,8 +1190,8 @@ bool KFileItemModel::lessThan(const KFileItem& a, const KFileItem& b) const } if (m_sortFoldersFirst || m_sortRole == SizeRole) { - const bool isDirA = a.isDir(); - const bool isDirB = b.isDir(); + const bool isDirA = a->item.isDir(); + const bool isDirB = b->item.isDir(); if (isDirA && !isDirB) { return true; } else if (!isDirA && isDirB) { @@ -942,20 +1199,26 @@ bool KFileItemModel::lessThan(const KFileItem& a, const KFileItem& b) const } } + result = sortRoleCompare(a, b); + + return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0; +} + +int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const +{ + const KFileItem& itemA = a->item; + const KFileItem& itemB = b->item; + + int result = 0; + switch (m_sortRole) { - case NameRole: { - result = stringCompare(a.text(), b.text()); - if (result == 0) { - // KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used - result = stringCompare(a.name(m_caseSensitivity == Qt::CaseInsensitive), - b.name(m_caseSensitivity == Qt::CaseInsensitive)); - } + case NameRole: + // The name role is handled as default fallback after the switch break; - } case DateRole: { - const KDateTime dateTimeA = a.time(KFileItem::ModificationTime); - const KDateTime dateTimeB = b.time(KFileItem::ModificationTime); + const KDateTime dateTimeA = itemA.time(KFileItem::ModificationTime); + const KDateTime dateTimeB = itemB.time(KFileItem::ModificationTime); if (dateTimeA < dateTimeB) { result = -1; } else if (dateTimeA > dateTimeB) { @@ -965,97 +1228,197 @@ bool KFileItemModel::lessThan(const KFileItem& a, const KFileItem& b) const } case SizeRole: { - // TODO: Implement sorting folders by the number of items inside. - // This is more tricky to get right because this number is retrieved - // asynchronously by KFileItemModelRolesUpdater. - const KIO::filesize_t sizeA = a.size(); - const KIO::filesize_t sizeB = b.size(); - if (sizeA < sizeB) { - result = -1; - } else if (sizeA > sizeB) { - result = +1; + if (itemA.isDir()) { + Q_ASSERT(itemB.isDir()); // see "if (m_sortFoldersFirst || m_sortRole == SizeRole)" above + + const QVariant valueA = a->values.value("size"); + const QVariant valueB = b->values.value("size"); + + if (valueA.isNull()) { + result = -1; + } else if (valueB.isNull()) { + result = +1; + } else { + result = valueA.value() - valueB.value(); + } + } else { + Q_ASSERT(!itemB.isDir()); // see "if (m_sortFoldersFirst || m_sortRole == SizeRole)" above + result = itemA.size() - itemB.size(); } break; } - default: + case TypeRole: { + result = QString::compare(a->values.value("type").toString(), + b->values.value("type").toString()); break; } - if (result == 0) { - // It must be assured that the sort order is always unique even if two values have been - // equal. In this case a comparison of the URL is done which is unique in all cases - // within KDirLister. - result = QString::compare(a.url().url(), b.url().url(), Qt::CaseSensitive); + case CommentRole: { + result = QString::compare(a->values.value("comment").toString(), + b->values.value("comment").toString()); + break; } - return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0; -} + case TagsRole: { + result = QString::compare(a->values.value("tags").toString(), + b->values.value("tags").toString()); + break; + } -void KFileItemModel::sort(const KFileItemList::iterator& startIterator, const KFileItemList::iterator& endIterator) -{ - KFileItemList::iterator start = startIterator; - KFileItemList::iterator end = endIterator; + case RatingRole: { + result = a->values.value("rating").toInt() - b->values.value("rating").toInt(); + break; + } - // The implementation is based on qSortHelper() from qalgorithms.h - // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). - // In opposite to qSort() it allows to use a member-function for the comparison of elements. - while (1) { - int span = int(end - start); - if (span < 2) { - return; - } + default: + break; + } - --end; - KFileItemList::iterator low = start, high = end - 1; - KFileItemList::iterator pivot = start + span / 2; + if (result != 0) { + // The current sort role was sufficient to define an order + return result; + } - if (lessThan(*end, *start)) { - qSwap(*end, *start); - } - if (span == 2) { - return; - } + // Fallback #1: Compare the text of the items + result = stringCompare(itemA.text(), itemB.text()); + if (result != 0) { + return result; + } - if (lessThan(*pivot, *start)) { - qSwap(*pivot, *start); - } - if (lessThan(*end, *pivot)) { - qSwap(*end, *pivot); - } - if (span == 3) { - return; - } + // Fallback #2: KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used + result = stringCompare(itemA.name(m_caseSensitivity == Qt::CaseInsensitive), + itemB.name(m_caseSensitivity == Qt::CaseInsensitive)); + if (result != 0) { + return result; + } - qSwap(*pivot, *end); + // Fallback #3: It must be assured that the sort order is always unique even if two values have been + // equal. In this case a comparison of the URL is done which is unique in all cases + // within KDirLister. + return QString::compare(itemA.url().url(), itemB.url().url(), Qt::CaseSensitive); +} - while (low < high) { - while (low < high && lessThan(*low, *end)) { - ++low; - } +void KFileItemModel::sort(QList::iterator begin, + QList::iterator end) +{ + // The implementation is based on qStableSortHelper() from qalgorithms.h + // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + // In opposite to qStableSort() it allows to use a member-function for the comparison of elements. + + const int span = end - begin; + if (span < 2) { + return; + } + + const QList::iterator middle = begin + span / 2; + sort(begin, middle); + sort(middle, end); + merge(begin, middle, end); +} - while (high > low && lessThan(*end, *high)) { - --high; - } - if (low < high) { - qSwap(*low, *high); - ++low; - --high; - } else { - break; - } +void KFileItemModel::merge(QList::iterator begin, + QList::iterator pivot, + QList::iterator end) +{ + // The implementation is based on qMerge() from qalgorithms.h + // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + + const int len1 = pivot - begin; + const int len2 = end - pivot; + + if (len1 == 0 || len2 == 0) { + return; + } + + if (len1 + len2 == 2) { + if (lessThan(*(begin + 1), *(begin))) { + qSwap(*begin, *(begin + 1)); } + return; + } + + QList::iterator firstCut; + QList::iterator secondCut; + int len2Half; + if (len1 > len2) { + const int len1Half = len1 / 2; + firstCut = begin + len1Half; + secondCut = lowerBound(pivot, end, *firstCut); + len2Half = secondCut - pivot; + } else { + len2Half = len2 / 2; + secondCut = pivot + len2Half; + firstCut = upperBound(begin, pivot, *secondCut); + } + + reverse(firstCut, pivot); + reverse(pivot, secondCut); + reverse(firstCut, secondCut); + + const QList::iterator newPivot = firstCut + len2Half; + merge(begin, firstCut, newPivot); + merge(newPivot, secondCut, end); +} - if (lessThan(*low, *end)) { - ++low; +QList::iterator KFileItemModel::lowerBound(QList::iterator begin, + QList::iterator end, + const ItemData* value) +{ + // The implementation is based on qLowerBound() from qalgorithms.h + // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + + QList::iterator middle; + int n = int(end - begin); + int half; + + while (n > 0) { + half = n >> 1; + middle = begin + half; + if (lessThan(*middle, value)) { + begin = middle + 1; + n -= half + 1; + } else { + n = half; } + } + return begin; +} - qSwap(*end, *low); - sort(start, low); - - start = low + 1; - ++end; +QList::iterator KFileItemModel::upperBound(QList::iterator begin, + QList::iterator end, + const ItemData* value) +{ + // The implementation is based on qUpperBound() from qalgorithms.h + // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + + QList::iterator middle; + int n = end - begin; + int half; + + while (n > 0) { + half = n >> 1; + middle = begin + half; + if (lessThan(value, *middle)) { + n = half; + } else { + begin = middle + 1; + n -= half + 1; + } } + return begin; +} + +void KFileItemModel::reverse(QList::iterator begin, + QList::iterator end) +{ + // The implementation is based on qReverse() from qalgorithms.h + // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + + --end; + while (begin < end) { + qSwap(*begin++, *end--); + } } int KFileItemModel::stringCompare(const QString& a, const QString& b) const @@ -1080,10 +1443,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; @@ -1091,9 +1454,9 @@ int KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& // Check whether one item is the parent of the other item if (urlA.isParentOf(urlB)) { - return -1; + return (sortOrder() == Qt::AscendingOrder) ? -1 : +1; } else if (urlB.isParentOf(urlA)) { - return +1; + return (sortOrder() == Qt::AscendingOrder) ? +1 : -1; } // Determine the maximum common path of both items and @@ -1116,9 +1479,9 @@ 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; @@ -1126,7 +1489,27 @@ int KFileItemModel::expansionLevelsCompare(const KFileItem& a, const KFileItem& return +1; } - return stringCompare(subPathA, subPathB); + // 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 ItemData* parentA = a; + while (parentA && parentA->item.url() != parentUrlA) { + parentA = parentA->parent; + } + + const ItemData* parentB = b; + while (parentB && parentB->item.url() != parentUrlB) { + parentB = parentB->parent; + } + + 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); } QString KFileItemModel::subPath(const KFileItem& item, @@ -1148,7 +1531,7 @@ bool KFileItemModel::useMaximumUpdateInterval() const QList > KFileItemModel::nameRoleGroups() const { - Q_ASSERT(!m_data.isEmpty()); + Q_ASSERT(!m_itemData.isEmpty()); const int maxIndex = count() - 1; QList > groups; @@ -1161,7 +1544,7 @@ QList > KFileItemModel::nameRoleGroups() const continue; } - const QString name = m_data.at(i).value("name").toString(); + const QString name = m_itemData.at(i)->values.value("name").toString(); // Use the first character of the name as group indication QChar newFirstChar = name.at(0).toUpper(); @@ -1211,7 +1594,7 @@ QList > KFileItemModel::nameRoleGroups() const QList > KFileItemModel::sizeRoleGroups() const { - Q_ASSERT(!m_data.isEmpty()); + Q_ASSERT(!m_itemData.isEmpty()); const int maxIndex = count() - 1; QList > groups; @@ -1222,7 +1605,7 @@ QList > KFileItemModel::sizeRoleGroups() const continue; } - const KFileItem& item = m_sortedItems.at(i); + const KFileItem& item = m_itemData.at(i)->item; const KIO::filesize_t fileSize = !item.isNull() ? item.size() : ~0U; QString newGroupValue; if (!item.isNull() && item.isDir()) { @@ -1246,7 +1629,7 @@ QList > KFileItemModel::sizeRoleGroups() const QList > KFileItemModel::dateRoleGroups() const { - Q_ASSERT(!m_data.isEmpty()); + Q_ASSERT(!m_itemData.isEmpty()); const int maxIndex = count() - 1; QList > groups; @@ -1266,7 +1649,7 @@ QList > KFileItemModel::dateRoleGroups() const continue; } - const KDateTime modifiedTime = m_sortedItems.at(i).time(KFileItem::ModificationTime); + const KDateTime modifiedTime = m_itemData.at(i)->item.time(KFileItem::ModificationTime); const QDate modifiedDate = modifiedTime.date(); if (modifiedDate == previousModifiedDate) { // The current item is in the same group as the previous item @@ -1345,7 +1728,7 @@ QList > KFileItemModel::dateRoleGroups() const QList > KFileItemModel::permissionRoleGroups() const { - Q_ASSERT(!m_data.isEmpty()); + Q_ASSERT(!m_itemData.isEmpty()); const int maxIndex = count() - 1; QList > groups; @@ -1357,13 +1740,14 @@ QList > KFileItemModel::permissionRoleGroups() const continue; } - const QString newPermissionsString = m_data.at(i).value("permissions").toString(); + const ItemData* itemData = m_itemData.at(i); + const QString newPermissionsString = itemData->values.value("permissions").toString(); if (newPermissionsString == permissionsString) { continue; } permissionsString = newPermissionsString; - const QFileInfo info(m_sortedItems.at(i).url().pathOrUrl()); + const QFileInfo info(itemData->item.url().pathOrUrl()); // Set user string QString user; @@ -1414,9 +1798,31 @@ QList > KFileItemModel::permissionRoleGroups() const return groups; } +QList > KFileItemModel::ratingRoleGroups() const +{ + Q_ASSERT(!m_itemData.isEmpty()); + + const int maxIndex = count() - 1; + QList > groups; + + int groupValue; + for (int i = 0; i <= maxIndex; ++i) { + if (isChildItem(i)) { + continue; + } + const int newGroupValue = m_itemData.at(i)->values.value("rating").toInt(); + if (newGroupValue != groupValue) { + groupValue = newGroupValue; + groups.append(QPair(i, newGroupValue)); + } + } + + return groups; +} + QList > KFileItemModel::genericStringRoleGroups(const QByteArray& role) const { - Q_ASSERT(!m_data.isEmpty()); + Q_ASSERT(!m_itemData.isEmpty()); const int maxIndex = count() - 1; QList > groups; @@ -1426,7 +1832,7 @@ QList > KFileItemModel::genericStringRoleGroups(const QByte if (isChildItem(i)) { continue; } - const QString newGroupValue = m_data.at(i).value(role).toString(); + const QString newGroupValue = m_itemData.at(i)->values.value(role).toString(); if (newGroupValue != groupValue) { groupValue = newGroupValue; groups.append(QPair(i, newGroupValue));