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