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
.name().startsWith('.')) {
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 if (item
.name().startsWith('.')) {
97 applyHiddenItemEffect(item
);
100 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
101 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
102 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
103 if (itemRect
.intersects(visibleArea
)) {
104 orderedItems
.insert(0, item
);
106 orderedItems
.append(item
);
110 const QSize size
= m_view
->iconSize();
111 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, size
.width(), size
.height());
112 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
113 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
114 connect(job
, SIGNAL(finished(KJob
*)),
115 this, SLOT(slotPreviewJobFinished(KJob
*)));
117 m_previewJobs
.append(job
);
120 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
122 Q_ASSERT(!item
.isNull());
123 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
124 if (!m_showPreview
|| (item
.url().directory() != dirLister
->url().path())) {
125 // the preview has been canceled in the meanwhile or the preview
126 // job is still working on items of an older URL, hence
127 // the item is not part of the directory model anymore
131 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
132 if (idx
.isValid() && (idx
.column() == 0)) {
133 QPixmap newPixmap
= pixmap
;
134 if (item
.name().startsWith('.')) {
135 KIconEffect::semiTransparent(newPixmap
);
138 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
139 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
140 KIconEffect iconEffect
;
141 newPixmap
= iconEffect
.apply(newPixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
142 m_dolphinModel
->setData(idx
, QIcon(newPixmap
), Qt::DecorationRole
);
144 m_dolphinModel
->setData(idx
, QIcon(newPixmap
), Qt::DecorationRole
);
149 void IconManager::slotPreviewJobFinished(KJob
* job
)
151 const int index
= m_previewJobs
.indexOf(job
);
152 m_previewJobs
.removeAt(index
);
155 void IconManager::updateCutItems()
157 // restore the icons of all previously selected items to the
159 foreach (CutItem cutItem
, m_cutItemsCache
) {
160 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
161 if (index
.isValid()) {
162 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
165 m_cutItemsCache
.clear();
167 // ... and apply an item effect to all currently cut items
168 applyCutItemEffect();
171 bool IconManager::isCutItem(const KFileItem
& item
) const
173 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
174 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
176 const KUrl
& itemUrl
= item
.url();
177 foreach (KUrl url
, cutUrls
) {
178 if (url
== itemUrl
) {
186 void IconManager::applyCutItemEffect()
188 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
189 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
193 const KFileItemList
items(m_dolphinModel
->dirLister()->items());
194 foreach (KFileItem item
, items
) {
195 if (isCutItem(item
)) {
196 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
197 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
198 if (value
.type() == QVariant::Icon
) {
199 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
200 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
202 // remember current pixmap for the item to be able
203 // to restore it when other items get cut
205 cutItem
.url
= item
.url();
206 cutItem
.pixmap
= pixmap
;
207 m_cutItemsCache
.append(cutItem
);
209 // apply icon effect to the cut item
210 KIconEffect iconEffect
;
211 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
212 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
218 void IconManager::applyHiddenItemEffect(const KFileItem
& hiddenItem
)
220 const QModelIndex index
= m_dolphinModel
->indexForItem(hiddenItem
);
221 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
222 if (value
.type() == QVariant::Icon
) {
223 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
224 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
225 KIconEffect::semiTransparent(pixmap
);
226 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
230 #include "iconmanager.moc"