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 if (!m_showPreview
) {
120 // the preview has been canceled in the meantime
124 // check whether the item is part of the directory lister (it is possible
125 // that a preview from an old directory lister is received)
126 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
127 bool isOldPreview
= true;
128 const KUrl::List dirs
= dirLister
->directories();
129 const QString itemDir
= item
.url().directory();
130 foreach (KUrl url
, dirs
) {
131 if (url
.path() == itemDir
) {
132 isOldPreview
= false;
140 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
141 if (idx
.isValid() && (idx
.column() == 0)) {
142 QPixmap icon
= pixmap
;
143 if (item
.isHidden()) {
144 if (!icon
.hasAlpha()) {
145 // the semitransparent operation requires having an alpha mask
146 QPixmap
alphaMask(icon
.width(), icon
.height());
148 icon
.setAlphaChannel(alphaMask
);
150 KIconEffect::semiTransparent(icon
);
153 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
154 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
155 KIconEffect iconEffect
;
156 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
157 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
159 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
164 void IconManager::slotPreviewJobFinished(KJob
* job
)
166 const int index
= m_previewJobs
.indexOf(job
);
167 m_previewJobs
.removeAt(index
);
170 void IconManager::updateCutItems()
172 // restore the icons of all previously selected items to the
174 foreach (CutItem cutItem
, m_cutItemsCache
) {
175 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
176 if (index
.isValid()) {
177 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
180 m_cutItemsCache
.clear();
182 // ... and apply an item effect to all currently cut items
183 applyCutItemEffect();
186 bool IconManager::isCutItem(const KFileItem
& item
) const
188 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
189 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
191 const KUrl
& itemUrl
= item
.url();
192 foreach (KUrl url
, cutUrls
) {
193 if (url
== itemUrl
) {
201 void IconManager::applyCutItemEffect()
203 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
204 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
209 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
210 const KUrl::List dirs
= dirLister
->directories();
211 foreach (KUrl url
, dirs
) {
212 items
<< dirLister
->itemsForDir(url
);
215 foreach (KFileItem item
, items
) {
216 if (isCutItem(item
)) {
217 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
218 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
219 if (value
.type() == QVariant::Icon
) {
220 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
221 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
223 // remember current pixmap for the item to be able
224 // to restore it when other items get cut
226 cutItem
.url
= item
.url();
227 cutItem
.pixmap
= pixmap
;
228 m_cutItemsCache
.append(cutItem
);
230 // apply icon effect to the cut item
231 KIconEffect iconEffect
;
232 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
233 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
239 void IconManager::applyHiddenItemEffect(const KFileItem
& hiddenItem
)
241 const QModelIndex index
= m_dolphinModel
->indexForItem(hiddenItem
);
242 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
243 if (value
.type() == QVariant::Icon
) {
244 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
245 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
246 KIconEffect::semiTransparent(pixmap
);
247 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
251 #include "iconmanager.moc"