]> cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
SVN_SILENT made messages (.desktop file)
[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 <QIcon>
35
36 IconManager::IconManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) :
37 QObject(parent),
38 m_showPreview(false),
39 m_view(parent),
40 m_previewJobs(),
41 m_dolphinModel(0),
42 m_proxyModel(model),
43 m_cutItemsCache()
44 {
45 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
46 connect(m_dolphinModel->dirLister(), SIGNAL(newItems(const KFileItemList&)),
47 this, SLOT(generatePreviews(const KFileItemList&)));
48
49 QClipboard* clipboard = QApplication::clipboard();
50 connect(clipboard, SIGNAL(dataChanged()),
51 this, SLOT(updateCutItems()));
52 }
53
54 IconManager::~IconManager()
55 {
56 foreach (KJob* job, m_previewJobs) {
57 Q_ASSERT(job != 0);
58 job->kill();
59 }
60 m_previewJobs.clear();
61 }
62
63
64 void IconManager::setShowPreview(bool show)
65 {
66 if (m_showPreview != show) {
67 m_showPreview = show;
68 m_cutItemsCache.clear();
69 updateCutItems();
70 }
71 }
72
73 void IconManager::generatePreviews(const KFileItemList& items)
74 {
75 if (!m_showPreview) {
76 return;
77 }
78
79 const QRect visibleArea = m_view->viewport()->rect();
80
81 // Order the items in a way that the preview for the visible items
82 // is generated first, as this improves the feeled performance a lot.
83 KFileItemList orderedItems;
84 foreach (KFileItem item, items) {
85 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
86 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
87 const QRect itemRect = m_view->visualRect(proxyIndex);
88 if (itemRect.intersects(visibleArea)) {
89 orderedItems.insert(0, item);
90 } else {
91 orderedItems.append(item);
92 }
93 }
94
95 KIO::PreviewJob* job = KIO::filePreview(orderedItems, 128);
96 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
97 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
98 connect(job, SIGNAL(finished(KJob*)),
99 this, SLOT(slotPreviewJobFinished(KJob*)));
100
101 m_previewJobs.append(job);
102 }
103
104 void IconManager::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
105 {
106 Q_ASSERT(!item.isNull());
107 KDirLister* dirLister = m_dolphinModel->dirLister();
108 if (!m_showPreview || (item.url().directory() != dirLister->url().path())) {
109 // the preview has been canceled in the meanwhile or the preview
110 // job is still working on items of an older URL, hence
111 // the item is not part of the directory model anymore
112 return;
113 }
114
115 const QModelIndex idx = m_dolphinModel->indexForItem(item);
116 if (idx.isValid() && (idx.column() == 0)) {
117 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
118 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
119 KIconEffect iconEffect;
120 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
121 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
122 } else {
123 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
124 }
125 }
126 }
127
128 void IconManager::slotPreviewJobFinished(KJob* job)
129 {
130 const int index = m_previewJobs.indexOf(job);
131 m_previewJobs.removeAt(index);
132 }
133
134 void IconManager::updateCutItems()
135 {
136 // restore the icons of all previously selected items to the
137 // original state...
138 foreach (CutItem cutItem, m_cutItemsCache) {
139 const QModelIndex index = m_dolphinModel->indexForUrl(cutItem.url);
140 if (index.isValid()) {
141 m_dolphinModel->setData(index, QIcon(cutItem.pixmap), Qt::DecorationRole);
142 }
143 }
144 m_cutItemsCache.clear();
145
146 // ... and apply an item effect to all currently cut items
147 applyCutItemEffect();
148 }
149
150 bool IconManager::isCutItem(const KFileItem& item) const
151 {
152 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
153 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
154
155 const KUrl& itemUrl = item.url();
156 foreach (KUrl url, cutUrls) {
157 if (url == itemUrl) {
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165 void IconManager::applyCutItemEffect()
166 {
167 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
168 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
169 return;
170 }
171
172 const KFileItemList items(m_dolphinModel->dirLister()->items());
173 foreach (KFileItem item, items) {
174 if (isCutItem(item)) {
175 const QModelIndex index = m_dolphinModel->indexForItem(item);
176 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
177 if (value.type() == QVariant::Icon) {
178 const QIcon icon(qvariant_cast<QIcon>(value));
179 QPixmap pixmap = icon.pixmap(128, 128);
180
181 // remember current pixmap for the item to be able
182 // to restore it when other items get cut
183 CutItem cutItem;
184 cutItem.url = item.url();
185 cutItem.pixmap = pixmap;
186 m_cutItemsCache.append(cutItem);
187
188 // apply icon effect to the cut item
189 KIconEffect iconEffect;
190 pixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
191 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
192 }
193 }
194 }
195 }
196
197 #include "iconmanager.moc"