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