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),
50 m_minimumRolesWidths()
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 updateMinimumRolesWidths();
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
->setPreviewShown(show
);
90 bool KFileItemListView::previewsShown() const
92 return m_modelRolesUpdater
->isPreviewShown();
95 void KFileItemListView::setItemLayout(Layout layout
)
97 if (m_itemLayout
!= layout
) {
98 m_itemLayout
= layout
;
99 updateLayoutOfVisibleItems();
103 KFileItemListView::Layout
KFileItemListView::itemLayout() const
108 QSizeF
KFileItemListView::itemSizeHint(int index
) const
110 const QHash
<QByteArray
, QVariant
> values
= model()->data(index
);
111 const KItemListStyleOption
& option
= styleOption();
112 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
114 switch (m_itemLayout
) {
116 const QString text
= KStringHandler::preProcessWrap(values
["name"].toString());
118 const qreal maxWidth
= itemSize().width() - 2 * option
.margin
;
119 int textLinesCount
= 0;
122 // Calculate the number of lines required for wrapping the name
123 QTextOption
textOption(Qt::AlignHCenter
);
124 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
126 QTextLayout
layout(text
, option
.font
);
127 layout
.setTextOption(textOption
);
128 layout
.beginLayout();
129 while ((line
= layout
.createLine()).isValid()) {
130 line
.setLineWidth(maxWidth
);
131 line
.naturalTextWidth();
136 // Add one line for each additional information
137 textLinesCount
+= additionalRolesCount
;
139 const qreal height
= textLinesCount
* option
.fontMetrics
.height() +
142 return QSizeF(itemSize().width(), height
);
145 case CompactLayout
: {
146 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
147 // to show all roles without horizontal clipping.
148 qreal maximumRequiredWidth
= 0.0;
150 foreach (const QByteArray
& role
, visibleRoles()) {
151 const QString text
= KFileItemListWidget::roleText(role
, values
);
152 const qreal requiredWidth
= option
.fontMetrics
.width(text
);
153 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
156 const qreal width
= option
.margin
* 4 + option
.iconSize
+ maximumRequiredWidth
;
157 const qreal height
= option
.margin
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * option
.fontMetrics
.height());
158 return QSizeF(width
, height
);
161 case DetailsLayout
: {
162 // The width will be determined dynamically by KFileItemListView::visibleRoleSizes()
163 const qreal height
= option
.margin
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
164 return QSizeF(-1, height
);
175 QHash
<QByteArray
, QSizeF
> KFileItemListView::visibleRolesSizes(const KItemRangeList
& itemRanges
) const
180 QHash
<QByteArray
, QSizeF
> sizes
;
182 int calculatedItemCount
= 0;
183 bool maxTimeExceeded
= false;
184 foreach (const KItemRange
& itemRange
, itemRanges
) {
185 const int startIndex
= itemRange
.index
;
186 const int endIndex
= startIndex
+ itemRange
.count
- 1;
188 for (int i
= startIndex
; i
<= endIndex
; ++i
) {
189 foreach (const QByteArray
& visibleRole
, visibleRoles()) {
190 QSizeF maxSize
= sizes
.value(visibleRole
, QSizeF(0, 0));
191 const QSizeF itemSize
= visibleRoleSizeHint(i
, visibleRole
);
192 maxSize
= maxSize
.expandedTo(itemSize
);
193 sizes
.insert(visibleRole
, maxSize
);
196 if (calculatedItemCount
> 100 && timer
.elapsed() > 200) {
197 // When having several thousands of items calculating the sizes can get
198 // very expensive. We accept a possibly too small role-size in favour
199 // of having no blocking user interface.
200 #ifdef KFILEITEMLISTVIEW_DEBUG
201 kDebug() << "Timer exceeded, stopped after" << calculatedItemCount
<< "items";
203 maxTimeExceeded
= true;
206 ++calculatedItemCount
;
208 if (maxTimeExceeded
) {
213 #ifdef KFILEITEMLISTVIEW_DEBUG
214 int rangesItemCount
= 0;
215 foreach (const KItemRange
& itemRange
, itemRanges
) {
216 rangesItemCount
+= itemRange
.count
;
218 kDebug() << "[TIME] Calculated dynamic item size for " << rangesItemCount
<< "items:" << timer
.elapsed();
223 QPixmap
KFileItemListView::createDragPixmap(const QSet
<int>& indexes
) const
229 const int itemCount
= indexes
.count();
230 Q_ASSERT(itemCount
> 0);
232 // If more than one item is dragged, align the items inside a
233 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
236 if (itemCount
> 16) {
238 size
= KIconLoader::SizeSmall
;
239 } else if (itemCount
> 9) {
241 size
= KIconLoader::SizeSmallMedium
;
244 size
= KIconLoader::SizeMedium
;
247 if (itemCount
< xCount
) {
251 int yCount
= itemCount
/ xCount
;
252 if (itemCount
% xCount
!= 0) {
255 if (yCount
> xCount
) {
259 // Draw the selected items into the grid cells.
260 QPixmap
dragPixmap(xCount
* size
+ xCount
, yCount
* size
+ yCount
);
261 dragPixmap
.fill(Qt::transparent
);
263 QPainter
painter(&dragPixmap
);
266 QSetIterator
<int> it(indexes
);
267 while (it
.hasNext()) {
268 const int index
= it
.next();
270 QPixmap pixmap
= model()->data(index
).value("iconPixmap").value
<QPixmap
>();
271 if (pixmap
.isNull()) {
272 KIcon
icon(model()->data(index
).value("iconName").toString());
273 pixmap
= icon
.pixmap(size
, size
);
275 KPixmapModifier::scale(pixmap
, QSize(size
, size
));
278 painter
.drawPixmap(x
, y
, pixmap
);
281 if (x
>= dragPixmap
.width()) {
286 if (y
>= dragPixmap
.height()) {
294 void KFileItemListView::initializeItemListWidget(KItemListWidget
* item
)
296 KFileItemListWidget
* fileItemListWidget
= static_cast<KFileItemListWidget
*>(item
);
298 switch (m_itemLayout
) {
299 case IconsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::IconsLayout
); break;
300 case CompactLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::CompactLayout
); break;
301 case DetailsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::DetailsLayout
); break;
302 default: Q_ASSERT(false); break;
305 fileItemListWidget
->setAlternatingBackgroundColors(m_itemLayout
== DetailsLayout
);
308 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
310 // Even if the icons have a different size they are always aligned within
311 // the area defined by KItemStyleOption.iconSize and hence result in no
312 // change of the item-size.
313 const bool containsIconName
= changedRoles
.contains("iconName");
314 const bool containsIconPixmap
= changedRoles
.contains("iconPixmap");
315 const int count
= changedRoles
.count();
317 const bool iconChanged
= (containsIconName
&& containsIconPixmap
&& count
== 2) ||
318 (containsIconName
&& count
== 1) ||
319 (containsIconPixmap
&& count
== 1);
323 void KFileItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
326 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
328 if (m_modelRolesUpdater
) {
329 delete m_modelRolesUpdater
;
332 m_modelRolesUpdater
= new KFileItemModelRolesUpdater(static_cast<KFileItemModel
*>(current
), this);
333 const int size
= styleOption().iconSize
;
334 m_modelRolesUpdater
->setIconSize(QSize(size
, size
));
337 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
341 updateLayoutOfVisibleItems();
344 void KFileItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
348 triggerVisibleIndexRangeUpdate();
351 void KFileItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
355 triggerVisibleIndexRangeUpdate();
358 void KFileItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
365 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
369 triggerIconSizeUpdate();
372 void KFileItemListView::onTransactionBegin()
374 m_modelRolesUpdater
->setPaused(true);
377 void KFileItemListView::onTransactionEnd()
379 // Only unpause the model-roles-updater if no timer is active. If one
380 // timer is still active the model-roles-updater will be unpaused later as
381 // soon as the timer has been exceeded.
382 const bool timerActive
= m_updateVisibleIndexRangeTimer
->isActive() ||
383 m_updateIconSizeTimer
->isActive();
385 m_modelRolesUpdater
->setPaused(false);
389 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
391 KItemListView::resizeEvent(event
);
392 triggerVisibleIndexRangeUpdate();
395 void KFileItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
397 KItemListView::slotItemsRemoved(itemRanges
);
398 updateTimersInterval();
401 void KFileItemListView::slotSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
403 const QByteArray sortRole
= model()->sortRole();
404 if (!visibleRoles().contains(sortRole
)) {
408 KItemListView::slotSortRoleChanged(current
, previous
);
411 void KFileItemListView::triggerVisibleIndexRangeUpdate()
413 m_modelRolesUpdater
->setPaused(true);
414 m_updateVisibleIndexRangeTimer
->start();
417 void KFileItemListView::updateVisibleIndexRange()
419 if (!m_modelRolesUpdater
) {
423 const int index
= firstVisibleIndex();
424 const int count
= lastVisibleIndex() - index
+ 1;
425 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
427 if (m_updateIconSizeTimer
->isActive()) {
428 // If the icon-size update is pending do an immediate update
429 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
430 // an unnecessary expensive recreation of all previews afterwards.
431 m_updateIconSizeTimer
->stop();
432 const KItemListStyleOption
& option
= styleOption();
433 m_modelRolesUpdater
->setIconSize(QSize(option
.iconSize
, option
.iconSize
));
436 m_modelRolesUpdater
->setPaused(isTransactionActive());
437 updateTimersInterval();
440 void KFileItemListView::triggerIconSizeUpdate()
442 m_modelRolesUpdater
->setPaused(true);
443 m_updateIconSizeTimer
->start();
446 void KFileItemListView::updateIconSize()
448 if (!m_modelRolesUpdater
) {
452 const KItemListStyleOption
& option
= styleOption();
453 m_modelRolesUpdater
->setIconSize(QSize(option
.iconSize
, option
.iconSize
));
455 if (m_updateVisibleIndexRangeTimer
->isActive()) {
456 // If the visibility-index-range update is pending do an immediate update
457 // of the range before unpausing m_modelRolesUpdater. This prevents
458 // an unnecessary expensive recreation of all previews afterwards.
459 m_updateVisibleIndexRangeTimer
->stop();
460 const int index
= firstVisibleIndex();
461 const int count
= lastVisibleIndex() - index
+ 1;
462 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
465 m_modelRolesUpdater
->setPaused(isTransactionActive());
466 updateTimersInterval();
469 QSizeF
KFileItemListView::visibleRoleSizeHint(int index
, const QByteArray
& role
) const
471 const KItemListStyleOption
& option
= styleOption();
473 qreal width
= m_minimumRolesWidths
.value(role
, 0);
474 const qreal height
= option
.margin
* 2 + option
.fontMetrics
.height();
476 const QHash
<QByteArray
, QVariant
> values
= model()->data(index
);
477 const QString text
= KFileItemListWidget::roleText(role
, values
);
478 if (!text
.isEmpty()) {
479 const qreal columnMargin
= option
.margin
* 3;
480 width
= qMax(width
, qreal(2 * columnMargin
+ option
.fontMetrics
.width(text
)));
483 if (role
== "name") {
484 // Increase the width by the expansion-toggle and the current expansion level
485 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
486 width
+= option
.margin
+ expansionLevel
* itemSize().height() + KIconLoader::SizeSmall
;
488 // Increase the width by the required space for the icon
489 width
+= option
.margin
* 2 + option
.iconSize
;
492 return QSizeF(width
, height
);
495 void KFileItemListView::updateLayoutOfVisibleItems()
497 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
498 initializeItemListWidget(widget
);
500 triggerVisibleIndexRangeUpdate();
503 void KFileItemListView::updateTimersInterval()
509 // The ShortInterval is used for cases like switching the directory: If the
510 // model is empty and filled later the creation of the previews should be done
511 // as soon as possible. The LongInterval is used when the model already contains
512 // items and assures that operations like zooming don't result in too many temporary
513 // recreations of the previews.
515 const int interval
= (model()->count() <= 0) ? ShortInterval
: LongInterval
;
516 m_updateVisibleIndexRangeTimer
->setInterval(interval
);
517 m_updateIconSizeTimer
->setInterval(interval
);
520 void KFileItemListView::updateMinimumRolesWidths()
522 m_minimumRolesWidths
.clear();
524 const KItemListStyleOption
& option
= styleOption();
525 const QString sizeText
= QLatin1String("888888") + i18nc("@item:intable", "items");
526 m_minimumRolesWidths
.insert("size", option
.fontMetrics
.width(sizeText
));
529 void KFileItemListView::applyRolesToModel()
531 Q_ASSERT(qobject_cast
<KFileItemModel
*>(model()));
532 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(model());
534 // KFileItemModel does not distinct between "visible" and "invisible" roles.
535 // Add all roles that are mandatory for having a working KFileItemListView:
536 QSet
<QByteArray
> roles
= visibleRoles().toSet();
537 roles
.insert("iconPixmap");
538 roles
.insert("iconName");
539 roles
.insert("name");
540 roles
.insert("isDir");
541 if (m_itemLayout
== DetailsLayout
) {
542 roles
.insert("isExpanded");
543 roles
.insert("expansionLevel");
546 // Assure that the role that is used for sorting will be determined
547 roles
.insert(fileItemModel
->sortRole());
549 fileItemModel
->setRoles(roles
);
550 m_modelRolesUpdater
->setRoles(roles
);
553 #include "kfileitemlistview.moc"