]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistviewlayouter.cpp
Merge remote-tracking branch 'origin/KDE/4.11' into KDE/4.12
[dolphin.git] / src / kitemviews / private / kitemlistviewlayouter.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 "kitemlistviewlayouter.h"
21
22 #include <kitemviews/kitemmodelbase.h>
23 #include "kitemlistsizehintresolver.h"
24
25 #include <KDebug>
26
27 // #define KITEMLISTVIEWLAYOUTER_DEBUG
28
29 KItemListViewLayouter::KItemListViewLayouter(QObject* parent) :
30 QObject(parent),
31 m_dirty(true),
32 m_visibleIndexesDirty(true),
33 m_scrollOrientation(Qt::Vertical),
34 m_size(),
35 m_itemSize(128, 128),
36 m_itemMargin(),
37 m_headerHeight(0),
38 m_model(0),
39 m_sizeHintResolver(0),
40 m_scrollOffset(0),
41 m_maximumScrollOffset(0),
42 m_itemOffset(0),
43 m_maximumItemOffset(0),
44 m_firstVisibleIndex(-1),
45 m_lastVisibleIndex(-1),
46 m_columnWidth(0),
47 m_xPosInc(0),
48 m_columnCount(0),
49 m_groupItemIndexes(),
50 m_groupHeaderHeight(0),
51 m_groupHeaderMargin(0),
52 m_itemInfos()
53 {
54 }
55
56 KItemListViewLayouter::~KItemListViewLayouter()
57 {
58 }
59
60 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation)
61 {
62 if (m_scrollOrientation != orientation) {
63 m_scrollOrientation = orientation;
64 m_dirty = true;
65 }
66 }
67
68 Qt::Orientation KItemListViewLayouter::scrollOrientation() const
69 {
70 return m_scrollOrientation;
71 }
72
73 void KItemListViewLayouter::setSize(const QSizeF& size)
74 {
75 if (m_size != size) {
76 if (m_scrollOrientation == Qt::Vertical) {
77 if (m_size.width() != size.width()) {
78 m_dirty = true;
79 }
80 } else if (m_size.height() != size.height()) {
81 m_dirty = true;
82 }
83
84 m_size = size;
85 m_visibleIndexesDirty = true;
86 }
87 }
88
89 QSizeF KItemListViewLayouter::size() const
90 {
91 return m_size;
92 }
93
94 void KItemListViewLayouter::setItemSize(const QSizeF& size)
95 {
96 if (m_itemSize != size) {
97 m_itemSize = size;
98 m_dirty = true;
99 }
100 }
101
102 QSizeF KItemListViewLayouter::itemSize() const
103 {
104 return m_itemSize;
105 }
106
107 void KItemListViewLayouter::setItemMargin(const QSizeF& margin)
108 {
109 if (m_itemMargin != margin) {
110 m_itemMargin = margin;
111 m_dirty = true;
112 }
113 }
114
115 QSizeF KItemListViewLayouter::itemMargin() const
116 {
117 return m_itemMargin;
118 }
119
120 void KItemListViewLayouter::setHeaderHeight(qreal height)
121 {
122 if (m_headerHeight != height) {
123 m_headerHeight = height;
124 m_dirty = true;
125 }
126 }
127
128 qreal KItemListViewLayouter::headerHeight() const
129 {
130 return m_headerHeight;
131 }
132
133 void KItemListViewLayouter::setGroupHeaderHeight(qreal height)
134 {
135 if (m_groupHeaderHeight != height) {
136 m_groupHeaderHeight = height;
137 m_dirty = true;
138 }
139 }
140
141 qreal KItemListViewLayouter::groupHeaderHeight() const
142 {
143 return m_groupHeaderHeight;
144 }
145
146 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin)
147 {
148 if (m_groupHeaderMargin != margin) {
149 m_groupHeaderMargin = margin;
150 m_dirty = true;
151 }
152 }
153
154 qreal KItemListViewLayouter::groupHeaderMargin() const
155 {
156 return m_groupHeaderMargin;
157 }
158
159 void KItemListViewLayouter::setScrollOffset(qreal offset)
160 {
161 if (m_scrollOffset != offset) {
162 m_scrollOffset = offset;
163 m_visibleIndexesDirty = true;
164 }
165 }
166
167 qreal KItemListViewLayouter::scrollOffset() const
168 {
169 return m_scrollOffset;
170 }
171
172 qreal KItemListViewLayouter::maximumScrollOffset() const
173 {
174 const_cast<KItemListViewLayouter*>(this)->doLayout();
175 return m_maximumScrollOffset;
176 }
177
178 void KItemListViewLayouter::setItemOffset(qreal offset)
179 {
180 if (m_itemOffset != offset) {
181 m_itemOffset = offset;
182 m_visibleIndexesDirty = true;
183 }
184 }
185
186 qreal KItemListViewLayouter::itemOffset() const
187 {
188 return m_itemOffset;
189 }
190
191 qreal KItemListViewLayouter::maximumItemOffset() const
192 {
193 const_cast<KItemListViewLayouter*>(this)->doLayout();
194 return m_maximumItemOffset;
195 }
196
197 void KItemListViewLayouter::setModel(const KItemModelBase* model)
198 {
199 if (m_model != model) {
200 m_model = model;
201 m_dirty = true;
202 }
203 }
204
205 const KItemModelBase* KItemListViewLayouter::model() const
206 {
207 return m_model;
208 }
209
210 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver* sizeHintResolver)
211 {
212 if (m_sizeHintResolver != sizeHintResolver) {
213 m_sizeHintResolver = sizeHintResolver;
214 m_dirty = true;
215 }
216 }
217
218 const KItemListSizeHintResolver* KItemListViewLayouter::sizeHintResolver() const
219 {
220 return m_sizeHintResolver;
221 }
222
223 int KItemListViewLayouter::firstVisibleIndex() const
224 {
225 const_cast<KItemListViewLayouter*>(this)->doLayout();
226 return m_firstVisibleIndex;
227 }
228
229 int KItemListViewLayouter::lastVisibleIndex() const
230 {
231 const_cast<KItemListViewLayouter*>(this)->doLayout();
232 return m_lastVisibleIndex;
233 }
234
235 QRectF KItemListViewLayouter::itemRect(int index) const
236 {
237 const_cast<KItemListViewLayouter*>(this)->doLayout();
238 if (index < 0 || index >= m_itemInfos.count()) {
239 return QRectF();
240 }
241
242 QSizeF sizeHint;
243 if (m_sizeHintResolver) {
244 sizeHint = m_sizeHintResolver->sizeHint(index);
245 } else {
246 sizeHint = m_itemSize;
247 }
248
249 if (m_scrollOrientation == Qt::Horizontal) {
250 // Rotate the logical direction which is always vertical by 90°
251 // to get the physical horizontal direction
252 const QPointF logicalPos = m_itemInfos[index].pos;
253 QPointF pos(logicalPos.y(), logicalPos.x());
254 pos.rx() -= m_scrollOffset;
255 return QRectF(pos, sizeHint);
256 }
257
258 if (sizeHint.width() <= 0) {
259 // In Details View, a size hint with negative width is used internally.
260 sizeHint.rwidth() = m_itemSize.width();
261 }
262
263 QPointF pos = m_itemInfos[index].pos;
264 pos -= QPointF(m_itemOffset, m_scrollOffset);
265 return QRectF(pos, sizeHint);
266 }
267
268 QRectF KItemListViewLayouter::groupHeaderRect(int index) const
269 {
270 const_cast<KItemListViewLayouter*>(this)->doLayout();
271
272 const QRectF firstItemRect = itemRect(index);
273 QPointF pos = firstItemRect.topLeft();
274 if (pos.isNull()) {
275 return QRectF();
276 }
277
278 QSizeF size;
279 if (m_scrollOrientation == Qt::Vertical) {
280 pos.rx() = 0;
281 pos.ry() -= m_groupHeaderHeight;
282 size = QSizeF(m_size.width(), m_groupHeaderHeight);
283 } else {
284 pos.rx() -= m_itemMargin.width();
285 pos.ry() = 0;
286
287 // Determine the maximum width used in the
288 // current column. As the scroll-direction is
289 // Qt::Horizontal and m_itemRects is accessed directly,
290 // the logical height represents the visual width.
291 qreal headerWidth = minimumGroupHeaderWidth();
292 const qreal y = m_itemInfos[index].pos.y();
293 const int maxIndex = m_itemInfos.count() - 1;
294 while (index <= maxIndex) {
295 const QPointF pos = m_itemInfos[index].pos;
296 if (pos.y() != y) {
297 break;
298 }
299
300 qreal itemWidth;
301 if (m_sizeHintResolver) {
302 itemWidth = m_sizeHintResolver->sizeHint(index).width();
303 } else {
304 itemWidth = m_itemSize.width();
305 }
306
307 if (itemWidth > headerWidth) {
308 headerWidth = itemWidth;
309 }
310
311 ++index;
312 }
313
314 size = QSizeF(headerWidth, m_size.height());
315 }
316 return QRectF(pos, size);
317 }
318
319 int KItemListViewLayouter::itemColumn(int index) const
320 {
321 const_cast<KItemListViewLayouter*>(this)->doLayout();
322 if (index < 0 || index >= m_itemInfos.count()) {
323 return -1;
324 }
325
326 return (m_scrollOrientation == Qt::Vertical)
327 ? m_itemInfos[index].column
328 : m_itemInfos[index].row;
329 }
330
331 int KItemListViewLayouter::itemRow(int index) const
332 {
333 const_cast<KItemListViewLayouter*>(this)->doLayout();
334 if (index < 0 || index >= m_itemInfos.count()) {
335 return -1;
336 }
337
338 return (m_scrollOrientation == Qt::Vertical)
339 ? m_itemInfos[index].row
340 : m_itemInfos[index].column;
341 }
342
343 int KItemListViewLayouter::maximumVisibleItems() const
344 {
345 const_cast<KItemListViewLayouter*>(this)->doLayout();
346
347 const int height = static_cast<int>(m_size.height());
348 const int rowHeight = static_cast<int>(m_itemSize.height());
349 int rows = height / rowHeight;
350 if (height % rowHeight != 0) {
351 ++rows;
352 }
353
354 return rows * m_columnCount;
355 }
356
357 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
358 {
359 const_cast<KItemListViewLayouter*>(this)->doLayout();
360 return m_groupItemIndexes.contains(itemIndex);
361 }
362
363 void KItemListViewLayouter::markAsDirty()
364 {
365 m_dirty = true;
366 }
367
368
369 #ifndef QT_NO_DEBUG
370 bool KItemListViewLayouter::isDirty()
371 {
372 return m_dirty;
373 }
374 #endif
375
376 void KItemListViewLayouter::doLayout()
377 {
378 if (m_dirty) {
379 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
380 QElapsedTimer timer;
381 timer.start();
382 #endif
383 m_visibleIndexesDirty = true;
384
385 QSizeF itemSize = m_itemSize;
386 QSizeF itemMargin = m_itemMargin;
387 QSizeF size = m_size;
388
389 const bool grouped = createGroupHeaders();
390
391 const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
392 if (horizontalScrolling) {
393 // Flip everything so that the layout logically can work like having
394 // a vertical scrolling
395 itemSize.transpose();
396 itemMargin.transpose();
397 size.transpose();
398
399 if (grouped) {
400 // In the horizontal scrolling case all groups are aligned
401 // at the top, which decreases the available height. For the
402 // flipped data this means that the width must be decreased.
403 size.rwidth() -= m_groupHeaderHeight;
404 }
405 }
406
407 m_columnWidth = itemSize.width() + itemMargin.width();
408 const qreal widthForColumns = size.width() - itemMargin.width();
409 m_columnCount = qMax(1, int(widthForColumns / m_columnWidth));
410 m_xPosInc = itemMargin.width();
411
412 const int itemCount = m_model->count();
413 if (itemCount > m_columnCount && m_columnWidth >= 32) {
414 // Apply the unused width equally to each column
415 const qreal unusedWidth = widthForColumns - m_columnCount * m_columnWidth;
416 if (unusedWidth > 0) {
417 const qreal columnInc = unusedWidth / (m_columnCount + 1);
418 m_columnWidth += columnInc;
419 m_xPosInc += columnInc;
420 }
421 }
422
423 m_itemInfos.resize(itemCount);
424
425 qreal y = m_headerHeight + itemMargin.height();
426 int row = 0;
427
428 int index = 0;
429 while (index < itemCount) {
430 qreal x = m_xPosInc;
431 qreal maxItemHeight = itemSize.height();
432
433 if (grouped) {
434 if (horizontalScrolling) {
435 // All group headers will always be aligned on the top and not
436 // flipped like the other properties
437 x += m_groupHeaderHeight;
438 }
439
440 if (m_groupItemIndexes.contains(index)) {
441 // The item is the first item of a group.
442 // Increase the y-position to provide space
443 // for the group header.
444 if (index > 0) {
445 // Only add a margin if there has been added another
446 // group already before
447 y += m_groupHeaderMargin;
448 } else if (!horizontalScrolling) {
449 // The first group header should be aligned on top
450 y -= itemMargin.height();
451 }
452
453 if (!horizontalScrolling) {
454 y += m_groupHeaderHeight;
455 }
456 }
457 }
458
459 int column = 0;
460 while (index < itemCount && column < m_columnCount) {
461 qreal requiredItemHeight = itemSize.height();
462 if (m_sizeHintResolver) {
463 const QSizeF sizeHint = m_sizeHintResolver->sizeHint(index);
464 const qreal sizeHintHeight = horizontalScrolling ? sizeHint.width() : sizeHint.height();
465 if (sizeHintHeight > requiredItemHeight) {
466 requiredItemHeight = sizeHintHeight;
467 }
468 }
469
470 ItemInfo& itemInfo = m_itemInfos[index];
471 itemInfo.pos = QPointF(x, y);
472 itemInfo.column = column;
473 itemInfo.row = row;
474
475 if (grouped && horizontalScrolling) {
476 // When grouping is enabled in the horizontal mode, the header alignment
477 // looks like this:
478 // Header-1 Header-2 Header-3
479 // Item 1 Item 4 Item 7
480 // Item 2 Item 5 Item 8
481 // Item 3 Item 6 Item 9
482 // In this case 'requiredItemHeight' represents the column-width. We don't
483 // check the content of the header in the layouter to determine the required
484 // width, hence assure that at least a minimal width of 15 characters is given
485 // (in average a character requires the halve width of the font height).
486 //
487 // TODO: Let the group headers provide a minimum width and respect this width here
488 const qreal headerWidth = minimumGroupHeaderWidth();
489 if (requiredItemHeight < headerWidth) {
490 requiredItemHeight = headerWidth;
491 }
492 }
493
494 maxItemHeight = qMax(maxItemHeight, requiredItemHeight);
495 x += m_columnWidth;
496 ++index;
497 ++column;
498
499 if (grouped && m_groupItemIndexes.contains(index)) {
500 // The item represents the first index of a group
501 // and must aligned in the first column
502 break;
503 }
504 }
505
506 y += maxItemHeight + itemMargin.height();
507 ++row;
508 }
509
510 if (itemCount > 0) {
511 m_maximumScrollOffset = y;
512 m_maximumItemOffset = m_columnCount * m_columnWidth;
513 } else {
514 m_maximumScrollOffset = 0;
515 m_maximumItemOffset = 0;
516 }
517
518 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
519 kDebug() << "[TIME] doLayout() for " << m_model->count() << "items:" << timer.elapsed();
520 #endif
521 m_dirty = false;
522 }
523
524 updateVisibleIndexes();
525 }
526
527 void KItemListViewLayouter::updateVisibleIndexes()
528 {
529 if (!m_visibleIndexesDirty) {
530 return;
531 }
532
533 Q_ASSERT(!m_dirty);
534
535 if (m_model->count() <= 0) {
536 m_firstVisibleIndex = -1;
537 m_lastVisibleIndex = -1;
538 m_visibleIndexesDirty = false;
539 return;
540 }
541
542 const int maxIndex = m_model->count() - 1;
543
544 // Calculate the first visible index that is fully visible
545 int min = 0;
546 int max = maxIndex;
547 int mid = 0;
548 do {
549 mid = (min + max) / 2;
550 if (m_itemInfos[mid].pos.y() < m_scrollOffset) {
551 min = mid + 1;
552 } else {
553 max = mid - 1;
554 }
555 } while (min <= max);
556
557 if (mid > 0) {
558 // Include the row before the first fully visible index, as it might
559 // be partly visible
560 if (m_itemInfos[mid].pos.y() >= m_scrollOffset) {
561 --mid;
562 Q_ASSERT(m_itemInfos[mid].pos.y() < m_scrollOffset);
563 }
564
565 const qreal rowTop = m_itemInfos[mid].pos.y();
566 while (mid > 0 && m_itemInfos[mid - 1].pos.y() == rowTop) {
567 --mid;
568 }
569 }
570 m_firstVisibleIndex = mid;
571
572 // Calculate the last visible index that is (at least partly) visible
573 const int visibleHeight = (m_scrollOrientation == Qt::Horizontal) ? m_size.width() : m_size.height();
574 qreal bottom = m_scrollOffset + visibleHeight;
575 if (m_model->groupedSorting()) {
576 bottom += m_groupHeaderHeight;
577 }
578
579 min = m_firstVisibleIndex;
580 max = maxIndex;
581 do {
582 mid = (min + max) / 2;
583 if (m_itemInfos[mid].pos.y() <= bottom) {
584 min = mid + 1;
585 } else {
586 max = mid - 1;
587 }
588 } while (min <= max);
589
590 while (mid > 0 && m_itemInfos[mid].pos.y() > bottom) {
591 --mid;
592 }
593 m_lastVisibleIndex = mid;
594
595 m_visibleIndexesDirty = false;
596 }
597
598 bool KItemListViewLayouter::createGroupHeaders()
599 {
600 if (!m_model->groupedSorting()) {
601 return false;
602 }
603
604 m_groupItemIndexes.clear();
605
606 const QList<QPair<int, QVariant> > groups = m_model->groups();
607 if (groups.isEmpty()) {
608 return false;
609 }
610
611 for (int i = 0; i < groups.count(); ++i) {
612 const int firstItemIndex = groups.at(i).first;
613 m_groupItemIndexes.insert(firstItemIndex);
614 }
615
616 return true;
617 }
618
619 qreal KItemListViewLayouter::minimumGroupHeaderWidth() const
620 {
621 return 100;
622 }
623
624 #include "kitemlistviewlayouter.moc"