X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/3d4e6938f57fcf010b4fda7255384098327eb67d..8c6e717eadc966196f72b7552012c12f46c720ce:/src/iconmanager.cpp diff --git a/src/iconmanager.cpp b/src/iconmanager.cpp index 5940c909e..cde15b00b 100644 --- a/src/iconmanager.cpp +++ b/src/iconmanager.cpp @@ -24,7 +24,6 @@ #include #include -#include #include #include @@ -32,16 +31,19 @@ #include #include #include +#include #include IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) : 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 @@ -52,15 +54,14 @@ IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* 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,102 @@ void IconManager::setShowPreview(bool show) m_showPreview = show; m_cutItemsCache.clear(); updateCutItems(); + if (show) { + updatePreviews(); + } + } +} + +void IconManager::updatePreviews() +{ + 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::updateIcons(const KFileItemList& items) { - // make the icons of all hidden files semitransparent - foreach (KFileItem item, items) { - if (item.isHidden()) { - applyHiddenItemEffect(item); + if (m_showPreview) { + generatePreviews(items); + } +} + +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(); - if (!m_showPreview) { + // ... and apply an item effect to all currently cut items + applyCutItemEffect(); +} + +void IconManager::dispatchPreviewQueue() +{ + const int previewsCount = m_previews.count(); + if (previewsCount == 0) { return; } - // generate previews + // 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 Preview& preview = m_previews.first(); + replaceIcon(preview.item, preview.pixmap); + m_previews.pop_front(); + } + + if (m_previews.count() > 0) { + // there are still pending previews; if no preview job is + // working, poll more aggressively: + const int timeout = (m_previewJobs.count() > 0) ? 200 : 10; + m_previewTimer->start(timeout); + } +} + +void IconManager::generatePreviews(const KFileItemList &items) +{ + Q_ASSERT(m_showPreview); const QRect visibleArea = m_view->viewport()->rect(); // Order the items in a way that the preview for the visible items @@ -104,37 +184,48 @@ void IconManager::updateIcons(const KFileItemList& items) } const QSize size = m_view->iconSize(); - KIO::PreviewJob* job = KIO::filePreview(orderedItems, size.width(), size.height()); + 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::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(); - if (!m_showPreview || (item.url().directory() != dirLister->url().path())) { - // the preview has been canceled in the meanwhile or the preview - // job is still working on items of an older URL, hence - // the item is not part of the directory model anymore + 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; + } + } + if (isOldPreview) { return; } const QModelIndex idx = m_dolphinModel->indexForItem(item); if (idx.isValid() && (idx.column() == 0)) { QPixmap icon = pixmap; - if (item.isHidden()) { - if (!icon.hasAlpha()) { - // the semitransparent operation requires having an alpha mask - QPixmap alphaMask(icon.width(), icon.height()); - alphaMask.fill(); - icon.setAlphaChannel(alphaMask); - } - KIconEffect::semiTransparent(icon); + + 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(); @@ -148,28 +239,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(); @@ -192,7 +261,13 @@ void IconManager::applyCutItemEffect() return; } - const KFileItemList items(m_dolphinModel->dirLister()->items()); + KFileItemList items; + KDirLister* dirLister = m_dolphinModel->dirLister(); + const KUrl::List dirs = dirLister->directories(); + foreach (KUrl url, dirs) { + items << dirLister->itemsForDir(url); + } + foreach (KFileItem item, items) { if (isCutItem(item)) { const QModelIndex index = m_dolphinModel->indexForItem(item); @@ -217,16 +292,80 @@ void IconManager::applyCutItemEffect() } } -void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem) +bool IconManager::applyImageFrame(QPixmap& icon) { - 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(value)); - QPixmap pixmap = icon.pixmap(m_view->iconSize()); - KIconEffect::semiTransparent(pixmap); - m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole); + const QSize maxSize = m_view->iconSize(); + if ((maxSize.width() <= 24) || (maxSize.height() <= 24)) { + // the maximum size is too small for a frame + return false; } + + const int frame = 4; + const int doubleFrame = frame * 2; + + // resize the icon to the maximum size minus the space required for the frame + limitToSize(icon, QSize(maxSize.width() - doubleFrame, maxSize.height() - doubleFrame)); + + QPainter painter; + const QPalette palette = m_view->palette(); + QPixmap framedIcon(icon.size().width() + doubleFrame, icon.size().height() + doubleFrame); + framedIcon.fill(palette.color(QPalette::Normal, QPalette::Base)); + const int width = framedIcon.width() - 1; + const int height = framedIcon.height() - 1; + + 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); + + // dim image frame by 12.5 % + painter.setPen(QColor(0, 0, 0, 32)); + painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame); + painter.end(); + + icon = framedIcon; + + // provide an alpha channel for the border + QPixmap alphaChannel(icon.size()); + alphaChannel.fill(); + + QPainter alphaPainter(&alphaChannel); + alphaPainter.setBrush(Qt::NoBrush); + alphaPainter.setPen(QColor(32, 32, 32)); + alphaPainter.drawRect(0, 0, width, height); + alphaPainter.setPen(QColor(64, 64, 64)); + alphaPainter.drawRect(1, 1, width - 2, height - 2); + + icon.setAlphaChannel(alphaChannel); + return true; +} + +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); + } +} + +void IconManager::killJobs() +{ + foreach (KJob* job, m_previewJobs) { + Q_ASSERT(job != 0); + job->kill(); + } + m_previewJobs.clear(); } #include "iconmanager.moc"