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