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