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