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()
63 void IconManager::setShowPreview(bool show
)
65 if (m_showPreview
!= show
) {
67 m_cutItemsCache
.clear();
72 void IconManager::updatePreviews()
79 KFileItemList itemList
;
81 const int rowCount
= m_dolphinModel
->rowCount();
82 for (int row
= 0; row
< rowCount
; ++row
) {
83 const QModelIndex index
= m_dolphinModel
->index(row
, 0);
84 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
85 itemList
.append(item
);
88 generatePreviews(itemList
);
91 void IconManager::updateIcons(const KFileItemList
& items
)
93 // make the icons of all hidden files semitransparent
94 foreach (KFileItem item
, items
) {
95 if (item
.isHidden()) {
96 applyHiddenItemEffect(item
);
101 generatePreviews(items
);
105 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
107 Q_ASSERT(!item
.isNull());
108 if (!m_showPreview
) {
109 // the preview has been canceled in the meantime
113 // check whether the item is part of the directory lister (it is possible
114 // that a preview from an old directory lister is received)
115 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
116 bool isOldPreview
= true;
117 const KUrl::List dirs
= dirLister
->directories();
118 const QString itemDir
= item
.url().directory();
119 foreach (KUrl url
, dirs
) {
120 if (url
.path() == itemDir
) {
121 isOldPreview
= false;
129 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
130 if (idx
.isValid() && (idx
.column() == 0)) {
131 QPixmap icon
= pixmap
;
133 const QString mimeType
= item
.mimetype();
134 const QString mimeTypeGroup
= mimeType
.left(mimeType
.indexOf('/'));
135 if ((mimeTypeGroup
!= "image") || !applyImageFrame(icon
)) {
136 limitToSize(icon
, m_view
->iconSize());
139 if (item
.isHidden()) {
140 if (!icon
.hasAlpha()) {
141 // the semitransparent operation requires having an alpha mask
142 QPixmap
alphaMask(icon
.size());
144 icon
.setAlphaChannel(alphaMask
);
146 KIconEffect::semiTransparent(icon
);
149 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
150 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
151 KIconEffect iconEffect
;
152 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
153 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
155 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
160 void IconManager::slotPreviewJobFinished(KJob
* job
)
162 const int index
= m_previewJobs
.indexOf(job
);
163 m_previewJobs
.removeAt(index
);
166 void IconManager::updateCutItems()
168 // restore the icons of all previously selected items to the
170 foreach (CutItem cutItem
, m_cutItemsCache
) {
171 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
172 if (index
.isValid()) {
173 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
176 m_cutItemsCache
.clear();
178 // ... and apply an item effect to all currently cut items
179 applyCutItemEffect();
182 void IconManager::generatePreviews(const KFileItemList
&items
)
184 Q_ASSERT(m_showPreview
);
185 const QRect visibleArea
= m_view
->viewport()->rect();
187 // Order the items in a way that the preview for the visible items
188 // is generated first, as this improves the feeled performance a lot.
189 KFileItemList orderedItems
;
190 foreach (KFileItem item
, items
) {
191 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
192 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
193 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
194 if (itemRect
.intersects(visibleArea
)) {
195 orderedItems
.insert(0, item
);
197 orderedItems
.append(item
);
201 const QSize size
= m_view
->iconSize();
202 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, 128, 128);
203 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
204 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
205 connect(job
, SIGNAL(finished(KJob
*)),
206 this, SLOT(slotPreviewJobFinished(KJob
*)));
208 m_previewJobs
.append(job
);
211 bool IconManager::isCutItem(const KFileItem
& item
) const
213 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
214 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
216 const KUrl
& itemUrl
= item
.url();
217 foreach (KUrl url
, cutUrls
) {
218 if (url
== itemUrl
) {
226 void IconManager::applyCutItemEffect()
228 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
229 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
234 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
235 const KUrl::List dirs
= dirLister
->directories();
236 foreach (KUrl url
, dirs
) {
237 items
<< dirLister
->itemsForDir(url
);
240 foreach (KFileItem item
, items
) {
241 if (isCutItem(item
)) {
242 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
243 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
244 if (value
.type() == QVariant::Icon
) {
245 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
246 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
248 // remember current pixmap for the item to be able
249 // to restore it when other items get cut
251 cutItem
.url
= item
.url();
252 cutItem
.pixmap
= pixmap
;
253 m_cutItemsCache
.append(cutItem
);
255 // apply icon effect to the cut item
256 KIconEffect iconEffect
;
257 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
258 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
264 void IconManager::applyHiddenItemEffect(const KFileItem
& hiddenItem
)
266 const QModelIndex index
= m_dolphinModel
->indexForItem(hiddenItem
);
267 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
268 if (value
.type() == QVariant::Icon
) {
269 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
270 const QSize maxSize
= m_view
->iconSize();
271 QPixmap pixmap
= icon
.pixmap(maxSize
.height(), maxSize
.height()); // ignore the width
272 KIconEffect::semiTransparent(pixmap
);
273 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
277 bool IconManager::applyImageFrame(QPixmap
& icon
)
279 const QSize maxSize
= m_view
->iconSize();
280 if ((maxSize
.width() <= 24) || (maxSize
.height() <= 24)) {
281 // the maximum size is too small for a frame
286 const int doubleFrame
= frame
* 2;
288 // resize the icon to the maximum size minus the space required for the frame
289 limitToSize(icon
, QSize(maxSize
.width() - doubleFrame
, maxSize
.height() - doubleFrame
));
292 const QPalette palette
= m_view
->palette();
293 QPixmap
framedIcon(icon
.size().width() + doubleFrame
, icon
.size().height() + doubleFrame
);
294 framedIcon
.fill(palette
.color(QPalette::Normal
, QPalette::Base
));
295 const int width
= framedIcon
.width() - 1;
296 const int height
= framedIcon
.height() - 1;
298 painter
.begin(&framedIcon
);
299 painter
.drawPixmap(frame
, frame
, icon
);
301 // draw a white frame around the icon
302 painter
.setPen(Qt::NoPen
);
303 painter
.setBrush(palette
.brush(QPalette::Normal
, QPalette::Base
));
304 painter
.drawRect(0, 0, width
, frame
);
305 painter
.drawRect(0, height
- frame
, width
, frame
);
306 painter
.drawRect(0, frame
, frame
, height
- doubleFrame
);
307 painter
.drawRect(width
- frame
, frame
, frame
, height
- doubleFrame
);
310 painter
.setPen(palette
.color(QPalette::Text
));
311 painter
.setBrush(Qt::NoBrush
);
312 painter
.drawRect(0, 0, width
, height
);
313 painter
.drawRect(1, 1, width
- 2, height
- 2);
315 // dimm image frame by 25 %
316 painter
.setPen(QColor(0, 0, 0, 64));
317 painter
.drawRect(frame
, frame
, width
- doubleFrame
, height
- doubleFrame
);
322 // provide an alpha channel for the border
323 QPixmap
alphaChannel(icon
.size());
326 QPainter
alphaPainter(&alphaChannel
);
327 alphaPainter
.setBrush(Qt::NoBrush
);
328 alphaPainter
.setPen(QColor(32, 32, 32));
329 alphaPainter
.drawRect(0, 0, width
, height
);
330 alphaPainter
.setPen(QColor(64, 64, 64));
331 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
333 icon
.setAlphaChannel(alphaChannel
);
337 void IconManager::limitToSize(QPixmap
& icon
, const QSize
& maxSize
)
339 if ((icon
.width() > maxSize
.width()) || (icon
.height() > maxSize
.height())) {
340 icon
= icon
.scaled(maxSize
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
344 void IconManager::killJobs()
346 foreach (KJob
* job
, m_previewJobs
) {
350 m_previewJobs
.clear();
353 #include "iconmanager.moc"