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 "kitemlistgroupheader.h"
23 #include "kfileitemmodelrolesupdater.h"
24 #include "kfileitemlistwidget.h"
25 #include "kfileitemmodel.h"
27 #include <KStringHandler>
35 #define KFILEITEMLISTVIEW_DEBUG
38 const int ShortInterval
= 50;
39 const int LongInterval
= 300;
42 KFileItemListView::KFileItemListView(QGraphicsWidget
* parent
) :
43 KItemListView(parent
),
44 m_itemLayout(IconsLayout
),
45 m_modelRolesUpdater(0),
46 m_updateVisibleIndexRangeTimer(0),
47 m_updateIconSizeTimer(0),
48 m_minimumRolesWidths()
52 setScrollOrientation(Qt::Vertical
);
53 setWidgetCreator(new KItemListWidgetCreator
<KFileItemListWidget
>());
54 setGroupHeaderCreator(new KItemListGroupHeaderCreator
<KItemListGroupHeader
>());
56 m_updateVisibleIndexRangeTimer
= new QTimer(this);
57 m_updateVisibleIndexRangeTimer
->setSingleShot(true);
58 m_updateVisibleIndexRangeTimer
->setInterval(ShortInterval
);
59 connect(m_updateVisibleIndexRangeTimer
, SIGNAL(timeout()), this, SLOT(updateVisibleIndexRange()));
61 m_updateIconSizeTimer
= new QTimer(this);
62 m_updateIconSizeTimer
->setSingleShot(true);
63 m_updateIconSizeTimer
->setInterval(ShortInterval
);
64 connect(m_updateIconSizeTimer
, SIGNAL(timeout()), this, SLOT(updateIconSize()));
66 updateMinimumRolesWidths();
69 KFileItemListView::~KFileItemListView()
71 delete widgetCreator();
72 delete groupHeaderCreator();
74 delete m_modelRolesUpdater
;
75 m_modelRolesUpdater
= 0;
78 void KFileItemListView::setPreviewsShown(bool show
)
80 if (m_modelRolesUpdater
) {
81 m_modelRolesUpdater
->setPreviewShown(show
);
85 bool KFileItemListView::previewsShown() const
87 return m_modelRolesUpdater
->isPreviewShown();
90 void KFileItemListView::setItemLayout(Layout layout
)
92 if (m_itemLayout
!= layout
) {
93 m_itemLayout
= layout
;
94 updateLayoutOfVisibleItems();
98 KFileItemListView::Layout
KFileItemListView::itemLayout() const
103 QSizeF
KFileItemListView::itemSizeHint(int index
) const
105 const QHash
<QByteArray
, QVariant
> values
= model()->data(index
);
106 const KItemListStyleOption
& option
= styleOption();
107 const int additionalRolesCount
= qMax(visibleRoles().count() - 1, 0);
109 switch (m_itemLayout
) {
111 const QString text
= KStringHandler::preProcessWrap(values
["name"].toString());
113 const qreal maxWidth
= itemSize().width() - 2 * option
.margin
;
114 int textLinesCount
= 0;
117 // Calculate the number of lines required for wrapping the name
118 QTextOption
textOption(Qt::AlignHCenter
);
119 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
121 QTextLayout
layout(text
, option
.font
);
122 layout
.setTextOption(textOption
);
123 layout
.beginLayout();
124 while ((line
= layout
.createLine()).isValid()) {
125 line
.setLineWidth(maxWidth
);
126 line
.naturalTextWidth();
131 // Add one line for each additional information
132 textLinesCount
+= additionalRolesCount
;
134 const qreal height
= textLinesCount
* option
.fontMetrics
.height() +
137 return QSizeF(itemSize().width(), height
);
140 case CompactLayout
: {
141 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
142 // to show all roles without horizontal clipping.
143 qreal maximumRequiredWidth
= 0.0;
145 foreach (const QByteArray
& role
, visibleRoles()) {
146 const QString text
= values
[role
].toString();
147 const qreal requiredWidth
= option
.fontMetrics
.width(text
);
148 maximumRequiredWidth
= qMax(maximumRequiredWidth
, requiredWidth
);
151 const qreal width
= option
.margin
* 4 + option
.iconSize
+ maximumRequiredWidth
;
152 const qreal height
= option
.margin
* 2 + qMax(option
.iconSize
, (1 + additionalRolesCount
) * option
.fontMetrics
.height());
153 return QSizeF(width
, height
);
156 case DetailsLayout
: {
157 // The width will be determined dynamically by KFileItemListView::visibleRoleSizes()
158 const qreal height
= option
.margin
* 2 + qMax(option
.iconSize
, option
.fontMetrics
.height());
159 return QSizeF(-1, height
);
170 QHash
<QByteArray
, QSizeF
> KFileItemListView::visibleRoleSizes() const
175 QHash
<QByteArray
, QSizeF
> sizes
;
177 const int itemCount
= model()->count();
178 for (int i
= 0; i
< itemCount
; ++i
) {
179 foreach (const QByteArray
& visibleRole
, visibleRoles()) {
180 QSizeF maxSize
= sizes
.value(visibleRole
, QSizeF(0, 0));
181 const QSizeF itemSize
= visibleRoleSizeHint(i
, visibleRole
);
182 maxSize
= maxSize
.expandedTo(itemSize
);
183 sizes
.insert(visibleRole
, maxSize
);
186 if (i
> 100 && timer
.elapsed() > 200) {
187 // When having several thousands of items calculating the sizes can get
188 // very expensive. We accept a possibly too small role-size in favour
189 // of having no blocking user interface.
190 #ifdef KFILEITEMLISTVIEW_DEBUG
191 kDebug() << "Timer exceeded, stopped after" << i
<< "items";
197 #ifdef KFILEITEMLISTVIEW_DEBUG
198 kDebug() << "[TIME] Calculated dynamic item size for " << itemCount
<< "items:" << timer
.elapsed();
203 QPixmap
KFileItemListView::createDragPixmap(const QSet
<int>& indexes
) const
208 QSetIterator
<int> it(indexes
);
209 while (it
.hasNext()) {
210 const int index
= it
.next();
211 // TODO: Only one item is considered currently
212 pixmap
= model()->data(index
).value("iconPixmap").value
<QPixmap
>();
213 if (pixmap
.isNull()) {
214 KIcon
icon(model()->data(index
).value("iconName").toString());
215 pixmap
= icon
.pixmap(itemSize().toSize());
223 void KFileItemListView::initializeItemListWidget(KItemListWidget
* item
)
225 KFileItemListWidget
* fileItemListWidget
= static_cast<KFileItemListWidget
*>(item
);
227 switch (m_itemLayout
) {
228 case IconsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::IconsLayout
); break;
229 case CompactLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::CompactLayout
); break;
230 case DetailsLayout
: fileItemListWidget
->setLayout(KFileItemListWidget::DetailsLayout
); break;
231 default: Q_ASSERT(false); break;
235 void KFileItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
238 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
240 if (m_modelRolesUpdater
) {
241 delete m_modelRolesUpdater
;
244 m_modelRolesUpdater
= new KFileItemModelRolesUpdater(static_cast<KFileItemModel
*>(current
), this);
245 const int size
= styleOption().iconSize
;
246 m_modelRolesUpdater
->setIconSize(QSize(size
, size
));
249 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
253 updateLayoutOfVisibleItems();
256 void KFileItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
260 triggerVisibleIndexRangeUpdate();
263 void KFileItemListView::onOffsetChanged(qreal current
, qreal previous
)
267 triggerVisibleIndexRangeUpdate();
270 void KFileItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
274 Q_ASSERT(qobject_cast
<KFileItemModel
*>(model()));
275 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(model());
277 // KFileItemModel does not distinct between "visible" and "invisible" roles.
278 // Add all roles that are mandatory for having a working KFileItemListView:
279 QSet
<QByteArray
> keys
= current
.toSet();
280 QSet
<QByteArray
> roles
= keys
;
281 roles
.insert("iconPixmap");
282 roles
.insert("iconName");
283 roles
.insert("name"); // TODO: just don't allow to disable it
284 roles
.insert("isDir");
285 if (m_itemLayout
== DetailsLayout
) {
286 roles
.insert("isExpanded");
287 roles
.insert("expansionLevel");
290 fileItemModel
->setRoles(roles
);
292 m_modelRolesUpdater
->setRoles(keys
);
295 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
299 triggerIconSizeUpdate();
302 void KFileItemListView::onTransactionBegin()
304 m_modelRolesUpdater
->setPaused(true);
307 void KFileItemListView::onTransactionEnd()
309 // Only unpause the model-roles-updater if no timer is active. If one
310 // timer is still active the model-roles-updater will be unpaused later as
311 // soon as the timer has been exceeded.
312 const bool timerActive
= m_updateVisibleIndexRangeTimer
->isActive() ||
313 m_updateIconSizeTimer
->isActive();
315 m_modelRolesUpdater
->setPaused(false);
319 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
321 KItemListView::resizeEvent(event
);
322 triggerVisibleIndexRangeUpdate();
325 void KFileItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
327 KItemListView::slotItemsRemoved(itemRanges
);
328 updateTimersInterval();
331 void KFileItemListView::triggerVisibleIndexRangeUpdate()
333 m_modelRolesUpdater
->setPaused(true);
334 m_updateVisibleIndexRangeTimer
->start();
337 void KFileItemListView::updateVisibleIndexRange()
339 if (!m_modelRolesUpdater
) {
343 const int index
= firstVisibleIndex();
344 const int count
= lastVisibleIndex() - index
+ 1;
345 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
347 if (m_updateIconSizeTimer
->isActive()) {
348 // If the icon-size update is pending do an immediate update
349 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
350 // an unnecessary expensive recreation of all previews afterwards.
351 m_updateIconSizeTimer
->stop();
352 const KItemListStyleOption
& option
= styleOption();
353 m_modelRolesUpdater
->setIconSize(QSize(option
.iconSize
, option
.iconSize
));
356 m_modelRolesUpdater
->setPaused(isTransactionActive());
357 updateTimersInterval();
360 void KFileItemListView::triggerIconSizeUpdate()
362 m_modelRolesUpdater
->setPaused(true);
363 m_updateIconSizeTimer
->start();
366 void KFileItemListView::updateIconSize()
368 if (!m_modelRolesUpdater
) {
372 const KItemListStyleOption
& option
= styleOption();
373 m_modelRolesUpdater
->setIconSize(QSize(option
.iconSize
, option
.iconSize
));
375 if (m_updateVisibleIndexRangeTimer
->isActive()) {
376 // If the visibility-index-range update is pending do an immediate update
377 // of the range before unpausing m_modelRolesUpdater. This prevents
378 // an unnecessary expensive recreation of all previews afterwards.
379 m_updateVisibleIndexRangeTimer
->stop();
380 const int index
= firstVisibleIndex();
381 const int count
= lastVisibleIndex() - index
+ 1;
382 m_modelRolesUpdater
->setVisibleIndexRange(index
, count
);
385 m_modelRolesUpdater
->setPaused(isTransactionActive());
386 updateTimersInterval();
389 QSizeF
KFileItemListView::visibleRoleSizeHint(int index
, const QByteArray
& role
) const
391 const KItemListStyleOption
& option
= styleOption();
393 qreal width
= m_minimumRolesWidths
.value(role
, 0);
394 const qreal height
= option
.margin
* 2 + option
.fontMetrics
.height();
396 const QVariant value
= model()->data(index
).value(role
);
397 const QString text
= value
.toString();
398 if (!text
.isEmpty()) {
399 const qreal columnMargin
= option
.margin
* 3;
400 width
= qMax(width
, qreal(2 * columnMargin
+ option
.fontMetrics
.width(text
)));
403 if (role
== "name") {
404 const QHash
<QByteArray
, QVariant
> values
= model()->data(index
);
405 Q_ASSERT(values
.contains("expansionLevel"));
407 // Increase the width by the expansion-toggle and the current expansion level
408 const int expansionLevel
= values
.value("expansionLevel", 0).toInt();
409 width
+= option
.margin
+ expansionLevel
* itemSize().height() + KIconLoader::SizeSmall
;
411 // Increase the width by the required space for the icon
412 width
+= option
.margin
* 2 + option
.iconSize
;
415 return QSizeF(width
, height
);
418 void KFileItemListView::updateLayoutOfVisibleItems()
420 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
421 initializeItemListWidget(widget
);
423 triggerVisibleIndexRangeUpdate();
426 void KFileItemListView::updateTimersInterval()
432 // The ShortInterval is used for cases like switching the directory: If the
433 // model is empty and filled later the creation of the previews should be done
434 // as soon as possible. The LongInterval is used when the model already contains
435 // items and assures that operations like zooming don't result in too many temporary
436 // recreations of the previews.
438 const int interval
= (model()->count() <= 0) ? ShortInterval
: LongInterval
;
439 m_updateVisibleIndexRangeTimer
->setInterval(interval
);
440 m_updateIconSizeTimer
->setInterval(interval
);
443 void KFileItemListView::updateMinimumRolesWidths()
445 m_minimumRolesWidths
.clear();
447 const KItemListStyleOption
& option
= styleOption();
448 const QString sizeText
= QLatin1String("888888") + i18nc("@item:intable", "items");
449 m_minimumRolesWidths
.insert("size", option
.fontMetrics
.width(sizeText
));
452 #include "kfileitemlistview.moc"