]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
Forwardport 773570:
[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 KDirLister* dirLister = m_dolphinModel->dirLister();
120 if (!m_showPreview || (item.url().directory() != dirLister->url().path())) {
121 // the preview has been canceled in the meanwhile or the preview
122 // job is still working on items of an older URL, hence
123 // the item is not part of the directory model anymore
124 return;
125 }
126
127 const QModelIndex idx = m_dolphinModel->indexForItem(item);
128 if (idx.isValid() && (idx.column() == 0)) {
129 QPixmap icon = pixmap;
130 if (item.isHidden()) {
131 if (!icon.hasAlpha()) {
132 // the semitransparent operation requires having an alpha mask
133 QPixmap alphaMask(icon.width(), icon.height());
134 alphaMask.fill();
135 icon.setAlphaChannel(alphaMask);
136 }
137 KIconEffect::semiTransparent(icon);
138 }
139
140 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
141 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
142 KIconEffect iconEffect;
143 icon = iconEffect.apply(icon, KIconLoader::Desktop, KIconLoader::DisabledState);
144 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
145 } else {
146 m_dolphinModel->setData(idx, QIcon(icon), Qt::DecorationRole);
147 }
148 }
149 }
150
151 void IconManager::slotPreviewJobFinished(KJob* job)
152 {
153 const int index = m_previewJobs.indexOf(job);
154 m_previewJobs.removeAt(index);
155 }
156
157 void IconManager::updateCutItems()
158 {
159 // restore the icons of all previously selected items to the
160 // original state...
161 foreach (CutItem cutItem, m_cutItemsCache) {
162 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
163 if (index.isValid()) {
164 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
165 }
166 }
167 m_cutItemsCache.clear();
168
169 // ... and apply an item effect to all currently cut items
170 applyCutItemEffect();
171 }
172
173 bool IconManager::isCutItem(const KFileItem& item) const
174 {
175 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
176 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
177
178 const KUrl& itemUrl = item.url();
179 foreach (KUrl url, cutUrls) {
180 if (url == itemUrl) {
181 return true;
182 }
183 }
184
185 return false;
186 }
187
188 void IconManager::applyCutItemEffect()
189 {
190 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
191 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
192 return;
193 }
194
195 const KFileItemList items(m_dolphinModel->dirLister()->items());
196 foreach (KFileItem item, items) {
197 if (isCutItem(item)) {
198 const QModelIndex index = m_dolphinModel->indexForItem(item);
199 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
200 if (value.type() == QVariant::Icon) {
201 const QIcon icon(qvariant_cast<QIcon>(value));
202 QPixmap pixmap = icon.pixmap(m_view->iconSize());
203
204 // remember current pixmap for the item to be able
205 // to restore it when other items get cut
206 CutItem cutItem;
207 cutItem.url = item.url();
208 cutItem.pixmap = pixmap;
209 m_cutItemsCache.append(cutItem);
210
211 // apply icon effect to the cut item
212 KIconEffect iconEffect;
213 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
214 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
215 }
216 }
217 }
218 }
219
220 void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
221 {
222 const QModelIndex index = m_dolphinModel->indexForItem(hiddenItem);
223 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
224 if (value.type() == QVariant::Icon) {
225 const QIcon icon(qvariant_cast<QIcon>(value));
226 QPixmap pixmap = icon.pixmap(m_view->iconSize());
227 KIconEffect::semiTransparent(pixmap);
228 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
229 }
230 }
231
232 #include "iconmanager.moc"