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
)
97 generatePreviews(items
);
101 void IconManager::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
103 Q_ASSERT(!item
.isNull());
104 if (!m_showPreview
) {
105 // the preview has been canceled in the meantime
109 // check whether the item is part of the directory lister (it is possible
110 // that a preview from an old directory lister is received)
111 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
112 bool isOldPreview
= true;
113 const KUrl::List dirs
= dirLister
->directories();
114 const QString itemDir
= item
.url().directory();
115 foreach (KUrl url
, dirs
) {
116 if (url
.path() == itemDir
) {
117 isOldPreview
= false;
125 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
126 if (idx
.isValid() && (idx
.column() == 0)) {
127 QPixmap icon
= pixmap
;
129 const QString mimeType
= item
.mimetype();
130 const QString mimeTypeGroup
= mimeType
.left(mimeType
.indexOf('/'));
131 if ((mimeTypeGroup
!= "image") || !applyImageFrame(icon
)) {
132 limitToSize(icon
, m_view
->iconSize());
135 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
136 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
137 KIconEffect iconEffect
;
138 icon
= iconEffect
.apply(icon
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
139 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
141 m_dolphinModel
->setData(idx
, QIcon(icon
), Qt::DecorationRole
);
146 void IconManager::slotPreviewJobFinished(KJob
* job
)
148 const int index
= m_previewJobs
.indexOf(job
);
149 m_previewJobs
.removeAt(index
);
152 void IconManager::updateCutItems()
154 // restore the icons of all previously selected items to the
156 foreach (CutItem cutItem
, m_cutItemsCache
) {
157 const QModelIndex index
= m_dolphinModel
->indexForUrl(cutItem
.url
);
158 if (index
.isValid()) {
159 m_dolphinModel
->setData(index
, QIcon(cutItem
.pixmap
), Qt::DecorationRole
);
162 m_cutItemsCache
.clear();
164 // ... and apply an item effect to all currently cut items
165 applyCutItemEffect();
168 void IconManager::generatePreviews(const KFileItemList
&items
)
170 Q_ASSERT(m_showPreview
);
171 const QRect visibleArea
= m_view
->viewport()->rect();
173 // Order the items in a way that the preview for the visible items
174 // is generated first, as this improves the feeled performance a lot.
175 KFileItemList orderedItems
;
176 foreach (KFileItem item
, items
) {
177 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
178 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
179 const QRect itemRect
= m_view
->visualRect(proxyIndex
);
180 if (itemRect
.intersects(visibleArea
)) {
181 orderedItems
.insert(0, item
);
183 orderedItems
.append(item
);
187 const QSize size
= m_view
->iconSize();
188 KIO::PreviewJob
* job
= KIO::filePreview(orderedItems
, 128, 128);
189 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
190 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
191 connect(job
, SIGNAL(finished(KJob
*)),
192 this, SLOT(slotPreviewJobFinished(KJob
*)));
194 m_previewJobs
.append(job
);
197 bool IconManager::isCutItem(const KFileItem
& item
) const
199 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
200 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
202 const KUrl
& itemUrl
= item
.url();
203 foreach (KUrl url
, cutUrls
) {
204 if (url
== itemUrl
) {
212 void IconManager::applyCutItemEffect()
214 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
215 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
220 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
221 const KUrl::List dirs
= dirLister
->directories();
222 foreach (KUrl url
, dirs
) {
223 items
<< dirLister
->itemsForDir(url
);
226 foreach (KFileItem item
, items
) {
227 if (isCutItem(item
)) {
228 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
229 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
230 if (value
.type() == QVariant::Icon
) {
231 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
232 QPixmap pixmap
= icon
.pixmap(m_view
->iconSize());
234 // remember current pixmap for the item to be able
235 // to restore it when other items get cut
237 cutItem
.url
= item
.url();
238 cutItem
.pixmap
= pixmap
;
239 m_cutItemsCache
.append(cutItem
);
241 // apply icon effect to the cut item
242 KIconEffect iconEffect
;
243 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
244 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
250 bool IconManager::applyImageFrame(QPixmap
& icon
)
252 const QSize maxSize
= m_view
->iconSize();
253 if ((maxSize
.width() <= 24) || (maxSize
.height() <= 24)) {
254 // the maximum size is too small for a frame
259 const int doubleFrame
= frame
* 2;
261 // resize the icon to the maximum size minus the space required for the frame
262 limitToSize(icon
, QSize(maxSize
.width() - doubleFrame
, maxSize
.height() - doubleFrame
));
265 const QPalette palette
= m_view
->palette();
266 QPixmap
framedIcon(icon
.size().width() + doubleFrame
, icon
.size().height() + doubleFrame
);
267 framedIcon
.fill(palette
.color(QPalette::Normal
, QPalette::Base
));
268 const int width
= framedIcon
.width() - 1;
269 const int height
= framedIcon
.height() - 1;
271 painter
.begin(&framedIcon
);
272 painter
.drawPixmap(frame
, frame
, icon
);
274 // draw a white frame around the icon
275 painter
.setPen(Qt::NoPen
);
276 painter
.setBrush(palette
.brush(QPalette::Normal
, QPalette::Base
));
277 painter
.drawRect(0, 0, width
, frame
);
278 painter
.drawRect(0, height
- frame
, width
, frame
);
279 painter
.drawRect(0, frame
, frame
, height
- doubleFrame
);
280 painter
.drawRect(width
- frame
, frame
, frame
, height
- doubleFrame
);
283 painter
.setPen(palette
.color(QPalette::Text
));
284 painter
.setBrush(Qt::NoBrush
);
285 painter
.drawRect(0, 0, width
, height
);
286 painter
.drawRect(1, 1, width
- 2, height
- 2);
288 // dimm image frame by 25 %
289 painter
.setPen(QColor(0, 0, 0, 64));
290 painter
.drawRect(frame
, frame
, width
- doubleFrame
, height
- doubleFrame
);
295 // provide an alpha channel for the border
296 QPixmap
alphaChannel(icon
.size());
299 QPainter
alphaPainter(&alphaChannel
);
300 alphaPainter
.setBrush(Qt::NoBrush
);
301 alphaPainter
.setPen(QColor(32, 32, 32));
302 alphaPainter
.drawRect(0, 0, width
, height
);
303 alphaPainter
.setPen(QColor(64, 64, 64));
304 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
306 icon
.setAlphaChannel(alphaChannel
);
310 void IconManager::limitToSize(QPixmap
& icon
, const QSize
& maxSize
)
312 if ((icon
.width() > maxSize
.width()) || (icon
.height() > maxSize
.height())) {
313 icon
= icon
.scaled(maxSize
, Qt::KeepAspectRatio
, Qt::SmoothTransformation
);
317 void IconManager::killJobs()
319 foreach (KJob
* job
, m_previewJobs
) {
323 m_previewJobs
.clear();
326 #include "iconmanager.moc"