]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
* replace item.name().startsWith('.') by item.isHidden()
[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 newPixmap = pixmap;
130 if (item.isHidden()) {
131 KIconEffect::semiTransparent(newPixmap);
132 }
133
134 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
135 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
136 KIconEffect iconEffect;
137 newPixmap = iconEffect.apply(newPixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
138 m_dolphinModel->setData(idx, QIcon(newPixmap), Qt::DecorationRole);
139 } else {
140 m_dolphinModel->setData(idx, QIcon(newPixmap), Qt::DecorationRole);
141 }
142 }
143 }
144
145 void IconManager::slotPreviewJobFinished(KJob* job)
146 {
147 const int index = m_previewJobs.indexOf(job);
148 m_previewJobs.removeAt(index);
149 }
150
151 void IconManager::updateCutItems()
152 {
153 // restore the icons of all previously selected items to the
154 // original state...
155 foreach (CutItem cutItem, m_cutItemsCache) {
156 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
157 if (index.isValid()) {
158 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
159 }
160 }
161 m_cutItemsCache.clear();
162
163 // ... and apply an item effect to all currently cut items
164 applyCutItemEffect();
165 }
166
167 bool IconManager::isCutItem(const KFileItem& item) const
168 {
169 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
170 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
171
172 const KUrl& itemUrl = item.url();
173 foreach (KUrl url, cutUrls) {
174 if (url == itemUrl) {
175 return true;
176 }
177 }
178
179 return false;
180 }
181
182 void IconManager::applyCutItemEffect()
183 {
184 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
185 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
186 return;
187 }
188
189 const KFileItemList items(m_dolphinModel->dirLister()->items());
190 foreach (KFileItem item, items) {
191 if (isCutItem(item)) {
192 const QModelIndex index = m_dolphinModel->indexForItem(item);
193 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
194 if (value.type() == QVariant::Icon) {
195 const QIcon icon(qvariant_cast<QIcon>(value));
196 QPixmap pixmap = icon.pixmap(m_view->iconSize());
197
198 // remember current pixmap for the item to be able
199 // to restore it when other items get cut
200 CutItem cutItem;
201 cutItem.url = item.url();
202 cutItem.pixmap = pixmap;
203 m_cutItemsCache.append(cutItem);
204
205 // apply icon effect to the cut item
206 KIconEffect iconEffect;
207 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
208 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
209 }
210 }
211 }
212 }
213
214 void IconManager::applyHiddenItemEffect(const KFileItem& hiddenItem)
215 {
216 const QModelIndex index = m_dolphinModel->indexForItem(hiddenItem);
217 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
218 if (value.type() == QVariant::Icon) {
219 const QIcon icon(qvariant_cast<QIcon>(value));
220 QPixmap pixmap = icon.pixmap(m_view->iconSize());
221 KIconEffect::semiTransparent(pixmap);
222 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
223 }
224 }
225
226 #include "iconmanager.moc"