]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistviewlayouter.cpp
Make group-headers less ugly
[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_headerHeight(0),
37 m_model(0),
38 m_sizeHintResolver(0),
39 m_scrollOffset(0),
40 m_maximumScrollOffset(0),
41 m_itemOffset(0),
42 m_maximumItemOffset(0),
43 m_firstVisibleIndex(-1),
44 m_lastVisibleIndex(-1),
45 m_columnWidth(0),
46 m_xPosInc(0),
47 m_columnCount(0),
48 m_groupItemIndexes(),
49 m_groupHeaderHeight(0),
50 m_itemRects()
51 {
52 }
53
54 KItemListViewLayouter::~KItemListViewLayouter()
55 {
56 }
57
58 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation)
59 {
60 if (m_scrollOrientation != orientation) {
61 m_scrollOrientation = orientation;
62 m_dirty = true;
63 }
64 }
65
66 Qt::Orientation KItemListViewLayouter::scrollOrientation() const
67 {
68 return m_scrollOrientation;
69 }
70
71 void KItemListViewLayouter::setSize(const QSizeF& size)
72 {
73 if (m_size != size) {
74 m_size = size;
75 m_dirty = true;
76 }
77 }
78
79 QSizeF KItemListViewLayouter::size() const
80 {
81 return m_size;
82 }
83
84 void KItemListViewLayouter::setItemSize(const QSizeF& size)
85 {
86 if (m_itemSize != size) {
87 m_itemSize = size;
88 m_dirty = true;
89 }
90 }
91
92 QSizeF KItemListViewLayouter::itemSize() const
93 {
94 return m_itemSize;
95 }
96
97 void KItemListViewLayouter::setHeaderHeight(qreal height)
98 {
99 if (m_headerHeight != height) {
100 m_headerHeight = height;
101 m_dirty = true;
102 }
103 }
104
105 qreal KItemListViewLayouter::headerHeight() const
106 {
107 return m_headerHeight;
108 }
109
110 void KItemListViewLayouter::setGroupHeaderHeight(qreal height)
111 {
112 if (m_groupHeaderHeight != height) {
113 m_groupHeaderHeight = height;
114 m_dirty = true;
115 }
116 }
117
118 qreal KItemListViewLayouter::groupHeaderHeight() const
119 {
120 return m_groupHeaderHeight;
121 }
122
123 void KItemListViewLayouter::setScrollOffset(qreal offset)
124 {
125 if (m_scrollOffset != offset) {
126 m_scrollOffset = offset;
127 m_visibleIndexesDirty = true;
128 }
129 }
130
131 qreal KItemListViewLayouter::scrollOffset() const
132 {
133 return m_scrollOffset;
134 }
135
136 qreal KItemListViewLayouter::maximumScrollOffset() const
137 {
138 const_cast<KItemListViewLayouter*>(this)->doLayout();
139 return m_maximumScrollOffset;
140 }
141
142 void KItemListViewLayouter::setItemOffset(qreal offset)
143 {
144 if (m_itemOffset != offset) {
145 m_itemOffset = offset;
146 m_visibleIndexesDirty = true;
147 }
148 }
149
150 qreal KItemListViewLayouter::itemOffset() const
151 {
152 return m_itemOffset;
153 }
154
155 qreal KItemListViewLayouter::maximumItemOffset() const
156 {
157 const_cast<KItemListViewLayouter*>(this)->doLayout();
158 return m_maximumItemOffset;
159 }
160
161 void KItemListViewLayouter::setModel(const KItemModelBase* model)
162 {
163 if (m_model != model) {
164 m_model = model;
165 m_dirty = true;
166 }
167 }
168
169 const KItemModelBase* KItemListViewLayouter::model() const
170 {
171 return m_model;
172 }
173
174 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver* sizeHintResolver)
175 {
176 if (m_sizeHintResolver != sizeHintResolver) {
177 m_sizeHintResolver = sizeHintResolver;
178 m_dirty = true;
179 }
180 }
181
182 const KItemListSizeHintResolver* KItemListViewLayouter::sizeHintResolver() const
183 {
184 return m_sizeHintResolver;
185 }
186
187 int KItemListViewLayouter::firstVisibleIndex() const
188 {
189 const_cast<KItemListViewLayouter*>(this)->doLayout();
190 return m_firstVisibleIndex;
191 }
192
193 int KItemListViewLayouter::lastVisibleIndex() const
194 {
195 const_cast<KItemListViewLayouter*>(this)->doLayout();
196 return m_lastVisibleIndex;
197 }
198
199 QRectF KItemListViewLayouter::itemRect(int index) const
200 {
201 const_cast<KItemListViewLayouter*>(this)->doLayout();
202 if (index < 0 || index >= m_itemRects.count()) {
203 return QRectF();
204 }
205
206 if (m_scrollOrientation == Qt::Horizontal) {
207 // Rotate the logical direction which is always vertical by 90°
208 // to get the physical horizontal direction
209 const QRectF& b = m_itemRects[index];
210 QRectF bounds(b.y(), b.x(), b.height(), b.width());
211 QPointF pos = bounds.topLeft();
212 pos.rx() -= m_scrollOffset;
213 bounds.moveTo(pos);
214 return bounds;
215 }
216
217 QRectF bounds = m_itemRects[index];
218 bounds.moveTo(bounds.topLeft() - QPointF(m_itemOffset, m_scrollOffset));
219 return bounds;
220 }
221
222 QRectF KItemListViewLayouter::groupHeaderRect(int index) const
223 {
224 const QRectF firstItemRect = itemRect(index);
225 QPointF pos = firstItemRect.topLeft();
226 if (pos.isNull()) {
227 return QRectF();
228 }
229
230 pos.ry() -= m_groupHeaderHeight;
231
232 QSizeF size;
233 if (m_scrollOrientation == Qt::Vertical) {
234 pos.rx() = 0;
235 size = QSizeF(m_size.width(), m_groupHeaderHeight);
236 } else {
237 size = QSizeF(firstItemRect.width(), m_groupHeaderHeight);
238 }
239 return QRectF(pos, size);
240 }
241
242 int KItemListViewLayouter::maximumVisibleItems() const
243 {
244 const_cast<KItemListViewLayouter*>(this)->doLayout();
245
246 const int height = static_cast<int>(m_size.height());
247 const int rowHeight = static_cast<int>(m_itemSize.height());
248 int rows = height / rowHeight;
249 if (height % rowHeight != 0) {
250 ++rows;
251 }
252
253 return rows * m_columnCount;
254 }
255
256 int KItemListViewLayouter::itemsPerOffset() const
257 {
258 return m_columnCount;
259 }
260
261 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex) const
262 {
263 return m_groupItemIndexes.contains(itemIndex);
264 }
265
266 void KItemListViewLayouter::markAsDirty()
267 {
268 m_dirty = true;
269 }
270
271 void KItemListViewLayouter::doLayout()
272 {
273 if (m_dirty) {
274 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
275 QElapsedTimer timer;
276 timer.start();
277 #endif
278 m_visibleIndexesDirty = true;
279
280 QSizeF itemSize = m_itemSize;
281 QSizeF size = m_size;
282
283 const bool horizontalScrolling = (m_scrollOrientation == Qt::Horizontal);
284 if (horizontalScrolling) {
285 itemSize.setWidth(m_itemSize.height());
286 itemSize.setHeight(m_itemSize.width());
287 size.setWidth(m_size.height());
288 size.setHeight(m_size.width());
289 }
290
291 m_columnWidth = itemSize.width();
292 m_columnCount = qMax(1, int(size.width() / m_columnWidth));
293 m_xPosInc = 0;
294
295 const int itemCount = m_model->count();
296 if (itemCount > m_columnCount) {
297 // Apply the unused width equally to each column
298 const qreal unusedWidth = size.width() - m_columnCount * m_columnWidth;
299 if (unusedWidth > 0) {
300 const qreal columnInc = unusedWidth / (m_columnCount + 1);
301 m_columnWidth += columnInc;
302 m_xPosInc += columnInc;
303 }
304 }
305
306 int rowCount = itemCount / m_columnCount;
307 if (itemCount % m_columnCount != 0) {
308 ++rowCount;
309 }
310
311 m_itemRects.reserve(itemCount);
312
313 qreal y = m_headerHeight;
314 int rowIndex = 0;
315
316 const bool grouped = createGroupHeaders();
317
318 int index = 0;
319 while (index < itemCount) {
320 qreal x = m_xPosInc;
321 qreal maxItemHeight = itemSize.height();
322
323 if (grouped) {
324 if (horizontalScrolling) {
325 // All group headers will always be aligned on the top and not
326 // flipped like the other properties
327 x += m_groupHeaderHeight;
328 }
329
330 if (m_groupItemIndexes.contains(index)) {
331 if (!horizontalScrolling) {
332 // The item is the first item of a group.
333 // Increase the y-position to provide space
334 // for the group header.
335 y += m_groupHeaderHeight;
336 }
337 }
338 }
339
340 int column = 0;
341 while (index < itemCount && column < m_columnCount) {
342 qreal requiredItemHeight = itemSize.height();
343 if (m_sizeHintResolver) {
344 const QSizeF sizeHint = m_sizeHintResolver->sizeHint(index);
345 const qreal sizeHintHeight = horizontalScrolling ? sizeHint.width() : sizeHint.height();
346 if (sizeHintHeight > requiredItemHeight) {
347 requiredItemHeight = sizeHintHeight;
348 }
349 }
350
351 const QRectF bounds(x, y, itemSize.width(), requiredItemHeight);
352 if (index < m_itemRects.count()) {
353 m_itemRects[index] = bounds;
354 } else {
355 m_itemRects.append(bounds);
356 }
357
358 if (grouped && horizontalScrolling) {
359 // When grouping is enabled in the horizontal mode, the header alignment
360 // looks like this:
361 // Header-1 Header-2 Header-3
362 // Item 1 Item 4 Item 7
363 // Item 2 Item 5 Item 8
364 // Item 3 Item 6 Item 9
365 // In this case 'requiredItemHeight' represents the column-width. We don't
366 // check the content of the header in the layouter to determine the required
367 // width, hence assure that at least a minimal width of 15 characters is given
368 // (in average a character requires the halve width of the font height).
369 //
370 // TODO: Let the group headers provide a minimum width and respect this width here
371 const qreal minimumGroupHeaderWidth = m_groupHeaderHeight * 15 / 2;
372 if (requiredItemHeight < minimumGroupHeaderWidth) {
373 requiredItemHeight = minimumGroupHeaderWidth;
374 }
375 }
376
377 maxItemHeight = qMax(maxItemHeight, requiredItemHeight);
378 x += m_columnWidth;
379 ++index;
380 ++column;
381
382 if (grouped && m_groupItemIndexes.contains(index)) {
383 // The item represents the first index of a group
384 // and must aligned in the first column
385 break;
386 }
387 }
388
389 y += maxItemHeight;
390 ++rowIndex;
391 }
392 if (m_itemRects.count() > itemCount) {
393 m_itemRects.erase(m_itemRects.begin() + itemCount,
394 m_itemRects.end());
395 }
396
397 if (itemCount > 0) {
398 m_maximumScrollOffset = m_itemRects.last().bottom();
399 m_maximumItemOffset = m_columnCount * m_columnWidth;
400 } else {
401 m_maximumScrollOffset = 0;
402 m_maximumItemOffset = 0;
403 }
404
405 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
406 kDebug() << "[TIME] doLayout() for " << m_model->count() << "items:" << timer.elapsed();
407 #endif
408 m_dirty = false;
409 }
410
411 updateVisibleIndexes();
412 }
413
414 void KItemListViewLayouter::updateVisibleIndexes()
415 {
416 if (!m_visibleIndexesDirty) {
417 return;
418 }
419
420 Q_ASSERT(!m_dirty);
421
422 if (m_model->count() <= 0) {
423 m_firstVisibleIndex = -1;
424 m_lastVisibleIndex = -1;
425 m_visibleIndexesDirty = false;
426 return;
427 }
428
429 const int maxIndex = m_model->count() - 1;
430
431 // Calculate the first visible index that is (at least partly) visible
432 int min = 0;
433 int max = maxIndex;
434 int mid = 0;
435 do {
436 mid = (min + max) / 2;
437 if (m_itemRects[mid].bottom() < m_scrollOffset) {
438 min = mid + 1;
439 } else {
440 max = mid - 1;
441 }
442 } while (min <= max);
443
444 while (mid < maxIndex && m_itemRects[mid].bottom() < m_scrollOffset) {
445 ++mid;
446 }
447 m_firstVisibleIndex = mid;
448
449 // Calculate the last visible index that is (at least partly) visible
450 const int visibleHeight = (m_scrollOrientation == Qt::Horizontal) ? m_size.width() : m_size.height();
451 qreal bottom = m_scrollOffset + visibleHeight;
452 if (m_model->groupedSorting()) {
453 bottom += m_groupHeaderHeight;
454 }
455
456 min = m_firstVisibleIndex;
457 max = maxIndex;
458 do {
459 mid = (min + max) / 2;
460 if (m_itemRects[mid].y() <= bottom) {
461 min = mid + 1;
462 } else {
463 max = mid - 1;
464 }
465 } while (min <= max);
466
467 while (mid > 0 && m_itemRects[mid].y() > bottom) {
468 --mid;
469 }
470 m_lastVisibleIndex = mid;
471
472 m_visibleIndexesDirty = false;
473 }
474
475 bool KItemListViewLayouter::createGroupHeaders()
476 {
477 if (!m_model->groupedSorting()) {
478 return false;
479 }
480
481 m_groupItemIndexes.clear();
482
483 const QList<QPair<int, QVariant> > groups = m_model->groups();
484 if (groups.isEmpty()) {
485 return false;
486 }
487
488 for (int i = 0; i < groups.count(); ++i) {
489 const int firstItemIndex = groups.at(i).first;
490 m_groupItemIndexes.insert(firstItemIndex);
491 }
492
493 return true;
494 }
495
496 #include "kitemlistviewlayouter_p.moc"