]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodelrolesupdater.cpp
[Details mode] Allow to fill the column size of directories with actual size
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.cpp
index 8ea106a5ead6dcfb9e608378572b6cf810beaef3..c28e240a5f97ba6b54dc1bd6e5c5453a769da363 100644 (file)
 #include "kfileitemmodelrolesupdater.h"
 
 #include "kfileitemmodel.h"
+#include "private/kdirectorycontentscounter.h"
+#include "private/kpixmapmodifier.h"
 
 #include <KConfig>
 #include <KConfigGroup>
-#include <KSharedConfig>
-#include <KFileItem>
-#include <KIconLoader>
-#include <KJobWidgets>
 #include <KIO/JobUiDelegate>
 #include <KIO/PreviewJob>
-#include <KPluginLoader>
+#include <KIconLoader>
+#include <KJobWidgets>
 #include <KOverlayIconPlugin>
+#include <KPluginLoader>
+#include <KSharedConfig>
 
-#include "private/kpixmapmodifier.h"
-#include "private/kdirectorycontentscounter.h"
+#ifdef HAVE_BALOO
+#include "private/kbaloorolesprovider.h"
+#include <Baloo/File>
+#include <Baloo/FileMonitor>
+#endif
 
 #include <QApplication>
 #include <QPainter>
-#include <QPixmap>
 #include <QElapsedTimer>
 #include <QTimer>
 
-#include <algorithm>
-
-#ifdef HAVE_BALOO
-    #include "private/kbaloorolesprovider.h"
-    #include <Baloo/File>
-    #include <Baloo/FileMonitor>
-#endif
-
-
 // #define KFILEITEMMODELROLESUPDATER_DEBUG
 
 namespace {
@@ -88,21 +82,18 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO
     m_pendingIndexes(),
     m_pendingPreviewItems(),
     m_previewJob(),
-    m_recentlyChangedItemsTimer(0),
+    m_recentlyChangedItemsTimer(nullptr),
     m_recentlyChangedItems(),
     m_changedItems(),
-    m_directoryContentsCounter(0)
+    m_directoryContentsCounter(nullptr)
   #ifdef HAVE_BALOO
-  , m_balooFileMonitor(0)
+  , m_balooFileMonitor(nullptr)
   #endif
 {
     Q_ASSERT(model);
 
     const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
-    m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList());
-    if (m_enabledPlugins.isEmpty()) {
-        m_enabledPlugins = KIO::PreviewJob::defaultPlugins();
-    }
+    m_enabledPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
 
     connect(m_model, &KFileItemModel::itemsInserted,
             this,    &KFileItemModelRolesUpdater::slotItemsInserted);
@@ -116,9 +107,9 @@ KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QO
             this,    &KFileItemModelRolesUpdater::slotSortRoleChanged);
 
     // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
-    // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
+    // resolving of the roles. Postpone the resolving until no update has been done for 100 ms.
     m_recentlyChangedItemsTimer = new QTimer(this);
-    m_recentlyChangedItemsTimer->setInterval(1000);
+    m_recentlyChangedItemsTimer->setInterval(100);
     m_recentlyChangedItemsTimer->setSingleShot(true);
     connect(m_recentlyChangedItemsTimer, &QTimer::timeout, this, &KFileItemModelRolesUpdater::resolveRecentlyChangedItems);
 
@@ -299,7 +290,7 @@ void KFileItemModelRolesUpdater::setRoles(const QSet<QByteArray>& roles)
                     this, &KFileItemModelRolesUpdater::applyChangedBalooRoles);
         } else if (!hasBalooRole && m_balooFileMonitor) {
             delete m_balooFileMonitor;
-            m_balooFileMonitor = 0;
+            m_balooFileMonitor = nullptr;
         }
 #endif
 
@@ -363,7 +354,7 @@ void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList& itemRan
 
 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRanges)
 {
-    Q_UNUSED(itemRanges);
+    Q_UNUSED(itemRanges)
 
     const bool allItemsRemoved = (m_model->count() == 0);
 
@@ -413,10 +404,10 @@ void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRang
     }
 }
 
-void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange& itemRange, QList<int> movedToIndexes)
+void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange& itemRange, const QList<int> &movedToIndexes)
 {
-    Q_UNUSED(itemRange);
-    Q_UNUSED(movedToIndexes);
+    Q_UNUSED(itemRange)
+    Q_UNUSED(movedToIndexes)
 
     // The visible items might have changed.
     startUpdating();
@@ -425,7 +416,7 @@ void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange& itemRange, QLi
 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList& itemRanges,
                                                   const QSet<QByteArray>& roles)
 {
-    Q_UNUSED(roles);
+    Q_UNUSED(roles)
 
     // Find out if slotItemsChanged() has been done recently. If that is the
     // case, resolving the roles is postponed until a timer has exceeded
@@ -453,8 +444,8 @@ void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList& itemRang
 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray& current,
                                                      const QByteArray& previous)
 {
-    Q_UNUSED(current);
-    Q_UNUSED(previous);
+    Q_UNUSED(current)
+    Q_UNUSED(previous)
 
     if (m_resolvableRoles.contains(current)) {
         m_pendingSortRoleItems.clear();
@@ -503,14 +494,9 @@ void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPi
 
     QPixmap scaledPixmap = pixmap;
 
-    const QString mimeType = item.mimetype();
-    const int slashIndex = mimeType.indexOf(QLatin1Char('/'));
-    const bool isFontPreview = mimeType.right(slashIndex).contains(QLatin1String("font"));
-    const bool isFolderPreview = item.isDir();
-    const bool isWindowsExePreview = mimeType == QLatin1String("application/x-ms-dos-executable") ||
-                                     mimeType == QLatin1String("application/x-msdownload");
-
-    if (!isFolderPreview && !isFontPreview && !isWindowsExePreview) {
+    if (!pixmap.hasAlpha()
+        && m_iconSize.width()  > KIconLoader::SizeSmallMedium
+        && m_iconSize.height() > KIconLoader::SizeSmallMedium) {
         if (m_enlargeSmallPreviews) {
             KPixmapModifier::applyFrame(scaledPixmap, m_iconSize);
         } else {
@@ -534,13 +520,14 @@ void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPi
                                    scaledPixmap);
                 scaledPixmap = largeFrame;
             } else {
-                // The image must be shrinked as it is too large to fit into
+                // The image must be shrunk as it is too large to fit into
                 // the available icon size
                 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize);
             }
         }
     } else {
-        KPixmapModifier::scale(scaledPixmap, m_iconSize);
+        KPixmapModifier::scale(scaledPixmap, m_iconSize * qApp->devicePixelRatio());
+        scaledPixmap.setDevicePixelRatio(qApp->devicePixelRatio());
     }
 
     QHash<QByteArray, QVariant> data = rolesData(item);
@@ -597,7 +584,7 @@ void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem& item)
 
 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
 {
-    m_previewJob = 0;
+    m_previewJob = nullptr;
 
     if (m_state != PreviewJobRunning) {
         return;
@@ -722,6 +709,8 @@ void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString& file)
         return;
     }
     applyChangedBalooRolesForItem(item);
+#else
+    Q_UNUSED(file)
 #endif
 }
 
@@ -755,12 +744,12 @@ void KFileItemModelRolesUpdater::applyChangedBalooRolesForItem(const KFileItem &
             this,    &KFileItemModelRolesUpdater::slotItemsChanged);
 #else
 #ifndef Q_CC_MSVC
-    Q_UNUSED(item);
+    Q_UNUSED(item)
 #endif
 #endif
 }
 
-void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString& path, int count)
+void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString& path, int count, long size)
 {
     const bool getSizeRole = m_roles.contains("size");
     const bool getIsExpandableRole = m_roles.contains("isExpandable");
@@ -771,17 +760,16 @@ void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QStrin
             QHash<QByteArray, QVariant> data;
 
             if (getSizeRole) {
-                data.insert("size", count);
+                data.insert("count", count);
+                if (size != -1) {
+                    data.insert("size", QVariant::fromValue(size));
+                }
             }
             if (getIsExpandableRole) {
                 data.insert("isExpandable", count > 0);
             }
 
-            disconnect(m_model, &KFileItemModel::itemsChanged,
-                       this,    &KFileItemModelRolesUpdater::slotItemsChanged);
             m_model->setData(index, data);
-            connect(m_model, &KFileItemModel::itemsChanged,
-                    this,    &KFileItemModelRolesUpdater::slotItemsChanged);
         }
     }
 }
@@ -1007,7 +995,7 @@ void KFileItemModelRolesUpdater::applySortRole(int index)
         data.insert("type", item.mimeComment());
     } else if (m_model->sortRole() == "size" && item.isLocalFile() && item.isDir()) {
         const QString path = item.localPath();
-        data.insert("size", m_directoryContentsCounter->countDirectoryContentsSynchronously(path));
+        m_directoryContentsCounter->scanDirectory(path);
     } else {
         // Probably the sort role is a baloo role - just determine all roles.
         data = rolesData(item);
@@ -1080,7 +1068,7 @@ QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileIte
             // Tell m_directoryContentsCounter that we want to count the items
             // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
             const QString path = item.localPath();
-            m_directoryContentsCounter->addDirectory(path);
+            m_directoryContentsCounter->scanDirectory(path);
         } else if (getSizeRole) {
             data.insert("size", -1); // -1 indicates an unknown number of items
         }
@@ -1141,7 +1129,7 @@ void KFileItemModelRolesUpdater::killPreviewJob()
         disconnect(m_previewJob,  &KIO::PreviewJob::finished,
                    this, &KFileItemModelRolesUpdater::slotPreviewJobFinished);
         m_previewJob->kill();
-        m_previewJob = 0;
+        m_previewJob = nullptr;
         m_pendingPreviewItems.clear();
     }
 }
@@ -1160,7 +1148,7 @@ QList<int> KFileItemModelRolesUpdater::indexesToResolve() const
 
     // We need a reasonable upper limit for number of items to resolve after
     // and before the visible range. m_maximumVisibleItems can be quite large
-    // when using Compace View.
+    // when using Compact View.
     const int readAheadItems = qMin(ReadAheadPages * m_maximumVisibleItems, ResolveAllItemsLimit / 2);
 
     // Add items after the visible range.