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"
27 #include <KStringHandler>
28 #include "private/kpixmapmodifier.h"
38 // #define KFILEITEMLISTVIEW_DEBUG
41 const int ShortInterval
= 50;
42 const int LongInterval
= 300;
45 KFileItemListView::KFileItemListView(QGraphicsWidget
* parent
) :
46 KItemListView(parent
),
47 m_itemLayout(IconsLayout
),
48 m_modelRolesUpdater(0),
49 m_updateVisibleIndexRangeTimer(0),
50 m_updateIconSizeTimer(0)
54 setScrollOrientation(Qt::Vertical
);
55 setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
56 setGroupHeaderCreator(new KItemListGroupHeaderCreator
<KFileItemListGroupHeader
>());
58 m_updateVisibleIndexRangeTimer
= new QTimer(this);
59 m_updateVisibleIndexRangeTimer
->setSingleShot(true);
60 m_updateVisibleIndexRangeTimer
->setInterval(ShortInterval
);
61 connect(m_updateVisibleIndexRangeTimer
, SIGNAL(timeout()), this, SLOT(updateVisibleIndexRange()));
63 m_updateIconSizeTimer
= new QTimer(this);
64 m_updateIconSizeTimer
->setSingleShot(true);
65 m_updateIconSizeTimer
->setInterval(ShortInterval
);
66 connect(m_updateIconSizeTimer
, SIGNAL(timeout()), this, SLOT(updateIconSize()));
68 setVisibleRoles(QList
<QByteArray
>() << "name");
71 KFileItemListView::~KFileItemListView()
73 // The group headers are children of the widgets created by
74 // widgetCreator(). So it is mandatory to delete the group headers
76 delete groupHeaderCreator();
77 delete widgetCreator();
79 delete m_modelRolesUpdater
;
80 m_modelRolesUpdater
= 0;
83 void KFileItemListView::setPreviewsShown(bool show
)
85 if (m_modelRolesUpdater
) {
86 m_modelRolesUpdater
->setPreviewsShown(show
);
90 bool KFileItemListView::previewsShown() const
92 return m_modelRolesUpdater
? m_modelRolesUpdater
->previewsShown() : false;
95 void KFileItemListView::setEnlargeSmallPreviews(bool enlarge
)
97 if (m_modelRolesUpdater
) {
98 m_modelRolesUpdater
->setEnlargeSmallPreviews(enlarge
);
102 bool KFileItemListView::enlargeSmallPreviews() const
104 return m_modelRolesUpdater
? m_modelRolesUpdater
->enlargeSmallPreviews() : false;
107 void KFileItemListView::setItemLayout(Layout layout
)
109 if (m_itemLayout
!= layout
) {
110 const bool updateRoles
= (m_itemLayout
== DetailsLayout
|| layout
== DetailsLayout
);
111 m_itemLayout
= layout
;
113 // The details-layout requires some invisible roles that
114 // must be added to the model if the new layout is "details".
115 // If the old layout was "details" the roles will get removed.
118 updateLayoutOfVisibleItems();
122 KFileItemListView::Layout
KFileItemListView::itemLayout() const
127 void KFileItemListView::setEnabledPlugins(const QStringList
& list
)
129 if (m_modelRolesUpdater
) {
130 m_modelRolesUpdater
->setEnabledPlugins(list
);
134 QStringList
KFileItemListView::enabledPlugins() const
136 return m_modelRolesUpdater
? m_modelRolesUpdater
->enabledPlugins() : QStringList();
139 QPixmap
KFileItemListView::createDragPixmap(const QSet
<int>& indexes
) const
145 const int itemCount
= indexes
.count();
146 Q_ASSERT(itemCount
> 0);
148 // If more than one item is dragged, align the items inside a
149 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
152 if (itemCount
> 16) {
154 size
= KIconLoader::SizeSmall
;
155 } else if (itemCount
> 9) {
157 size
= KIconLoader::SizeSmallMedium
;
160 size
= KIconLoader::SizeMedium
;
163 if (itemCount
< xCount
) {
167 int yCount
= itemCount
/ xCount
;
168 if (itemCount
% xCount
!= 0) {
171 if (yCount
> xCount
) {
175 // Draw the selected items into the grid cells.
176 QPixmap
dragPixmap(xCount
* size
+ xCount
, yCount
* size
+ yCount
);
177 dragPixmap
.fill(Qt::transparent
);
179 QPainter
painter(&dragPixmap
);
182 QSetIterator
<int> it(indexes
);
183 while (it
.hasNext()) {
184 const int index
= it
.next();
186 QPixmap pixmap
= model()->data(index
).value("iconPixmap").value
<QPixmap
>();
187 if (pixmap
.isNull()) {
188 KIcon
icon(model()->data(index
).value("iconName").toString());
189 pixmap
= icon
.pixmap(size
, size
);
191 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
194 painter
.drawPixmap(x
, y
, pixmap
);
197 if (x
>= dragPixmap
.width()) {
202 if (y
>= dragPixmap
.height()) {
210 void KFileItemListView::initializeItemListWidget(KItemListWidget
* item
)
212 KFileItemListWidget
* fileItemListWidget
= static_cast<KFileItemListWidget
*>(item
);
214 switch (m_itemLayout
) {
215 case IconsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::IconsLayout
); break;
216 case CompactLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::CompactLayout
); break;
217 case DetailsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::DetailsLayout
); break;
218 default: Q_ASSERT(false); break;
221 fileItemListWidget
->setSupportsItemExpanding(supportsItemExpanding());
224 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
226 // Even if the icons have a different size they are always aligned within
227 // the area defined by KItemStyleOption.iconSize and hence result in no
228 // change of the item-size.
229 const bool containsIconName
= changedRoles
.contains("iconName");
230 const bool containsIconPixmap
= changedRoles
.contains("iconPixmap");
231 const int count
= changedRoles
.count();
233 const bool iconChanged
= (containsIconName
&& containsIconPixmap
&& count
== 2) ||
234 (containsIconName
&& count
== 1) ||
235 (containsIconPixmap
&& count
== 1);
239 void KFileItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
242 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
244 delete m_modelRolesUpdater
;
245 m_modelRolesUpdater
= new KFileItemModelRolesUpdater(static_cast<KFileItemModel
*>(current
), this);
246 m_modelRolesUpdater
->setIconSize(availableIconSize());
251 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
255 updateLayoutOfVisibleItems();
258 void KFileItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
262 triggerVisibleIndexRangeUpdate();
265 void KFileItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
269 triggerVisibleIndexRangeUpdate();
272 void KFileItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
279 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
283 triggerIconSizeUpdate();
286 void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
288 Q_UNUSED(supportsExpanding
);
290 updateLayoutOfVisibleItems();
293 void KFileItemListView::onTransactionBegin()
295 m_modelRolesUpdater
->setPaused(true);
298 void KFileItemListView::onTransactionEnd()
300 // Only unpause the model-roles-updater if no timer is active. If one
301 // timer is still active the model-roles-updater will be unpaused later as
302 // soon as the timer has been exceeded.
303 const bool timerActive
= m_updateVisibleIndexRangeTimer
->isActive() ||
304 m_updateIconSizeTimer
->isActive();
306 m_modelRolesUpdater
->setPaused(false);
310 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
312 KItemListView::resizeEvent(event
);
313 triggerVisibleIndexRangeUpdate();
316 void KFileItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
318 KItemListView::slotItemsRemoved(itemRanges
);
319 updateTimersInterval();
322 void KFileItemListView::slotSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
324 const QByteArray sortRole
= model()->sortRole();
325 if (!visibleRoles().contains(sortRole
)) {
329 KItemListView::slotSortRoleChanged(current
, previous
);
332 void KFileItemListView::triggerVisibleIndexRangeUpdate()
337 m_modelRolesUpdater
->setPaused(true);
338 m_updateVisibleIndexRangeTimer
->start();
341 void KFileItemListView::updateVisibleIndexRange()
343 if (!m_modelRolesUpdater
) {
347 const int index
= firstVisibleIndex();
348 const int count
= lastVisibleIndex() - index
+ 1;
349 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
351 if (m_updateIconSizeTimer
->isActive()) {
352 // If the icon-size update is pending do an immediate update
353 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
354 // an unnecessary expensive recreation of all previews afterwards.
355 m_updateIconSizeTimer
->stop();
356 m_modelRolesUpdater
->setIconSize(availableIconSize());
359 m_modelRolesUpdater
->setPaused(isTransactionActive());
360 updateTimersInterval();
363 void KFileItemListView::triggerIconSizeUpdate()
368 m_modelRolesUpdater
->setPaused(true);
369 m_updateIconSizeTimer
->start();
372 void KFileItemListView::updateIconSize()
374 if (!m_modelRolesUpdater
) {
378 m_modelRolesUpdater
->setIconSize(availableIconSize());
380 if (m_updateVisibleIndexRangeTimer
->isActive()) {
381 // If the visibility-index-range update is pending do an immediate update
382 // of the range before unpausing m_modelRolesUpdater. This prevents
383 // an unnecessary expensive recreation of all previews afterwards.
384 m_updateVisibleIndexRangeTimer
->stop();
385 const int index
= firstVisibleIndex();
386 const int count
= lastVisibleIndex() - index
+ 1;
387 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
390 m_modelRolesUpdater
->setPaused(isTransactionActive());
391 updateTimersInterval();
394 void KFileItemListView::updateLayoutOfVisibleItems()
400 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
401 initializeItemListWidget(widget
);
403 triggerVisibleIndexRangeUpdate();
406 void KFileItemListView::updateTimersInterval()
412 // The ShortInterval is used for cases like switching the directory: If the
413 // model is empty and filled later the creation of the previews should be done
414 // as soon as possible. The LongInterval is used when the model already contains
415 // items and assures that operations like zooming don't result in too many temporary
416 // recreations of the previews.
418 const int interval
= (model()->count() <= 0) ? ShortInterval
: LongInterval
;
419 m_updateVisibleIndexRangeTimer
->setInterval(interval
);
420 m_updateIconSizeTimer
->setInterval(interval
);
423 void KFileItemListView::applyRolesToModel()
429 Q_ASSERT(qobject_cast
<KFileItemModel
*>(model()));
430 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(model());
432 // KFileItemModel does not distinct between "visible" and "invisible" roles.
433 // Add all roles that are mandatory for having a working KFileItemListView:
434 QSet
<QByteArray
> roles
= visibleRoles().toSet();
435 roles
.insert("iconPixmap");
436 roles
.insert("iconName");
437 roles
.insert("name");
438 roles
.insert("isDir");
439 if (supportsItemExpanding()) {
440 roles
.insert("isExpanded");
441 roles
.insert("isExpandable");
442 roles
.insert("expandedParentsCount");
445 // Assure that the role that is used for sorting will be determined
446 roles
.insert(fileItemModel
->sortRole());
448 fileItemModel
->setRoles(roles
);
449 m_modelRolesUpdater
->setRoles(roles
);
452 QSize
KFileItemListView::availableIconSize() const
454 const KItemListStyleOption
& option
= styleOption();
455 const int iconSize
= option
.iconSize
;
456 if (m_itemLayout
== IconsLayout
) {
457 const int maxIconWidth
= itemSize().width() - 2 * option
.padding
;
458 return QSize(maxIconWidth
, iconSize
);
461 return QSize(iconSize
, iconSize
);
464 #include "kfileitemlistview.moc"