]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewlayouter.cpp
Group header layout fixes
[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 pos.ry() -= m_groupHeaderHeight;
261
262 QSizeF size;
263 if (m_scrollOrientation == Qt::Vertical) {
264 pos.rx() = 0;
265 size = QSizeF(m_size.width(), m_groupHeaderHeight);
266 } else {
267 size = QSizeF(minimumGroupHeaderWidth(), m_groupHeaderHeight);
268 }
269 return QRectF(pos, size);
270 }
271
272 int KItemListViewLayouter::maximumVisibleItems() const
273 {
274 const_cast<KItemListViewLayouter*>(this)->doLayout();
275
276 const int height = static_cast<int>(m_size.height());
277 const int rowHeight = static_cast<int>(m_itemSize.height());
278 int rows = height / rowHeight;
279 if (height % rowHeight != 0) {
280 ++rows;
281 }
282
283 return rows * m_columnCount;
284 }
285
286 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
287 {
288 const_cast<KItemListViewLayouter*>(this)->doLayout();
289 return m_groupItemIndexes.contains(itemIndex);
290 }
291
292 void KItemListViewLayouter::markAsDirty()
293 {
294 m_dirty = true;
295 }
296
297 void KItemListViewLayouter::doLayout()
298 {
299 if (m_dirty) {
300 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
301 QElapsedTimer timer;
302 timer.start();
303 #endif
304 m_visibleIndexesDirty = true;
305
306 QSizeF itemSize = m_itemSize;
307 QSizeF itemMargin = m_itemMargin;
308 QSizeF size = m_size;
309
310 const bool grouped = createGroupHeaders();
311
312 const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
313 if (horizontalScrolling) {
314 // Flip everything so that the layout logically can work like having
315 // a vertical scrolling
316 itemSize.setWidth(m_itemSize.height());
317 itemSize.setHeight(m_itemSize.width());
318 itemMargin.setWidth(m_itemMargin.height());
319 itemMargin.setHeight(m_itemMargin.width());
320 size.setWidth(m_size.height());
321 size.setHeight(m_size.width());
322
323 if (grouped) {
324 // In the horizontal scrolling case all groups are aligned
325 // at the top, which decreases the available height. For the
326 // flipped data this means that the width must be decreased.
327 size.rwidth() -= m_groupHeaderMargin + m_groupHeaderHeight;
328 }
329 }
330
331 m_columnWidth = itemSize.width() + itemMargin.width();
332 const qreal widthForColumns = size.width() - itemMargin.width();
333 m_columnCount = qMax(1, int(widthForColumns / m_columnWidth));
334 m_xPosInc = itemMargin.width();
335
336 const int itemCount = m_model->count();
337 if (itemCount > m_columnCount && m_columnWidth >= 32) {
338 // Apply the unused width equally to each column
339 const qreal unusedWidth = widthForColumns - m_columnCount * m_columnWidth;
340 if (unusedWidth > 0) {
341 const qreal columnInc = unusedWidth / (m_columnCount + 1);
342 m_columnWidth += columnInc;
343 m_xPosInc += columnInc;
344 }
345 }
346
347 int rowCount = itemCount / m_columnCount;
348 if (itemCount % m_columnCount != 0) {
349 ++rowCount;
350 }
351
352 m_itemRects.reserve(itemCount);
353
354 qreal y = m_headerHeight + itemMargin.height();
355 int rowIndex = 0;
356
357 int index = 0;
358 while (index < itemCount) {
359 qreal x = m_xPosInc;
360 qreal maxItemHeight = itemSize.height();
361
362 if (grouped) {
363 if (horizontalScrolling) {
364 // All group headers will always be aligned on the top and not
365 // flipped like the other properties
366 x += m_groupHeaderMargin + m_groupHeaderHeight;
367 }
368
369 if (m_groupItemIndexes.contains(index) && !horizontalScrolling) {
370 // The item is the first item of a group.
371 // Increase the y-position to provide space
372 // for the group header.
373 if (index == 0) {
374 // The first group header should be aligned on top
375 y -= itemMargin.height();
376 } else {
377 // Only add a margin if there has been added another
378 // group already before
379 y += m_groupHeaderMargin;
380 }
381 y += m_groupHeaderHeight;
382 }
383 }
384
385 int column = 0;
386 while (index < itemCount && column < m_columnCount) {
387 qreal requiredItemHeight = itemSize.height();
388 if (m_sizeHintResolver) {
389 const QSizeF sizeHint = m_sizeHintResolver->sizeHint(index);
390 const qreal sizeHintHeight = horizontalScrolling ? sizeHint.width() : sizeHint.height();
391 if (sizeHintHeight > requiredItemHeight) {
392 requiredItemHeight = sizeHintHeight;
393 }
394 }
395
396 const QRectF bounds(x, y, itemSize.width(), requiredItemHeight);
397 if (index < m_itemRects.count()) {
398 m_itemRects[index] = bounds;
399 } else {
400 m_itemRects.append(bounds);
401 }
402
403 if (grouped && horizontalScrolling) {
404 // When grouping is enabled in the horizontal mode, the header alignment
405 // looks like this:
406 // Header-1 Header-2 Header-3
407 // Item 1 Item 4 Item 7
408 // Item 2 Item 5 Item 8
409 // Item 3 Item 6 Item 9
410 // In this case 'requiredItemHeight' represents the column-width. We don't
411 // check the content of the header in the layouter to determine the required
412 // width, hence assure that at least a minimal width of 15 characters is given
413 // (in average a character requires the halve width of the font height).
414 //
415 // TODO: Let the group headers provide a minimum width and respect this width here
416 const qreal headerWidth = minimumGroupHeaderWidth();
417 if (requiredItemHeight < headerWidth) {
418 requiredItemHeight = headerWidth;
419 }
420 }
421
422 maxItemHeight = qMax(maxItemHeight, requiredItemHeight);
423 x += m_columnWidth;
424 ++index;
425 ++column;
426
427 if (grouped && m_groupItemIndexes.contains(index)) {
428 // The item represents the first index of a group
429 // and must aligned in the first column
430 break;
431 }
432 }
433
434 y += maxItemHeight + itemMargin.height();
435 ++rowIndex;
436 }
437 if (m_itemRects.count() > itemCount) {
438 m_itemRects.erase(m_itemRects.begin() + itemCount,
439 m_itemRects.end());
440 }
441
442 if (itemCount > 0) {
443 // Calculate the maximum y-range of the last row for m_maximumScrollOffset
444 m_maximumScrollOffset = m_itemRects.last().bottom();
445 const qreal rowY = m_itemRects.last().y();
446
447 int index = m_itemRects.count() - 2;
448 while (index >= 0 && m_itemRects.at(index).bottom() >= rowY) {
449 m_maximumScrollOffset = qMax(m_maximumScrollOffset, m_itemRects.at(index).bottom());
450 --index;
451 }
452
453 m_maximumScrollOffset += itemMargin.height();
454
455 m_maximumItemOffset = m_columnCount * m_columnWidth;
456 } else {
457 m_maximumScrollOffset = 0;
458 m_maximumItemOffset = 0;
459 }
460
461 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
462 kDebug() << "[TIME] doLayout() for " << m_model->count() << "items:" << timer.elapsed();
463 #endif
464 m_dirty = false;
465 }
466
467 updateVisibleIndexes();
468 }
469
470 void KItemListViewLayouter::updateVisibleIndexes()
471 {
472 if (!m_visibleIndexesDirty) {
473 return;
474 }
475
476 Q_ASSERT(!m_dirty);
477
478 if (m_model->count() <= 0) {
479 m_firstVisibleIndex = -1;
480 m_lastVisibleIndex = -1;
481 m_visibleIndexesDirty = false;
482 return;
483 }
484
485 const int maxIndex = m_model->count() - 1;
486
487 // Calculate the first visible index that is fully visible
488 int min = 0;
489 int max = maxIndex;
490 int mid = 0;
491 do {
492 mid = (min + max) / 2;
493 if (m_itemRects[mid].top() < m_scrollOffset) {
494 min = mid + 1;
495 } else {
496 max = mid - 1;
497 }
498 } while (min <= max);
499
500 if (mid > 0) {
501 // Include the row before the first fully visible index, as it might
502 // be partly visible
503 if (m_itemRects[mid].top() >= m_scrollOffset) {
504 --mid;
505 Q_ASSERT(m_itemRects[mid].top() < m_scrollOffset);
506 }
507
508 const qreal rowTop = m_itemRects[mid].top();
509 while (mid > 0 && m_itemRects[mid - 1].top() == rowTop) {
510 --mid;
511 }
512 }
513 m_firstVisibleIndex = mid;
514
515 // Calculate the last visible index that is (at least partly) visible
516 const int visibleHeight = (m_scrollOrientation == Qt::Horizontal) ? m_size.width() : m_size.height();
517 qreal bottom = m_scrollOffset + visibleHeight;
518 if (m_model->groupedSorting()) {
519 bottom += m_groupHeaderHeight;
520 }
521
522 min = m_firstVisibleIndex;
523 max = maxIndex;
524 do {
525 mid = (min + max) / 2;
526 if (m_itemRects[mid].y() <= bottom) {
527 min = mid + 1;
528 } else {
529 max = mid - 1;
530 }
531 } while (min <= max);
532
533 while (mid > 0 && m_itemRects[mid].y() > bottom) {
534 --mid;
535 }
536 m_lastVisibleIndex = mid;
537
538 m_visibleIndexesDirty = false;
539 }
540
541 bool KItemListViewLayouter::createGroupHeaders()
542 {
543 if (!m_model->groupedSorting()) {
544 return false;
545 }
546
547 m_groupItemIndexes.clear();
548
549 const QList<QPair<int, QVariant> > groups = m_model->groups();
550 if (groups.isEmpty()) {
551 return false;
552 }
553
554 for (int i = 0; i < groups.count(); ++i) {
555 const int firstItemIndex = groups.at(i).first;
556 m_groupItemIndexes.insert(firstItemIndex);
557 }
558
559 return true;
560 }
561
562 qreal KItemListViewLayouter::minimumGroupHeaderWidth() const
563 {
564 return m_groupHeaderHeight * 15 / 2;
565 }
566
567 #include "kitemlistviewlayouter_p.moc"