]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
Before applying the cut item effect to a preview, assure that the cut items cache...
[dolphin.git] / src / iconmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "iconmanager.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
24
25 #include <kiconeffect.h>
26 #include <kio/previewjob.h>
27 #include <kdirlister.h>
28 #include <konqmimedata.h>
29
30 #include <QApplication>
31 #include <QAbstractItemView>
32 #include <QClipboard>
33 #include <QColor>
34 #include <QPainter>
35 #include <QIcon>
36
37 IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) :
38 QObject(parent),
39 m_showPreview(false),
40 m_view(parent),
41 m_previewTimer(0),
42 m_previewJobs(),
43 m_dolphinModel(0),
44 m_proxyModel(model),
45 m_cutItemsCache(),
46 m_previews()
47 {
48 Q_ASSERT(m_view->iconSize().isValid()); // each view must provide its current icon size
49
50 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
51 connect(m_dolphinModel->dirLister(), SIGNAL(newItems(const KFileItemList&)),
52 this, SLOT(generatePreviews(const KFileItemList&)));
53
54 QClipboard* clipboard = QApplication::clipboard();
55 connect(clipboard, SIGNAL(dataChanged()),
56 this, SLOT(updateCutItems()));
57
58 m_previewTimer = new QTimer(this);
59 connect(m_previewTimer, SIGNAL(timeout()), this, SLOT(dispatchPreviewQueue()));
60 }
61
62 IconManager::~IconManager()
63 {
64 killJobs();
65 }
66
67
68 void IconManager::setShowPreview(bool show)
69 {
70 if (m_showPreview != show) {
71 m_showPreview = show;
72 m_cutItemsCache.clear();
73 updateCutItems();
74 if (show) {
75 updatePreviews();
76 }
77 }
78 }
79
80 void IconManager::updatePreviews()
81 {
82 if (!m_showPreview) {
83 return;
84 }
85
86 killJobs();
87 KFileItemList itemList;
88
89 const int rowCount = m_dolphinModel->rowCount();
90 for (int row = 0; row < rowCount; ++row) {
91 const QModelIndex index = m_dolphinModel->index(row, 0);
92 KFileItem item = m_dolphinModel->itemForIndex(index);
93 itemList.append(item);
94 }
95
96 generatePreviews(itemList);
97 }
98
99 void IconManager::generatePreviews(const KFileItemList& items)
100 {
101 applyCutItemEffect();
102
103 if (!m_showPreview) {
104 return;
105 }
106
107 const QRect visibleArea = m_view->viewport()->rect();
108
109 // Order the items in a way that the preview for the visible items
110 // is generated first, as this improves the feeled performance a lot.
111 KFileItemList orderedItems;
112 foreach (const KFileItem &item, items) {
113 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
114 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
115 const QRect itemRect = m_view->visualRect(proxyIndex);
116 if (itemRect.intersects(visibleArea)) {
117 orderedItems.insert(0, item);
118 } else {
119 orderedItems.append(item);
120 }
121 }
122
123 const QSize size = m_view->iconSize();
124 KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128, 128);
125 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
126 this, SLOT(addToPreviewQueue(const KFileItem&, const QPixmap&)));
127 connect(job, SIGNAL(finished(KJob*)),
128 this, SLOT(slotPreviewJobFinished(KJob*)));
129
130 m_previewJobs.append(job);
131 m_previewTimer->start(200);
132 }
133
134 void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap)
135 {
136 ItemInfo preview;
137 preview.url = item.url();
138 preview.pixmap = pixmap;
139 m_previews.append(preview);
140 }
141
142 void IconManager::slotPreviewJobFinished(KJob* job)
143 {
144 const int index = m_previewJobs.indexOf(job);
145 m_previewJobs.removeAt(index);
146 }
147
148 void IconManager::updateCutItems()
149 {
150 // restore the icons of all previously selected items to the
151 // original state...
152 foreach (const ItemInfo& cutItem, m_cutItemsCache) {
153 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
154 if (index.isValid()) {
155 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
156 }
157 }
158 m_cutItemsCache.clear();
159
160 // ... and apply an item effect to all currently cut items
161 applyCutItemEffect();
162 }
163
164 void IconManager::dispatchPreviewQueue()
165 {
166 int previewsCount = m_previews.count();
167 if (previewsCount > 0) {
168 // Applying the previews to the model must be done step by step
169 // in larger blocks: Applying a preview immediately when getting the signal
170 // 'gotPreview()' from the PreviewJob is too expensive, as a relayout
171 // of the view would be triggered for each single preview.
172
173 int dispatchCount = 30;
174 if (dispatchCount > previewsCount) {
175 dispatchCount = previewsCount;
176 }
177
178 for (int i = 0; i < dispatchCount; ++i) {
179 const ItemInfo& preview = m_previews.first();
180 replaceIcon(preview.url, preview.pixmap);
181 m_previews.pop_front();
182 }
183
184 previewsCount = m_previews.count();
185 }
186
187 const bool workingPreviewJobs = (m_previewJobs.count() > 0);
188 if (workingPreviewJobs) {
189 // poll for previews as long as not all preview jobs are finished
190 m_previewTimer->start(200);
191 } else if (previewsCount > 0) {
192 // all preview jobs are finished but there are still pending previews
193 // in the queue -> poll more aggressively
194 m_previewTimer->start(10);
195 }
196 }
197
198 void IconManager::replaceIcon(const KUrl& url, const QPixmap& pixmap)
199 {
200 Q_ASSERT(url.isValid());
201 if (!m_showPreview) {
202 // the preview has been canceled in the meantime
203 return;
204 }
205
206 // check whether the item is part of the directory lister (it is possible
207 // that a preview from an old directory lister is received)
208 KDirLister* dirLister = m_dolphinModel->dirLister();
209 bool isOldPreview = true;
210 const KUrl::List dirs = dirLister->directories();
211 const QString itemDir = url.directory();
212 foreach (const KUrl& url, dirs) {
213 if (url.path() == itemDir) {
214 isOldPreview = false;
215 break;
216 }
217 }
218 if (isOldPreview) {
219 return;
220 }
221
222 const QModelIndex idx = m_dolphinModel->indexForUrl(url);
223 if (idx.isValid() && (idx.column() == 0)) {
224 QPixmap icon = pixmap;
225
226 const KFileItem item = m_dolphinModel->itemForIndex(idx);
227 const QString mimeType = item.mimetype();
228 const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
229 if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
230 limitToSize(icon, m_view->iconSize());
231 }
232
233 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
234 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
235 // Remember the current icon in the cache for cut items before
236 // the disabled effect is applied. This makes it possible restoring
237 // the uncut version again when cutting other items.
238 QList<ItemInfo>::iterator begin = m_cutItemsCache.begin();
239 QList<ItemInfo>::iterator end = m_cutItemsCache.end();
240 for (QList<ItemInfo>::iterator it = begin; it != end; ++it) {
241 if ((*it).url == item.url()) {
242 (*it).pixmap = icon;
243 break;
244 }
245 }
246
247 // apply the disabled effect to the icon for marking it as "cut item"
248 // and apply the icon to the item
249 KIconEffect iconEffect;
250 icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
251 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
252 } else {
253 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
254 }
255 }
256 }
257
258 bool IconManager::isCutItem(const KFileItem& item) const
259 {
260 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
261 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
262
263 const KUrl itemUrl = item.url();
264 foreach (const KUrl& url, cutUrls) {
265 if (url == itemUrl) {
266 return true;
267 }
268 }
269
270 return false;
271 }
272
273 void IconManager::applyCutItemEffect()
274 {
275 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
276 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
277 return;
278 }
279
280 KFileItemList items;
281 KDirLister* dirLister = m_dolphinModel->dirLister();
282 const KUrl::List dirs = dirLister->directories();
283 foreach (const KUrl& url, dirs) {
284 items << dirLister->itemsForDir(url);
285 }
286
287 foreach (const KFileItem& item, items) {
288 if (isCutItem(item)) {
289 const QModelIndex index = m_dolphinModel->indexForItem(item);
290 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
291 if (value.type() == QVariant::Icon) {
292 const QIcon icon(qvariant_cast<QIcon>(value));
293 const QSize actualSize = icon.actualSize(m_view->iconSize());
294 QPixmap pixmap = icon.pixmap(actualSize);
295
296 // remember current pixmap for the item to be able
297 // to restore it when other items get cut
298 ItemInfo cutItem;
299 cutItem.url = item.url();
300 cutItem.pixmap = pixmap;
301 m_cutItemsCache.append(cutItem);
302
303 // apply icon effect to the cut item
304 KIconEffect iconEffect;
305 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
306 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
307 }
308 }
309 }
310 }
311
312 bool IconManager::applyImageFrame(QPixmap& icon)
313 {
314 const QSize maxSize = m_view->iconSize();
315 const bool applyFrame = (maxSize.width() > KIconLoader::SizeSmallMedium) &&
316 (maxSize.height() > KIconLoader::SizeSmallMedium) &&
317 ((icon.width() > KIconLoader::SizeLarge) ||
318 (icon.height() > KIconLoader::SizeLarge));
319 if (!applyFrame) {
320 // the maximum size or the image itself is too small for a frame
321 return false;
322 }
323
324 const int frame = 4;
325 const int doubleFrame = frame * 2;
326
327 // resize the icon to the maximum size minus the space required for the frame
328 limitToSize(icon, QSize(maxSize.width() - doubleFrame, maxSize.height() - doubleFrame));
329
330 QPainter painter;
331 const QPalette palette = m_view->palette();
332 QPixmap framedIcon(icon.size().width() + doubleFrame, icon.size().height() + doubleFrame);
333 framedIcon.fill(palette.color(QPalette::Normal, QPalette::Base));
334 const int width = framedIcon.width() - 1;
335 const int height = framedIcon.height() - 1;
336
337 painter.begin(&framedIcon);
338 painter.drawPixmap(frame, frame, icon);
339
340 // add a border
341 painter.setPen(palette.color(QPalette::Text));
342 painter.setBrush(Qt::NoBrush);
343 painter.drawRect(0, 0, width, height);
344 painter.drawRect(1, 1, width - 2, height - 2);
345
346 // dim image frame by 12.5 %
347 painter.setPen(QColor(0, 0, 0, 32));
348 painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame);
349 painter.end();
350
351 icon = framedIcon;
352
353 // provide an alpha channel for the border
354 QPixmap alphaChannel(icon.size());
355 alphaChannel.fill();
356
357 QPainter alphaPainter(&alphaChannel);
358 alphaPainter.setBrush(Qt::NoBrush);
359 alphaPainter.setPen(QColor(32, 32, 32));
360 alphaPainter.drawRect(0, 0, width, height);
361 alphaPainter.setPen(QColor(64, 64, 64));
362 alphaPainter.drawRect(1, 1, width - 2, height - 2);
363
364 icon.setAlphaChannel(alphaChannel);
365 return true;
366 }
367
368 void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
369 {
370 if ((icon.width() > maxSize.width()) || (icon.height() > maxSize.height())) {
371 icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
372 }
373 }
374
375 void IconManager::killJobs()
376 {
377 foreach (KJob* job, m_previewJobs) {
378 Q_ASSERT(job != 0);
379 job->kill();
380 }
381 m_previewJobs.clear();
382 }
383
384 #include "iconmanager.moc"