]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/iconmanager.cpp
Bypassed a Qt-issue where enabling the menu-animation for QApplication emits a clicke...
[dolphin.git] / src / iconmanager.cpp
index bb057b7d6552a7ddc67de1f9b06db7a437ade605..b46328ceaef89d9094304b1b84d3befa1c01c43e 100644 (file)
@@ -38,29 +38,30 @@ IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel*
     QObject(parent),
     m_showPreview(false),
     m_view(parent),
+    m_previewTimer(0),
     m_previewJobs(),
     m_dolphinModel(0),
     m_proxyModel(model),
-    m_cutItemsCache()
+    m_cutItemsCache(),
+    m_previews()
 {
     Q_ASSERT(m_view->iconSize().isValid());  // each view must provide its current icon size
 
     m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
     connect(m_dolphinModel->dirLister(), SIGNAL(newItems(const KFileItemList&)),
-            this, SLOT(updateIcons(const KFileItemList&)));
+            this, SLOT(generatePreviews(const KFileItemList&)));
 
     QClipboard* clipboard = QApplication::clipboard();
     connect(clipboard, SIGNAL(dataChanged()),
             this, SLOT(updateCutItems()));
+
+    m_previewTimer = new QTimer(this);
+    connect(m_previewTimer, SIGNAL(timeout()), this, SLOT(dispatchPreviewQueue()));
 }
 
 IconManager::~IconManager()
 {
-    foreach (KJob* job, m_previewJobs) {
-        Q_ASSERT(job != 0);
-        job->kill();
-    }
-    m_previewJobs.clear();
+    killJobs();
 }
 
 
@@ -70,23 +71,37 @@ void IconManager::setShowPreview(bool show)
         m_showPreview = show;
         m_cutItemsCache.clear();
         updateCutItems();
+        if (show) {
+            updatePreviews();
+        }
     }
 }
 
-void IconManager::updateIcons(const KFileItemList& items)
+void IconManager::updatePreviews()
 {
-    // make the icons of all hidden files semitransparent
-    foreach (KFileItem item, items) {
-        if (item.isHidden()) {
-            applyHiddenItemEffect(item);
-        }
+    if (!m_showPreview) {
+        return;
     }
 
+    killJobs();
+    KFileItemList itemList;
+
+    const int rowCount = m_dolphinModel->rowCount();
+    for (int row = 0; row < rowCount; ++row) {
+        const QModelIndex index = m_dolphinModel->index(row, 0);
+        KFileItem item = m_dolphinModel->itemForIndex(index);
+        itemList.append(item);
+    }
+
+    generatePreviews(itemList);
+}
+
+void IconManager::generatePreviews(const KFileItemList &items)
+{
     if (!m_showPreview) {
         return;
     }
 
-    // generate previews
     const QRect visibleArea = m_view->viewport()->rect();
 
     // Order the items in a way that the preview for the visible items
@@ -106,11 +121,76 @@ void IconManager::updateIcons(const KFileItemList& items)
     const QSize size = m_view->iconSize();
     KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128, 128);
     connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
-            this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
+            this, SLOT(addToPreviewQueue(const KFileItem&, const QPixmap&)));
     connect(job, SIGNAL(finished(KJob*)),
             this, SLOT(slotPreviewJobFinished(KJob*)));
 
     m_previewJobs.append(job);
+    m_previewTimer->start(200);
+}
+
+void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap)
+{
+    Preview preview;
+    preview.item = item;
+    preview.pixmap = pixmap;
+    m_previews.append(preview);
+}
+
+void IconManager::slotPreviewJobFinished(KJob* job)
+{
+    const int index = m_previewJobs.indexOf(job);
+    m_previewJobs.removeAt(index);
+}
+
+void IconManager::updateCutItems()
+{
+    // restore the icons of all previously selected items to the
+    // original state...
+    foreach (CutItem cutItem, m_cutItemsCache) {
+        const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
+        if (index.isValid()) {
+            m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
+        }
+    }
+    m_cutItemsCache.clear();
+
+    // ... and apply an item effect to all currently cut items
+    applyCutItemEffect();
+}
+
+void IconManager::dispatchPreviewQueue()
+{
+    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 > m_previews.count()) {
+            dispatchCount = m_previews.count();
+        }
+
+        for (int i = 0; i < dispatchCount; ++i) {
+            const Preview& preview = m_previews.first();
+            replaceIcon(preview.item, preview.pixmap);
+            m_previews.pop_front();
+        }
+
+        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
+        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);
+    }
 }
 
 void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
@@ -147,16 +227,6 @@ void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
             limitToSize(icon, m_view->iconSize());
         }
 
-        if (item.isHidden()) {
-            if (!icon.hasAlpha()) {
-                // the semitransparent operation requires having an alpha mask
-                QPixmap alphaMask(icon.size());
-                alphaMask.fill();
-                icon.setAlphaChannel(alphaMask);
-            }
-            KIconEffect::semiTransparent(icon);
-        }
-
         const QMimeData* mimeData = QApplication::clipboard()->mimeData();
         if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
             KIconEffect iconEffect;
@@ -168,28 +238,6 @@ void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
     }
 }
 
-void IconManager::slotPreviewJobFinished(KJob* job)
-{
-    const int index = m_previewJobs.indexOf(job);
-    m_previewJobs.removeAt(index);
-}
-
-void IconManager::updateCutItems()
-{
-    // restore the icons of all previously selected items to the
-    // original state...
-    foreach (CutItem cutItem, m_cutItemsCache) {
-        const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
-        if (index.isValid()) {
-            m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
-        }
-    }
-    m_cutItemsCache.clear();
-
-    // ... and apply an item effect to all currently cut items
-    applyCutItemEffect();
-}
-
 bool IconManager::isCutItem(const KFileItem& item) const
 {
     const QMimeData* mimeData = QApplication::clipboard()->mimeData();
@@ -243,24 +291,15 @@ void IconManager::applyCutItemEffect()
     }
 }
 
-void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
-{
-    const QModelIndex index = m_dolphinModel->indexForItem(hiddenItem);
-    const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
-    if (value.type() == QVariant::Icon) {
-        const QIcon icon(qvariant_cast<QIcon>(value));
-        const QSize maxSize = m_view->iconSize();
-        QPixmap pixmap = icon.pixmap(maxSize.height(), maxSize.height()); // ignore the width
-        KIconEffect::semiTransparent(pixmap);
-        m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
-    }
-}
-
 bool IconManager::applyImageFrame(QPixmap& icon)
 {
     const QSize maxSize = m_view->iconSize();
-    if ((maxSize.width() <= 24) || (maxSize.height() <= 24)) {
-        // the maximum size is too small for a frame
+    const bool applyFrame = (maxSize.width()  > KIconLoader::SizeSmallMedium) &&
+                            (maxSize.height() > KIconLoader::SizeSmallMedium) &&
+                            ((icon.width()  > KIconLoader::SizeLarge) ||
+                             (icon.height() > KIconLoader::SizeLarge));
+    if (!applyFrame) {
+        // the maximum size or the image itself is too small for a frame
         return false;
     }
 
@@ -280,22 +319,14 @@ bool IconManager::applyImageFrame(QPixmap& icon)
     painter.begin(&framedIcon);
     painter.drawPixmap(frame, frame, icon);
 
-    // draw a white frame around the icon
-    painter.setPen(Qt::NoPen);
-    painter.setBrush(palette.brush(QPalette::Normal, QPalette::Base));
-    painter.drawRect(0, 0, width, frame);
-    painter.drawRect(0, height - frame, width, frame);
-    painter.drawRect(0, frame, frame,  height - doubleFrame);
-    painter.drawRect(width - frame, frame, frame,  height - doubleFrame);
-
     // add a border
     painter.setPen(palette.color(QPalette::Text));
     painter.setBrush(Qt::NoBrush);
     painter.drawRect(0, 0, width, height);
     painter.drawRect(1, 1, width - 2, height - 2);
 
-    // dimm image frame by 25 %
-    painter.setPen(QColor(0, 0, 0, 64));
+    // dim image frame by 12.5 %
+    painter.setPen(QColor(0, 0, 0, 32));
     painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame);
     painter.end();
 
@@ -323,4 +354,13 @@ void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
     }
 }
 
+void IconManager::killJobs()
+{
+    foreach (KJob* job, m_previewJobs) {
+        Q_ASSERT(job != 0);
+        job->kill();
+    }
+    m_previewJobs.clear();
+}
+
 #include "iconmanager.moc"