]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/iconmanager.cpp
Provide a 'int selectedItemsCount() const' interface for performance reasons, so...
[dolphin.git] / src / iconmanager.cpp
index 97dac53b7b269440e8a6a37ba7d84174e4495889..11a3e28641bb104086ec09c407d794a00055d23c 100644 (file)
@@ -85,6 +85,8 @@ IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel*
     QObject(parent),
     m_showPreview(false),
     m_clearItemQueues(true),
+    m_hasCutSelection(false),
+    m_pendingVisiblePreviews(0),
     m_view(parent),
     m_previewTimer(0),
     m_scrollAreaTimer(0),
@@ -137,7 +139,6 @@ IconManager::~IconManager()
     }
 }
 
-
 void IconManager::setShowPreview(bool show)
 {
     if (m_showPreview != show) {
@@ -211,9 +212,60 @@ void IconManager::generatePreviews(const KFileItemList& items)
 
 void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap)
 {
+    if (!m_showPreview) {
+        // the preview has been canceled in the meantime
+        return;
+    }
+    const KUrl url = item.url();
+
+    // check whether the item is part of the directory lister (it is possible
+    // that a preview from an old directory lister is received)
+    KDirLister* dirLister = m_dolphinModel->dirLister();
+    bool isOldPreview = true;
+    const KUrl::List dirs = dirLister->directories();
+    const QString itemDir = url.directory();
+    foreach (const KUrl& url, dirs) {
+        if (url.path() == itemDir) {
+            isOldPreview = false;
+            break;
+        }
+    }
+    if (isOldPreview) {
+        return;
+    }
+
+    QPixmap icon = pixmap;
+
+    const QString mimeType = item.mimetype();
+    const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
+    if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
+        limitToSize(icon, m_view->iconSize());
+    }
+
+    if (m_hasCutSelection && isCutItem(item)) {
+        // Remember the current icon in the cache for cut items before
+        // the disabled effect is applied. This makes it possible restoring
+        // the uncut version again when cutting other items.
+        QList<ItemInfo>::iterator begin = m_cutItemsCache.begin();
+        QList<ItemInfo>::iterator end   = m_cutItemsCache.end();
+        for (QList<ItemInfo>::iterator it = begin; it != end; ++it) {
+            if ((*it).url == item.url()) {
+                (*it).pixmap = icon;
+                break;
+            }
+        }
+
+        // apply the disabled effect to the icon for marking it as "cut item"
+        // and apply the icon to the item
+        KIconEffect iconEffect;
+        icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
+    }
+
+    // remember the preview and URL, so that it can be applied to the model
+    // in IconManager::dispatchPreviewQueue()
     ItemInfo preview;
-    preview.url = item.url();
-    preview.pixmap = pixmap;
+    preview.url = url;
+    preview.pixmap = icon;
     m_previews.append(preview);
 
     m_dispatchedItems.append(item);
@@ -227,6 +279,8 @@ void IconManager::slotPreviewJobFinished(KJob* job)
     if ((m_previewJobs.count() == 0) && m_clearItemQueues) {
         m_pendingItems.clear();
         m_dispatchedItems.clear();
+        m_pendingVisiblePreviews = 0;
+        QMetaObject::invokeMethod(this, "dispatchPreviewQueue", Qt::QueuedConnection);
     }
 }
 
@@ -248,36 +302,33 @@ void IconManager::updateCutItems()
 
 void IconManager::dispatchPreviewQueue()
 {
-    int previewsCount = m_previews.count();
+    const int previewsCount = m_previews.count();
     if (previewsCount > 0) {
         // Applying the previews to the model must be done step by step
         // in larger blocks: Applying a preview immediately when getting the signal
         // 'gotPreview()' from the PreviewJob is too expensive, as a relayout
         // of the view would be triggered for each single preview.
-
-        int dispatchCount = 30;
-        if (dispatchCount > previewsCount) {
-            dispatchCount = previewsCount;
-        }
-
         LayoutBlocker blocker(m_view);
-        for (int i = 0; i < dispatchCount; ++i) {
+        for (int i = 0; i < previewsCount; ++i) {
             const ItemInfo& preview = m_previews.first();
-            replaceIcon(preview.url, preview.pixmap);
+
+            const QModelIndex idx = m_dolphinModel->indexForUrl(preview.url);
+            if (idx.isValid() && (idx.column() == 0)) {
+                m_dolphinModel->setData(idx, QIcon(preview.pixmap), Qt::DecorationRole);
+            }
+
             m_previews.pop_front();
+            if (m_pendingVisiblePreviews > 0) {
+                --m_pendingVisiblePreviews;
+            }
         }
-
-        previewsCount = m_previews.count();
     }
 
-    const bool workingPreviewJobs = (m_previewJobs.count() > 0);
-    if (workingPreviewJobs) {
-        // poll for previews as long as not all preview jobs are finished
+    if (m_pendingVisiblePreviews > 0) {
+        // As long as there are pending previews for visible items, poll
+        // the preview queue each 200 ms. If there are no pending previews,
+        // the queue is dispatched in slotPreviewJobFinished().
         m_previewTimer->start(200);
-    } else if (previewsCount > 0) {
-        // all preview jobs are finished but there are still pending previews
-        // in the queue -> poll more aggressively
-        m_previewTimer->start(10);
     }
 }
 
@@ -310,6 +361,9 @@ void IconManager::resumePreviews()
     }
     m_dispatchedItems.clear();
 
+    m_pendingVisiblePreviews = 0;
+    dispatchPreviewQueue();
+
     KFileItemList orderedItems = m_pendingItems;
     orderItems(orderedItems);
 
@@ -324,66 +378,6 @@ void IconManager::resumePreviews()
     startPreviewJob(orderedItems);
 }
 
-void IconManager::replaceIcon(const KUrl& url, const QPixmap& pixmap)
-{
-    Q_ASSERT(url.isValid());
-    if (!m_showPreview) {
-        // the preview has been canceled in the meantime
-        return;
-    }
-
-    // check whether the item is part of the directory lister (it is possible
-    // that a preview from an old directory lister is received)
-    KDirLister* dirLister = m_dolphinModel->dirLister();
-    bool isOldPreview = true;
-    const KUrl::List dirs = dirLister->directories();
-    const QString itemDir = url.directory();
-    foreach (const KUrl& url, dirs) {
-        if (url.path() == itemDir) {
-            isOldPreview = false;
-            break;
-        }
-    }
-    if (isOldPreview) {
-        return;
-    }
-
-    const QModelIndex idx = m_dolphinModel->indexForUrl(url);
-    if (idx.isValid() && (idx.column() == 0)) {
-        QPixmap icon = pixmap;
-
-        const KFileItem item = m_dolphinModel->itemForIndex(idx);
-        const QString mimeType = item.mimetype();
-        const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
-        if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
-            limitToSize(icon, m_view->iconSize());
-        }
-
-        const QMimeData* mimeData = QApplication::clipboard()->mimeData();
-        if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
-            // Remember the current icon in the cache for cut items before
-            // the disabled effect is applied. This makes it possible restoring
-            // the uncut version again when cutting other items.
-            QList<ItemInfo>::iterator begin = m_cutItemsCache.begin();
-            QList<ItemInfo>::iterator end   = m_cutItemsCache.end();
-            for (QList<ItemInfo>::iterator it = begin; it != end; ++it) {
-                if ((*it).url == item.url()) {
-                    (*it).pixmap = icon;
-                    break;
-                }
-            }
-
-            // apply the disabled effect to the icon for marking it as "cut item"
-            // and apply the icon to the item
-            KIconEffect iconEffect;
-            icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
-            m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
-        } else {
-            m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
-        }
-    }
-}
-
 bool IconManager::isCutItem(const KFileItem& item) const
 {
     const QMimeData* mimeData = QApplication::clipboard()->mimeData();
@@ -402,7 +396,8 @@ bool IconManager::isCutItem(const KFileItem& item) const
 void IconManager::applyCutItemEffect()
 {
     const QMimeData* mimeData = QApplication::clipboard()->mimeData();
-    if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
+    m_hasCutSelection = KonqMimeData::decodeIsCutSelection(mimeData);
+    if (!m_hasCutSelection) {
         return;
     }
 
@@ -497,7 +492,7 @@ bool IconManager::applyImageFrame(QPixmap& icon)
 void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
 {
     if ((icon.width() > maxSize.width()) || (icon.height() > maxSize.height())) {
-        icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+        icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::FastTransformation);
     }
 }
 
@@ -507,6 +502,9 @@ void IconManager::startPreviewJob(const KFileItemList& items)
         return;
     }
 
+    const QMimeData* mimeData = QApplication::clipboard()->mimeData();
+    m_hasCutSelection = KonqMimeData::decodeIsCutSelection(mimeData);
+
     const QSize size = m_view->iconSize();
     KIO::PreviewJob* job = KIO::filePreview(items, 128, 128);
     connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
@@ -543,6 +541,7 @@ void IconManager::orderItems(KFileItemList& items)
     const int rowCount = m_proxyModel->rowCount();
     const QRect visibleArea = m_view->viewport()->rect();
 
+    int insertPos = 0;
     if (itemCount * 10 > rowCount) {
         // Algorithm 1: The number of items is > 10 % of the row count. Parse all rows
         // and check whether the received row is part of the item list.
@@ -568,7 +567,9 @@ void IconManager::orderItems(KFileItemList& items)
                 // to the front of the list, so that the preview is
                 // generated earlier.
                 items.removeAt(index);
-                items.insert(0, item);
+                items.insert(insertPos, item);
+                ++insertPos;
+                ++m_pendingVisiblePreviews;
             }
         }
     } else {
@@ -583,8 +584,10 @@ void IconManager::orderItems(KFileItemList& items)
                 // The current item is (at least partly) visible. Move it
                 // to the front of the list, so that the preview is
                 // generated earlier.
-                items.insert(0, items[i]);
+                items.insert(insertPos, items[i]);
                 items.removeAt(i + 1);
+                ++insertPos;
+                ++m_pendingVisiblePreviews;
             }
         }
     }