]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
take care to restart the timer even when no previews are in the queue: as long as...
[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(updateIcons(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::updateIcons(const KFileItemList& items)
100 {
101 if (m_showPreview) {
102 generatePreviews(items);
103 }
104 }
105
106 void IconManager::addToPreviewQueue(const KFileItem& item, const QPixmap& pixmap)
107 {
108 Preview preview;
109 preview.item = item;
110 preview.pixmap = pixmap;
111 m_previews.append(preview);
112 }
113
114 void IconManager::slotPreviewJobFinished(KJob* job)
115 {
116 const int index = m_previewJobs.indexOf(job);
117 m_previewJobs.removeAt(index);
118 }
119
120 void IconManager::updateCutItems()
121 {
122 // restore the icons of all previously selected items to the
123 // original state...
124 foreach (CutItem cutItem, m_cutItemsCache) {
125 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
126 if (index.isValid()) {
127 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
128 }
129 }
130 m_cutItemsCache.clear();
131
132 // ... and apply an item effect to all currently cut items
133 applyCutItemEffect();
134 }
135
136 void IconManager::dispatchPreviewQueue()
137 {
138 int previewsCount = m_previews.count();
139 if (previewsCount > 0) {
140 // Applying the previews to the model must be done step by step
141 // in larger blocks: Applying a preview immediately when getting the signal
142 // 'gotPreview()' from the PreviewJob is too expensive, as a relayout
143 // of the view would be triggered for each single preview.
144
145 int dispatchCount = 30;
146 if (dispatchCount > m_previews.count()) {
147 dispatchCount = m_previews.count();
148 }
149
150 for (int i = 0; i < dispatchCount; ++i) {
151 const Preview& preview = m_previews.first();
152 replaceIcon(preview.item, preview.pixmap);
153 m_previews.pop_front();
154 }
155
156 previewsCount = m_previews.count();
157 }
158
159 const bool workingPreviewJobs = (m_previewJobs.count() > 0);
160 if (workingPreviewJobs) {
161 // poll for previews as long as not all preview jobs are finished
162 m_previewTimer->start(200);
163 } else if (previewsCount > 0) {
164 // all preview jobs are finished but there are still pending previews
165 // in the queue -> poll more aggressively
166 m_previewTimer->start(10);
167 }
168 }
169
170 void IconManager::generatePreviews(const KFileItemList &items)
171 {
172 Q_ASSERT(m_showPreview);
173 const QRect visibleArea = m_view->viewport()->rect();
174
175 // Order the items in a way that the preview for the visible items
176 // is generated first, as this improves the feeled performance a lot.
177 KFileItemList orderedItems;
178 foreach (KFileItem item, items) {
179 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
180 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
181 const QRect itemRect = m_view->visualRect(proxyIndex);
182 if (itemRect.intersects(visibleArea)) {
183 orderedItems.insert(0, item);
184 } else {
185 orderedItems.append(item);
186 }
187 }
188
189 const QSize size = m_view->iconSize();
190 KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128, 128);
191 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
192 this, SLOT(addToPreviewQueue(const KFileItem&, const QPixmap&)));
193 connect(job, SIGNAL(finished(KJob*)),
194 this, SLOT(slotPreviewJobFinished(KJob*)));
195
196 m_previewJobs.append(job);
197 m_previewTimer->start(200);
198 }
199
200 void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
201 {
202 Q_ASSERT(!item.isNull());
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 = item.url().directory();
214 foreach (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->indexForItem(item);
225 if (idx.isValid() && (idx.column() == 0)) {
226 QPixmap icon = pixmap;
227
228 const QString mimeType = item.mimetype();
229 const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
230 if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
231 limitToSize(icon, m_view->iconSize());
232 }
233
234 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
235 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
236 KIconEffect iconEffect;
237 icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
238 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
239 } else {
240 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
241 }
242 }
243 }
244
245 bool IconManager::isCutItem(const KFileItem& item) const
246 {
247 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
248 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
249
250 const KUrl& itemUrl = item.url();
251 foreach (KUrl url, cutUrls) {
252 if (url == itemUrl) {
253 return true;
254 }
255 }
256
257 return false;
258 }
259
260 void IconManager::applyCutItemEffect()
261 {
262 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
263 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
264 return;
265 }
266
267 KFileItemList items;
268 KDirLister* dirLister = m_dolphinModel->dirLister();
269 const KUrl::List dirs = dirLister->directories();
270 foreach (KUrl url, dirs) {
271 items << dirLister->itemsForDir(url);
272 }
273
274 foreach (KFileItem item, items) {
275 if (isCutItem(item)) {
276 const QModelIndex index = m_dolphinModel->indexForItem(item);
277 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
278 if (value.type() == QVariant::Icon) {
279 const QIcon icon(qvariant_cast<QIcon>(value));
280 QPixmap pixmap = icon.pixmap(m_view->iconSize());
281
282 // remember current pixmap for the item to be able
283 // to restore it when other items get cut
284 CutItem cutItem;
285 cutItem.url = item.url();
286 cutItem.pixmap = pixmap;
287 m_cutItemsCache.append(cutItem);
288
289 // apply icon effect to the cut item
290 KIconEffect iconEffect;
291 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
292 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
293 }
294 }
295 }
296 }
297
298 bool IconManager::applyImageFrame(QPixmap& icon)
299 {
300 const QSize maxSize = m_view->iconSize();
301 if ((maxSize.width() <= 24) || (maxSize.height() <= 24)) {
302 // the maximum size is too small for a frame
303 return false;
304 }
305
306 const int frame = 4;
307 const int doubleFrame = frame * 2;
308
309 // resize the icon to the maximum size minus the space required for the frame
310 limitToSize(icon, QSize(maxSize.width() - doubleFrame, maxSize.height() - doubleFrame));
311
312 QPainter painter;
313 const QPalette palette = m_view->palette();
314 QPixmap framedIcon(icon.size().width() + doubleFrame, icon.size().height() + doubleFrame);
315 framedIcon.fill(palette.color(QPalette::Normal, QPalette::Base));
316 const int width = framedIcon.width() - 1;
317 const int height = framedIcon.height() - 1;
318
319 painter.begin(&framedIcon);
320 painter.drawPixmap(frame, frame, icon);
321
322 // draw a white frame around the icon
323 painter.setPen(Qt::NoPen);
324 painter.setBrush(palette.brush(QPalette::Normal, QPalette::Base));
325 painter.drawRect(0, 0, width, frame);
326 painter.drawRect(0, height - frame, width, frame);
327 painter.drawRect(0, frame, frame, height - doubleFrame);
328 painter.drawRect(width - frame, frame, frame, height - doubleFrame);
329
330 // add a border
331 painter.setPen(palette.color(QPalette::Text));
332 painter.setBrush(Qt::NoBrush);
333 painter.drawRect(0, 0, width, height);
334 painter.drawRect(1, 1, width - 2, height - 2);
335
336 // dim image frame by 12.5 %
337 painter.setPen(QColor(0, 0, 0, 32));
338 painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame);
339 painter.end();
340
341 icon = framedIcon;
342
343 // provide an alpha channel for the border
344 QPixmap alphaChannel(icon.size());
345 alphaChannel.fill();
346
347 QPainter alphaPainter(&alphaChannel);
348 alphaPainter.setBrush(Qt::NoBrush);
349 alphaPainter.setPen(QColor(32, 32, 32));
350 alphaPainter.drawRect(0, 0, width, height);
351 alphaPainter.setPen(QColor(64, 64, 64));
352 alphaPainter.drawRect(1, 1, width - 2, height - 2);
353
354 icon.setAlphaChannel(alphaChannel);
355 return true;
356 }
357
358 void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
359 {
360 if ((icon.width() > maxSize.width()) || (icon.height() > maxSize.height())) {
361 icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
362 }
363 }
364
365 void IconManager::killJobs()
366 {
367 foreach (KJob* job, m_previewJobs) {
368 Q_ASSERT(job != 0);
369 job->kill();
370 }
371 m_previewJobs.clear();
372 }
373
374 #include "iconmanager.moc"