]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistview.cpp
Increase the width of the first column automatically
[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 "kitemlistgroupheader.h"
23 #include "kfileitemmodelrolesupdater.h"
24 #include "kfileitemlistwidget.h"
25 #include "kfileitemmodel.h"
26 #include <KLocale>
27 #include <KStringHandler>
28
29 #include <KDebug>
30 #include <KIcon>
31
32 #include <QTextLine>
33 #include <QTimer>
34
35 #define KFILEITEMLISTVIEW_DEBUG
36
37 namespace {
38 const int ShortInterval = 50;
39 const int LongInterval = 300;
40 }
41
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()
49 {
50 setAcceptDrops(true);
51
52 setScrollOrientation(Qt::Vertical);
53 setWidgetCreator(new KItemListWidgetCreator<KFileItemListWidget>());
54 setGroupHeaderCreator(new KItemListGroupHeaderCreator<KItemListGroupHeader>());
55
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()));
60
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()));
65
66 updateMinimumRolesWidths();
67 }
68
69 KFileItemListView::~KFileItemListView()
70 {
71 delete widgetCreator();
72 delete groupHeaderCreator();
73
74 delete m_modelRolesUpdater;
75 m_modelRolesUpdater = 0;
76 }
77
78 void KFileItemListView::setPreviewsShown(bool show)
79 {
80 if (m_modelRolesUpdater) {
81 m_modelRolesUpdater->setPreviewShown(show);
82 }
83 }
84
85 bool KFileItemListView::previewsShown() const
86 {
87 return m_modelRolesUpdater->isPreviewShown();
88 }
89
90 void KFileItemListView::setItemLayout(Layout layout)
91 {
92 if (m_itemLayout != layout) {
93 m_itemLayout = layout;
94 updateLayoutOfVisibleItems();
95 }
96 }
97
98 KFileItemListView::Layout KFileItemListView::itemLayout() const
99 {
100 return m_itemLayout;
101 }
102
103 QSizeF KFileItemListView::itemSizeHint(int index) const
104 {
105 const QHash<QByteArray, QVariant> values = model()->data(index);
106 const KItemListStyleOption& option = styleOption();
107 const int additionalRolesCount = qMax(visibleRoles().count() - 1, 0);
108
109 switch (m_itemLayout) {
110 case IconsLayout: {
111 const QString text = KStringHandler::preProcessWrap(values["name"].toString());
112
113 const qreal maxWidth = itemSize().width() - 2 * option.margin;
114 int textLinesCount = 0;
115 QTextLine line;
116
117 // Calculate the number of lines required for wrapping the name
118 QTextOption textOption(Qt::AlignHCenter);
119 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
120
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();
127 ++textLinesCount;
128 }
129 layout.endLayout();
130
131 // Add one line for each additional information
132 textLinesCount += additionalRolesCount;
133
134 const qreal height = textLinesCount * option.fontMetrics.height() +
135 option.iconSize +
136 option.margin * 4;
137 return QSizeF(itemSize().width(), height);
138 }
139
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;
144
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);
149 }
150
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);
154 }
155
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);
160 }
161
162 default:
163 Q_ASSERT(false);
164 break;
165 }
166
167 return QSize();
168 }
169
170 QHash<QByteArray, QSizeF> KFileItemListView::visibleRoleSizes() const
171 {
172 QElapsedTimer timer;
173 timer.start();
174
175 QHash<QByteArray, QSizeF> sizes;
176
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);
184 }
185
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";
192 #endif
193 break;
194 }
195 }
196
197 // Stretch the width of the first role so that the full visible view-width
198 // is used to show all roles.
199 const qreal availableWidth = size().width();
200
201 qreal usedWidth = 0;
202 QHashIterator<QByteArray, QSizeF> it(sizes);
203 while (it.hasNext()) {
204 it.next();
205 usedWidth += it.value().width();
206 }
207
208 if (usedWidth < availableWidth) {
209 const QByteArray role = visibleRoles().first();
210 QSizeF firstRoleSize = sizes.value(role);
211 firstRoleSize.rwidth() += availableWidth - usedWidth;
212 sizes.insert(role, firstRoleSize);
213 }
214
215 #ifdef KFILEITEMLISTVIEW_DEBUG
216 kDebug() << "[TIME] Calculated dynamic item size for " << itemCount << "items:" << timer.elapsed();
217 #endif
218 return sizes;
219 }
220
221 QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
222 {
223 QPixmap pixmap;
224
225 if (model()) {
226 QSetIterator<int> it(indexes);
227 while (it.hasNext()) {
228 const int index = it.next();
229 // TODO: Only one item is considered currently
230 pixmap = model()->data(index).value("iconPixmap").value<QPixmap>();
231 if (pixmap.isNull()) {
232 KIcon icon(model()->data(index).value("iconName").toString());
233 pixmap = icon.pixmap(itemSize().toSize());
234 }
235 }
236 }
237
238 return pixmap;
239 }
240
241 void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
242 {
243 KFileItemListWidget* fileItemListWidget = static_cast<KFileItemListWidget*>(item);
244
245 switch (m_itemLayout) {
246 case IconsLayout: fileItemListWidget->setLayout(KFileItemListWidget::IconsLayout); break;
247 case CompactLayout: fileItemListWidget->setLayout(KFileItemListWidget::CompactLayout); break;
248 case DetailsLayout: fileItemListWidget->setLayout(KFileItemListWidget::DetailsLayout); break;
249 default: Q_ASSERT(false); break;
250 }
251 }
252
253 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
254 {
255 // Even if the icons have a different size they are always aligned within
256 // the area defined by KItemStyleOption.iconSize and hence result in no
257 // change of the item-size.
258 const bool containsIconName = changedRoles.contains("iconName");
259 const bool containsIconPixmap = changedRoles.contains("iconPixmap");
260 const int count = changedRoles.count();
261
262 const bool iconChanged = (containsIconName && containsIconPixmap && count == 2) ||
263 (containsIconName && count == 1) ||
264 (containsIconPixmap && count == 1);
265 return !iconChanged;
266 }
267
268 void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
269 {
270 Q_UNUSED(previous);
271 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
272
273 if (m_modelRolesUpdater) {
274 delete m_modelRolesUpdater;
275 }
276
277 m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
278 const int size = styleOption().iconSize;
279 m_modelRolesUpdater->setIconSize(QSize(size, size));
280 }
281
282 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
283 {
284 Q_UNUSED(current);
285 Q_UNUSED(previous);
286 updateLayoutOfVisibleItems();
287 }
288
289 void KFileItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
290 {
291 Q_UNUSED(current);
292 Q_UNUSED(previous);
293 triggerVisibleIndexRangeUpdate();
294 }
295
296 void KFileItemListView::onOffsetChanged(qreal current, qreal previous)
297 {
298 Q_UNUSED(current);
299 Q_UNUSED(previous);
300 triggerVisibleIndexRangeUpdate();
301 }
302
303 void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
304 {
305 Q_UNUSED(previous);
306
307 Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
308 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
309
310 // KFileItemModel does not distinct between "visible" and "invisible" roles.
311 // Add all roles that are mandatory for having a working KFileItemListView:
312 QSet<QByteArray> keys = current.toSet();
313 QSet<QByteArray> roles = keys;
314 roles.insert("iconPixmap");
315 roles.insert("iconName");
316 roles.insert("name"); // TODO: just don't allow to disable it
317 roles.insert("isDir");
318 if (m_itemLayout == DetailsLayout) {
319 roles.insert("isExpanded");
320 roles.insert("expansionLevel");
321 }
322
323 fileItemModel->setRoles(roles);
324
325 m_modelRolesUpdater->setRoles(keys);
326 }
327
328 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
329 {
330 Q_UNUSED(current);
331 Q_UNUSED(previous);
332 triggerIconSizeUpdate();
333 }
334
335 void KFileItemListView::onTransactionBegin()
336 {
337 m_modelRolesUpdater->setPaused(true);
338 }
339
340 void KFileItemListView::onTransactionEnd()
341 {
342 // Only unpause the model-roles-updater if no timer is active. If one
343 // timer is still active the model-roles-updater will be unpaused later as
344 // soon as the timer has been exceeded.
345 const bool timerActive = m_updateVisibleIndexRangeTimer->isActive() ||
346 m_updateIconSizeTimer->isActive();
347 if (!timerActive) {
348 m_modelRolesUpdater->setPaused(false);
349 }
350 }
351
352 void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
353 {
354 KItemListView::resizeEvent(event);
355 triggerVisibleIndexRangeUpdate();
356 markVisibleRolesSizesAsDirty();
357 }
358
359 void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
360 {
361 KItemListView::slotItemsRemoved(itemRanges);
362 updateTimersInterval();
363 }
364
365 void KFileItemListView::triggerVisibleIndexRangeUpdate()
366 {
367 m_modelRolesUpdater->setPaused(true);
368 m_updateVisibleIndexRangeTimer->start();
369 }
370
371 void KFileItemListView::updateVisibleIndexRange()
372 {
373 if (!m_modelRolesUpdater) {
374 return;
375 }
376
377 const int index = firstVisibleIndex();
378 const int count = lastVisibleIndex() - index + 1;
379 m_modelRolesUpdater->setVisibleIndexRange(index, count);
380
381 if (m_updateIconSizeTimer->isActive()) {
382 // If the icon-size update is pending do an immediate update
383 // of the icon-size before unpausing m_modelRolesUpdater. This prevents
384 // an unnecessary expensive recreation of all previews afterwards.
385 m_updateIconSizeTimer->stop();
386 const KItemListStyleOption& option = styleOption();
387 m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
388 }
389
390 m_modelRolesUpdater->setPaused(isTransactionActive());
391 updateTimersInterval();
392 }
393
394 void KFileItemListView::triggerIconSizeUpdate()
395 {
396 m_modelRolesUpdater->setPaused(true);
397 m_updateIconSizeTimer->start();
398 }
399
400 void KFileItemListView::updateIconSize()
401 {
402 if (!m_modelRolesUpdater) {
403 return;
404 }
405
406 const KItemListStyleOption& option = styleOption();
407 m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
408
409 if (m_updateVisibleIndexRangeTimer->isActive()) {
410 // If the visibility-index-range update is pending do an immediate update
411 // of the range before unpausing m_modelRolesUpdater. This prevents
412 // an unnecessary expensive recreation of all previews afterwards.
413 m_updateVisibleIndexRangeTimer->stop();
414 const int index = firstVisibleIndex();
415 const int count = lastVisibleIndex() - index + 1;
416 m_modelRolesUpdater->setVisibleIndexRange(index, count);
417 }
418
419 m_modelRolesUpdater->setPaused(isTransactionActive());
420 updateTimersInterval();
421 }
422
423 QSizeF KFileItemListView::visibleRoleSizeHint(int index, const QByteArray& role) const
424 {
425 const KItemListStyleOption& option = styleOption();
426
427 qreal width = m_minimumRolesWidths.value(role, 0);
428 const qreal height = option.margin * 2 + option.fontMetrics.height();
429
430 const QVariant value = model()->data(index).value(role);
431 const QString text = value.toString();
432 if (!text.isEmpty()) {
433 const qreal columnMargin = option.margin * 3;
434 width = qMax(width, qreal(2 * columnMargin + option.fontMetrics.width(text)));
435 }
436
437 if (role == "name") {
438 const QHash<QByteArray, QVariant> values = model()->data(index);
439 Q_ASSERT(values.contains("expansionLevel"));
440
441 // Increase the width by the expansion-toggle and the current expansion level
442 const int expansionLevel = values.value("expansionLevel", 0).toInt();
443 width += option.margin + expansionLevel * itemSize().height() + KIconLoader::SizeSmall;
444
445 // Increase the width by the required space for the icon
446 width += option.margin * 2 + option.iconSize;
447 }
448
449 return QSizeF(width, height);
450 }
451
452 void KFileItemListView::updateLayoutOfVisibleItems()
453 {
454 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
455 initializeItemListWidget(widget);
456 }
457 triggerVisibleIndexRangeUpdate();
458 }
459
460 void KFileItemListView::updateTimersInterval()
461 {
462 if (!model()) {
463 return;
464 }
465
466 // The ShortInterval is used for cases like switching the directory: If the
467 // model is empty and filled later the creation of the previews should be done
468 // as soon as possible. The LongInterval is used when the model already contains
469 // items and assures that operations like zooming don't result in too many temporary
470 // recreations of the previews.
471
472 const int interval = (model()->count() <= 0) ? ShortInterval : LongInterval;
473 m_updateVisibleIndexRangeTimer->setInterval(interval);
474 m_updateIconSizeTimer->setInterval(interval);
475 }
476
477 void KFileItemListView::updateMinimumRolesWidths()
478 {
479 m_minimumRolesWidths.clear();
480
481 const KItemListStyleOption& option = styleOption();
482 const QString sizeText = QLatin1String("888888") + i18nc("@item:intable", "items");
483 m_minimumRolesWidths.insert("size", option.fontMetrics.width(sizeText));
484 }
485
486 #include "kfileitemlistview.moc"