]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
SVN_SILENT made messages (.desktop file)
[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_previewJobs(),
42 m_dolphinModel(0),
43 m_proxyModel(model),
44 m_cutItemsCache()
45 {
46 Q_ASSERT(m_view->iconSize().isValid()); // each view must provide its current icon size
47
48 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
49 connect(m_dolphinModel->dirLister(), SIGNAL(newItems(const KFileItemList&)),
50 this, SLOT(updateIcons(const KFileItemList&)));
51
52 QClipboard* clipboard = QApplication::clipboard();
53 connect(clipboard, SIGNAL(dataChanged()),
54 this, SLOT(updateCutItems()));
55 }
56
57 IconManager::~IconManager()
58 {
59 killJobs();
60 }
61
62
63 void IconManager::setShowPreview(bool show)
64 {
65 if (m_showPreview != show) {
66 m_showPreview = show;
67 m_cutItemsCache.clear();
68 updateCutItems();
69 }
70 }
71
72 void IconManager::updatePreviews()
73 {
74 if (!m_showPreview) {
75 return;
76 }
77
78 killJobs();
79 KFileItemList itemList;
80
81 const int rowCount = m_dolphinModel->rowCount();
82 for (int row = 0; row < rowCount; ++row) {
83 const QModelIndex index = m_dolphinModel->index(row, 0);
84 KFileItem item = m_dolphinModel->itemForIndex(index);
85 itemList.append(item);
86 }
87
88 generatePreviews(itemList);
89 }
90
91 void IconManager::updateIcons(const KFileItemList& items)
92 {
93 // make the icons of all hidden files semitransparent
94 foreach (KFileItem item, items) {
95 if (item.isHidden()) {
96 applyHiddenItemEffect(item);
97 }
98 }
99
100 if (m_showPreview) {
101 generatePreviews(items);
102 }
103 }
104
105 void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
106 {
107 Q_ASSERT(!item.isNull());
108 if (!m_showPreview) {
109 // the preview has been canceled in the meantime
110 return;
111 }
112
113 // check whether the item is part of the directory lister (it is possible
114 // that a preview from an old directory lister is received)
115 KDirLister* dirLister = m_dolphinModel->dirLister();
116 bool isOldPreview = true;
117 const KUrl::List dirs = dirLister->directories();
118 const QString itemDir = item.url().directory();
119 foreach (KUrl url, dirs) {
120 if (url.path() == itemDir) {
121 isOldPreview = false;
122 break;
123 }
124 }
125 if (isOldPreview) {
126 return;
127 }
128
129 const QModelIndex idx = m_dolphinModel->indexForItem(item);
130 if (idx.isValid() && (idx.column() == 0)) {
131 QPixmap icon = pixmap;
132
133 const QString mimeType = item.mimetype();
134 const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/'));
135 if ((mimeTypeGroup != "image") || !applyImageFrame(icon)) {
136 limitToSize(icon, m_view->iconSize());
137 }
138
139 if (item.isHidden()) {
140 if (!icon.hasAlpha()) {
141 // the semitransparent operation requires having an alpha mask
142 QPixmap alphaMask(icon.size());
143 alphaMask.fill();
144 icon.setAlphaChannel(alphaMask);
145 }
146 KIconEffect::semiTransparent(icon);
147 }
148
149 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
150 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
151 KIconEffect iconEffect;
152 icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
153 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
154 } else {
155 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
156 }
157 }
158 }
159
160 void IconManager::slotPreviewJobFinished(KJob* job)
161 {
162 const int index = m_previewJobs.indexOf(job);
163 m_previewJobs.removeAt(index);
164 }
165
166 void IconManager::updateCutItems()
167 {
168 // restore the icons of all previously selected items to the
169 // original state...
170 foreach (CutItem cutItem, m_cutItemsCache) {
171 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
172 if (index.isValid()) {
173 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
174 }
175 }
176 m_cutItemsCache.clear();
177
178 // ... and apply an item effect to all currently cut items
179 applyCutItemEffect();
180 }
181
182 void IconManager::generatePreviews(const KFileItemList &items)
183 {
184 Q_ASSERT(m_showPreview);
185 const QRect visibleArea = m_view->viewport()->rect();
186
187 // Order the items in a way that the preview for the visible items
188 // is generated first, as this improves the feeled performance a lot.
189 KFileItemList orderedItems;
190 foreach (KFileItem item, items) {
191 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
192 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
193 const QRect itemRect = m_view->visualRect(proxyIndex);
194 if (itemRect.intersects(visibleArea)) {
195 orderedItems.insert(0, item);
196 } else {
197 orderedItems.append(item);
198 }
199 }
200
201 const QSize size = m_view->iconSize();
202 KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128, 128);
203 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
204 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
205 connect(job, SIGNAL(finished(KJob*)),
206 this, SLOT(slotPreviewJobFinished(KJob*)));
207
208 m_previewJobs.append(job);
209 }
210
211 bool IconManager::isCutItem(const KFileItem& item) const
212 {
213 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
214 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
215
216 const KUrl& itemUrl = item.url();
217 foreach (KUrl url, cutUrls) {
218 if (url == itemUrl) {
219 return true;
220 }
221 }
222
223 return false;
224 }
225
226 void IconManager::applyCutItemEffect()
227 {
228 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
229 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
230 return;
231 }
232
233 KFileItemList items;
234 KDirLister* dirLister = m_dolphinModel->dirLister();
235 const KUrl::List dirs = dirLister->directories();
236 foreach (KUrl url, dirs) {
237 items << dirLister->itemsForDir(url);
238 }
239
240 foreach (KFileItem item, items) {
241 if (isCutItem(item)) {
242 const QModelIndex index = m_dolphinModel->indexForItem(item);
243 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
244 if (value.type() == QVariant::Icon) {
245 const QIcon icon(qvariant_cast<QIcon>(value));
246 QPixmap pixmap = icon.pixmap(m_view->iconSize());
247
248 // remember current pixmap for the item to be able
249 // to restore it when other items get cut
250 CutItem cutItem;
251 cutItem.url = item.url();
252 cutItem.pixmap = pixmap;
253 m_cutItemsCache.append(cutItem);
254
255 // apply icon effect to the cut item
256 KIconEffect iconEffect;
257 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
258 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
259 }
260 }
261 }
262 }
263
264 void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
265 {
266 const QModelIndex index = m_dolphinModel->indexForItem(hiddenItem);
267 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
268 if (value.type() == QVariant::Icon) {
269 const QIcon icon(qvariant_cast<QIcon>(value));
270 const QSize maxSize = m_view->iconSize();
271 QPixmap pixmap = icon.pixmap(maxSize.height(), maxSize.height()); // ignore the width
272 KIconEffect::semiTransparent(pixmap);
273 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
274 }
275 }
276
277 bool IconManager::applyImageFrame(QPixmap& icon)
278 {
279 const QSize maxSize = m_view->iconSize();
280 if ((maxSize.width() <= 24) || (maxSize.height() <= 24)) {
281 // the maximum size is too small for a frame
282 return false;
283 }
284
285 const int frame = 4;
286 const int doubleFrame = frame * 2;
287
288 // resize the icon to the maximum size minus the space required for the frame
289 limitToSize(icon, QSize(maxSize.width() - doubleFrame, maxSize.height() - doubleFrame));
290
291 QPainter painter;
292 const QPalette palette = m_view->palette();
293 QPixmap framedIcon(icon.size().width() + doubleFrame, icon.size().height() + doubleFrame);
294 framedIcon.fill(palette.color(QPalette::Normal, QPalette::Base));
295 const int width = framedIcon.width() - 1;
296 const int height = framedIcon.height() - 1;
297
298 painter.begin(&framedIcon);
299 painter.drawPixmap(frame, frame, icon);
300
301 // draw a white frame around the icon
302 painter.setPen(Qt::NoPen);
303 painter.setBrush(palette.brush(QPalette::Normal, QPalette::Base));
304 painter.drawRect(0, 0, width, frame);
305 painter.drawRect(0, height - frame, width, frame);
306 painter.drawRect(0, frame, frame, height - doubleFrame);
307 painter.drawRect(width - frame, frame, frame, height - doubleFrame);
308
309 // add a border
310 painter.setPen(palette.color(QPalette::Text));
311 painter.setBrush(Qt::NoBrush);
312 painter.drawRect(0, 0, width, height);
313 painter.drawRect(1, 1, width - 2, height - 2);
314
315 // dimm image frame by 25 %
316 painter.setPen(QColor(0, 0, 0, 64));
317 painter.drawRect(frame, frame, width - doubleFrame, height - doubleFrame);
318 painter.end();
319
320 icon = framedIcon;
321
322 // provide an alpha channel for the border
323 QPixmap alphaChannel(icon.size());
324 alphaChannel.fill();
325
326 QPainter alphaPainter(&alphaChannel);
327 alphaPainter.setBrush(Qt::NoBrush);
328 alphaPainter.setPen(QColor(32, 32, 32));
329 alphaPainter.drawRect(0, 0, width, height);
330 alphaPainter.setPen(QColor(64, 64, 64));
331 alphaPainter.drawRect(1, 1, width - 2, height - 2);
332
333 icon.setAlphaChannel(alphaChannel);
334 return true;
335 }
336
337 void IconManager::limitToSize(QPixmap& icon, const QSize& maxSize)
338 {
339 if ((icon.width() > maxSize.width()) || (icon.height() > maxSize.height())) {
340 icon = icon.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
341 }
342 }
343
344 void IconManager::killJobs()
345 {
346 foreach (KJob* job, m_previewJobs) {
347 Q_ASSERT(job != 0);
348 job->kill();
349 }
350 m_previewJobs.clear();
351 }
352
353 #include "iconmanager.moc"