X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/629ec98952bcf38bc99f2b11bc37bdc2ec7aabcc..4e4c8d01e364c8d49427115dc242d999052b1e4e:/src/iconmanager.cpp diff --git a/src/iconmanager.cpp b/src/iconmanager.cpp index 002f4e86d..b334a3e5c 100644 --- a/src/iconmanager.cpp +++ b/src/iconmanager.cpp @@ -32,14 +32,60 @@ #include #include #include +#include #include #include #include +/** + * If the passed item view is an instance of QListView, expensive + * layout operations are blocked in the constructor and are unblocked + * again in the destructor. + * + * This helper class is a workaround for the following huge performance + * problem when having directories with several 1000 items: + * - each change of an icon emits a dataChanged() signal from the model + * - QListView iterates through all items on each dataChanged() signal + * and invokes QItemDelegate::sizeHint() + * - the sizeHint() implementation of KFileItemDelegate is quite complex, + * invoking it 1000 times for each icon change might block the UI + * + * QListView does not invoke QItemDelegate::sizeHint() when the + * uniformItemSize property has been set to true, so this property is + * set before exchanging a block of icons. It is important to reset + * it again before the event loop is entered, otherwise QListView + * would not get the correct size hints after dispatching the layoutChanged() + * signal. + */ +class LayoutBlocker { +public: + LayoutBlocker(QAbstractItemView* view) : + m_uniformSizes(false), + m_view(qobject_cast(view)) + { + if (m_view != 0) { + m_uniformSizes = m_view->uniformItemSizes(); + m_view->setUniformItemSizes(true); + } + } + + ~LayoutBlocker() + { + if (m_view != 0) { + m_view->setUniformItemSizes(m_uniformSizes); + } + } + +private: + bool m_uniformSizes; + QListView* m_view; +}; + IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) : QObject(parent), m_showPreview(false), m_clearItemQueues(true), + m_pendingVisiblePreviews(0), m_view(parent), m_previewTimer(0), m_scrollAreaTimer(0), @@ -154,28 +200,11 @@ void IconManager::generatePreviews(const KFileItemList& items) return; } - // 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: using KDirModel::itemForUrl() would lead to a more - // readable code, but it is a lot slower in comparison to itemListContains(). - const QRect visibleArea = m_view->viewport()->rect(); - KFileItemList orderedItems; - const int rowCount = m_proxyModel->rowCount(); - 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); - const KUrl url = item.url(); - if (itemListContains(items, url)) { - if (itemRect.intersects(visibleArea)) { - orderedItems.insert(0, item); - m_pendingItems.insert(0, url); - } else { - orderedItems.append(item); - m_pendingItems.append(url); - } - } + KFileItemList orderedItems = items; + orderItems(orderedItems); + + foreach (const KFileItem& item, orderedItems) { + m_pendingItems.append(item); } startPreviewJob(orderedItems); @@ -188,7 +217,7 @@ void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap preview.pixmap = pixmap; m_previews.append(preview); - m_dispatchedItems.append(item.url()); + m_dispatchedItems.append(item); } void IconManager::slotPreviewJobFinished(KJob* job) @@ -199,6 +228,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); } } @@ -220,35 +251,28 @@ 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; - } - - for (int i = 0; i < dispatchCount; ++i) { + LayoutBlocker blocker(m_view); + for (int i = 0; i < previewsCount; ++i) { const ItemInfo& preview = m_previews.first(); replaceIcon(preview.url, preview.pixmap); 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); } } @@ -269,11 +293,11 @@ void IconManager::resumePreviews() // 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 KUrl& url, m_dispatchedItems) { - QList::iterator begin = m_pendingItems.begin(); - QList::iterator end = m_pendingItems.end(); - for (QList::iterator it = begin; it != end; ++it) { - if ((*it) == url) { + 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; } @@ -281,30 +305,8 @@ void IconManager::resumePreviews() } m_dispatchedItems.clear(); - // Create a new preview job for the remaining 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: using KDirModel::itemForUrl() would lead to a more - // readable code, but it is a lot slower in comparison - // to m_pendingItems.contains(). - const QRect visibleArea = m_view->viewport()->rect(); - KFileItemList orderedItems; - - const int rowCount = m_proxyModel->rowCount(); - 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); - const KUrl url = item.url(); - if (m_pendingItems.contains(url)) { - if (itemRect.intersects(visibleArea)) { - orderedItems.insert(0, item); - } else { - orderedItems.append(item); - } - } - } + KFileItemList orderedItems = m_pendingItems; + orderItems(orderedItems); // Kill all suspended preview jobs. Usually when a preview job // has been finished, slotPreviewJobFinished() clears all item queues. @@ -520,14 +522,69 @@ void IconManager::killPreviewJobs() m_previewJobs.clear(); } -bool IconManager::itemListContains(const KFileItemList& items, const KUrl& url) const +void IconManager::orderItems(KFileItemList& items) { - foreach (const KFileItem& item, items) { - if (url == item.url()) { - return true; + // 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); + ++m_pendingVisiblePreviews; + } + } + } 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); + ++m_pendingVisiblePreviews; + } } } - return false; } #include "iconmanager.moc"