X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/e348bc58267b3dc06f2fc044f9e5ce5a5dfcd087..aebe7a9010926c4eb2c24e4e5af39e5f3c6a386b:/src/kitemviews/kfileitemmodel.cpp diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 231bfe077..787d36ae6 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -33,6 +33,9 @@ #include #include +#include +#include + // #define KFILEITEMMODEL_DEBUG KFileItemModel::KFileItemModel(QObject* parent) : @@ -120,6 +123,11 @@ 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); + } + m_dirLister->openUrl(url, KDirLister::Reload); } @@ -241,7 +249,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); @@ -438,6 +446,29 @@ bool KFileItemModel::setExpanded(int index, bool expanded) itemsToRemove.append(m_itemData.at(index)->item); ++index; } + + QSet urlsToRemove; + urlsToRemove.reserve(itemsToRemove.count() + 1); + urlsToRemove.insert(url); + foreach (const KFileItem& item, itemsToRemove) { + KUrl url = item.url(); + url.adjustPath(KUrl::RemoveTrailingSlash); + urlsToRemove.insert(url); + } + + QSet::iterator it = m_filteredItems.begin(); + while (it != m_filteredItems.end()) { + const KUrl url = it->url(); + KUrl parentUrl = url.upUrl(); + parentUrl.adjustPath(KUrl::RemoveTrailingSlash); + + if (urlsToRemove.contains(parentUrl)) { + it = m_filteredItems.erase(it); + } else { + ++it; + } + } + removeItems(itemsToRemove); } @@ -613,11 +644,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(); } @@ -795,6 +826,33 @@ void KFileItemModel::slotItemsDeleted(const KFileItemList& items) foreach (const KFileItem& item, itemsToRemove) { m_filteredItems.remove(item); } + + if (m_requestRole[ExpandedParentsCountRole] && m_expandedParentsCountRoot >= 0) { + // Remove all filtered children of deleted items. First, we put the + // deleted URLs into a set to provide fast lookup while iterating + // over m_filteredItems and prevent quadratic complexity if there + // are N removed items and N filtered items. + QSet urlsToRemove; + urlsToRemove.reserve(itemsToRemove.count()); + foreach (const KFileItem& item, itemsToRemove) { + KUrl url = item.url(); + url.adjustPath(KUrl::RemoveTrailingSlash); + urlsToRemove.insert(url); + } + + QSet::iterator it = m_filteredItems.begin(); + while (it != m_filteredItems.end()) { + const KUrl url = it->url(); + KUrl parentUrl = url.upUrl(); + parentUrl.adjustPath(KUrl::RemoveTrailingSlash); + + if (urlsToRemove.contains(parentUrl)) { + it = m_filteredItems.erase(it); + } else { + ++it; + } + } + } } removeItems(itemsToRemove); @@ -1275,10 +1333,6 @@ QHash KFileItemModel::retrieveData(const KFileItem& item) 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()); } @@ -1499,7 +1553,7 @@ int KFileItemModel::expandedParentsCountCompare(const ItemData* a, const ItemDat if (index > maxIndex) { index = maxIndex; } - while ((pathA.at(index) != QLatin1Char('/') || pathB.at(index) != QLatin1Char('/')) && index > 0) { + while (index > 0 && (pathA.at(index) != QLatin1Char('/') || pathB.at(index) != QLatin1Char('/'))) { --index; } @@ -1557,6 +1611,11 @@ bool KFileItemModel::useMaximumUpdateInterval() const return !m_dirLister->url().isLocalFile(); } +static bool localeAwareLessThan(const QChar& c1, const QChar& c2) +{ + return QString::localeAwareCompare(c1, c2) < 0; +} + QList > KFileItemModel::nameRoleGroups() const { Q_ASSERT(!m_itemData.isEmpty()); @@ -1566,7 +1625,6 @@ QList > KFileItemModel::nameRoleGroups() const QString groupValue; QChar firstChar; - bool isLetter = false; for (int i = 0; i <= maxIndex; ++i) { if (isChildItem(i)) { continue; @@ -1582,31 +1640,31 @@ QList > KFileItemModel::nameRoleGroups() const if (firstChar != newFirstChar) { QString newGroupValue; - if (newFirstChar >= QLatin1Char('A') && newFirstChar <= QLatin1Char('Z')) { - // Apply group 'A' - 'Z' - newGroupValue = newFirstChar; - isLetter = true; + if (newFirstChar.isLetter()) { + // Try to find a matching group in the range 'A' to 'Z'. + static std::vector lettersAtoZ; + if (lettersAtoZ.empty()) { + for (char c = 'A'; c <= 'Z'; ++c) { + lettersAtoZ.push_back(QLatin1Char(c)); + } + } + + std::vector::iterator it = std::lower_bound(lettersAtoZ.begin(), lettersAtoZ.end(), newFirstChar, localeAwareLessThan); + if (it != lettersAtoZ.end()) { + if (localeAwareLessThan(newFirstChar, *it) && it != lettersAtoZ.begin()) { + // newFirstChar belongs to the group preceding *it. + // Example: for an umlaut 'A' in the German locale, *it would be 'B' now. + --it; + } + newGroupValue = *it; + } else { + newGroupValue = newFirstChar; + } } else if (newFirstChar >= QLatin1Char('0') && newFirstChar <= QLatin1Char('9')) { // Apply group '0 - 9' for any name that starts with a digit newGroupValue = i18nc("@title:group Groups that start with a digit", "0 - 9"); - isLetter = false; } else { - if (isLetter) { - // If the current group is 'A' - 'Z' check whether a locale character - // fits into the existing group. - // TODO: This does not work in the case if e.g. the group 'O' starts with - // an umlaut 'O' -> provide unit-test to document this known issue - const QChar prevChar(firstChar.unicode() - ushort(1)); - const QChar nextChar(firstChar.unicode() + ushort(1)); - const QString currChar(newFirstChar); - const bool partOfCurrentGroup = currChar.localeAwareCompare(prevChar) > 0 && - currChar.localeAwareCompare(nextChar) < 0; - if (partOfCurrentGroup) { - continue; - } - } newGroupValue = i18nc("@title:group", "Others"); - isLetter = false; } if (newGroupValue != groupValue) {