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