]>
cloud.milkyroute.net Git - dolphin.git/blob - src/iconmanager.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "iconmanager.h"
22 #include "dolphinmodel.h"
24 #include <kiconeffect.h>
25 #include <kio/previewjob.h>
27 #include <kdirlister.h>
28 #include <konqmimedata.h>
30 #include <QApplication>
34 IconManager::IconManager(QObject
* parent
, DolphinModel
* model
) :
38 m_dolphinModel(model
),
41 connect(model
->dirLister(), SIGNAL(newItems(const KFileItemList
&)),
42 this, SLOT(generatePreviews(const KFileItemList
&)));
44 QClipboard
* clipboard
= QApplication::clipboard();
45 connect(clipboard
, SIGNAL(dataChanged()),
46 this, SLOT(updateCutItems()));
49 IconManager::~IconManager()
51 foreach (KJob
* job
, m_previewJobs
) {
55 m_previewJobs
.clear();
59 void IconManager::setShowPreview(bool show
)
61 if (m_showPreview
!= show
) {
63 m_cutItemsCache
.clear();
68 void IconManager::generatePreviews(const KFileItemList
& items
)
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
*)));
80 m_previewJobs
.append(job
);
83 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
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
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
);
102 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
108 void IconManager::slotPreviewJobFinished(KJob
* job
)
110 const int index
= m_previewJobs
.indexOf(job
);
111 m_previewJobs
.removeAt(index
);
114 void IconManager::updateCutItems()
116 // restore the icons of all previously selected items to the
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
);
124 m_cutItemsCache
.clear();
126 // ... and apply an item effect to all currently cut items
127 applyCutItemEffect();
130 bool IconManager::isCutItem(const KFileItem
& item
) const
132 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
133 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
135 const KUrl
& itemUrl
= item
.url();
136 foreach (KUrl url
, cutUrls
) {
137 if (url
== itemUrl
) {
145 void IconManager::applyCutItemEffect()
147 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
148 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
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);
161 // remember current pixmap for the item to be able
162 // to restore it when other items get cut
164 cutItem
.url
= item
.url();
165 cutItem
.pixmap
= pixmap
;
166 m_cutItemsCache
.append(cutItem
);
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
);
177 #include "iconmanager.moc"