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>
27 #include <kdirlister.h>
28 #include <konqmimedata.h>
30 #include <QApplication>
31 #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
, 128, 128);
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
;
144 const QString mimeType
= item
.mimetype();
145 const QString mimeTypeGroup
= mimeType
.left(mimeType
.indexOf('/'));
146 if ((mimeTypeGroup
!= "image") || !applyImageFrame(icon
)) {
147 limitToSize(icon
, m_view
->iconSize());
150 if (item
.isHidden()) {
151 if (!icon
.hasAlpha()) {
152 // the semitransparent operation requires having an alpha mask
153 QPixmap
alphaMask(icon
.size());
155 icon
.setAlphaChannel(alphaMask
);
157 KIconEffect::semiTransparent(icon
);
160 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
161 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
162 KIconEffect iconEffect
;
163 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
164 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
166 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
171 void IconManager::slotPreviewJobFinished(KJob
* job
)
173 const int index
= m_previewJobs
.indexOf(job
);
174 m_previewJobs
.removeAt(index
);
177 void IconManager::updateCutItems()
179 // restore the icons of all previously selected items to the
181 foreach (CutItem cutItem
, m_cutItemsCache
) {
182 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
183 if (index
.isValid()) {
184 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
187 m_cutItemsCache
.clear();
189 // ... and apply an item effect to all currently cut items
190 applyCutItemEffect();
193 bool IconManager::isCutItem(const KFileItem
& item
) const
195 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
196 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
198 const KUrl
& itemUrl
= item
.url();
199 foreach (KUrl url
, cutUrls
) {
200 if (url
== itemUrl
) {
208 void IconManager::applyCutItemEffect()
210 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
211 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
216 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
217 const KUrl::List dirs
= dirLister
->directories();
218 foreach (KUrl url
, dirs
) {
219 items
<< dirLister
->itemsForDir(url
);
222 foreach (KFileItem item
, items
) {
223 if (isCutItem(item
)) {
224 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
225 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
226 if (value
.type() == QVariant::Icon
) {
227 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
228 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
230 // remember current pixmap for the item to be able
231 // to restore it when other items get cut
233 cutItem
.url
= item
.url();
234 cutItem
.pixmap
= pixmap
;
235 m_cutItemsCache
.append(cutItem
);
237 // apply icon effect to the cut item
238 KIconEffect iconEffect
;
239 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
240 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
246 void IconManager::applyHiddenItemEffect(const KFileItem
& hiddenItem
)
248 const QModelIndex index
= m_dolphinModel
->indexForItem(hiddenItem
);
249 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
250 if (value
.type() == QVariant::Icon
) {
251 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
252 const QSize maxSize
= m_view
->iconSize();
253 QPixmap pixmap
= icon
.pixmap(maxSize
.height(), maxSize
.height()); // ignore the width
254 KIconEffect::semiTransparent(pixmap
);
255 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
259 bool IconManager::applyImageFrame(QPixmap
& icon
)
261 const QSize maxSize
= m_view
->iconSize();
262 if ((maxSize
.width() <= 24) || (maxSize
.height() <= 24)) {
263 // the maximum size is too small for a frame
268 const int doubleFrame
= frame
* 2;
270 // resize the icon to the maximum size minus the space required for the frame
271 limitToSize(icon
, QSize(maxSize
.width() - doubleFrame
, maxSize
.height() - doubleFrame
));
274 const QPalette palette
= m_view
->palette();
275 QPixmap
framedIcon(icon
.size().width() + doubleFrame
, icon
.size().height() + doubleFrame
);
276 framedIcon
.fill(palette
.color(QPalette::Normal
, QPalette::Base
));
277 const int width
= framedIcon
.width() - 1;
278 const int height
= framedIcon
.height() - 1;
280 painter
.begin(&framedIcon
);
281 painter
.drawPixmap(frame
, frame
, icon
);
283 // draw a white frame around the icon
284 painter
.setPen(Qt::NoPen
);
285 painter
.setBrush(palette
.brush(QPalette::Normal
, QPalette::Base
));
286 painter
.drawRect(0, 0, width
, frame
);
287 painter
.drawRect(0, height
- frame
, width
, frame
);
288 painter
.drawRect(0, frame
, frame
, height
- doubleFrame
);
289 painter
.drawRect(width
- frame
, frame
, frame
, height
- doubleFrame
);
292 painter
.setPen(palette
.color(QPalette::Text
));
293 painter
.setBrush(Qt::NoBrush
);
294 painter
.drawRect(0, 0, width
, height
);
295 painter
.drawRect(1, 1, width
- 2, height
- 2);
297 // dimm image frame by 25 %
298 painter
.setPen(QColor(0, 0, 0, 64));
299 painter
.drawRect(frame
, frame
, width
- doubleFrame
, height
- doubleFrame
);
304 // provide an alpha channel for the border
305 QPixmap
alphaChannel(icon
.size());
308 QPainter
alphaPainter(&alphaChannel
);
309 alphaPainter
.setBrush(Qt::NoBrush
);
310 alphaPainter
.setPen(QColor(32, 32, 32));
311 alphaPainter
.drawRect(0, 0, width
, height
);
312 alphaPainter
.setPen(QColor(64, 64, 64));
313 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
315 icon
.setAlphaChannel(alphaChannel
);
319 void IconManager::limitToSize(QPixmap
& icon
, const QSize
& maxSize
)
321 if ((icon
.width() > maxSize
.width()) || (icon
.height() > maxSize
.height())) {
322 icon
= icon
.scaled(maxSize
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
326 #include "iconmanager.moc"