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();
75 void IconManager::updatePreviews()
82 KFileItemList itemList
;
84 const int rowCount
= m_dolphinModel
->rowCount();
85 for (int row
= 0; row
< rowCount
; ++row
) {
86 const QModelIndex index
= m_dolphinModel
->index(row
, 0);
87 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
88 itemList
.append(item
);
91 generatePreviews(itemList
);
94 void IconManager::updateIcons(const KFileItemList
& items
)
96 // make the icons of all hidden files semitransparent
97 foreach (KFileItem item
, items
) {
98 if (item
.isHidden()) {
99 applyHiddenItemEffect(item
);
104 generatePreviews(items
);
108 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
110 Q_ASSERT(!item
.isNull());
111 if (!m_showPreview
) {
112 // the preview has been canceled in the meantime
116 // check whether the item is part of the directory lister (it is possible
117 // that a preview from an old directory lister is received)
118 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
119 bool isOldPreview
= true;
120 const KUrl::List dirs
= dirLister
->directories();
121 const QString itemDir
= item
.url().directory();
122 foreach (KUrl url
, dirs
) {
123 if (url
.path() == itemDir
) {
124 isOldPreview
= false;
132 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
133 if (idx
.isValid() && (idx
.column() == 0)) {
134 QPixmap icon
= pixmap
;
136 const QString mimeType
= item
.mimetype();
137 const QString mimeTypeGroup
= mimeType
.left(mimeType
.indexOf('/'));
138 if ((mimeTypeGroup
!= "image") || !applyImageFrame(icon
)) {
139 limitToSize(icon
, m_view
->iconSize());
142 if (item
.isHidden()) {
143 if (!icon
.hasAlpha()) {
144 // the semitransparent operation requires having an alpha mask
145 QPixmap
alphaMask(icon
.size());
147 icon
.setAlphaChannel(alphaMask
);
149 KIconEffect::semiTransparent(icon
);
152 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
153 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
154 KIconEffect iconEffect
;
155 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
156 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
158 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
163 void IconManager::slotPreviewJobFinished(KJob
* job
)
165 const int index
= m_previewJobs
.indexOf(job
);
166 m_previewJobs
.removeAt(index
);
169 void IconManager::updateCutItems()
171 // restore the icons of all previously selected items to the
173 foreach (CutItem cutItem
, m_cutItemsCache
) {
174 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
175 if (index
.isValid()) {
176 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
179 m_cutItemsCache
.clear();
181 // ... and apply an item effect to all currently cut items
182 applyCutItemEffect();
185 void IconManager::generatePreviews(const KFileItemList
&items
)
187 Q_ASSERT(m_showPreview
);
188 const QRect visibleArea
= m_view
->viewport()->rect();
190 // Order the items in a way that the preview for the visible items
191 // is generated first, as this improves the feeled performance a lot.
192 KFileItemList orderedItems
;
193 foreach (KFileItem item
, items
) {
194 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
195 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
196 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
197 if (itemRect
.intersects(visibleArea
)) {
198 orderedItems
.insert(0, item
);
200 orderedItems
.append(item
);
204 const QSize size
= m_view
->iconSize();
205 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, 128, 128);
206 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
207 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
208 connect(job
, SIGNAL(finished(KJob
*)),
209 this, SLOT(slotPreviewJobFinished(KJob
*)));
211 m_previewJobs
.append(job
);
214 bool IconManager::isCutItem(const KFileItem
& item
) const
216 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
217 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
219 const KUrl
& itemUrl
= item
.url();
220 foreach (KUrl url
, cutUrls
) {
221 if (url
== itemUrl
) {
229 void IconManager::applyCutItemEffect()
231 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
232 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
237 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
238 const KUrl::List dirs
= dirLister
->directories();
239 foreach (KUrl url
, dirs
) {
240 items
<< dirLister
->itemsForDir(url
);
243 foreach (KFileItem item
, items
) {
244 if (isCutItem(item
)) {
245 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
246 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
247 if (value
.type() == QVariant::Icon
) {
248 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
249 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
251 // remember current pixmap for the item to be able
252 // to restore it when other items get cut
254 cutItem
.url
= item
.url();
255 cutItem
.pixmap
= pixmap
;
256 m_cutItemsCache
.append(cutItem
);
258 // apply icon effect to the cut item
259 KIconEffect iconEffect
;
260 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
261 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
267 void IconManager::applyHiddenItemEffect(const KFileItem
& hiddenItem
)
269 const QModelIndex index
= m_dolphinModel
->indexForItem(hiddenItem
);
270 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
271 if (value
.type() == QVariant::Icon
) {
272 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
273 const QSize maxSize
= m_view
->iconSize();
274 QPixmap pixmap
= icon
.pixmap(maxSize
.height(), maxSize
.height()); // ignore the width
275 KIconEffect::semiTransparent(pixmap
);
276 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
280 bool IconManager::applyImageFrame(QPixmap
& icon
)
282 const QSize maxSize
= m_view
->iconSize();
283 if ((maxSize
.width() <= 24) || (maxSize
.height() <= 24)) {
284 // the maximum size is too small for a frame
289 const int doubleFrame
= frame
* 2;
291 // resize the icon to the maximum size minus the space required for the frame
292 limitToSize(icon
, QSize(maxSize
.width() - doubleFrame
, maxSize
.height() - doubleFrame
));
295 const QPalette palette
= m_view
->palette();
296 QPixmap
framedIcon(icon
.size().width() + doubleFrame
, icon
.size().height() + doubleFrame
);
297 framedIcon
.fill(palette
.color(QPalette::Normal
, QPalette::Base
));
298 const int width
= framedIcon
.width() - 1;
299 const int height
= framedIcon
.height() - 1;
301 painter
.begin(&framedIcon
);
302 painter
.drawPixmap(frame
, frame
, icon
);
304 // draw a white frame around the icon
305 painter
.setPen(Qt::NoPen
);
306 painter
.setBrush(palette
.brush(QPalette::Normal
, QPalette::Base
));
307 painter
.drawRect(0, 0, width
, frame
);
308 painter
.drawRect(0, height
- frame
, width
, frame
);
309 painter
.drawRect(0, frame
, frame
, height
- doubleFrame
);
310 painter
.drawRect(width
- frame
, frame
, frame
, height
- doubleFrame
);
313 painter
.setPen(palette
.color(QPalette::Text
));
314 painter
.setBrush(Qt::NoBrush
);
315 painter
.drawRect(0, 0, width
, height
);
316 painter
.drawRect(1, 1, width
- 2, height
- 2);
318 // dimm image frame by 25 %
319 painter
.setPen(QColor(0, 0, 0, 64));
320 painter
.drawRect(frame
, frame
, width
- doubleFrame
, height
- doubleFrame
);
325 // provide an alpha channel for the border
326 QPixmap
alphaChannel(icon
.size());
329 QPainter
alphaPainter(&alphaChannel
);
330 alphaPainter
.setBrush(Qt::NoBrush
);
331 alphaPainter
.setPen(QColor(32, 32, 32));
332 alphaPainter
.drawRect(0, 0, width
, height
);
333 alphaPainter
.setPen(QColor(64, 64, 64));
334 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
336 icon
.setAlphaChannel(alphaChannel
);
340 void IconManager::limitToSize(QPixmap
& icon
, const QSize
& maxSize
)
342 if ((icon
.width() > maxSize
.width()) || (icon
.height() > maxSize
.height())) {
343 icon
= icon
.scaled(maxSize
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
347 void IconManager::killJobs()
349 foreach (KJob
* job
, m_previewJobs
) {
353 m_previewJobs
.clear();
356 #include "iconmanager.moc"