#include <QClipboard>
#include <QColor>
#include <QPainter>
+#include <QScrollBar>
#include <QIcon>
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_cutItemsCache()
+ m_cutItemsCache(),
+ m_previews(),
+ m_pendingItems(),
+ m_dispatchedItems()
{
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);
+ 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();
}
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);
}
generatePreviews(itemList);
+ updateCutItems();
}
-void IconManager::updateIcons(const KFileItemList& items)
+void IconManager::generatePreviews(const KFileItemList& items)
{
- if (m_showPreview) {
- generatePreviews(items);
- }
-}
+ applyCutItemEffect();
-void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
-{
- Q_ASSERT(!item.isNull());
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 = item.url().directory();
- foreach (KUrl url, dirs) {
- if (url.path() == itemDir) {
- isOldPreview = false;
- break;
+ // Order the items in a way that the preview for the visible items
+ // is generated first, as this improves the feeled performance a lot.
+ const QRect visibleArea = m_view->viewport()->rect();
+ 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);
+ m_pendingItems.insert(0, item.url());
+ } else {
+ orderedItems.append(item);
+ m_pendingItems.append(item.url());
}
}
- if (isOldPreview) {
- return;
- }
- const QModelIndex idx = m_dolphinModel->indexForItem(item);
- if (idx.isValid() && (idx.column() == 0)) {
- QPixmap icon = pixmap;
+ startPreviewJob(orderedItems);
+}
- const QString mimeType = item.mimetype();
- const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
- if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
- limitToSize(icon, m_view->iconSize());
- }
+void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap)
+{
+ ItemInfo preview;
+ preview.url = item.url();
+ preview.pixmap = pixmap;
+ m_previews.append(preview);
- const QMimeData* mimeData = QApplication::clipboard()->mimeData();
- if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(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);
- }
- }
+ m_dispatchedItems.append(item.url());
}
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()
{
// restore the icons of all previously selected items to the
// original state...
- foreach (CutItem cutItem, m_cutItemsCache) {
+ foreach (const ItemInfo& cutItem, m_cutItemsCache) {
const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
if (index.isValid()) {
m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
applyCutItemEffect();
}
-void IconManager::generatePreviews(const KFileItemList &items)
+void IconManager::dispatchPreviewQueue()
{
- Q_ASSERT(m_showPreview);
- const QRect visibleArea = m_view->viewport()->rect();
+ 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) {
+ const ItemInfo& preview = m_previews.first();
+ replaceIcon(preview.url, 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::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 KUrl& url, m_dispatchedItems) {
+ QList<KUrl>::iterator begin = m_pendingItems.begin();
+ QList<KUrl>::iterator end = m_pendingItems.end();
+ for (QList<KUrl>::iterator it = begin; it != end; ++it) {
+ if ((*it) == url) {
+ m_pendingItems.erase(it);
+ break;
+ }
+ }
+ }
+ 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.
+ const QRect visibleArea = m_view->viewport()->rect();
KFileItemList orderedItems;
- foreach (KFileItem item, items) {
- const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
+ foreach (const KUrl& url, m_pendingItems) {
+ const QModelIndex dirIndex = m_dolphinModel->indexForUrl(url);
+ const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
const QRect itemRect = m_view->visualRect(proxyIndex);
if (itemRect.intersects(visibleArea)) {
}
}
- 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&)));
- connect(job, SIGNAL(finished(KJob*)),
- this, SLOT(slotPreviewJobFinished(KJob*)));
+ // 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;
- m_previewJobs.append(job);
+ 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();
const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
- const KUrl& itemUrl = item.url();
- foreach (KUrl url, cutUrls) {
+ const KUrl itemUrl = item.url();
+ foreach (const KUrl& url, cutUrls) {
if (url == itemUrl) {
return true;
}
KFileItemList items;
KDirLister* dirLister = m_dolphinModel->dirLister();
const KUrl::List dirs = dirLister->directories();
- foreach (KUrl url, dirs) {
+ foreach (const KUrl& url, dirs) {
items << dirLister->itemsForDir(url);
}
- foreach (KFileItem item, items) {
+ foreach (const KFileItem& item, items) {
if (isCutItem(item)) {
const QModelIndex index = m_dolphinModel->indexForItem(item);
const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
if (value.type() == QVariant::Icon) {
const QIcon icon(qvariant_cast<QIcon>(value));
- QPixmap pixmap = icon.pixmap(m_view->iconSize());
+ const QSize actualSize = icon.actualSize(m_view->iconSize());
+ QPixmap pixmap = icon.pixmap(actualSize);
// remember current pixmap for the item to be able
// to restore it when other items get cut
- CutItem cutItem;
+ ItemInfo cutItem;
cutItem.url = item.url();
cutItem.pixmap = pixmap;
m_cutItemsCache.append(cutItem);
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;
}
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();
}
}
-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);