1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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 "kfileitemlistview.h"
22 #include "kfileitemlistgroupheader.h"
23 #include "kfileitemmodelrolesupdater.h"
24 #include "kfileitemlistwidget.h"
25 #include "kfileitemmodel.h"
26 #include "kpixmapmodifier_p.h"
28 #include <KStringHandler>
37 // #define KFILEITEMLISTVIEW_DEBUG
40 const int ShortInterval
= 50;
41 const int LongInterval
= 300;
44 KFileItemListView::KFileItemListView(QGraphicsWidget
* parent
) :
45 KItemListView(parent
),
46 m_itemLayout(IconsLayout
),
47 m_modelRolesUpdater(0),
48 m_updateVisibleIndexRangeTimer(0),
49 m_updateIconSizeTimer(0)
53 setScrollOrientation(Qt::Vertical
);
54 setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
55 setGroupHeaderCreator(new KItemListGroupHeaderCreator
<KFileItemListGroupHeader
>());
57 m_updateVisibleIndexRangeTimer
= new QTimer(this);
58 m_updateVisibleIndexRangeTimer
->setSingleShot(true);
59 m_updateVisibleIndexRangeTimer
->setInterval(ShortInterval
);
60 connect(m_updateVisibleIndexRangeTimer
, SIGNAL(timeout()), this, SLOT(updateVisibleIndexRange()));
62 m_updateIconSizeTimer
= new QTimer(this);
63 m_updateIconSizeTimer
->setSingleShot(true);
64 m_updateIconSizeTimer
->setInterval(ShortInterval
);
65 connect(m_updateIconSizeTimer
, SIGNAL(timeout()), this, SLOT(updateIconSize()));
67 setVisibleRoles(QList
<QByteArray
>() << "name");
70 KFileItemListView::~KFileItemListView()
72 // The group headers are children of the widgets created by
73 // widgetCreator(). So it is mandatory to delete the group headers
75 delete groupHeaderCreator();
76 delete widgetCreator();
78 delete m_modelRolesUpdater
;
79 m_modelRolesUpdater
= 0;
82 void KFileItemListView::setPreviewsShown(bool show
)
84 if (m_modelRolesUpdater
) {
85 m_modelRolesUpdater
->setPreviewShown(show
);
89 bool KFileItemListView::previewsShown() const
91 return m_modelRolesUpdater
->isPreviewShown();
94 void KFileItemListView::setItemLayout(Layout layout
)
96 if (m_itemLayout
!= layout
) {
97 const bool updateRoles
= (m_itemLayout
== DetailsLayout
|| layout
== DetailsLayout
);
98 m_itemLayout
= layout
;
100 // The details-layout requires some invisible roles that
101 // must be added to the model if the new layout is "details".
102 // If the old layout was "details" the roles will get removed.
105 updateLayoutOfVisibleItems();
109 KFileItemListView::Layout
KFileItemListView::itemLayout() const
114 void KFileItemListView::setEnabledPlugins(const QStringList
& list
)
116 if (m_modelRolesUpdater
) {
117 m_modelRolesUpdater
->setEnabledPlugins(list
);
121 QStringList
KFileItemListView::enabledPlugins() const
123 return m_modelRolesUpdater
? m_modelRolesUpdater
->enabledPlugins() : QStringList();
126 QPixmap
KFileItemListView::createDragPixmap(const QSet
<int>& indexes
) const
132 const int itemCount
= indexes
.count();
133 Q_ASSERT(itemCount
> 0);
135 // If more than one item is dragged, align the items inside a
136 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
139 if (itemCount
> 16) {
141 size
= KIconLoader::SizeSmall
;
142 } else if (itemCount
> 9) {
144 size
= KIconLoader::SizeSmallMedium
;
147 size
= KIconLoader::SizeMedium
;
150 if (itemCount
< xCount
) {
154 int yCount
= itemCount
/ xCount
;
155 if (itemCount
% xCount
!= 0) {
158 if (yCount
> xCount
) {
162 // Draw the selected items into the grid cells.
163 QPixmap
dragPixmap(xCount
* size
+ xCount
, yCount
* size
+ yCount
);
164 dragPixmap
.fill(Qt::transparent
);
166 QPainter
painter(&dragPixmap
);
169 QSetIterator
<int> it(indexes
);
170 while (it
.hasNext()) {
171 const int index
= it
.next();
173 QPixmap pixmap
= model()->data(index
).value("iconPixmap").value
<QPixmap
>();
174 if (pixmap
.isNull()) {
175 KIcon
icon(model()->data(index
).value("iconName").toString());
176 pixmap
= icon
.pixmap(size
, size
);
178 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
181 painter
.drawPixmap(x
, y
, pixmap
);
184 if (x
>= dragPixmap
.width()) {
189 if (y
>= dragPixmap
.height()) {
197 void KFileItemListView::initializeItemListWidget(KItemListWidget
* item
)
199 KFileItemListWidget
* fileItemListWidget
= static_cast<KFileItemListWidget
*>(item
);
201 switch (m_itemLayout
) {
202 case IconsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::IconsLayout
); break;
203 case CompactLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::CompactLayout
); break;
204 case DetailsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::DetailsLayout
); break;
205 default: Q_ASSERT(false); break;
208 fileItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
211 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
213 // Even if the icons have a different size they are always aligned within
214 // the area defined by KItemStyleOption.iconSize and hence result in no
215 // change of the item-size.
216 const bool containsIconName
= changedRoles
.contains("iconName");
217 const bool containsIconPixmap
= changedRoles
.contains("iconPixmap");
218 const int count
= changedRoles
.count();
220 const bool iconChanged
= (containsIconName
&& containsIconPixmap
&& count
== 2) ||
221 (containsIconName
&& count
== 1) ||
222 (containsIconPixmap
&& count
== 1);
226 void KFileItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
229 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
231 delete m_modelRolesUpdater
;
232 m_modelRolesUpdater
= new KFileItemModelRolesUpdater(static_cast<KFileItemModel
*>(current
), this);
233 m_modelRolesUpdater
->setIconSize(availableIconSize());
238 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
242 updateLayoutOfVisibleItems();
245 void KFileItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
249 triggerVisibleIndexRangeUpdate();
252 void KFileItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
256 triggerVisibleIndexRangeUpdate();
259 void KFileItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
266 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
270 triggerIconSizeUpdate();
273 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
275 Q_UNUSED(supportsExpanding
);
277 updateLayoutOfVisibleItems();
280 void KFileItemListView::onTransactionBegin()
282 m_modelRolesUpdater
->setPaused(true);
285 void KFileItemListView::onTransactionEnd()
287 // Only unpause the model-roles-updater if no timer is active. If one
288 // timer is still active the model-roles-updater will be unpaused later as
289 // soon as the timer has been exceeded.
290 const bool timerActive
= m_updateVisibleIndexRangeTimer
->isActive() ||
291 m_updateIconSizeTimer
->isActive();
293 m_modelRolesUpdater
->setPaused(false);
297 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
299 KItemListView::resizeEvent(event
);
300 triggerVisibleIndexRangeUpdate();
303 void KFileItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
305 KItemListView::slotItemsRemoved(itemRanges
);
306 updateTimersInterval();
309 void KFileItemListView::slotSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
311 const QByteArray sortRole
= model()->sortRole();
312 if (!visibleRoles().contains(sortRole
)) {
316 KItemListView::slotSortRoleChanged(current
, previous
);
319 void KFileItemListView::triggerVisibleIndexRangeUpdate()
324 m_modelRolesUpdater
->setPaused(true);
325 m_updateVisibleIndexRangeTimer
->start();
328 void KFileItemListView::updateVisibleIndexRange()
330 if (!m_modelRolesUpdater
) {
334 const int index
= firstVisibleIndex();
335 const int count
= lastVisibleIndex() - index
+ 1;
336 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
338 if (m_updateIconSizeTimer
->isActive()) {
339 // If the icon-size update is pending do an immediate update
340 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
341 // an unnecessary expensive recreation of all previews afterwards.
342 m_updateIconSizeTimer
->stop();
343 m_modelRolesUpdater
->setIconSize(availableIconSize());
346 m_modelRolesUpdater
->setPaused(isTransactionActive());
347 updateTimersInterval();
350 void KFileItemListView::triggerIconSizeUpdate()
355 m_modelRolesUpdater
->setPaused(true);
356 m_updateIconSizeTimer
->start();
359 void KFileItemListView::updateIconSize()
361 if (!m_modelRolesUpdater
) {
365 m_modelRolesUpdater
->setIconSize(availableIconSize());
367 if (m_updateVisibleIndexRangeTimer
->isActive()) {
368 // If the visibility-index-range update is pending do an immediate update
369 // of the range before unpausing m_modelRolesUpdater. This prevents
370 // an unnecessary expensive recreation of all previews afterwards.
371 m_updateVisibleIndexRangeTimer
->stop();
372 const int index
= firstVisibleIndex();
373 const int count
= lastVisibleIndex() - index
+ 1;
374 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
377 m_modelRolesUpdater
->setPaused(isTransactionActive());
378 updateTimersInterval();
381 void KFileItemListView::updateLayoutOfVisibleItems()
387 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
388 initializeItemListWidget(widget
);
390 triggerVisibleIndexRangeUpdate();
393 void KFileItemListView::updateTimersInterval()
399 // The ShortInterval is used for cases like switching the directory: If the
400 // model is empty and filled later the creation of the previews should be done
401 // as soon as possible. The LongInterval is used when the model already contains
402 // items and assures that operations like zooming don't result in too many temporary
403 // recreations of the previews.
405 const int interval
= (model()->count() <= 0) ? ShortInterval
: LongInterval
;
406 m_updateVisibleIndexRangeTimer
->setInterval(interval
);
407 m_updateIconSizeTimer
->setInterval(interval
);
410 void KFileItemListView::applyRolesToModel()
416 Q_ASSERT(qobject_cast
<KFileItemModel
*>(model()));
417 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(model());
419 // KFileItemModel does not distinct between "visible" and "invisible" roles.
420 // Add all roles that are mandatory for having a working KFileItemListView:
421 QSet
<QByteArray
> roles
= visibleRoles().toSet();
422 roles
.insert("iconPixmap");
423 roles
.insert("iconName");
424 roles
.insert("name");
425 roles
.insert("isDir");
426 if (supportsItemExpanding()) {
427 roles
.insert("isExpanded");
428 roles
.insert("isExpandable");
429 roles
.insert("expandedParentsCount");
432 // Assure that the role that is used for sorting will be determined
433 roles
.insert(fileItemModel
->sortRole());
435 fileItemModel
->setRoles(roles
);
436 m_modelRolesUpdater
->setRoles(roles
);
439 QSize
KFileItemListView::availableIconSize() const
441 const KItemListStyleOption
& option
= styleOption();
442 const int iconSize
= option
.iconSize
;
443 if (m_itemLayout
== IconsLayout
) {
444 const int maxIconWidth
= itemSize().width() - 2 * option
.padding
;
445 return QSize(maxIconWidth
, iconSize
);
448 return QSize(iconSize
, iconSize
);
451 #include "kfileitemlistview.moc"