]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistview.cpp
Fix possible crash if no model is set
[dolphin.git] / src / kitemviews / kfileitemlistview.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "kfileitemlistview.h"
21
22 #include "kfileitemlistgroupheader.h"
23 #include "kfileitemmodelrolesupdater.h"
24 #include "kfileitemlistwidget.h"
25 #include "kfileitemmodel.h"
26 #include "kpixmapmodifier_p.h"
27 #include <KLocale>
28 #include <KStringHandler>
29
30 #include <KDebug>
31 #include <KIcon>
32
33 #include <QPainter>
34 #include <QTextLine>
35 #include <QTimer>
36
37 // #define KFILEITEMLISTVIEW_DEBUG
38
39 namespace {
40 const int ShortInterval = 50;
41 const int LongInterval = 300;
42 }
43
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()
51 {
52 setAcceptDrops(true);
53
54 setScrollOrientation(Qt::Vertical);
55 setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
56 setGroupHeaderCreator(new KItemListGroupHeaderCreator<KFileItemListGroupHeader>());
57
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()));
62
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()));
67
68 setVisibleRoles(QList<QByteArray>() << "name");
69
70 updateMinimumRolesWidths();
71 }
72
73 KFileItemListView::~KFileItemListView()
74 {
75 // The group headers are children of the widgets created by
76 // widgetCreator(). So it is mandatory to delete the group headers
77 // first.
78 delete groupHeaderCreator();
79 delete widgetCreator();
80
81 delete m_modelRolesUpdater;
82 m_modelRolesUpdater = 0;
83 }
84
85 void KFileItemListView::setPreviewsShown(bool show)
86 {
87 if (m_modelRolesUpdater) {
88 m_modelRolesUpdater->setPreviewShown(show);
89 }
90 }
91
92 bool KFileItemListView::previewsShown() const
93 {
94 return m_modelRolesUpdater->isPreviewShown();
95 }
96
97 void KFileItemListView::setItemLayout(Layout layout)
98 {
99 if (m_itemLayout != layout) {
100 const bool updateRoles = (m_itemLayout == DetailsLayout || layout == DetailsLayout);
101 m_itemLayout = layout;
102 if (updateRoles) {
103 // The details-layout requires some invisible roles that
104 // must be added to the model if the new layout is "details".
105 // If the old layout was "details" the roles will get removed.
106 applyRolesToModel();
107 }
108 updateLayoutOfVisibleItems();
109
110 setSupportsItemExpanding(m_itemLayout == DetailsLayout);
111 }
112 }
113
114 KFileItemListView::Layout KFileItemListView::itemLayout() const
115 {
116 return m_itemLayout;
117 }
118
119 void KFileItemListView::setEnabledPlugins(const QStringList& list)
120 {
121 if (m_modelRolesUpdater) {
122 m_modelRolesUpdater->setEnabledPlugins(list);
123 }
124 }
125
126 QStringList KFileItemListView::enabledPlugins() const
127 {
128 return m_modelRolesUpdater ? m_modelRolesUpdater->enabledPlugins() : QStringList();
129 }
130
131 QSizeF KFileItemListView::itemSizeHint(int index) const
132 {
133 const QHash<QByteArray, QVariant> values = model()->data(index);
134 const KItemListStyleOption& option = styleOption();
135 const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
136
137 switch (m_itemLayout) {
138 case IconsLayout: {
139 const QString text = KStringHandler::preProcessWrap(values["name"].toString());
140
141 const qreal maxWidth = itemSize().width() - 2 * option.padding;
142 int textLinesCount = 0;
143 QTextLine line;
144
145 // Calculate the number of lines required for wrapping the name
146 QTextOption textOption(Qt::AlignHCenter);
147 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
148
149 QTextLayout layout(text, option.font);
150 layout.setTextOption(textOption);
151 layout.beginLayout();
152 while ((line = layout.createLine()).isValid()) {
153 line.setLineWidth(maxWidth);
154 line.naturalTextWidth();
155 ++textLinesCount;
156 }
157 layout.endLayout();
158
159 // Add one line for each additional information
160 textLinesCount += additionalRolesCount;
161
162 const qreal height = textLinesCount * option.fontMetrics.height() +
163 option.iconSize +
164 option.padding * 3;
165 return QSizeF(itemSize().width(), height);
166 }
167
168 case CompactLayout: {
169 // For each row exactly one role is shown. Calculate the maximum required width that is necessary
170 // to show all roles without horizontal clipping.
171 qreal maximumRequiredWidth = 0.0;
172
173 foreach (const QByteArray& role, visibleRoles()) {
174 const QString text = KFileItemListWidget::roleText(role, values);
175 const qreal requiredWidth = option.fontMetrics.width(text);
176 maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
177 }
178
179 const qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
180 const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.height());
181 return QSizeF(width, height);
182 }
183
184 case DetailsLayout: {
185 // The width will be determined dynamically by KFileItemListView::visibleRoleSizes()
186 const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height());
187 return QSizeF(-1, height);
188 }
189
190 default:
191 Q_ASSERT(false);
192 break;
193 }
194
195 return QSize();
196 }
197
198 QHash<QByteArray, QSizeF> KFileItemListView::visibleRolesSizes(const KItemRangeList& itemRanges) const
199 {
200 QElapsedTimer timer;
201 timer.start();
202
203 QHash<QByteArray, QSizeF> sizes;
204
205 int calculatedItemCount = 0;
206 bool maxTimeExceeded = false;
207 foreach (const KItemRange& itemRange, itemRanges) {
208 const int startIndex = itemRange.index;
209 const int endIndex = startIndex + itemRange.count - 1;
210
211 for (int i = startIndex; i <= endIndex; ++i) {
212 foreach (const QByteArray& visibleRole, visibleRoles()) {
213 QSizeF maxSize = sizes.value(visibleRole, QSizeF(0, 0));
214 const QSizeF itemSize = visibleRoleSizeHint(i, visibleRole);
215 maxSize = maxSize.expandedTo(itemSize);
216 sizes.insert(visibleRole, maxSize);
217 }
218
219 if (calculatedItemCount > 100 && timer.elapsed() > 200) {
220 // When having several thousands of items calculating the sizes can get
221 // very expensive. We accept a possibly too small role-size in favour
222 // of having no blocking user interface.
223 #ifdef KFILEITEMLISTVIEW_DEBUG
224 kDebug() << "Timer exceeded, stopped after" << calculatedItemCount << "items";
225 #endif
226 maxTimeExceeded = true;
227 break;
228 }
229 ++calculatedItemCount;
230 }
231 if (maxTimeExceeded) {
232 break;
233 }
234 }
235
236 #ifdef KFILEITEMLISTVIEW_DEBUG
237 int rangesItemCount = 0;
238 foreach (const KItemRange& itemRange, itemRanges) {
239 rangesItemCount += itemRange.count;
240 }
241 kDebug() << "[TIME] Calculated dynamic item size for " << rangesItemCount << "items:" << timer.elapsed();
242 #endif
243 return sizes;
244 }
245
246 QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
247 {
248 if (!model()) {
249 return QPixmap();
250 }
251
252 const int itemCount = indexes.count();
253 Q_ASSERT(itemCount > 0);
254
255 // If more than one item is dragged, align the items inside a
256 // rectangular grid. The maximum grid size is limited to 5 x 5 items.
257 int xCount;
258 int size;
259 if (itemCount > 16) {
260 xCount = 5;
261 size = KIconLoader::SizeSmall;
262 } else if (itemCount > 9) {
263 xCount = 4;
264 size = KIconLoader::SizeSmallMedium;
265 } else {
266 xCount = 3;
267 size = KIconLoader::SizeMedium;
268 }
269
270 if (itemCount < xCount) {
271 xCount = itemCount;
272 }
273
274 int yCount = itemCount / xCount;
275 if (itemCount % xCount != 0) {
276 ++yCount;
277 }
278 if (yCount > xCount) {
279 yCount = xCount;
280 }
281
282 // Draw the selected items into the grid cells.
283 QPixmap dragPixmap(xCount * size + xCount, yCount * size + yCount);
284 dragPixmap.fill(Qt::transparent);
285
286 QPainter painter(&dragPixmap);
287 int x = 0;
288 int y = 0;
289 QSetIterator<int> it(indexes);
290 while (it.hasNext()) {
291 const int index = it.next();
292
293 QPixmap pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
294 if (pixmap.isNull()) {
295 KIcon icon(model()->data(index).value("iconName").toString());
296 pixmap = icon.pixmap(size, size);
297 } else {
298 KPixmapModifier::scale(pixmap, QSize(size, size));
299 }
300
301 painter.drawPixmap(x, y, pixmap);
302
303 x += size + 1;
304 if (x >= dragPixmap.width()) {
305 x = 0;
306 y += size + 1;
307 }
308
309 if (y >= dragPixmap.height()) {
310 break;
311 }
312 }
313
314 return dragPixmap;
315 }
316
317 void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
318 {
319 KFileItemListWidget* fileItemListWidget = static_cast<KFileItemListWidget*>(item);
320
321 switch (m_itemLayout) {
322 case IconsLayout: fileItemListWidget->setLayout(KFileItemListWidget::IconsLayout); break;
323 case CompactLayout: fileItemListWidget->setLayout(KFileItemListWidget::CompactLayout); break;
324 case DetailsLayout: fileItemListWidget->setLayout(KFileItemListWidget::DetailsLayout); break;
325 default: Q_ASSERT(false); break;
326 }
327
328 fileItemListWidget->setAlternatingBackgroundColors(m_itemLayout == DetailsLayout &&
329 visibleRoles().count() > 1);
330 }
331
332 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
333 {
334 // Even if the icons have a different size they are always aligned within
335 // the area defined by KItemStyleOption.iconSize and hence result in no
336 // change of the item-size.
337 const bool containsIconName = changedRoles.contains("iconName");
338 const bool containsIconPixmap = changedRoles.contains("iconPixmap");
339 const int count = changedRoles.count();
340
341 const bool iconChanged = (containsIconName && containsIconPixmap && count == 2) ||
342 (containsIconName && count == 1) ||
343 (containsIconPixmap && count == 1);
344 return !iconChanged;
345 }
346
347 void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
348 {
349 Q_UNUSED(previous);
350 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
351
352 delete m_modelRolesUpdater;
353 m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
354 m_modelRolesUpdater->setIconSize(availableIconSize());
355
356 applyRolesToModel();
357 }
358
359 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
360 {
361 Q_UNUSED(current);
362 Q_UNUSED(previous);
363 updateLayoutOfVisibleItems();
364 }
365
366 void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
367 {
368 Q_UNUSED(current);
369 Q_UNUSED(previous);
370 triggerVisibleIndexRangeUpdate();
371 }
372
373 void KFileItemListView::onScrollOffsetChanged(qreal current, qreal previous)
374 {
375 Q_UNUSED(current);
376 Q_UNUSED(previous);
377 triggerVisibleIndexRangeUpdate();
378 }
379
380 void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
381 {
382 applyRolesToModel();
383
384 if (m_itemLayout == DetailsLayout) {
385 // Only enable the alternating background colors if more than one role
386 // is visible
387 const int previousCount = previous.count();
388 const int currentCount = current.count();
389 if ((previousCount <= 1 && currentCount > 1) || (previousCount > 1 && currentCount <= 1)) {
390 const bool enabled = (currentCount > 1);
391 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
392 widget->setAlternatingBackgroundColors(enabled);
393 }
394 }
395 }
396 }
397
398 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
399 {
400 Q_UNUSED(current);
401 Q_UNUSED(previous);
402 triggerIconSizeUpdate();
403 }
404
405 void KFileItemListView::onTransactionBegin()
406 {
407 m_modelRolesUpdater->setPaused(true);
408 }
409
410 void KFileItemListView::onTransactionEnd()
411 {
412 // Only unpause the model-roles-updater if no timer is active. If one
413 // timer is still active the model-roles-updater will be unpaused later as
414 // soon as the timer has been exceeded.
415 const bool timerActive = m_updateVisibleIndexRangeTimer->isActive() ||
416 m_updateIconSizeTimer->isActive();
417 if (!timerActive) {
418 m_modelRolesUpdater->setPaused(false);
419 }
420 }
421
422 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
423 {
424 KItemListView::resizeEvent(event);
425 triggerVisibleIndexRangeUpdate();
426 }
427
428 void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
429 {
430 KItemListView::slotItemsRemoved(itemRanges);
431 updateTimersInterval();
432 }
433
434 void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
435 {
436 const QByteArray sortRole = model()->sortRole();
437 if (!visibleRoles().contains(sortRole)) {
438 applyRolesToModel();
439 }
440
441 KItemListView::slotSortRoleChanged(current, previous);
442 }
443
444 void KFileItemListView::triggerVisibleIndexRangeUpdate()
445 {
446 if (!model()) {
447 return;
448 }
449 m_modelRolesUpdater->setPaused(true);
450 m_updateVisibleIndexRangeTimer->start();
451 }
452
453 void KFileItemListView::updateVisibleIndexRange()
454 {
455 if (!m_modelRolesUpdater) {
456 return;
457 }
458
459 const int index = firstVisibleIndex();
460 const int count = lastVisibleIndex() - index + 1;
461 m_modelRolesUpdater->setVisibleIndexRange(index, count);
462
463 if (m_updateIconSizeTimer->isActive()) {
464 // If the icon-size update is pending do an immediate update
465 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
466 // an unnecessary expensive recreation of all previews afterwards.
467 m_updateIconSizeTimer->stop();
468 m_modelRolesUpdater->setIconSize(availableIconSize());
469 }
470
471 m_modelRolesUpdater->setPaused(isTransactionActive());
472 updateTimersInterval();
473 }
474
475 void KFileItemListView::triggerIconSizeUpdate()
476 {
477 if (!model()) {
478 return;
479 }
480 m_modelRolesUpdater->setPaused(true);
481 m_updateIconSizeTimer->start();
482 }
483
484 void KFileItemListView::updateIconSize()
485 {
486 if (!m_modelRolesUpdater) {
487 return;
488 }
489
490 m_modelRolesUpdater->setIconSize(availableIconSize());
491
492 if (m_updateVisibleIndexRangeTimer->isActive()) {
493 // If the visibility-index-range update is pending do an immediate update
494 // of the range before unpausing m_modelRolesUpdater. This prevents
495 // an unnecessary expensive recreation of all previews afterwards.
496 m_updateVisibleIndexRangeTimer->stop();
497 const int index = firstVisibleIndex();
498 const int count = lastVisibleIndex() - index + 1;
499 m_modelRolesUpdater->setVisibleIndexRange(index, count);
500 }
501
502 m_modelRolesUpdater->setPaused(isTransactionActive());
503 updateTimersInterval();
504 }
505
506 QSizeF KFileItemListView::visibleRoleSizeHint(int index, const QByteArray& role) const
507 {
508 const KItemListStyleOption& option = styleOption();
509
510 qreal width = m_minimumRolesWidths.value(role, 0);
511 const qreal height = option.padding * 2 + option.fontMetrics.height();
512
513 const QHash<QByteArray, QVariant> values = model()->data(index);
514 const QString text = KFileItemListWidget::roleText(role, values);
515 if (!text.isEmpty()) {
516 const qreal columnPadding = option.padding * 3;
517 width = qMax(width, qreal(2 * columnPadding + option.fontMetrics.width(text)));
518 }
519
520 if (role == "name") {
521 // Increase the width by the expansion-toggle and the current expansion level
522 const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
523 width += option.padding + expandedParentsCount * itemSize().height() + KIconLoader::SizeSmall;
524
525 // Increase the width by the required space for the icon
526 width += option.padding * 2 + option.iconSize;
527 }
528
529 return QSizeF(width, height);
530 }
531
532 void KFileItemListView::updateLayoutOfVisibleItems()
533 {
534 if (!model()) {
535 return;
536 }
537
538 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
539 initializeItemListWidget(widget);
540 }
541 triggerVisibleIndexRangeUpdate();
542 }
543
544 void KFileItemListView::updateTimersInterval()
545 {
546 if (!model()) {
547 return;
548 }
549
550 // The ShortInterval is used for cases like switching the directory: If the
551 // model is empty and filled later the creation of the previews should be done
552 // as soon as possible. The LongInterval is used when the model already contains
553 // items and assures that operations like zooming don't result in too many temporary
554 // recreations of the previews.
555
556 const int interval = (model()->count() <= 0) ? ShortInterval : LongInterval;
557 m_updateVisibleIndexRangeTimer->setInterval(interval);
558 m_updateIconSizeTimer->setInterval(interval);
559 }
560
561 void KFileItemListView::updateMinimumRolesWidths()
562 {
563 m_minimumRolesWidths.clear();
564
565 const KItemListStyleOption& option = styleOption();
566 const QString sizeText = QLatin1String("888888") + i18nc("@item:intable", "items");
567 m_minimumRolesWidths.insert("size", option.fontMetrics.width(sizeText));
568 }
569
570 void KFileItemListView::applyRolesToModel()
571 {
572 if (!model()) {
573 return;
574 }
575
576 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
577 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
578
579 // KFileItemModel does not distinct between "visible" and "invisible" roles.
580 // Add all roles that are mandatory for having a working KFileItemListView:
581 QSet<QByteArray> roles = visibleRoles().toSet();
582 roles.insert("iconPixmap");
583 roles.insert("iconName");
584 roles.insert("name");
585 roles.insert("isDir");
586 if (m_itemLayout == DetailsLayout) {
587 roles.insert("isExpanded");
588 roles.insert("isExpandable");
589 roles.insert("expandedParentsCount");
590 }
591
592 // Assure that the role that is used for sorting will be determined
593 roles.insert(fileItemModel->sortRole());
594
595 fileItemModel->setRoles(roles);
596 m_modelRolesUpdater->setRoles(roles);
597 }
598
599 QSize KFileItemListView::availableIconSize() const
600 {
601 const KItemListStyleOption& option = styleOption();
602 const int iconSize = option.iconSize;
603 if (m_itemLayout == IconsLayout) {
604 const int maxIconWidth = itemSize().width() - 2 * option.padding;
605 return QSize(maxIconWidth, iconSize);
606 }
607
608 return QSize(iconSize, iconSize);
609 }
610
611 #include "kfileitemlistview.moc"