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>
36 IconManager::IconManager(QAbstractItemView
* parent
, DolphinSortFilterProxyModel
* model
) :
45 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
46 connect(m_dolphinModel
->dirLister(), SIGNAL(newItems(const KFileItemList
&)),
47 this, SLOT(generatePreviews(const KFileItemList
&)));
49 QClipboard
* clipboard
= QApplication::clipboard();
50 connect(clipboard
, SIGNAL(dataChanged()),
51 this, SLOT(updateCutItems()));
54 IconManager::~IconManager()
56 foreach (KJob
* job
, m_previewJobs
) {
60 m_previewJobs
.clear();
64 void IconManager::setShowPreview(bool show
)
66 if (m_showPreview
!= show
) {
68 m_cutItemsCache
.clear();
73 void IconManager::generatePreviews(const KFileItemList
& items
)
79 const QRect visibleArea
= m_view
->viewport()->rect();
81 // Order the items in a way that the preview for the visible items
82 // is generated first, as this improves the feeled performance a lot.
83 KFileItemList orderedItems
;
84 foreach (KFileItem item
, items
) {
85 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
86 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
87 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
88 if (itemRect
.intersects(visibleArea
)) {
89 orderedItems
.insert(0, item
);
91 orderedItems
.append(item
);
95 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, 128);
96 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
97 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
98 connect(job
, SIGNAL(finished(KJob
*)),
99 this, SLOT(slotPreviewJobFinished(KJob
*)));
101 m_previewJobs
.append(job
);
104 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
106 Q_ASSERT(!item
.isNull());
107 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
108 if (!m_showPreview
|| (item
.url().directory() != dirLister
->url().path())) {
109 // the preview has been canceled in the meanwhile or the preview
110 // job is still working on items of an older URL, hence
111 // the item is not part of the directory model anymore
115 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
116 if (idx
.isValid() && (idx
.column() == 0)) {
117 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
118 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
119 KIconEffect iconEffect
;
120 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
121 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
123 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
128 void IconManager::slotPreviewJobFinished(KJob
* job
)
130 const int index
= m_previewJobs
.indexOf(job
);
131 m_previewJobs
.removeAt(index
);
134 void IconManager::updateCutItems()
136 // restore the icons of all previously selected items to the
138 foreach (CutItem cutItem
, m_cutItemsCache
) {
139 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
140 if (index
.isValid()) {
141 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
144 m_cutItemsCache
.clear();
146 // ... and apply an item effect to all currently cut items
147 applyCutItemEffect();
150 bool IconManager::isCutItem(const KFileItem
& item
) const
152 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
153 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
155 const KUrl
& itemUrl
= item
.url();
156 foreach (KUrl url
, cutUrls
) {
157 if (url
== itemUrl
) {
165 void IconManager::applyCutItemEffect()
167 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
168 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
172 const KFileItemList
items(m_dolphinModel
->dirLister()->items());
173 foreach (KFileItem item
, items
) {
174 if (isCutItem(item
)) {
175 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
176 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
177 if (value
.type() == QVariant::Icon
) {
178 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
179 QPixmap pixmap
= icon
.pixmap(128, 128);
181 // remember current pixmap for the item to be able
182 // to restore it when other items get cut
184 cutItem
.url
= item
.url();
185 cutItem
.pixmap
= pixmap
;
186 m_cutItemsCache
.append(cutItem
);
188 // apply icon effect to the cut item
189 KIconEffect iconEffect
;
190 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
191 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
197 #include "iconmanager.moc"