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
) :
48 Q_ASSERT(m_view
->iconSize().isValid()); // each view must provide its current icon size
50 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
51 connect(m_dolphinModel
->dirLister(), SIGNAL(newItems(const KFileItemList
&)),
52 this, SLOT(updateIcons(const KFileItemList
&)));
54 QClipboard
* clipboard
= QApplication::clipboard();
55 connect(clipboard
, SIGNAL(dataChanged()),
56 this, SLOT(updateCutItems()));
58 m_previewTimer
= new QTimer(this);
59 connect(m_previewTimer
, SIGNAL(timeout()), this, SLOT(dispatchPreviewQueue()));
62 IconManager::~IconManager()
68 void IconManager::setShowPreview(bool show
)
70 if (m_showPreview
!= show
) {
72 m_cutItemsCache
.clear();
80 void IconManager::updatePreviews()
87 KFileItemList itemList
;
89 const int rowCount
= m_dolphinModel
->rowCount();
90 for (int row
= 0; row
< rowCount
; ++row
) {
91 const QModelIndex index
= m_dolphinModel
->index(row
, 0);
92 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
93 itemList
.append(item
);
96 generatePreviews(itemList
);
99 void IconManager::updateIcons(const KFileItemList
& items
)
102 generatePreviews(items
);
106 void IconManager::addToPreviewQueue(const KFileItem
& item
, const QPixmap
& pixmap
)
110 preview
.pixmap
= pixmap
;
111 m_previews
.append(preview
);
114 void IconManager::slotPreviewJobFinished(KJob
* job
)
116 const int index
= m_previewJobs
.indexOf(job
);
117 m_previewJobs
.removeAt(index
);
120 void IconManager::updateCutItems()
122 // restore the icons of all previously selected items to the
124 foreach (CutItem cutItem
, m_cutItemsCache
) {
125 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
126 if (index
.isValid()) {
127 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
130 m_cutItemsCache
.clear();
132 // ... and apply an item effect to all currently cut items
133 applyCutItemEffect();
136 void IconManager::dispatchPreviewQueue()
138 const int previewsCount
= m_previews
.count();
139 if (previewsCount
== 0) {
143 // Applying the previews to the model must be done step by step
144 // in larger blocks: Applying a preview immediately when getting the signal
145 // 'gotPreview()' from the PreviewJob is too expensive, as a relayout
146 // of the view would be triggered for each single preview.
148 int dispatchCount
= 30;
149 if (dispatchCount
> previewsCount
) {
150 dispatchCount
= previewsCount
;
153 for (int i
= 0; i
< dispatchCount
; ++i
) {
154 const Preview
& preview
= m_previews
.first();
155 replaceIcon(preview
.item
, preview
.pixmap
);
156 m_previews
.pop_front();
159 if (m_previews
.count() > 0) {
160 // there are still pending previews; if no preview job is
161 // working, poll more aggressively:
162 const int timeout
= (m_previewJobs
.count() > 0) ? 200 : 10;
163 m_previewTimer
->start(timeout
);
167 void IconManager::generatePreviews(const KFileItemList
&items
)
169 Q_ASSERT(m_showPreview
);
170 const QRect visibleArea
= m_view
->viewport()->rect();
172 // Order the items in a way that the preview for the visible items
173 // is generated first, as this improves the feeled performance a lot.
174 KFileItemList orderedItems
;
175 foreach (KFileItem item
, items
) {
176 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
177 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
178 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
179 if (itemRect
.intersects(visibleArea
)) {
180 orderedItems
.insert(0, item
);
182 orderedItems
.append(item
);
186 const QSize size
= m_view
->iconSize();
187 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, 128, 128);
188 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
189 this, SLOT(addToPreviewQueue(const KFileItem
&, const QPixmap
&)));
190 connect(job
, SIGNAL(finished(KJob
*)),
191 this, SLOT(slotPreviewJobFinished(KJob
*)));
193 m_previewJobs
.append(job
);
194 m_previewTimer
->start(200);
197 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
199 Q_ASSERT(!item
.isNull());
200 if (!m_showPreview
) {
201 // the preview has been canceled in the meantime
205 // check whether the item is part of the directory lister (it is possible
206 // that a preview from an old directory lister is received)
207 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
208 bool isOldPreview
= true;
209 const KUrl::List dirs
= dirLister
->directories();
210 const QString itemDir
= item
.url().directory();
211 foreach (KUrl url
, dirs
) {
212 if (url
.path() == itemDir
) {
213 isOldPreview
= false;
221 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
222 if (idx
.isValid() && (idx
.column() == 0)) {
223 QPixmap icon
= pixmap
;
225 const QString mimeType
= item
.mimetype();
226 const QString mimeTypeGroup
= mimeType
.left(mimeType
.indexOf('/'));
227 if ((mimeTypeGroup
!= "image") || !applyImageFrame(icon
)) {
228 limitToSize(icon
, m_view
->iconSize());
231 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
232 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
233 KIconEffect iconEffect
;
234 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
235 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
237 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
242 bool IconManager::isCutItem(const KFileItem
& item
) const
244 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
245 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
247 const KUrl
& itemUrl
= item
.url();
248 foreach (KUrl url
, cutUrls
) {
249 if (url
== itemUrl
) {
257 void IconManager::applyCutItemEffect()
259 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
260 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
265 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
266 const KUrl::List dirs
= dirLister
->directories();
267 foreach (KUrl url
, dirs
) {
268 items
<< dirLister
->itemsForDir(url
);
271 foreach (KFileItem item
, items
) {
272 if (isCutItem(item
)) {
273 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
274 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
275 if (value
.type() == QVariant::Icon
) {
276 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
277 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
279 // remember current pixmap for the item to be able
280 // to restore it when other items get cut
282 cutItem
.url
= item
.url();
283 cutItem
.pixmap
= pixmap
;
284 m_cutItemsCache
.append(cutItem
);
286 // apply icon effect to the cut item
287 KIconEffect iconEffect
;
288 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
289 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
295 bool IconManager::applyImageFrame(QPixmap
& icon
)
297 const QSize maxSize
= m_view
->iconSize();
298 if ((maxSize
.width() <= 24) || (maxSize
.height() <= 24)) {
299 // the maximum size is too small for a frame
304 const int doubleFrame
= frame
* 2;
306 // resize the icon to the maximum size minus the space required for the frame
307 limitToSize(icon
, QSize(maxSize
.width() - doubleFrame
, maxSize
.height() - doubleFrame
));
310 const QPalette palette
= m_view
->palette();
311 QPixmap
framedIcon(icon
.size().width() + doubleFrame
, icon
.size().height() + doubleFrame
);
312 framedIcon
.fill(palette
.color(QPalette::Normal
, QPalette::Base
));
313 const int width
= framedIcon
.width() - 1;
314 const int height
= framedIcon
.height() - 1;
316 painter
.begin(&framedIcon
);
317 painter
.drawPixmap(frame
, frame
, icon
);
319 // draw a white frame around the icon
320 painter
.setPen(Qt::NoPen
);
321 painter
.setBrush(palette
.brush(QPalette::Normal
, QPalette::Base
));
322 painter
.drawRect(0, 0, width
, frame
);
323 painter
.drawRect(0, height
- frame
, width
, frame
);
324 painter
.drawRect(0, frame
, frame
, height
- doubleFrame
);
325 painter
.drawRect(width
- frame
, frame
, frame
, height
- doubleFrame
);
328 painter
.setPen(palette
.color(QPalette::Text
));
329 painter
.setBrush(Qt::NoBrush
);
330 painter
.drawRect(0, 0, width
, height
);
331 painter
.drawRect(1, 1, width
- 2, height
- 2);
333 // dim image frame by 12.5 %
334 painter
.setPen(QColor(0, 0, 0, 32));
335 painter
.drawRect(frame
, frame
, width
- doubleFrame
, height
- doubleFrame
);
340 // provide an alpha channel for the border
341 QPixmap
alphaChannel(icon
.size());
344 QPainter
alphaPainter(&alphaChannel
);
345 alphaPainter
.setBrush(Qt::NoBrush
);
346 alphaPainter
.setPen(QColor(32, 32, 32));
347 alphaPainter
.drawRect(0, 0, width
, height
);
348 alphaPainter
.setPen(QColor(64, 64, 64));
349 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
351 icon
.setAlphaChannel(alphaChannel
);
355 void IconManager::limitToSize(QPixmap
& icon
, const QSize
& maxSize
)
357 if ((icon
.width() > maxSize
.width()) || (icon
.height() > maxSize
.height())) {
358 icon
= icon
.scaled(maxSize
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
362 void IconManager::killJobs()
364 foreach (KJob
* job
, m_previewJobs
) {
368 m_previewJobs
.clear();
371 #include "iconmanager.moc"