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
->setPreviewsShown(show
);
89 bool KFileItemListView::previewsShown() const
91 return m_modelRolesUpdater
? m_modelRolesUpdater
->previewsShown() : false;
94 void KFileItemListView::setEnlargeSmallPreviews(bool enlarge
)
96 if (m_modelRolesUpdater
) {
97 m_modelRolesUpdater
->setEnlargeSmallPreviews(enlarge
);
101 bool KFileItemListView::enlargeSmallPreviews() const
103 return m_modelRolesUpdater
? m_modelRolesUpdater
->enlargeSmallPreviews() : false;
106 void KFileItemListView::setItemLayout(Layout layout
)
108 if (m_itemLayout
!= layout
) {
109 const bool updateRoles
= (m_itemLayout
== DetailsLayout
|| layout
== DetailsLayout
);
110 m_itemLayout
= layout
;
112 // The details-layout requires some invisible roles that
113 // must be added to the model if the new layout is "details".
114 // If the old layout was "details" the roles will get removed.
117 updateLayoutOfVisibleItems();
121 KFileItemListView::Layout
KFileItemListView::itemLayout() const
126 void KFileItemListView::setEnabledPlugins(const QStringList
& list
)
128 if (m_modelRolesUpdater
) {
129 m_modelRolesUpdater
->setEnabledPlugins(list
);
133 QStringList
KFileItemListView::enabledPlugins() const
135 return m_modelRolesUpdater
? m_modelRolesUpdater
->enabledPlugins() : QStringList();
138 QPixmap
KFileItemListView::createDragPixmap(const QSet
<int>& indexes
) const
144 const int itemCount
= indexes
.count();
145 Q_ASSERT(itemCount
> 0);
147 // If more than one item is dragged, align the items inside a
148 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
151 if (itemCount
> 16) {
153 size
= KIconLoader::SizeSmall
;
154 } else if (itemCount
> 9) {
156 size
= KIconLoader::SizeSmallMedium
;
159 size
= KIconLoader::SizeMedium
;
162 if (itemCount
< xCount
) {
166 int yCount
= itemCount
/ xCount
;
167 if (itemCount
% xCount
!= 0) {
170 if (yCount
> xCount
) {
174 // Draw the selected items into the grid cells.
175 QPixmap
dragPixmap(xCount
* size
+ xCount
, yCount
* size
+ yCount
);
176 dragPixmap
.fill(Qt::transparent
);
178 QPainter
painter(&dragPixmap
);
181 QSetIterator
<int> it(indexes
);
182 while (it
.hasNext()) {
183 const int index
= it
.next();
185 QPixmap pixmap
= model()->data(index
).value("iconPixmap").value
<QPixmap
>();
186 if (pixmap
.isNull()) {
187 KIcon
icon(model()->data(index
).value("iconName").toString());
188 pixmap
= icon
.pixmap(size
, size
);
190 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
193 painter
.drawPixmap(x
, y
, pixmap
);
196 if (x
>= dragPixmap
.width()) {
201 if (y
>= dragPixmap
.height()) {
209 void KFileItemListView::initializeItemListWidget(KItemListWidget
* item
)
211 KFileItemListWidget
* fileItemListWidget
= static_cast<KFileItemListWidget
*>(item
);
213 switch (m_itemLayout
) {
214 case IconsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::IconsLayout
); break;
215 case CompactLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::CompactLayout
); break;
216 case DetailsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::DetailsLayout
); break;
217 default: Q_ASSERT(false); break;
220 fileItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
223 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
225 // Even if the icons have a different size they are always aligned within
226 // the area defined by KItemStyleOption.iconSize and hence result in no
227 // change of the item-size.
228 const bool containsIconName
= changedRoles
.contains("iconName");
229 const bool containsIconPixmap
= changedRoles
.contains("iconPixmap");
230 const int count
= changedRoles
.count();
232 const bool iconChanged
= (containsIconName
&& containsIconPixmap
&& count
== 2) ||
233 (containsIconName
&& count
== 1) ||
234 (containsIconPixmap
&& count
== 1);
238 void KFileItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
241 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
243 delete m_modelRolesUpdater
;
244 m_modelRolesUpdater
= new KFileItemModelRolesUpdater(static_cast<KFileItemModel
*>(current
), this);
245 m_modelRolesUpdater
->setIconSize(availableIconSize());
250 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
254 updateLayoutOfVisibleItems();
257 void KFileItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
261 triggerVisibleIndexRangeUpdate();
264 void KFileItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
268 triggerVisibleIndexRangeUpdate();
271 void KFileItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
278 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
282 triggerIconSizeUpdate();
285 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
287 Q_UNUSED(supportsExpanding
);
289 updateLayoutOfVisibleItems();
292 void KFileItemListView::onTransactionBegin()
294 m_modelRolesUpdater
->setPaused(true);
297 void KFileItemListView::onTransactionEnd()
299 // Only unpause the model-roles-updater if no timer is active. If one
300 // timer is still active the model-roles-updater will be unpaused later as
301 // soon as the timer has been exceeded.
302 const bool timerActive
= m_updateVisibleIndexRangeTimer
->isActive() ||
303 m_updateIconSizeTimer
->isActive();
305 m_modelRolesUpdater
->setPaused(false);
309 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
311 KItemListView::resizeEvent(event
);
312 triggerVisibleIndexRangeUpdate();
315 void KFileItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
317 KItemListView::slotItemsRemoved(itemRanges
);
318 updateTimersInterval();
321 void KFileItemListView::slotSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
323 const QByteArray sortRole
= model()->sortRole();
324 if (!visibleRoles().contains(sortRole
)) {
328 KItemListView::slotSortRoleChanged(current
, previous
);
331 void KFileItemListView::triggerVisibleIndexRangeUpdate()
336 m_modelRolesUpdater
->setPaused(true);
337 m_updateVisibleIndexRangeTimer
->start();
340 void KFileItemListView::updateVisibleIndexRange()
342 if (!m_modelRolesUpdater
) {
346 const int index
= firstVisibleIndex();
347 const int count
= lastVisibleIndex() - index
+ 1;
348 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
350 if (m_updateIconSizeTimer
->isActive()) {
351 // If the icon-size update is pending do an immediate update
352 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
353 // an unnecessary expensive recreation of all previews afterwards.
354 m_updateIconSizeTimer
->stop();
355 m_modelRolesUpdater
->setIconSize(availableIconSize());
358 m_modelRolesUpdater
->setPaused(isTransactionActive());
359 updateTimersInterval();
362 void KFileItemListView::triggerIconSizeUpdate()
367 m_modelRolesUpdater
->setPaused(true);
368 m_updateIconSizeTimer
->start();
371 void KFileItemListView::updateIconSize()
373 if (!m_modelRolesUpdater
) {
377 m_modelRolesUpdater
->setIconSize(availableIconSize());
379 if (m_updateVisibleIndexRangeTimer
->isActive()) {
380 // If the visibility-index-range update is pending do an immediate update
381 // of the range before unpausing m_modelRolesUpdater. This prevents
382 // an unnecessary expensive recreation of all previews afterwards.
383 m_updateVisibleIndexRangeTimer
->stop();
384 const int index
= firstVisibleIndex();
385 const int count
= lastVisibleIndex() - index
+ 1;
386 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
389 m_modelRolesUpdater
->setPaused(isTransactionActive());
390 updateTimersInterval();
393 void KFileItemListView::updateLayoutOfVisibleItems()
399 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
400 initializeItemListWidget(widget
);
402 triggerVisibleIndexRangeUpdate();
405 void KFileItemListView::updateTimersInterval()
411 // The ShortInterval is used for cases like switching the directory: If the
412 // model is empty and filled later the creation of the previews should be done
413 // as soon as possible. The LongInterval is used when the model already contains
414 // items and assures that operations like zooming don't result in too many temporary
415 // recreations of the previews.
417 const int interval
= (model()->count() <= 0) ? ShortInterval
: LongInterval
;
418 m_updateVisibleIndexRangeTimer
->setInterval(interval
);
419 m_updateIconSizeTimer
->setInterval(interval
);
422 void KFileItemListView::applyRolesToModel()
428 Q_ASSERT(qobject_cast
<KFileItemModel
*>(model()));
429 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(model());
431 // KFileItemModel does not distinct between "visible" and "invisible" roles.
432 // Add all roles that are mandatory for having a working KFileItemListView:
433 QSet
<QByteArray
> roles
= visibleRoles().toSet();
434 roles
.insert("iconPixmap");
435 roles
.insert("iconName");
436 roles
.insert("name");
437 roles
.insert("isDir");
438 if (supportsItemExpanding()) {
439 roles
.insert("isExpanded");
440 roles
.insert("isExpandable");
441 roles
.insert("expandedParentsCount");
444 // Assure that the role that is used for sorting will be determined
445 roles
.insert(fileItemModel
->sortRole());
447 fileItemModel
->setRoles(roles
);
448 m_modelRolesUpdater
->setRoles(roles
);
451 QSize
KFileItemListView::availableIconSize() const
453 const KItemListStyleOption
& option
= styleOption();
454 const int iconSize
= option
.iconSize
;
455 if (m_itemLayout
== IconsLayout
) {
456 const int maxIconWidth
= itemSize().width() - 2 * option
.padding
;
457 return QSize(maxIconWidth
, iconSize
);
460 return QSize(iconSize
, iconSize
);
463 #include "kfileitemlistview.moc"