1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "iconmanager.h"
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
25 #include <kiconeffect.h>
26 #include <kio/previewjob.h>
27 #include <kdirlister.h>
28 #include <konqmimedata.h>
30 #include <QApplication>
31 #include <QAbstractItemView>
37 IconManager::IconManager(QAbstractItemView
* parent
, DolphinSortFilterProxyModel
* model
) :
48 Q_ASSERT(m_view
->iconSize().isValid()); // each view must provide its current icon size
50 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
51 connect(m_dolphinModel
->dirLister(), SIGNAL(newItems(const KFileItemList
&)),
52 this, SLOT(generatePreviews(const KFileItemList
&)));
54 QClipboard
* clipboard
= QApplication::clipboard();
55 connect(clipboard
, SIGNAL(dataChanged()),
56 this, SLOT(updateCutItems()));
58 m_previewTimer
= new QTimer(this);
59 connect(m_previewTimer
, SIGNAL(timeout()), this, SLOT(dispatchPreviewQueue()));
62 IconManager::~IconManager()
68 void IconManager::setShowPreview(bool show
)
70 if (m_showPreview
!= show
) {
72 m_cutItemsCache
.clear();
80 void IconManager::updatePreviews()
87 m_cutItemsCache
.clear();
89 KFileItemList itemList
;
90 const int rowCount
= m_dolphinModel
->rowCount();
91 for (int row
= 0; row
< rowCount
; ++row
) {
92 const QModelIndex index
= m_dolphinModel
->index(row
, 0);
93 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
94 itemList
.append(item
);
97 generatePreviews(itemList
);
101 void IconManager::generatePreviews(const KFileItemList
& items
)
103 applyCutItemEffect();
105 if (!m_showPreview
) {
109 const QRect visibleArea
= m_view
->viewport()->rect();
111 // Order the items in a way that the preview for the visible items
112 // is generated first, as this improves the feeled performance a lot.
113 KFileItemList orderedItems
;
114 foreach (const KFileItem
&item
, items
) {
115 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
116 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
117 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
118 if (itemRect
.intersects(visibleArea
)) {
119 orderedItems
.insert(0, item
);
121 orderedItems
.append(item
);
125 const QSize size
= m_view
->iconSize();
126 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, 128, 128);
127 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
128 this, SLOT(addToPreviewQueue(const KFileItem
&, const QPixmap
&)));
129 connect(job
, SIGNAL(finished(KJob
*)),
130 this, SLOT(slotPreviewJobFinished(KJob
*)));
132 m_previewJobs
.append(job
);
133 m_previewTimer
->start(200);
136 void IconManager::addToPreviewQueue(const KFileItem
& item
, const QPixmap
& pixmap
)
139 preview
.url
= item
.url();
140 preview
.pixmap
= pixmap
;
141 m_previews
.append(preview
);
144 void IconManager::slotPreviewJobFinished(KJob
* job
)
146 const int index
= m_previewJobs
.indexOf(job
);
147 m_previewJobs
.removeAt(index
);
150 void IconManager::updateCutItems()
152 // restore the icons of all previously selected items to the
154 foreach (const ItemInfo
& cutItem
, m_cutItemsCache
) {
155 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
156 if (index
.isValid()) {
157 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
160 m_cutItemsCache
.clear();
162 // ... and apply an item effect to all currently cut items
163 applyCutItemEffect();
166 void IconManager::dispatchPreviewQueue()
168 int previewsCount
= m_previews
.count();
169 if (previewsCount
> 0) {
170 // Applying the previews to the model must be done step by step
171 // in larger blocks: Applying a preview immediately when getting the signal
172 // 'gotPreview()' from the PreviewJob is too expensive, as a relayout
173 // of the view would be triggered for each single preview.
175 int dispatchCount
= 30;
176 if (dispatchCount
> previewsCount
) {
177 dispatchCount
= previewsCount
;
180 for (int i
= 0; i
< dispatchCount
; ++i
) {
181 const ItemInfo
& preview
= m_previews
.first();
182 replaceIcon(preview
.url
, preview
.pixmap
);
183 m_previews
.pop_front();
186 previewsCount
= m_previews
.count();
189 const bool workingPreviewJobs
= (m_previewJobs
.count() > 0);
190 if (workingPreviewJobs
) {
191 // poll for previews as long as not all preview jobs are finished
192 m_previewTimer
->start(200);
193 } else if (previewsCount
> 0) {
194 // all preview jobs are finished but there are still pending previews
195 // in the queue -> poll more aggressively
196 m_previewTimer
->start(10);
200 void IconManager::replaceIcon(const KUrl
& url
, const QPixmap
& pixmap
)
202 Q_ASSERT(url
.isValid());
203 if (!m_showPreview
) {
204 // the preview has been canceled in the meantime
208 // check whether the item is part of the directory lister (it is possible
209 // that a preview from an old directory lister is received)
210 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
211 bool isOldPreview
= true;
212 const KUrl::List dirs
= dirLister
->directories();
213 const QString itemDir
= url
.directory();
214 foreach (const KUrl
& url
, dirs
) {
215 if (url
.path() == itemDir
) {
216 isOldPreview
= false;
224 const QModelIndex idx
= m_dolphinModel
->indexForUrl(url
);
225 if (idx
.isValid() && (idx
.column() == 0)) {
226 QPixmap icon
= pixmap
;
228 const KFileItem item
= m_dolphinModel
->itemForIndex(idx
);
229 const QString mimeType
= item
.mimetype();
230 const QString mimeTypeGroup
= mimeType
.left(mimeType
.indexOf('/'));
231 if ((mimeTypeGroup
!= "image") || !applyImageFrame(icon
)) {
232 limitToSize(icon
, m_view
->iconSize());
235 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
236 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
237 // Remember the current icon in the cache for cut items before
238 // the disabled effect is applied. This makes it possible restoring
239 // the uncut version again when cutting other items.
240 QList
<ItemInfo
>::iterator begin
= m_cutItemsCache
.begin();
241 QList
<ItemInfo
>::iterator end
= m_cutItemsCache
.end();
242 for (QList
<ItemInfo
>::iterator it
= begin
; it
!= end
; ++it
) {
243 if ((*it
).url
== item
.url()) {
249 // apply the disabled effect to the icon for marking it as "cut item"
250 // and apply the icon to the item
251 KIconEffect iconEffect
;
252 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
253 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
255 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
260 bool IconManager::isCutItem(const KFileItem
& item
) const
262 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
263 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
265 const KUrl itemUrl
= item
.url();
266 foreach (const KUrl
& url
, cutUrls
) {
267 if (url
== itemUrl
) {
275 void IconManager::applyCutItemEffect()
277 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
278 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
283 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
284 const KUrl::List dirs
= dirLister
->directories();
285 foreach (const KUrl
& url
, dirs
) {
286 items
<< dirLister
->itemsForDir(url
);
289 foreach (const KFileItem
& item
, items
) {
290 if (isCutItem(item
)) {
291 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
292 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
293 if (value
.type() == QVariant::Icon
) {
294 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
295 const QSize actualSize
= icon
.actualSize(m_view
->iconSize());
296 QPixmap pixmap
= icon
.pixmap(actualSize
);
298 // remember current pixmap for the item to be able
299 // to restore it when other items get cut
301 cutItem
.url
= item
.url();
302 cutItem
.pixmap
= pixmap
;
303 m_cutItemsCache
.append(cutItem
);
305 // apply icon effect to the cut item
306 KIconEffect iconEffect
;
307 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
308 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
314 bool IconManager::applyImageFrame(QPixmap
& icon
)
316 const QSize maxSize
= m_view
->iconSize();
317 const bool applyFrame
= (maxSize
.width() > KIconLoader::SizeSmallMedium
) &&
318 (maxSize
.height() > KIconLoader::SizeSmallMedium
) &&
319 ((icon
.width() > KIconLoader::SizeLarge
) ||
320 (icon
.height() > KIconLoader::SizeLarge
));
322 // the maximum size or the image itself is too small for a frame
327 const int doubleFrame
= frame
* 2;
329 // resize the icon to the maximum size minus the space required for the frame
330 limitToSize(icon
, QSize(maxSize
.width() - doubleFrame
, maxSize
.height() - doubleFrame
));
333 const QPalette palette
= m_view
->palette();
334 QPixmap
framedIcon(icon
.size().width() + doubleFrame
, icon
.size().height() + doubleFrame
);
335 framedIcon
.fill(palette
.color(QPalette::Normal
, QPalette::Base
));
336 const int width
= framedIcon
.width() - 1;
337 const int height
= framedIcon
.height() - 1;
339 painter
.begin(&framedIcon
);
340 painter
.drawPixmap(frame
, frame
, icon
);
343 painter
.setPen(palette
.color(QPalette::Text
));
344 painter
.setBrush(Qt::NoBrush
);
345 painter
.drawRect(0, 0, width
, height
);
346 painter
.drawRect(1, 1, width
- 2, height
- 2);
348 // dim image frame by 12.5 %
349 painter
.setPen(QColor(0, 0, 0, 32));
350 painter
.drawRect(frame
, frame
, width
- doubleFrame
, height
- doubleFrame
);
355 // provide an alpha channel for the border
356 QPixmap
alphaChannel(icon
.size());
359 QPainter
alphaPainter(&alphaChannel
);
360 alphaPainter
.setBrush(Qt::NoBrush
);
361 alphaPainter
.setPen(QColor(32, 32, 32));
362 alphaPainter
.drawRect(0, 0, width
, height
);
363 alphaPainter
.setPen(QColor(64, 64, 64));
364 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
366 icon
.setAlphaChannel(alphaChannel
);
370 void IconManager::limitToSize(QPixmap
& icon
, const QSize
& maxSize
)
372 if ((icon
.width() > maxSize
.width()) || (icon
.height() > maxSize
.height())) {
373 icon
= icon
.scaled(maxSize
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
377 void IconManager::killJobs()
379 foreach (KJob
* job
, m_previewJobs
) {
383 m_previewJobs
.clear();
386 #include "iconmanager.moc"