]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
Bugfix: Currently, if you open dolphin, select a bunch of files, and right click and
[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 <kdebug.h>
28 #include <kdirlister.h>
29 #include <konqmimedata.h>
30
31 #include <QApplication>
32 #include <QAbstractItemView>
33 #include <QClipboard>
34 #include <QColor>
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, size.width(), size.height());
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 if (item.isHidden()) {
144 if (!icon.hasAlpha()) {
145 // the semitransparent operation requires having an alpha mask
146 QPixmap alphaMask(icon.width(), icon.height());
147 alphaMask.fill();
148 icon.setAlphaChannel(alphaMask);
149 }
150 KIconEffect::semiTransparent(icon);
151 }
152
153 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
154 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
155 KIconEffect iconEffect;
156 icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
157 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
158 } else {
159 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
160 }
161 }
162 }
163
164 void IconManager::slotPreviewJobFinished(KJob* job)
165 {
166 const int index = m_previewJobs.indexOf(job);
167 m_previewJobs.removeAt(index);
168 }
169
170 void IconManager::updateCutItems()
171 {
172 // restore the icons of all previously selected items to the
173 // original state...
174 foreach (CutItem cutItem, m_cutItemsCache) {
175 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
176 if (index.isValid()) {
177 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
178 }
179 }
180 m_cutItemsCache.clear();
181
182 // ... and apply an item effect to all currently cut items
183 applyCutItemEffect();
184 }
185
186 bool IconManager::isCutItem(const KFileItem& item) const
187 {
188 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
189 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
190
191 const KUrl& itemUrl = item.url();
192 foreach (KUrl url, cutUrls) {
193 if (url == itemUrl) {
194 return true;
195 }
196 }
197
198 return false;
199 }
200
201 void IconManager::applyCutItemEffect()
202 {
203 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
204 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
205 return;
206 }
207
208 KFileItemList items;
209 KDirLister* dirLister = m_dolphinModel->dirLister();
210 const KUrl::List dirs = dirLister->directories();
211 foreach (KUrl url, dirs) {
212 items << dirLister->itemsForDir(url);
213 }
214
215 foreach (KFileItem item, items) {
216 if (isCutItem(item)) {
217 const QModelIndex index = m_dolphinModel->indexForItem(item);
218 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
219 if (value.type() == QVariant::Icon) {
220 const QIcon icon(qvariant_cast<QIcon>(value));
221 QPixmap pixmap = icon.pixmap(m_view->iconSize());
222
223 // remember current pixmap for the item to be able
224 // to restore it when other items get cut
225 CutItem cutItem;
226 cutItem.url = item.url();
227 cutItem.pixmap = pixmap;
228 m_cutItemsCache.append(cutItem);
229
230 // apply icon effect to the cut item
231 KIconEffect iconEffect;
232 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
233 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
234 }
235 }
236 }
237 }
238
239 void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
240 {
241 const QModelIndex index = m_dolphinModel->indexForItem(hiddenItem);
242 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
243 if (value.type() == QVariant::Icon) {
244 const QIcon icon(qvariant_cast<QIcon>(value));
245 QPixmap pixmap = icon.pixmap(m_view->iconSize());
246 KIconEffect::semiTransparent(pixmap);
247 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
248 }
249 }
250
251 #include "iconmanager.moc"