]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
assure that the cut item cache gets updated too when invoking IconManager::updatePrev...
[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 m_cutItemsCache.clear();
88
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);
95 }
96
97 generatePreviews(itemList);
98 updateCutItems();
99 }
100
101 void IconManager::generatePreviews(const KFileItemList& items)
102 {
103 applyCutItemEffect();
104
105 if (!m_showPreview) {
106 return;
107 }
108
109 const QRect visibleArea = m_view->viewport()->rect();
110
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);
120 } else {
121 orderedItems.append(item);
122 }
123 }
124
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*)));
131
132 m_previewJobs.append(job);
133 m_previewTimer->start(200);
134 }
135
136 void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap)
137 {
138 ItemInfo preview;
139 preview.url = item.url();
140 preview.pixmap = pixmap;
141 m_previews.append(preview);
142 }
143
144 void IconManager::slotPreviewJobFinished(KJob* job)
145 {
146 const int index = m_previewJobs.indexOf(job);
147 m_previewJobs.removeAt(index);
148 }
149
150 void IconManager::updateCutItems()
151 {
152 // restore the icons of all previously selected items to the
153 // original state...
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);
158 }
159 }
160 m_cutItemsCache.clear();
161
162 // ... and apply an item effect to all currently cut items
163 applyCutItemEffect();
164 }
165
166 void IconManager::dispatchPreviewQueue()
167 {
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.
174
175 int dispatchCount = 30;
176 if (dispatchCount > previewsCount) {
177 dispatchCount = previewsCount;
178 }
179
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();
184 }
185
186 previewsCount = m_previews.count();
187 }
188
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);
197 }
198 }
199
200 void IconManager::replaceIcon(const KUrl& url, const QPixmap& pixmap)
201 {
202 Q_ASSERT(url.isValid());
203 if (!m_showPreview) {
204 // the preview has been canceled in the meantime
205 return;
206 }
207
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;
217 break;
218 }
219 }
220 if (isOldPreview) {
221 return;
222 }
223
224 const QModelIndex idx = m_dolphinModel->indexForUrl(url);
225 if (idx.isValid() && (idx.column() == 0)) {
226 QPixmap icon = pixmap;
227
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());
233 }
234
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()) {
244 (*it).pixmap = icon;
245 break;
246 }
247 }
248
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);
254 } else {
255 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
256 }
257 }
258 }
259
260 bool IconManager::isCutItem(const KFileItem& item) const
261 {
262 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
263 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
264
265 const KUrl itemUrl = item.url();
266 foreach (const KUrl& url, cutUrls) {
267 if (url == itemUrl) {
268 return true;
269 }
270 }
271
272 return false;
273 }
274
275 void IconManager::applyCutItemEffect()
276 {
277 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
278 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
279 return;
280 }
281
282 KFileItemList items;
283 KDirLister* dirLister = m_dolphinModel->dirLister();
284 const KUrl::List dirs = dirLister->directories();
285 foreach (const KUrl& url, dirs) {
286 items << dirLister->itemsForDir(url);
287 }
288
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);
297
298 // remember current pixmap for the item to be able
299 // to restore it when other items get cut
300 ItemInfo cutItem;
301 cutItem.url = item.url();
302 cutItem.pixmap = pixmap;
303 m_cutItemsCache.append(cutItem);
304
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);
309 }
310 }
311 }
312 }
313
314 bool IconManager::applyImageFrame(QPixmap& icon)
315 {
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));
321 if (!applyFrame) {
322 // the maximum size or the image itself is too small for a frame
323 return false;
324 }
325
326 const int frame = 4;
327 const int doubleFrame = frame * 2;
328
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));
331
332 QPainter painter;
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;
338
339 painter.begin(&framedIcon);
340 painter.drawPixmap(frame, frame, icon);
341
342 // add a border
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);
347
348 // dim image frame by 12.5 %
349 painter.setPen(QColor(0, 0, 0, 32));
350 painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame);
351 painter.end();
352
353 icon = framedIcon;
354
355 // provide an alpha channel for the border
356 QPixmap alphaChannel(icon.size());
357 alphaChannel.fill();
358
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);
365
366 icon.setAlphaChannel(alphaChannel);
367 return true;
368 }
369
370 void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
371 {
372 if ((icon.width() > maxSize.width()) || (icon.height() > maxSize.height())) {
373 icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
374 }
375 }
376
377 void IconManager::killJobs()
378 {
379 foreach (KJob* job, m_previewJobs) {
380 Q_ASSERT(job != 0);
381 job->kill();
382 }
383 m_previewJobs.clear();
384 }
385
386 #include "iconmanager.moc"