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"
23 #include "dolphinsortfilterproxymodel.h"
25 #include <kiconeffect.h>
26 #include <kio/previewjob.h>
28 #include <kdirlister.h>
29 #include <konqmimedata.h>
31 #include <QApplication>
32 #include <QAbstractItemView>
37 IconManager::IconManager(QAbstractItemView
* parent
, DolphinSortFilterProxyModel
* model
) :
46 Q_ASSERT(m_view
->iconSize().isValid()); // each view must provide its current icon size
48 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
49 connect(m_dolphinModel
->dirLister(), SIGNAL(newItems(const KFileItemList
&)),
50 this, SLOT(updateIcons(const KFileItemList
&)));
52 QClipboard
* clipboard
= QApplication::clipboard();
53 connect(clipboard
, SIGNAL(dataChanged()),
54 this, SLOT(updateCutItems()));
57 IconManager::~IconManager()
59 foreach (KJob
* job
, m_previewJobs
) {
63 m_previewJobs
.clear();
67 void IconManager::setShowPreview(bool show
)
69 if (m_showPreview
!= show
) {
71 m_cutItemsCache
.clear();
76 void IconManager::updateIcons(const KFileItemList
& items
)
78 // make the icons of all hidden files semitransparent
79 foreach (KFileItem item
, items
) {
80 if (item
.isHidden()) {
81 applyHiddenItemEffect(item
);
90 const QRect visibleArea
= m_view
->viewport()->rect();
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
);
102 orderedItems
.append(item
);
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
*)));
113 m_previewJobs
.append(job
);
116 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
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
127 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
128 if (idx
.isValid() && (idx
.column() == 0)) {
129 QPixmap icon
= pixmap
;
130 if (item
.isHidden()) {
131 if (!icon
.hasAlpha()) {
132 // the semitransparent operation requires having an alpha mask
133 QPixmap
alphaMask(icon
.width(), icon
.height());
135 icon
.setAlphaChannel(alphaMask
);
137 KIconEffect::semiTransparent(icon
);
140 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
141 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
142 KIconEffect iconEffect
;
143 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
144 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
146 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
151 void IconManager::slotPreviewJobFinished(KJob
* job
)
153 const int index
= m_previewJobs
.indexOf(job
);
154 m_previewJobs
.removeAt(index
);
157 void IconManager::updateCutItems()
159 // restore the icons of all previously selected items to the
161 foreach (CutItem cutItem
, m_cutItemsCache
) {
162 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
163 if (index
.isValid()) {
164 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
167 m_cutItemsCache
.clear();
169 // ... and apply an item effect to all currently cut items
170 applyCutItemEffect();
173 bool IconManager::isCutItem(const KFileItem
& item
) const
175 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
176 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
178 const KUrl
& itemUrl
= item
.url();
179 foreach (KUrl url
, cutUrls
) {
180 if (url
== itemUrl
) {
188 void IconManager::applyCutItemEffect()
190 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
191 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
195 const KFileItemList
items(m_dolphinModel
->dirLister()->items());
196 foreach (KFileItem item
, items
) {
197 if (isCutItem(item
)) {
198 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
199 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
200 if (value
.type() == QVariant::Icon
) {
201 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
202 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
204 // remember current pixmap for the item to be able
205 // to restore it when other items get cut
207 cutItem
.url
= item
.url();
208 cutItem
.pixmap
= pixmap
;
209 m_cutItemsCache
.append(cutItem
);
211 // apply icon effect to the cut item
212 KIconEffect iconEffect
;
213 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
214 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
220 void IconManager::applyHiddenItemEffect(const KFileItem
& hiddenItem
)
222 const QModelIndex index
= m_dolphinModel
->indexForItem(hiddenItem
);
223 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
224 if (value
.type() == QVariant::Icon
) {
225 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
226 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
227 KIconEffect::semiTransparent(pixmap
);
228 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
232 #include "iconmanager.moc"