]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
- Fix some compiler warnings.
[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.name().startsWith('.')) {
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 if (item.name().startsWith('.')) {
97 applyHiddenItemEffect(item);
98 }
99
100 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
101 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
102 const QRect itemRect = m_view->visualRect(proxyIndex);
103 if (itemRect.intersects(visibleArea)) {
104 orderedItems.insert(0, item);
105 } else {
106 orderedItems.append(item);
107 }
108 }
109
110 const QSize size = m_view->iconSize();
111 KIO::PreviewJob* job = KIO::filePreview(orderedItems, size.width(), size.height());
112 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
113 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
114 connect(job, SIGNAL(finished(KJob*)),
115 this, SLOT(slotPreviewJobFinished(KJob*)));
116
117 m_previewJobs.append(job);
118 }
119
120 void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
121 {
122 Q_ASSERT(!item.isNull());
123 KDirLister* dirLister = m_dolphinModel->dirLister();
124 if (!m_showPreview || (item.url().directory() != dirLister->url().path())) {
125 // the preview has been canceled in the meanwhile or the preview
126 // job is still working on items of an older URL, hence
127 // the item is not part of the directory model anymore
128 return;
129 }
130
131 const QModelIndex idx = m_dolphinModel->indexForItem(item);
132 if (idx.isValid() && (idx.column() == 0)) {
133 QPixmap newPixmap = pixmap;
134 if (item.name().startsWith('.')) {
135 KIconEffect::semiTransparent(newPixmap);
136 }
137
138 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
139 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
140 KIconEffect iconEffect;
141 newPixmap = iconEffect.apply(newPixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
142 m_dolphinModel->setData(idx, QIcon(newPixmap), Qt::DecorationRole);
143 } else {
144 m_dolphinModel->setData(idx, QIcon(newPixmap), Qt::DecorationRole);
145 }
146 }
147 }
148
149 void IconManager::slotPreviewJobFinished(KJob* job)
150 {
151 const int index = m_previewJobs.indexOf(job);
152 m_previewJobs.removeAt(index);
153 }
154
155 void IconManager::updateCutItems()
156 {
157 // restore the icons of all previously selected items to the
158 // original state...
159 foreach (CutItem cutItem, m_cutItemsCache) {
160 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
161 if (index.isValid()) {
162 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
163 }
164 }
165 m_cutItemsCache.clear();
166
167 // ... and apply an item effect to all currently cut items
168 applyCutItemEffect();
169 }
170
171 bool IconManager::isCutItem(const KFileItem& item) const
172 {
173 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
174 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
175
176 const KUrl& itemUrl = item.url();
177 foreach (KUrl url, cutUrls) {
178 if (url == itemUrl) {
179 return true;
180 }
181 }
182
183 return false;
184 }
185
186 void IconManager::applyCutItemEffect()
187 {
188 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
189 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
190 return;
191 }
192
193 const KFileItemList items(m_dolphinModel->dirLister()->items());
194 foreach (KFileItem item, items) {
195 if (isCutItem(item)) {
196 const QModelIndex index = m_dolphinModel->indexForItem(item);
197 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
198 if (value.type() == QVariant::Icon) {
199 const QIcon icon(qvariant_cast<QIcon>(value));
200 QPixmap pixmap = icon.pixmap(m_view->iconSize());
201
202 // remember current pixmap for the item to be able
203 // to restore it when other items get cut
204 CutItem cutItem;
205 cutItem.url = item.url();
206 cutItem.pixmap = pixmap;
207 m_cutItemsCache.append(cutItem);
208
209 // apply icon effect to the cut item
210 KIconEffect iconEffect;
211 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
212 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
213 }
214 }
215 }
216 }
217
218 void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
219 {
220 const QModelIndex index = m_dolphinModel->indexForItem(hiddenItem);
221 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
222 if (value.type() == QVariant::Icon) {
223 const QIcon icon(qvariant_cast<QIcon>(value));
224 QPixmap pixmap = icon.pixmap(m_view->iconSize());
225 KIconEffect::semiTransparent(pixmap);
226 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
227 }
228 }
229
230 #include "iconmanager.moc"