1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kitemlistviewlayouter_p.h"
22 #include "kitemmodelbase.h"
23 #include "kitemlistsizehintresolver_p.h"
27 // #define KITEMLISTVIEWLAYOUTER_DEBUG
29 KItemListViewLayouter::KItemListViewLayouter(QObject
* parent
) :
32 m_visibleIndexesDirty(true),
33 m_scrollOrientation(Qt::Vertical
),
39 m_sizeHintResolver(0),
41 m_maximumScrollOffset(0),
43 m_maximumItemOffset(0),
44 m_firstVisibleIndex(-1),
45 m_lastVisibleIndex(-1),
50 m_groupHeaderHeight(0),
51 m_groupHeaderMargin(0),
56 KItemListViewLayouter::~KItemListViewLayouter()
60 void KItemListViewLayouter::setScrollOrientation(Qt::Orientation orientation
)
62 if (m_scrollOrientation
!= orientation
) {
63 m_scrollOrientation
= orientation
;
68 Qt::Orientation
KItemListViewLayouter::scrollOrientation() const
70 return m_scrollOrientation
;
73 void KItemListViewLayouter::setSize(const QSizeF
& size
)
81 QSizeF
KItemListViewLayouter::size() const
86 void KItemListViewLayouter::setItemSize(const QSizeF
& size
)
88 if (m_itemSize
!= size
) {
94 QSizeF
KItemListViewLayouter::itemSize() const
99 void KItemListViewLayouter::setItemMargin(const QSizeF
& margin
)
101 if (m_itemMargin
!= margin
) {
102 m_itemMargin
= margin
;
107 QSizeF
KItemListViewLayouter::itemMargin() const
112 void KItemListViewLayouter::setHeaderHeight(qreal height
)
114 if (m_headerHeight
!= height
) {
115 m_headerHeight
= height
;
120 qreal
KItemListViewLayouter::headerHeight() const
122 return m_headerHeight
;
125 void KItemListViewLayouter::setGroupHeaderHeight(qreal height
)
127 if (m_groupHeaderHeight
!= height
) {
128 m_groupHeaderHeight
= height
;
133 qreal
KItemListViewLayouter::groupHeaderHeight() const
135 return m_groupHeaderHeight
;
138 void KItemListViewLayouter::setGroupHeaderMargin(qreal margin
)
140 if (m_groupHeaderMargin
!= margin
) {
141 m_groupHeaderMargin
= margin
;
146 qreal
KItemListViewLayouter::groupHeaderMargin() const
148 return m_groupHeaderMargin
;
151 void KItemListViewLayouter::setScrollOffset(qreal offset
)
153 if (m_scrollOffset
!= offset
) {
154 m_scrollOffset
= offset
;
155 m_visibleIndexesDirty
= true;
159 qreal
KItemListViewLayouter::scrollOffset() const
161 return m_scrollOffset
;
164 qreal
KItemListViewLayouter::maximumScrollOffset() const
166 const_cast<KItemListViewLayouter
*>(this)->doLayout();
167 return m_maximumScrollOffset
;
170 void KItemListViewLayouter::setItemOffset(qreal offset
)
172 if (m_itemOffset
!= offset
) {
173 m_itemOffset
= offset
;
174 m_visibleIndexesDirty
= true;
178 qreal
KItemListViewLayouter::itemOffset() const
183 qreal
KItemListViewLayouter::maximumItemOffset() const
185 const_cast<KItemListViewLayouter
*>(this)->doLayout();
186 return m_maximumItemOffset
;
189 void KItemListViewLayouter::setModel(const KItemModelBase
* model
)
191 if (m_model
!= model
) {
197 const KItemModelBase
* KItemListViewLayouter::model() const
202 void KItemListViewLayouter::setSizeHintResolver(const KItemListSizeHintResolver
* sizeHintResolver
)
204 if (m_sizeHintResolver
!= sizeHintResolver
) {
205 m_sizeHintResolver
= sizeHintResolver
;
210 const KItemListSizeHintResolver
* KItemListViewLayouter::sizeHintResolver() const
212 return m_sizeHintResolver
;
215 int KItemListViewLayouter::firstVisibleIndex() const
217 const_cast<KItemListViewLayouter
*>(this)->doLayout();
218 return m_firstVisibleIndex
;
221 int KItemListViewLayouter::lastVisibleIndex() const
223 const_cast<KItemListViewLayouter
*>(this)->doLayout();
224 return m_lastVisibleIndex
;
227 QRectF
KItemListViewLayouter::itemRect(int index
) const
229 const_cast<KItemListViewLayouter
*>(this)->doLayout();
230 if (index
< 0 || index
>= m_itemRects
.count()) {
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
;
245 QRectF bounds
= m_itemRects
[index
];
246 bounds
.moveTo(bounds
.topLeft() - QPointF(m_itemOffset
, m_scrollOffset
));
250 QRectF
KItemListViewLayouter::groupHeaderRect(int index
) const
252 const_cast<KItemListViewLayouter
*>(this)->doLayout();
254 const QRectF firstItemRect
= itemRect(index
);
255 QPointF pos
= firstItemRect
.topLeft();
260 pos
.ry() -= m_groupHeaderHeight
;
263 if (m_scrollOrientation
== Qt::Vertical
) {
265 size
= QSizeF(m_size
.width(), m_groupHeaderHeight
);
267 size
= QSizeF(minimumGroupHeaderWidth(), m_groupHeaderHeight
);
269 return QRectF(pos
, size
);
272 int KItemListViewLayouter::maximumVisibleItems() const
274 const_cast<KItemListViewLayouter
*>(this)->doLayout();
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) {
283 return rows
* m_columnCount
;
286 bool KItemListViewLayouter::isFirstGroupItem(int itemIndex
) const
288 const_cast<KItemListViewLayouter
*>(this)->doLayout();
289 return m_groupItemIndexes
.contains(itemIndex
);
292 void KItemListViewLayouter::markAsDirty()
297 void KItemListViewLayouter::doLayout()
300 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
304 m_visibleIndexesDirty
= true;
306 QSizeF itemSize
= m_itemSize
;
307 QSizeF itemMargin
= m_itemMargin
;
308 QSizeF size
= m_size
;
310 const bool grouped
= createGroupHeaders();
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());
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
;
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();
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
;
347 int rowCount
= itemCount
/ m_columnCount
;
348 if (itemCount
% m_columnCount
!= 0) {
352 m_itemRects
.reserve(itemCount
);
354 qreal y
= m_headerHeight
+ itemMargin
.height();
358 while (index
< itemCount
) {
360 qreal maxItemHeight
= itemSize
.height();
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
;
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.
374 // The first group header should be aligned on top
375 y
-= itemMargin
.height();
377 // Only add a margin if there has been added another
378 // group already before
379 y
+= m_groupHeaderMargin
;
381 y
+= m_groupHeaderHeight
;
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
;
396 const QRectF
bounds(x
, y
, itemSize
.width(), requiredItemHeight
);
397 if (index
< m_itemRects
.count()) {
398 m_itemRects
[index
] = bounds
;
400 m_itemRects
.append(bounds
);
403 if (grouped
&& horizontalScrolling
) {
404 // When grouping is enabled in the horizontal mode, the header alignment
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).
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
;
422 maxItemHeight
= qMax(maxItemHeight
, requiredItemHeight
);
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
434 y
+= maxItemHeight
+ itemMargin
.height();
437 if (m_itemRects
.count() > itemCount
) {
438 m_itemRects
.erase(m_itemRects
.begin() + itemCount
,
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();
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());
453 m_maximumScrollOffset
+= itemMargin
.height();
455 m_maximumItemOffset
= m_columnCount
* m_columnWidth
;
457 m_maximumScrollOffset
= 0;
458 m_maximumItemOffset
= 0;
461 #ifdef KITEMLISTVIEWLAYOUTER_DEBUG
462 kDebug() << "[TIME] doLayout() for " << m_model
->count() << "items:" << timer
.elapsed();
467 updateVisibleIndexes();
470 void KItemListViewLayouter::updateVisibleIndexes()
472 if (!m_visibleIndexesDirty
) {
478 if (m_model
->count() <= 0) {
479 m_firstVisibleIndex
= -1;
480 m_lastVisibleIndex
= -1;
481 m_visibleIndexesDirty
= false;
485 const int maxIndex
= m_model
->count() - 1;
487 // Calculate the first visible index that is fully visible
492 mid
= (min
+ max
) / 2;
493 if (m_itemRects
[mid
].top() < m_scrollOffset
) {
498 } while (min
<= max
);
501 // Include the row before the first fully visible index, as it might
503 if (m_itemRects
[mid
].top() >= m_scrollOffset
) {
505 Q_ASSERT(m_itemRects
[mid
].top() < m_scrollOffset
);
508 const qreal rowTop
= m_itemRects
[mid
].top();
509 while (mid
> 0 && m_itemRects
[mid
- 1].top() == rowTop
) {
513 m_firstVisibleIndex
= mid
;
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
;
522 min
= m_firstVisibleIndex
;
525 mid
= (min
+ max
) / 2;
526 if (m_itemRects
[mid
].y() <= bottom
) {
531 } while (min
<= max
);
533 while (mid
> 0 && m_itemRects
[mid
].y() > bottom
) {
536 m_lastVisibleIndex
= mid
;
538 m_visibleIndexesDirty
= false;
541 bool KItemListViewLayouter::createGroupHeaders()
543 if (!m_model
->groupedSorting()) {
547 m_groupItemIndexes
.clear();
549 const QList
<QPair
<int, QVariant
> > groups
= m_model
->groups();
550 if (groups
.isEmpty()) {
554 for (int i
= 0; i
< groups
.count(); ++i
) {
555 const int firstItemIndex
= groups
.at(i
).first
;
556 m_groupItemIndexes
.insert(firstItemIndex
);
562 qreal
KItemListViewLayouter::minimumGroupHeaderWidth() const
564 return m_groupHeaderHeight
* 15 / 2;
567 #include "kitemlistviewlayouter_p.moc"