]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Introduce "isExpandable" role
authorPeter Penz <peter.penz19@gmail.com>
Fri, 23 Dec 2011 22:05:08 +0000 (23:05 +0100)
committerPeter Penz <peter.penz19@gmail.com>
Fri, 23 Dec 2011 22:19:48 +0000 (23:19 +0100)
The role is used to determine whether a directory can be expanded at all. This
is e.g. not the case if a directory has 0 items or the target-URL is different
from the item-URL.

The expansion toggle will get hidden if a directory is not expandable.

CCBUG: 288521

src/kitemviews/kfileitemlistview.cpp
src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemlistwidget.h
src/kitemviews/kfileitemmodel.cpp
src/kitemviews/kfileitemmodel.h
src/kitemviews/kfileitemmodelrolesupdater.cpp
src/kitemviews/kfileitemmodelrolesupdater.h
src/tests/kfileitemmodeltest.cpp

index 89fec9d0f8b28e1afbbb1193784df100951671da..86db28d3d1b26d2d0864d888e8e5bbf4b90bdd65 100644 (file)
@@ -586,6 +586,7 @@ void KFileItemListView::applyRolesToModel()
     roles.insert("isDir");
     if (m_itemLayout == DetailsLayout) {
         roles.insert("isExpanded");
+        roles.insert("isExpandable");
         roles.insert("expansionLevel");
     }
 
index 7a36214b65f20f84e9968c53642dfa326c4c4990..d3e792ea0d7b76324fb96b7c67a0b9698c699c4a 100644 (file)
@@ -44,7 +44,7 @@ KFileItemListWidget::KFileItemListWidget(QGraphicsItem* parent) :
     KItemListWidget(parent),
     m_isCut(false),
     m_isHidden(false),
-    m_isDir(false),
+    m_isExpandable(false),
     m_dirtyLayout(true),
     m_dirtyContent(true),
     m_dirtyContentRoles(),
@@ -95,7 +95,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte
     KItemListWidget::paint(painter, option, widget);
 
     // Draw expansion toggle '>' or 'V'
-    if (m_isDir && !m_expansionArea.isEmpty()) {
+    if (m_isExpandable) {
         QStyleOption arrowOption;
         arrowOption.rect = m_expansionArea.toRect();
         const QStyle::PrimitiveElement arrow = data()["isExpanded"].toBool()
@@ -169,7 +169,7 @@ QRectF KFileItemListWidget::textRect() const
 QRectF KFileItemListWidget::expansionToggleRect() const
 {
     const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
-    return m_isDir ? m_expansionArea : QRectF();
+    return m_isExpandable ? m_expansionArea : QRectF();
 }
 
 QRectF KFileItemListWidget::selectionToggleRect() const
@@ -405,7 +405,7 @@ void KFileItemListWidget::triggerCacheRefreshing()
     refreshCache();
 
     const QHash<QByteArray, QVariant> values = data();
-    m_isDir = values["isDir"].toBool();
+    m_isExpandable = values["isExpandable"].toBool();
     m_isHidden = values["name"].toString().startsWith(QLatin1Char('.'));
 
     updateExpansionArea();
index 8f7397b754b248b950488b0635ef53fa660268c5..91f00fe73f52672e5c23a11ed8e824a99f33b049 100644 (file)
@@ -128,7 +128,7 @@ private:
 private:
     bool m_isCut;
     bool m_isHidden;
-    bool m_isDir;
+    bool m_isExpandable;
 
     bool m_dirtyLayout;
     bool m_dirtyContent;
index b40865f8fee3d7d16b2199d1c09cc76274f1e0b7..baf168ddf1df3050903aec365a2a13bffd172701 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).
@@ -394,7 +394,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 +445,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 +462,6 @@ void KFileItemModel::restoreExpandedUrls(const QSet<KUrl>& urls)
 
 void KFileItemModel::setExpanded(const QSet<KUrl>& urls)
 {
-
     const KDirLister* dirLister = m_dirLister.data();
     if (!dirLister) {
         return;
@@ -1097,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);
@@ -1169,6 +1169,10 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
         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();
index 2992de0658820d19578715066cdd03343f20b41d..07634655ca4d409976e2799426106150091af1f2 100644 (file)
@@ -199,6 +199,7 @@ private:
         RatingRole,
         IsDirRole,
         IsExpandedRole,
+        IsExpandableRole,
         ExpansionLevelRole,
         RolesCount // Mandatory last entry
     };
index 671352cacdaac9a1ae37ae3b11c6e817b1e492ce..0ecc0e9731d420b2198e6cc2ef095010f7598054 100644 (file)
@@ -356,7 +356,8 @@ void KFileItemModelRolesUpdater::resolvePendingRoles()
 
     const bool hasSlowRoles = m_previewShown
                               || m_roles.contains("size")
-                              || m_roles.contains("type");
+                              || m_roles.contains("type")
+                              || m_roles.contains("isExpandable");
     const ResolveHint resolveHint = hasSlowRoles ? ResolveFast : ResolveAll;
 
     // Resolving the MIME type can be expensive. Assure that not more than MaxBlockTimeout ms are
@@ -715,13 +716,19 @@ QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileIte
 {
     QHash<QByteArray, QVariant> data;
 
-    if (m_roles.contains("size")) {
-        if (item.isDir() && item.isLocalFile()) {
-            const QString path = item.localPath();
-            const int count = subDirectoriesCount(path);
-            if (count >= 0) {
+    const bool getSizeRole = m_roles.contains("size");
+    const bool getIsExpandableRole = m_roles.contains("isExpandable");
+
+    if ((getSizeRole || getIsExpandableRole) && item.isDir() && item.isLocalFile()) {
+        const QString path = item.localPath();
+        const int count = subDirectoriesCount(path);
+        if (count >= 0) {
+            if (getSizeRole) {
                 data.insert("size", KIO::filesize_t(count));
             }
+            if (getIsExpandableRole) {
+                data.insert("isExpandable", count > 0);
+            }
         }
     }
 
@@ -770,11 +777,17 @@ KFileItemList KFileItemModelRolesUpdater::sortedItems(const QSet<KFileItem>& ite
     return itemList;
 }
 
-int KFileItemModelRolesUpdater::subDirectoriesCount(const QString& path)
+int KFileItemModelRolesUpdater::subDirectoriesCount(const QString& path) const
 {
+    const bool countHiddenFiles = m_model->showHiddenFiles();
+
 #ifdef Q_WS_WIN
     QDir dir(path);
-    return dir.entryList(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::System).count();
+    QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::System;
+    if (countHiddenFiles) {
+        filters |= QDir::Hidden;
+    }
+    return dir.entryList(filters).count();
 #else
     // Taken from kdelibs/kio/kio/kdirmodel.cpp
     // Copyright (C) 2006 David Faure <faure@kde.org>
@@ -786,8 +799,8 @@ int KFileItemModelRolesUpdater::subDirectoriesCount(const QString& path)
         struct dirent *dirEntry = 0;
         while ((dirEntry = ::readdir(dir))) { // krazy:exclude=syscalls
             if (dirEntry->d_name[0] == '.') {
-                if (dirEntry->d_name[1] == '\0') {
-                    // Skip "."
+                if (dirEntry->d_name[1] == '\0' || !countHiddenFiles) {
+                    // Skip "." or hidden files
                     continue;
                 }
                 if (dirEntry->d_name[1] == '.' && dirEntry->d_name[2] == '\0') {
index 065f069c84184e05e51c01000520ba65830a5960..b7d8ceab0ff5ceb8361c0aad9144b93934af4051 100644 (file)
@@ -157,7 +157,7 @@ private:
 
     KFileItemList sortedItems(const QSet<KFileItem>& items) const;
 
-    static int subDirectoriesCount(const QString& path);
+    int subDirectoriesCount(const QString& path) const;
 
 private:
     // Property for setPaused()/isPaused().
index ac0c59952f0f7cacc1889bed1c2efeffde2ca7b7..86a2c04a4d836b81427a5102c7d993c908899e12 100644 (file)
@@ -372,7 +372,7 @@ void KFileItemModelTest::testExpandItems()
     // yields the correct result for "a/a/1" and "a/a-1/", whis is non-trivial because they share the
     // first three characters.
     QSet<QByteArray> modelRoles = m_model->roles();
-    modelRoles << "isExpanded" << "expansionLevel";
+    modelRoles << "isExpanded" << "isExpandable" << "expansionLevel";
     m_model->setRoles(modelRoles);
 
     QStringList files;