X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/8931e14eec50cd8a4693460839edfde8690ca297..f56394e276358bccfda58bb82de984dfbdee5015:/src/iconmanager.cpp diff --git a/src/iconmanager.cpp b/src/iconmanager.cpp index 2a3827134..94432102c 100644 --- a/src/iconmanager.cpp +++ b/src/iconmanager.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -32,18 +33,24 @@ #include #include #include +#include #include IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) : QObject(parent), m_showPreview(false), + m_clearItemQueues(true), m_view(parent), m_previewTimer(0), + m_scrollAreaTimer(0), m_previewJobs(), m_dolphinModel(0), m_proxyModel(model), + m_mimeTypeResolver(0), m_cutItemsCache(), - m_previews() + m_previews(), + m_pendingItems(), + m_dispatchedItems() { Q_ASSERT(m_view->iconSize().isValid()); // each view must provide its current icon size @@ -56,12 +63,33 @@ IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* this, SLOT(updateCutItems())); m_previewTimer = new QTimer(this); + m_previewTimer->setSingleShot(true); connect(m_previewTimer, SIGNAL(timeout()), this, SLOT(dispatchPreviewQueue())); + + // Whenever the scrollbar values have been changed, the pending previews should + // be reordered in a way that the previews for the visible items are generated + // first. The reordering is done with a small delay, so that during moving the + // scrollbars the CPU load is kept low. + m_scrollAreaTimer = new QTimer(this); + m_scrollAreaTimer->setSingleShot(true); + m_scrollAreaTimer->setInterval(200); + connect(m_scrollAreaTimer, SIGNAL(timeout()), + this, SLOT(resumePreviews())); + connect(m_view->horizontalScrollBar(), SIGNAL(valueChanged(int)), + this, SLOT(pausePreviews())); + connect(m_view->verticalScrollBar(), SIGNAL(valueChanged(int)), + this, SLOT(pausePreviews())); } IconManager::~IconManager() { - killJobs(); + killPreviewJobs(); + m_pendingItems.clear(); + m_dispatchedItems.clear(); + if (m_mimeTypeResolver != 0) { + m_mimeTypeResolver->deleteLater(); + m_mimeTypeResolver = 0; + } } @@ -75,6 +103,16 @@ void IconManager::setShowPreview(bool show) updatePreviews(); } } + + if (show && (m_mimeTypeResolver != 0)) { + // don't resolve the MIME types if the preview is turned on + m_mimeTypeResolver->deleteLater(); + m_mimeTypeResolver = 0; + } else if (!show && (m_mimeTypeResolver == 0)) { + // the preview is turned off: resolve the MIME-types so that + // the icons gets updated + m_mimeTypeResolver = new KMimeTypeResolver(m_view, m_dolphinModel); + } } void IconManager::updatePreviews() @@ -83,9 +121,12 @@ void IconManager::updatePreviews() return; } - killJobs(); - KFileItemList itemList; + killPreviewJobs(); + m_cutItemsCache.clear(); + m_pendingItems.clear(); + m_dispatchedItems.clear(); + KFileItemList itemList; const int rowCount = m_dolphinModel->rowCount(); for (int row = 0; row < rowCount; ++row) { const QModelIndex index = m_dolphinModel->index(row, 0); @@ -94,6 +135,15 @@ void IconManager::updatePreviews() } generatePreviews(itemList); + updateCutItems(); +} + +void IconManager::cancelPreviews() +{ + killPreviewJobs(); + m_cutItemsCache.clear(); + m_pendingItems.clear(); + m_dispatchedItems.clear(); } void IconManager::generatePreviews(const KFileItemList& items) @@ -104,31 +154,14 @@ void IconManager::generatePreviews(const KFileItemList& items) return; } - const QRect visibleArea = m_view->viewport()->rect(); + KFileItemList orderedItems = items; + orderItems(orderedItems); - // Order the items in a way that the preview for the visible items - // is generated first, as this improves the feeled performance a lot. - KFileItemList orderedItems; - foreach (const KFileItem &item, items) { - const QModelIndex dirIndex = m_dolphinModel->indexForItem(item); - const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); - const QRect itemRect = m_view->visualRect(proxyIndex); - if (itemRect.intersects(visibleArea)) { - orderedItems.insert(0, item); - } else { - orderedItems.append(item); - } + foreach (const KFileItem& item, orderedItems) { + m_pendingItems.append(item); } - const QSize size = m_view->iconSize(); - KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128, 128); - connect(job, SIGNAL(gotPreview(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); + startPreviewJob(orderedItems); } void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap) @@ -137,12 +170,19 @@ void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap preview.url = item.url(); preview.pixmap = pixmap; m_previews.append(preview); + + m_dispatchedItems.append(item); } void IconManager::slotPreviewJobFinished(KJob* job) { const int index = m_previewJobs.indexOf(job); m_previewJobs.removeAt(index); + + if ((m_previewJobs.count() == 0) && m_clearItemQueues) { + m_pendingItems.clear(); + m_dispatchedItems.clear(); + } } void IconManager::updateCutItems() @@ -195,6 +235,49 @@ void IconManager::dispatchPreviewQueue() } } +void IconManager::pausePreviews() +{ + foreach (KJob* job, m_previewJobs) { + Q_ASSERT(job != 0); + job->suspend(); + } + m_scrollAreaTimer->start(); +} + +void IconManager::resumePreviews() +{ + // Before creating new preview jobs the m_pendingItems queue must be + // cleaned up by removing the already dispatched items. Implementation + // note: The order of the m_dispatchedItems queue and the m_pendingItems + // queue is usually equal. So even when having a lot of elements the + // nested loop is no performance bottle neck, as the inner loop is only + // entered once in most cases. + foreach (const KFileItem& item, m_dispatchedItems) { + KFileItemList::iterator begin = m_pendingItems.begin(); + KFileItemList::iterator end = m_pendingItems.end(); + for (KFileItemList::iterator it = begin; it != end; ++it) { + if ((*it).url() == item.url()) { + m_pendingItems.erase(it); + break; + } + } + } + m_dispatchedItems.clear(); + + KFileItemList orderedItems = m_pendingItems; + orderItems(orderedItems); + + // Kill all suspended preview jobs. Usually when a preview job + // has been finished, slotPreviewJobFinished() clears all item queues. + // This is not wanted in this case, as a new job is created afterwards + // for m_pendingItems. + m_clearItemQueues = false; + killPreviewJobs(); + m_clearItemQueues = true; + + startPreviewJob(orderedItems); +} + void IconManager::replaceIcon(const KUrl& url, const QPixmap& pixmap) { Q_ASSERT(url.isValid()); @@ -372,7 +455,24 @@ void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize) } } -void IconManager::killJobs() +void IconManager::startPreviewJob(const KFileItemList& items) +{ + if (items.count() == 0) { + return; + } + + const QSize size = m_view->iconSize(); + KIO::PreviewJob* job = KIO::filePreview(items, 128, 128); + connect(job, SIGNAL(gotPreview(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::killPreviewJobs() { foreach (KJob* job, m_previewJobs) { Q_ASSERT(job != 0); @@ -381,4 +481,67 @@ void IconManager::killJobs() m_previewJobs.clear(); } +void IconManager::orderItems(KFileItemList& items) +{ + // Order the items in a way that the preview for the visible items + // is generated first, as this improves the feeled performance a lot. + // + // Implementation note: 2 different algorithms are used for the sorting. + // Algorithm 1 is faster when having a lot of items in comparison + // to the number of rows in the model. Algorithm 2 is faster + // when having quite less items in comparison to the number of rows in + // the model. Choosing the right algorithm is important when having directories + // with several hundreds or thousands of items. + + const int itemCount = items.count(); + const int rowCount = m_proxyModel->rowCount(); + const QRect visibleArea = m_view->viewport()->rect(); + + 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. + for (int row = 0; row < rowCount; ++row) { + const QModelIndex proxyIndex = m_proxyModel->index(row, 0); + const QRect itemRect = m_view->visualRect(proxyIndex); + const QModelIndex dirIndex = m_proxyModel->mapToSource(proxyIndex); + + KFileItem item = m_dolphinModel->itemForIndex(dirIndex); // O(1) + const KUrl url = item.url(); + + // check whether the item is part of the item list 'items' + int index = -1; + for (int i = 0; i < itemCount; ++i) { + if (items[i].url() == url) { + index = i; + break; + } + } + + if ((index > 0) && itemRect.intersects(visibleArea)) { + // The current item is (at least partly) visible. Move it + // to the front of the list, so that the preview is + // generated earlier. + items.removeAt(index); + items.insert(0, item); + } + } + } else { + // Algorithm 2: The number of items is <= 10 % of the row count. In this case iterate + // all items and receive the corresponding row from the item. + for (int i = 0; i < itemCount; ++i) { + const QModelIndex dirIndex = m_dolphinModel->indexForItem(items[i]); // O(n) (n = number of rows) + const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex); + const QRect itemRect = m_view->visualRect(proxyIndex); + + if (itemRect.intersects(visibleArea)) { + // 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.removeAt(i + 1); + } + } + } +} + #include "iconmanager.moc"