1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
23 #include "kitemlistview.h"
25 #include "kitemlistcontroller.h"
26 #include "kitemlistgroupheader.h"
27 #include "kitemlistheader_p.h"
28 #include "kitemlistrubberband_p.h"
29 #include "kitemlistselectionmanager.h"
30 #include "kitemlistsizehintresolver_p.h"
31 #include "kitemlistviewlayouter_p.h"
32 #include "kitemlistviewanimation_p.h"
33 #include "kitemlistwidget.h"
38 #include <QGraphicsSceneMouseEvent>
40 #include <QPropertyAnimation>
42 #include <QStyleOptionRubberBand>
46 // Time in ms until reaching the autoscroll margin triggers
47 // an initial autoscrolling
48 const int InitialAutoScrollDelay
= 700;
50 // Delay in ms for triggering the next autoscroll
51 const int RepeatingAutoScrollDelay
= 1000 / 60;
54 KItemListView::KItemListView(QGraphicsWidget
* parent
) :
55 QGraphicsWidget(parent
),
57 m_activeTransactions(0),
62 m_visibleRolesSizes(),
63 m_stretchedVisibleRolesSizes(),
65 m_groupHeaderCreator(0),
69 m_sizeHintResolver(0),
74 m_oldMaximumScrollOffset(0),
76 m_oldMaximumItemOffset(0),
77 m_skipAutoScrollForRubberBand(false),
80 m_autoScrollIncrement(0),
83 m_useHeaderWidths(false)
85 setAcceptHoverEvents(true);
87 m_sizeHintResolver
= new KItemListSizeHintResolver(this);
89 m_layouter
= new KItemListViewLayouter(this);
90 m_layouter
->setSizeHintResolver(m_sizeHintResolver
);
92 m_animation
= new KItemListViewAnimation(this);
93 connect(m_animation
, SIGNAL(finished(QGraphicsWidget
*,KItemListViewAnimation::AnimationType
)),
94 this, SLOT(slotAnimationFinished(QGraphicsWidget
*,KItemListViewAnimation::AnimationType
)));
96 m_layoutTimer
= new QTimer(this);
97 m_layoutTimer
->setInterval(300);
98 m_layoutTimer
->setSingleShot(true);
99 connect(m_layoutTimer
, SIGNAL(timeout()), this, SLOT(slotLayoutTimerFinished()));
101 m_rubberBand
= new KItemListRubberBand(this);
102 connect(m_rubberBand
, SIGNAL(activationChanged(bool)), this, SLOT(slotRubberBandActivationChanged(bool)));
105 KItemListView::~KItemListView()
107 delete m_sizeHintResolver
;
108 m_sizeHintResolver
= 0;
111 void KItemListView::setScrollOrientation(Qt::Orientation orientation
)
113 const Qt::Orientation previousOrientation
= m_layouter
->scrollOrientation();
114 if (orientation
== previousOrientation
) {
118 m_layouter
->setScrollOrientation(orientation
);
119 m_animation
->setScrollOrientation(orientation
);
120 m_sizeHintResolver
->clearCache();
123 onScrollOrientationChanged(orientation
, previousOrientation
);
124 emit
scrollOrientationChanged(orientation
, previousOrientation
);
127 Qt::Orientation
KItemListView::scrollOrientation() const
129 return m_layouter
->scrollOrientation();
132 void KItemListView::setItemSize(const QSizeF
& itemSize
)
134 const QSizeF previousSize
= m_itemSize
;
135 if (itemSize
== previousSize
) {
139 m_itemSize
= itemSize
;
141 const bool emptySize
= itemSize
.isEmpty();
143 updateVisibleRolesSizes();
145 if (itemSize
.width() < previousSize
.width() || itemSize
.height() < previousSize
.height()) {
146 prepareLayoutForIncreasedItemCount(itemSize
, ItemSize
);
148 m_layouter
->setItemSize(itemSize
);
152 m_sizeHintResolver
->clearCache();
154 onItemSizeChanged(itemSize
, previousSize
);
157 QSizeF
KItemListView::itemSize() const
162 void KItemListView::setScrollOffset(qreal offset
)
168 const qreal previousOffset
= m_layouter
->scrollOffset();
169 if (offset
== previousOffset
) {
173 m_layouter
->setScrollOffset(offset
);
174 m_animation
->setScrollOffset(offset
);
175 if (!m_layoutTimer
->isActive()) {
176 doLayout(NoAnimation
, 0, 0);
179 onScrollOffsetChanged(offset
, previousOffset
);
182 qreal
KItemListView::scrollOffset() const
184 return m_layouter
->scrollOffset();
187 qreal
KItemListView::maximumScrollOffset() const
189 return m_layouter
->maximumScrollOffset();
192 void KItemListView::setItemOffset(qreal offset
)
194 m_layouter
->setItemOffset(offset
);
196 m_header
->setPos(-offset
, 0);
198 if (!m_layoutTimer
->isActive()) {
199 doLayout(NoAnimation
, 0, 0);
204 qreal
KItemListView::itemOffset() const
206 return m_layouter
->itemOffset();
209 qreal
KItemListView::maximumItemOffset() const
211 return m_layouter
->maximumItemOffset();
214 void KItemListView::setVisibleRoles(const QList
<QByteArray
>& roles
)
216 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
217 m_visibleRoles
= roles
;
219 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
220 while (it
.hasNext()) {
222 KItemListWidget
* widget
= it
.value();
223 widget
->setVisibleRoles(roles
);
224 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
227 m_sizeHintResolver
->clearCache();
228 m_layouter
->markAsDirty();
231 m_header
->setVisibleRoles(roles
);
232 m_header
->setVisibleRolesWidths(headerRolesWidths());
233 m_useHeaderWidths
= false;
236 updateVisibleRolesSizes();
239 onVisibleRolesChanged(roles
, previousRoles
);
242 QList
<QByteArray
> KItemListView::visibleRoles() const
244 return m_visibleRoles
;
247 void KItemListView::setAutoScroll(bool enabled
)
249 if (enabled
&& !m_autoScrollTimer
) {
250 m_autoScrollTimer
= new QTimer(this);
251 m_autoScrollTimer
->setSingleShot(false);
252 connect(m_autoScrollTimer
, SIGNAL(timeout()), this, SLOT(triggerAutoScrolling()));
253 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
254 } else if (!enabled
&& m_autoScrollTimer
) {
255 delete m_autoScrollTimer
;
256 m_autoScrollTimer
= 0;
261 bool KItemListView::autoScroll() const
263 return m_autoScrollTimer
!= 0;
266 KItemListController
* KItemListView::controller() const
271 KItemModelBase
* KItemListView::model() const
276 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
)
278 m_widgetCreator
= widgetCreator
;
281 KItemListWidgetCreatorBase
* KItemListView::widgetCreator() const
283 return m_widgetCreator
;
286 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
)
288 m_groupHeaderCreator
= groupHeaderCreator
;
291 KItemListGroupHeaderCreatorBase
* KItemListView::groupHeaderCreator() const
293 return m_groupHeaderCreator
;
296 void KItemListView::setStyleOption(const KItemListStyleOption
& option
)
298 const KItemListStyleOption previousOption
= m_styleOption
;
299 m_styleOption
= option
;
301 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
302 while (it
.hasNext()) {
304 it
.value()->setStyleOption(option
);
307 m_sizeHintResolver
->clearCache();
309 onStyleOptionChanged(option
, previousOption
);
312 const KItemListStyleOption
& KItemListView::styleOption() const
314 return m_styleOption
;
317 void KItemListView::setGeometry(const QRectF
& rect
)
319 QGraphicsWidget::setGeometry(rect
);
325 if (m_model
->count() > 0) {
326 prepareLayoutForIncreasedItemCount(rect
.size(), LayouterSize
);
328 m_layouter
->setSize(rect
.size());
331 if (!m_layoutTimer
->isActive()) {
332 m_layoutTimer
->start();
335 // Changing the geometry does not require to do an expensive
336 // update of the visible-roles sizes, only the stretched sizes
337 // need to be adjusted to the new size.
338 updateStretchedVisibleRolesSizes();
341 int KItemListView::itemAt(const QPointF
& pos
) const
343 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
344 while (it
.hasNext()) {
347 const KItemListWidget
* widget
= it
.value();
348 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
349 if (widget
->contains(mappedPos
)) {
357 bool KItemListView::isAboveSelectionToggle(int index
, const QPointF
& pos
) const
364 bool KItemListView::isAboveExpansionToggle(int index
, const QPointF
& pos
) const
366 const KItemListWidget
* widget
= m_visibleItems
.value(index
);
368 const QRectF expansionToggleRect
= widget
->expansionToggleRect();
369 if (!expansionToggleRect
.isEmpty()) {
370 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
371 return expansionToggleRect
.contains(mappedPos
);
377 int KItemListView::firstVisibleIndex() const
379 return m_layouter
->firstVisibleIndex();
382 int KItemListView::lastVisibleIndex() const
384 return m_layouter
->lastVisibleIndex();
387 QSizeF
KItemListView::itemSizeHint(int index
) const
393 QHash
<QByteArray
, QSizeF
> KItemListView::visibleRolesSizes(const KItemRangeList
& itemRanges
) const
395 Q_UNUSED(itemRanges
);
396 return QHash
<QByteArray
, QSizeF
>();
399 QRectF
KItemListView::itemBoundingRect(int index
) const
401 return m_layouter
->itemBoundingRect(index
);
404 int KItemListView::itemsPerOffset() const
406 return m_layouter
->itemsPerOffset();
409 void KItemListView::beginTransaction()
411 ++m_activeTransactions
;
412 if (m_activeTransactions
== 1) {
413 onTransactionBegin();
417 void KItemListView::endTransaction()
419 --m_activeTransactions
;
420 if (m_activeTransactions
< 0) {
421 m_activeTransactions
= 0;
422 kWarning() << "Mismatch between beginTransaction()/endTransaction()";
425 if (m_activeTransactions
== 0) {
431 bool KItemListView::isTransactionActive() const
433 return m_activeTransactions
> 0;
437 void KItemListView::setHeaderShown(bool show
)
440 if (show
&& !m_header
) {
441 m_header
= new KItemListHeader(this);
442 m_header
->setPos(0, 0);
443 m_header
->setModel(m_model
);
444 m_header
->setVisibleRoles(m_visibleRoles
);
445 m_header
->setVisibleRolesWidths(headerRolesWidths());
446 m_header
->setZValue(1);
448 m_useHeaderWidths
= false;
450 connect(m_header
, SIGNAL(visibleRoleWidthChanged(QByteArray
,qreal
,qreal
)),
451 this, SLOT(slotVisibleRoleWidthChanged(QByteArray
,qreal
,qreal
)));
453 m_layouter
->setHeaderHeight(m_header
->size().height());
454 } else if (!show
&& m_header
) {
457 m_useHeaderWidths
= false;
458 m_layouter
->setHeaderHeight(0);
462 bool KItemListView::isHeaderShown() const
464 return m_header
!= 0;
467 QPixmap
KItemListView::createDragPixmap(const QSet
<int>& indexes
) const
473 void KItemListView::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
475 QGraphicsWidget::paint(painter
, option
, widget
);
477 if (m_rubberBand
->isActive()) {
478 QRectF rubberBandRect
= QRectF(m_rubberBand
->startPosition(),
479 m_rubberBand
->endPosition()).normalized();
481 const QPointF topLeft
= rubberBandRect
.topLeft();
482 if (scrollOrientation() == Qt::Vertical
) {
483 rubberBandRect
.moveTo(topLeft
.x(), topLeft
.y() - scrollOffset());
485 rubberBandRect
.moveTo(topLeft
.x() - scrollOffset(), topLeft
.y());
488 QStyleOptionRubberBand opt
;
489 opt
.initFrom(widget
);
490 opt
.shape
= QRubberBand::Rectangle
;
492 opt
.rect
= rubberBandRect
.toRect();
493 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
497 void KItemListView::initializeItemListWidget(KItemListWidget
* item
)
502 bool KItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
504 Q_UNUSED(changedRoles
);
508 void KItemListView::onControllerChanged(KItemListController
* current
, KItemListController
* previous
)
514 void KItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
520 void KItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
526 void KItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
532 void KItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
538 void KItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
544 void KItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
550 void KItemListView::onTransactionBegin()
554 void KItemListView::onTransactionEnd()
558 bool KItemListView::event(QEvent
* event
)
560 // Forward all events to the controller and handle them there
561 if (m_controller
&& m_controller
->processEvent(event
, transform())) {
565 return QGraphicsWidget::event(event
);
568 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent
* event
)
570 m_mousePos
= transform().map(event
->pos());
574 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
)
576 QGraphicsWidget::mouseMoveEvent(event
);
578 m_mousePos
= transform().map(event
->pos());
579 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
580 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
584 void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
)
586 event
->setAccepted(true);
590 void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent
*event
)
592 QGraphicsWidget::dragMoveEvent(event
);
594 m_mousePos
= transform().map(event
->pos());
595 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
596 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
600 void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
)
602 QGraphicsWidget::dragLeaveEvent(event
);
603 setAutoScroll(false);
606 void KItemListView::dropEvent(QGraphicsSceneDragDropEvent
* event
)
608 QGraphicsWidget::dropEvent(event
);
609 setAutoScroll(false);
612 QList
<KItemListWidget
*> KItemListView::visibleItemListWidgets() const
614 return m_visibleItems
.values();
617 void KItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
619 QGraphicsWidget::resizeEvent(event
);
620 if (m_itemSize
.isEmpty() && m_useHeaderWidths
) {
621 QSizeF dynamicItemSize
= m_layouter
->itemSize();
622 const QSizeF newSize
= event
->newSize();
624 if (m_itemSize
.width() < 0) {
625 const qreal requiredWidth
= visibleRolesSizesWidthSum();
626 if (newSize
.width() > requiredWidth
) {
627 dynamicItemSize
.setWidth(newSize
.width());
629 const qreal headerWidth
= qMax(newSize
.width(), requiredWidth
);
630 m_header
->resize(headerWidth
, m_header
->size().height());
633 if (m_itemSize
.height() < 0) {
634 const qreal requiredHeight
= visibleRolesSizesHeightSum();
635 if (newSize
.height() > requiredHeight
) {
636 dynamicItemSize
.setHeight(newSize
.height());
638 // TODO: KItemListHeader is not prepared for vertical alignment
641 m_layouter
->setItemSize(dynamicItemSize
);
645 void KItemListView::slotItemsInserted(const KItemRangeList
& itemRanges
)
647 updateVisibleRolesSizes(itemRanges
);
649 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
650 if (hasMultipleRanges
) {
654 int previouslyInsertedCount
= 0;
655 foreach (const KItemRange
& range
, itemRanges
) {
656 // range.index is related to the model before anything has been inserted.
657 // As in each loop the current item-range gets inserted the index must
658 // be increased by the already previously inserted items.
659 const int index
= range
.index
+ previouslyInsertedCount
;
660 const int count
= range
.count
;
661 if (index
< 0 || count
<= 0) {
662 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
665 previouslyInsertedCount
+= count
;
667 m_sizeHintResolver
->itemsInserted(index
, count
);
669 // Determine which visible items must be moved
670 QList
<int> itemsToMove
;
671 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
672 while (it
.hasNext()) {
674 const int visibleItemIndex
= it
.key();
675 if (visibleItemIndex
>= index
) {
676 itemsToMove
.append(visibleItemIndex
);
680 // Update the indexes of all KItemListWidget instances that are located
681 // after the inserted items. It is important to adjust the indexes in the order
682 // from the highest index to the lowest index to prevent overlaps when setting the new index.
684 for (int i
= itemsToMove
.count() - 1; i
>= 0; --i
) {
685 KItemListWidget
* widget
= m_visibleItems
.value(itemsToMove
[i
]);
687 setWidgetIndex(widget
, widget
->index() + count
);
690 m_layouter
->markAsDirty();
691 if (m_model
->count() == count
&& maximumScrollOffset() > size().height()) {
692 kDebug() << "Scrollbar required, skipping layout";
693 const int scrollBarExtent
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
694 QSizeF layouterSize
= m_layouter
->size();
695 if (scrollOrientation() == Qt::Vertical
) {
696 layouterSize
.rwidth() -= scrollBarExtent
;
698 layouterSize
.rheight() -= scrollBarExtent
;
700 m_layouter
->setSize(layouterSize
);
703 if (!hasMultipleRanges
) {
704 doLayout(Animation
, index
, count
);
710 m_controller
->selectionManager()->itemsInserted(itemRanges
);
713 if (hasMultipleRanges
) {
718 void KItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
720 updateVisibleRolesSizes();
722 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
723 if (hasMultipleRanges
) {
727 for (int i
= itemRanges
.count() - 1; i
>= 0; --i
) {
728 const KItemRange
& range
= itemRanges
.at(i
);
729 const int index
= range
.index
;
730 const int count
= range
.count
;
731 if (index
< 0 || count
<= 0) {
732 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
736 m_sizeHintResolver
->itemsRemoved(index
, count
);
738 const int firstRemovedIndex
= index
;
739 const int lastRemovedIndex
= index
+ count
- 1;
740 const int lastIndex
= m_model
->count() + count
- 1;
742 // Remove all KItemListWidget instances that got deleted
743 for (int i
= firstRemovedIndex
; i
<= lastRemovedIndex
; ++i
) {
744 KItemListWidget
* widget
= m_visibleItems
.value(i
);
749 m_animation
->stop(widget
);
750 // Stopping the animation might lead to recycling the widget if
751 // it is invisible (see slotAnimationFinished()).
752 // Check again whether it is still visible:
753 if (!m_visibleItems
.contains(i
)) {
757 if (m_model
->count() == 0) {
758 // For performance reasons no animation is done when all items have
760 recycleWidget(widget
);
762 // Animate the removing of the items. Special case: When removing an item there
763 // is no valid model index available anymore. For the
764 // remove-animation the item gets removed from m_visibleItems but the widget
765 // will stay alive until the animation has been finished and will
766 // be recycled (deleted) in KItemListView::slotAnimationFinished().
767 m_visibleItems
.remove(i
);
768 widget
->setIndex(-1);
769 m_animation
->start(widget
, KItemListViewAnimation::DeleteAnimation
);
773 // Update the indexes of all KItemListWidget instances that are located
774 // after the deleted items
775 for (int i
= lastRemovedIndex
+ 1; i
<= lastIndex
; ++i
) {
776 KItemListWidget
* widget
= m_visibleItems
.value(i
);
778 const int newIndex
= i
- count
;
779 setWidgetIndex(widget
, newIndex
);
783 m_layouter
->markAsDirty();
784 if (!hasMultipleRanges
) {
785 doLayout(Animation
, index
, -count
);
791 m_controller
->selectionManager()->itemsRemoved(itemRanges
);
794 if (hasMultipleRanges
) {
799 void KItemListView::slotItemsMoved(const KItemRange
& itemRange
, const QList
<int>& movedToIndexes
)
801 const int firstVisibleMovedIndex
= qMax(firstVisibleIndex(), itemRange
.index
);
802 const int lastVisibleMovedIndex
= qMin(lastVisibleIndex(), itemRange
.index
+ itemRange
.count
- 1);
804 for (int index
= firstVisibleMovedIndex
; index
<= lastVisibleMovedIndex
; ++index
) {
805 KItemListWidget
* widget
= m_visibleItems
.value(index
);
807 updateWidgetProperties(widget
, index
);
812 m_controller
->selectionManager()->itemsMoved(itemRange
, movedToIndexes
);
816 void KItemListView::slotItemsChanged(const KItemRangeList
& itemRanges
,
817 const QSet
<QByteArray
>& roles
)
819 const bool updateSizeHints
= itemSizeHintUpdateRequired(roles
);
820 if (updateSizeHints
) {
821 updateVisibleRolesSizes(itemRanges
);
824 foreach (const KItemRange
& itemRange
, itemRanges
) {
825 const int index
= itemRange
.index
;
826 const int count
= itemRange
.count
;
828 if (updateSizeHints
) {
829 m_sizeHintResolver
->itemsChanged(index
, count
, roles
);
830 m_layouter
->markAsDirty();
831 if (!m_layoutTimer
->isActive()) {
832 m_layoutTimer
->start();
836 // Apply the changed roles to the visible item-widgets
837 const int lastIndex
= index
+ count
- 1;
838 for (int i
= index
; i
<= lastIndex
; ++i
) {
839 KItemListWidget
* widget
= m_visibleItems
.value(i
);
841 widget
->setData(m_model
->data(i
), roles
);
848 void KItemListView::slotGroupedSortingChanged(bool current
)
853 void KItemListView::slotCurrentChanged(int current
, int previous
)
857 KItemListWidget
* previousWidget
= m_visibleItems
.value(previous
, 0);
858 if (previousWidget
) {
859 Q_ASSERT(previousWidget
->isCurrent());
860 previousWidget
->setCurrent(false);
863 KItemListWidget
* currentWidget
= m_visibleItems
.value(current
, 0);
865 Q_ASSERT(!currentWidget
->isCurrent());
866 currentWidget
->setCurrent(true);
869 const QRectF viewGeometry
= geometry();
870 const QRectF currentBoundingRect
= itemBoundingRect(current
);
872 if (!viewGeometry
.contains(currentBoundingRect
)) {
873 // Make sure that the new current item is fully visible in the view.
874 qreal newOffset
= scrollOffset();
875 if (currentBoundingRect
.top() < viewGeometry
.top()) {
876 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
877 newOffset
+= currentBoundingRect
.top() - viewGeometry
.top();
878 } else if ((currentBoundingRect
.bottom() > viewGeometry
.bottom())) {
879 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
880 newOffset
+= currentBoundingRect
.bottom() - viewGeometry
.bottom();
881 } else if (currentBoundingRect
.left() < viewGeometry
.left()) {
882 if (scrollOrientation() == Qt::Horizontal
) {
883 newOffset
+= currentBoundingRect
.left() - viewGeometry
.left();
885 } else if ((currentBoundingRect
.right() > viewGeometry
.right())) {
886 if (scrollOrientation() == Qt::Horizontal
) {
887 newOffset
+= currentBoundingRect
.right() - viewGeometry
.right();
891 if (newOffset
!= scrollOffset()) {
892 emit
scrollTo(newOffset
);
897 void KItemListView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
901 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
902 while (it
.hasNext()) {
904 const int index
= it
.key();
905 KItemListWidget
* widget
= it
.value();
906 widget
->setSelected(current
.contains(index
));
910 void KItemListView::slotAnimationFinished(QGraphicsWidget
* widget
,
911 KItemListViewAnimation::AnimationType type
)
913 KItemListWidget
* itemListWidget
= qobject_cast
<KItemListWidget
*>(widget
);
914 Q_ASSERT(itemListWidget
);
917 case KItemListViewAnimation::DeleteAnimation
: {
918 // As we recycle the widget in this case it is important to assure that no
919 // other animation has been started. This is a convention in KItemListView and
920 // not a requirement defined by KItemListViewAnimation.
921 Q_ASSERT(!m_animation
->isStarted(itemListWidget
));
923 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
924 // by m_visibleWidgets and must be deleted manually after the animation has
926 KItemListGroupHeader
* header
= m_visibleGroups
.value(itemListWidget
);
928 m_groupHeaderCreator
->recycle(header
);
929 m_visibleGroups
.remove(itemListWidget
);
931 m_widgetCreator
->recycle(itemListWidget
);
935 case KItemListViewAnimation::CreateAnimation
:
936 case KItemListViewAnimation::MovingAnimation
:
937 case KItemListViewAnimation::ResizeAnimation
: {
938 const int index
= itemListWidget
->index();
939 const bool invisible
= (index
< m_layouter
->firstVisibleIndex()) ||
940 (index
> m_layouter
->lastVisibleIndex());
941 if (invisible
&& !m_animation
->isStarted(itemListWidget
)) {
942 recycleWidget(itemListWidget
);
951 void KItemListView::slotLayoutTimerFinished()
953 m_layouter
->setSize(geometry().size());
954 doLayout(Animation
, 0, 0);
957 void KItemListView::slotRubberBandPosChanged()
962 void KItemListView::slotRubberBandActivationChanged(bool active
)
965 connect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
966 connect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
967 m_skipAutoScrollForRubberBand
= true;
969 disconnect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
970 disconnect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
971 m_skipAutoScrollForRubberBand
= false;
977 void KItemListView::slotVisibleRoleWidthChanged(const QByteArray
& role
,
981 Q_UNUSED(previousWidth
);
983 m_useHeaderWidths
= true;
985 if (m_visibleRolesSizes
.contains(role
)) {
986 QSizeF roleSize
= m_visibleRolesSizes
.value(role
);
987 roleSize
.setWidth(currentWidth
);
988 m_visibleRolesSizes
.insert(role
, roleSize
);
989 m_stretchedVisibleRolesSizes
.insert(role
, roleSize
);
991 // Apply the new size to the layouter
992 QSizeF dynamicItemSize
= m_itemSize
;
993 if (dynamicItemSize
.width() < 0) {
994 const qreal requiredWidth
= visibleRolesSizesWidthSum();
995 dynamicItemSize
.setWidth(qMax(size().width(), requiredWidth
));
997 if (dynamicItemSize
.height() < 0) {
998 const qreal requiredHeight
= visibleRolesSizesHeightSum();
999 dynamicItemSize
.setHeight(qMax(size().height(), requiredHeight
));
1002 m_layouter
->setItemSize(dynamicItemSize
);
1004 // Update the role sizes for all visible widgets
1005 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
1006 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1013 void KItemListView::triggerAutoScrolling()
1015 if (!m_autoScrollTimer
) {
1020 int visibleSize
= 0;
1021 if (scrollOrientation() == Qt::Vertical
) {
1022 pos
= m_mousePos
.y();
1023 visibleSize
= size().height();
1025 pos
= m_mousePos
.x();
1026 visibleSize
= size().width();
1029 if (m_autoScrollTimer
->interval() == InitialAutoScrollDelay
) {
1030 m_autoScrollIncrement
= 0;
1033 m_autoScrollIncrement
= calculateAutoScrollingIncrement(pos
, visibleSize
, m_autoScrollIncrement
);
1034 if (m_autoScrollIncrement
== 0) {
1035 // The mouse position is not above an autoscroll margin (the autoscroll timer
1036 // will be restarted in mouseMoveEvent())
1037 m_autoScrollTimer
->stop();
1041 if (m_rubberBand
->isActive() && m_skipAutoScrollForRubberBand
) {
1042 // If a rubberband selection is ongoing the autoscrolling may only get triggered
1043 // if the direction of the rubberband is similar to the autoscroll direction. This
1044 // prevents that starting to create a rubberband within the autoscroll margins starts
1045 // an autoscrolling.
1047 const qreal minDiff
= 4; // Ignore any autoscrolling if the rubberband is very small
1048 const qreal diff
= (scrollOrientation() == Qt::Vertical
)
1049 ? m_rubberBand
->endPosition().y() - m_rubberBand
->startPosition().y()
1050 : m_rubberBand
->endPosition().x() - m_rubberBand
->startPosition().x();
1051 if (qAbs(diff
) < minDiff
|| (m_autoScrollIncrement
< 0 && diff
> 0) || (m_autoScrollIncrement
> 0 && diff
< 0)) {
1052 // The rubberband direction is different from the scroll direction (e.g. the rubberband has
1053 // been moved up although the autoscroll direction might be down)
1054 m_autoScrollTimer
->stop();
1059 // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
1060 // the autoscrolling may not get skipped anymore until a new rubberband is created
1061 m_skipAutoScrollForRubberBand
= false;
1063 setScrollOffset(scrollOffset() + m_autoScrollIncrement
);
1065 // Trigger the autoscroll timer which will periodically call
1066 // triggerAutoScrolling()
1067 m_autoScrollTimer
->start(RepeatingAutoScrollDelay
);
1070 void KItemListView::setController(KItemListController
* controller
)
1072 if (m_controller
!= controller
) {
1073 KItemListController
* previous
= m_controller
;
1075 KItemListSelectionManager
* selectionManager
= previous
->selectionManager();
1076 disconnect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1077 disconnect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
1080 m_controller
= controller
;
1083 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
1084 connect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1085 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
1088 onControllerChanged(controller
, previous
);
1092 void KItemListView::setModel(KItemModelBase
* model
)
1094 if (m_model
== model
) {
1098 KItemModelBase
* previous
= m_model
;
1101 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1102 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1103 disconnect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
1104 this, SLOT(slotItemsInserted(KItemRangeList
)));
1105 disconnect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
1106 this, SLOT(slotItemsRemoved(KItemRangeList
)));
1107 disconnect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
1108 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
1109 disconnect(m_model
, SIGNAL(groupedSortingChanged(bool)),
1110 this, SLOT(slotGroupedSortingChanged(bool)));
1114 m_layouter
->setModel(model
);
1115 m_grouped
= model
->groupedSorting();
1118 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1119 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1120 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
1121 this, SLOT(slotItemsInserted(KItemRangeList
)));
1122 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
1123 this, SLOT(slotItemsRemoved(KItemRangeList
)));
1124 connect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
1125 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
1126 connect(m_model
, SIGNAL(groupedSortingChanged(bool)),
1127 this, SLOT(slotGroupedSortingChanged(bool)));
1130 onModelChanged(model
, previous
);
1133 KItemListRubberBand
* KItemListView::rubberBand() const
1135 return m_rubberBand
;
1138 void KItemListView::updateLayout()
1140 doLayout(Animation
, 0, 0);
1144 void KItemListView::doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
)
1146 if (m_layoutTimer
->isActive()) {
1147 kDebug() << "Stopping layout timer, synchronous layout requested";
1148 m_layoutTimer
->stop();
1151 if (m_model
->count() < 0 || m_activeTransactions
> 0) {
1155 //markVisibleRolesSizesAsDirty();
1157 const int firstVisibleIndex
= m_layouter
->firstVisibleIndex();
1158 const int lastVisibleIndex
= m_layouter
->lastVisibleIndex();
1159 if (firstVisibleIndex
< 0) {
1160 emitOffsetChanges();
1164 // Do a sanity check of the scroll-offset property: When properties of the itemlist-view have been changed
1165 // it might be possible that the maximum offset got changed too. Assure that the full visible range
1166 // is still shown if the maximum offset got decreased.
1167 const qreal visibleOffsetRange
= (scrollOrientation() == Qt::Horizontal
) ? size().width() : size().height();
1168 const qreal maxOffsetToShowFullRange
= maximumScrollOffset() - visibleOffsetRange
;
1169 if (scrollOffset() > maxOffsetToShowFullRange
) {
1170 m_layouter
->setScrollOffset(qMax(qreal(0), maxOffsetToShowFullRange
));
1173 // Determine all items that are completely invisible and might be
1174 // reused for items that just got (at least partly) visible.
1175 // Items that do e.g. an animated moving of their position are not
1176 // marked as invisible: This assures that a scrolling inside the view
1177 // can be done without breaking an animation.
1178 QList
<int> reusableItems
;
1179 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
1180 while (it
.hasNext()) {
1182 KItemListWidget
* widget
= it
.value();
1183 const int index
= widget
->index();
1184 const bool invisible
= (index
< firstVisibleIndex
) || (index
> lastVisibleIndex
);
1185 if (invisible
&& !m_animation
->isStarted(widget
)) {
1186 widget
->setVisible(false);
1187 reusableItems
.append(index
);
1191 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
1192 // instances from invisible items are reused. If no reusable items are
1193 // found then new KItemListWidget instances get created.
1194 const bool animate
= (hint
== Animation
);
1195 for (int i
= firstVisibleIndex
; i
<= lastVisibleIndex
; ++i
) {
1196 bool applyNewPos
= true;
1197 bool wasHidden
= false;
1199 const QRectF itemBounds
= m_layouter
->itemBoundingRect(i
);
1200 const QPointF newPos
= itemBounds
.topLeft();
1201 KItemListWidget
* widget
= m_visibleItems
.value(i
);
1204 if (!reusableItems
.isEmpty()) {
1205 // Reuse a KItemListWidget instance from an invisible item
1206 const int oldIndex
= reusableItems
.takeLast();
1207 widget
= m_visibleItems
.value(oldIndex
);
1208 setWidgetIndex(widget
, i
);
1210 // No reusable KItemListWidget instance is available, create a new one
1211 widget
= createWidget(i
);
1213 widget
->resize(itemBounds
.size());
1215 if (animate
&& changedCount
< 0) {
1216 // Items have been deleted, move the created item to the
1217 // imaginary old position.
1218 const QRectF itemBoundingRect
= m_layouter
->itemBoundingRect(i
- changedCount
);
1219 if (itemBoundingRect
.isEmpty()) {
1220 const QPointF invisibleOldPos
= (scrollOrientation() == Qt::Vertical
)
1221 ? QPointF(0, size().height()) : QPointF(size().width(), 0);
1222 widget
->setPos(invisibleOldPos
);
1224 widget
->setPos(itemBoundingRect
.topLeft());
1226 applyNewPos
= false;
1228 } else if (m_animation
->isStarted(widget
, KItemListViewAnimation::MovingAnimation
)) {
1229 applyNewPos
= false;
1233 const bool itemsRemoved
= (changedCount
< 0);
1234 const bool itemsInserted
= (changedCount
> 0);
1236 if (itemsRemoved
&& (i
>= changedIndex
+ changedCount
+ 1)) {
1237 // The item is located after the removed items. Animate the moving of the position.
1238 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1239 applyNewPos
= false;
1240 } else if (itemsInserted
&& i
>= changedIndex
) {
1241 // The item is located after the first inserted item
1242 if (i
<= changedIndex
+ changedCount
- 1) {
1243 // The item is an inserted item. Animate the appearing of the item.
1244 // For performance reasons no animation is done when changedCount is equal
1245 // to all available items.
1246 if (changedCount
< m_model
->count()) {
1247 m_animation
->start(widget
, KItemListViewAnimation::CreateAnimation
);
1249 } else if (!m_animation
->isStarted(widget
, KItemListViewAnimation::CreateAnimation
)) {
1250 // The item was already there before, so animate the moving of the position.
1251 // No moving animation is done if the item is animated by a create animation: This
1252 // prevents a "move animation mess" when inserting several ranges in parallel.
1253 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1254 applyNewPos
= false;
1256 } else if (!itemsRemoved
&& !itemsInserted
&& !wasHidden
) {
1257 // The size of the view might have been changed. Animate the moving of the position.
1258 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1259 applyNewPos
= false;
1264 widget
->setPos(newPos
);
1267 Q_ASSERT(widget
->index() == i
);
1268 widget
->setVisible(true);
1270 if (widget
->size() != itemBounds
.size()) {
1271 m_animation
->start(widget
, KItemListViewAnimation::ResizeAnimation
, itemBounds
.size());
1275 // Delete invisible KItemListWidget instances that have not been reused
1276 foreach (int index
, reusableItems
) {
1277 recycleWidget(m_visibleItems
.value(index
));
1280 emitOffsetChanges();
1283 void KItemListView::emitOffsetChanges()
1285 const qreal newScrollOffset
= m_layouter
->scrollOffset();
1286 if (m_oldScrollOffset
!= newScrollOffset
) {
1287 emit
scrollOffsetChanged(newScrollOffset
, m_oldScrollOffset
);
1288 m_oldScrollOffset
= newScrollOffset
;
1291 const qreal newMaximumScrollOffset
= m_layouter
->maximumScrollOffset();
1292 if (m_oldMaximumScrollOffset
!= newMaximumScrollOffset
) {
1293 emit
maximumScrollOffsetChanged(newMaximumScrollOffset
, m_oldMaximumScrollOffset
);
1294 m_oldMaximumScrollOffset
= newMaximumScrollOffset
;
1297 const qreal newItemOffset
= m_layouter
->itemOffset();
1298 if (m_oldItemOffset
!= newItemOffset
) {
1299 emit
itemOffsetChanged(newItemOffset
, m_oldItemOffset
);
1300 m_oldItemOffset
= newItemOffset
;
1303 const qreal newMaximumItemOffset
= m_layouter
->maximumItemOffset();
1304 if (m_oldMaximumItemOffset
!= newMaximumItemOffset
) {
1305 emit
maximumItemOffsetChanged(newMaximumItemOffset
, m_oldMaximumItemOffset
);
1306 m_oldMaximumItemOffset
= newMaximumItemOffset
;
1310 KItemListWidget
* KItemListView::createWidget(int index
)
1312 KItemListWidget
* widget
= m_widgetCreator
->create(this);
1313 widget
->setFlag(QGraphicsItem::ItemStacksBehindParent
);
1315 updateWidgetProperties(widget
, index
);
1316 m_visibleItems
.insert(index
, widget
);
1319 if (m_layouter
->isFirstGroupItem(index
)) {
1320 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1321 header
->setPos(0, -50);
1322 header
->resize(50, 50);
1323 m_visibleGroups
.insert(widget
, header
);
1327 initializeItemListWidget(widget
);
1331 void KItemListView::recycleWidget(KItemListWidget
* widget
)
1334 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1336 m_groupHeaderCreator
->recycle(header
);
1337 m_visibleGroups
.remove(widget
);
1341 m_visibleItems
.remove(widget
->index());
1342 m_widgetCreator
->recycle(widget
);
1345 void KItemListView::setWidgetIndex(KItemListWidget
* widget
, int index
)
1348 bool createHeader
= m_layouter
->isFirstGroupItem(index
);
1349 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1352 createHeader
= false;
1354 m_groupHeaderCreator
->recycle(header
);
1355 m_visibleGroups
.remove(widget
);
1360 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1361 header
->setPos(0, -50);
1362 header
->resize(50, 50);
1363 m_visibleGroups
.insert(widget
, header
);
1367 const int oldIndex
= widget
->index();
1368 m_visibleItems
.remove(oldIndex
);
1369 updateWidgetProperties(widget
, index
);
1370 m_visibleItems
.insert(index
, widget
);
1372 initializeItemListWidget(widget
);
1375 void KItemListView::prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
)
1377 // Calculate the first visible index and last visible index for the current size
1378 const int currentFirst
= m_layouter
->firstVisibleIndex();
1379 const int currentLast
= m_layouter
->lastVisibleIndex();
1381 const QSizeF currentSize
= (sizeType
== LayouterSize
) ? m_layouter
->size() : m_layouter
->itemSize();
1383 // Calculate the first visible index and last visible index for the new size
1384 setLayouterSize(size
, sizeType
);
1385 const int newFirst
= m_layouter
->firstVisibleIndex();
1386 const int newLast
= m_layouter
->lastVisibleIndex();
1388 if ((currentFirst
!= newFirst
) || (currentLast
!= newLast
)) {
1389 // At least one index has been changed. Assure that widgets for all possible
1390 // visible items get created so that a move-animation can be started later.
1391 const int maxVisibleItems
= m_layouter
->maximumVisibleItems();
1392 int minFirst
= qMin(newFirst
, currentFirst
);
1393 const int maxLast
= qMax(newLast
, currentLast
);
1395 if (maxLast
- minFirst
+ 1 < maxVisibleItems
) {
1396 // Increasing the size might result in a smaller KItemListView::offset().
1397 // Decrease the first visible index in a way that at least the maximum
1398 // visible items are shown.
1399 minFirst
= qMax(0, maxLast
- maxVisibleItems
+ 1);
1402 if (maxLast
- minFirst
> maxVisibleItems
+ maxVisibleItems
/ 2) {
1403 // The creating of widgets is quite expensive. Assure that never more
1404 // than 50 % of the maximum visible items get created for the animations.
1408 setLayouterSize(currentSize
, sizeType
);
1409 for (int i
= minFirst
; i
<= maxLast
; ++i
) {
1410 if (!m_visibleItems
.contains(i
)) {
1411 KItemListWidget
* widget
= createWidget(i
);
1412 const QPointF pos
= m_layouter
->itemBoundingRect(i
).topLeft();
1413 widget
->setPos(pos
);
1416 setLayouterSize(size
, sizeType
);
1420 void KItemListView::setLayouterSize(const QSizeF
& size
, SizeType sizeType
)
1423 case LayouterSize
: m_layouter
->setSize(size
); break;
1424 case ItemSize
: m_layouter
->setItemSize(size
); break;
1429 void KItemListView::updateWidgetProperties(KItemListWidget
* widget
, int index
)
1431 widget
->setVisibleRoles(m_visibleRoles
);
1432 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1433 widget
->setStyleOption(m_styleOption
);
1435 const KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
1436 widget
->setCurrent(index
== selectionManager
->currentItem());
1437 widget
->setSelected(selectionManager
->isSelected(index
));
1438 widget
->setHovered(false);
1439 widget
->setAlternatingBackgroundColors(false);
1440 widget
->setIndex(index
);
1441 widget
->setData(m_model
->data(index
));
1444 QHash
<QByteArray
, qreal
> KItemListView::headerRolesWidths() const
1446 QHash
<QByteArray
, qreal
> rolesWidths
;
1448 QHashIterator
<QByteArray
, QSizeF
> it(m_stretchedVisibleRolesSizes
);
1449 while (it
.hasNext()) {
1451 rolesWidths
.insert(it
.key(), it
.value().width());
1457 void KItemListView::updateVisibleRolesSizes(const KItemRangeList
& itemRanges
)
1459 if (!m_itemSize
.isEmpty() || m_useHeaderWidths
) {
1463 const int itemCount
= m_model
->count();
1464 int rangesItemCount
= 0;
1465 foreach (const KItemRange
& range
, itemRanges
) {
1466 rangesItemCount
+= range
.count
;
1469 if (itemCount
== rangesItemCount
) {
1470 m_visibleRolesSizes
= visibleRolesSizes(itemRanges
);
1472 // Assure the the sizes are not smaller than the minimum defined by the header
1473 // TODO: Currently only implemented for a top-aligned header
1474 const qreal minHeaderRoleWidth
= m_header
->minimumRoleWidth();
1475 QMutableHashIterator
<QByteArray
, QSizeF
> it (m_visibleRolesSizes
);
1476 while (it
.hasNext()) {
1478 const QSizeF
& size
= it
.value();
1479 if (size
.width() < minHeaderRoleWidth
) {
1480 const QSizeF
newSize(minHeaderRoleWidth
, size
.height());
1481 m_visibleRolesSizes
.insert(it
.key(), newSize
);
1486 // Only a sub range of the roles need to be determined.
1487 // The chances are good that the sizes of the sub ranges
1488 // already fit into the available sizes and hence no
1489 // expensive update might be required.
1490 bool updateRequired
= false;
1492 const QHash
<QByteArray
, QSizeF
> updatedSizes
= visibleRolesSizes(itemRanges
);
1493 QHashIterator
<QByteArray
, QSizeF
> it(updatedSizes
);
1494 while (it
.hasNext()) {
1496 const QByteArray
& role
= it
.key();
1497 const QSizeF
& updatedSize
= it
.value();
1498 const QSizeF currentSize
= m_visibleRolesSizes
.value(role
);
1499 if (updatedSize
.width() > currentSize
.width() || updatedSize
.height() > currentSize
.height()) {
1500 m_visibleRolesSizes
.insert(role
, updatedSize
);
1501 updateRequired
= true;
1505 if (!updateRequired
) {
1506 // All the updated sizes are smaller than the current sizes and no change
1507 // of the stretched roles-widths is required
1512 updateStretchedVisibleRolesSizes();
1515 void KItemListView::updateVisibleRolesSizes()
1517 const int itemCount
= m_model
->count();
1518 if (itemCount
> 0) {
1519 updateVisibleRolesSizes(KItemRangeList() << KItemRange(0, itemCount
));
1523 void KItemListView::updateStretchedVisibleRolesSizes()
1525 if (!m_itemSize
.isEmpty() || m_useHeaderWidths
) {
1529 // Calculate the maximum size of an item by considering the
1530 // visible role sizes and apply them to the layouter. If the
1531 // size does not use the available view-size it the size of the
1532 // first role will get stretched.
1533 m_stretchedVisibleRolesSizes
= m_visibleRolesSizes
;
1534 const QByteArray role
= visibleRoles().first();
1535 QSizeF firstRoleSize
= m_stretchedVisibleRolesSizes
.value(role
);
1537 QSizeF dynamicItemSize
= m_itemSize
;
1539 if (dynamicItemSize
.width() <= 0) {
1540 const qreal requiredWidth
= visibleRolesSizesWidthSum();
1541 const qreal availableWidth
= size().width();
1542 if (requiredWidth
< availableWidth
) {
1543 // Stretch the first role to use the whole width for the item
1544 firstRoleSize
.rwidth() += availableWidth
- requiredWidth
;
1545 m_stretchedVisibleRolesSizes
.insert(role
, firstRoleSize
);
1547 dynamicItemSize
.setWidth(qMax(requiredWidth
, availableWidth
));
1550 if (dynamicItemSize
.height() <= 0) {
1551 const qreal requiredHeight
= visibleRolesSizesHeightSum();
1552 const qreal availableHeight
= size().height();
1553 if (requiredHeight
< availableHeight
) {
1554 // Stretch the first role to use the whole height for the item
1555 firstRoleSize
.rheight() += availableHeight
- requiredHeight
;
1556 m_stretchedVisibleRolesSizes
.insert(role
, firstRoleSize
);
1558 dynamicItemSize
.setHeight(qMax(requiredHeight
, availableHeight
));
1561 m_layouter
->setItemSize(dynamicItemSize
);
1564 m_header
->setVisibleRolesWidths(headerRolesWidths());
1565 m_header
->resize(dynamicItemSize
.width(), m_header
->size().height());
1568 // Update the role sizes for all visible widgets
1569 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
1570 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1574 qreal
KItemListView::visibleRolesSizesWidthSum() const
1577 QHashIterator
<QByteArray
, QSizeF
> it(m_visibleRolesSizes
);
1578 while (it
.hasNext()) {
1580 widthSum
+= it
.value().width();
1585 qreal
KItemListView::visibleRolesSizesHeightSum() const
1587 qreal heightSum
= 0;
1588 QHashIterator
<QByteArray
, QSizeF
> it(m_visibleRolesSizes
);
1589 while (it
.hasNext()) {
1591 heightSum
+= it
.value().height();
1596 QRectF
KItemListView::headerBoundaries() const
1598 return m_header
? m_header
->geometry() : QRectF();
1601 int KItemListView::calculateAutoScrollingIncrement(int pos
, int range
, int oldInc
)
1605 const int minSpeed
= 4;
1606 const int maxSpeed
= 128;
1607 const int speedLimiter
= 96;
1608 const int autoScrollBorder
= 64;
1610 // Limit the increment that is allowed to be added in comparison to 'oldInc'.
1611 // This assures that the autoscrolling speed grows gradually.
1612 const int incLimiter
= 1;
1614 if (pos
< autoScrollBorder
) {
1615 inc
= -minSpeed
+ qAbs(pos
- autoScrollBorder
) * (pos
- autoScrollBorder
) / speedLimiter
;
1616 inc
= qMax(inc
, -maxSpeed
);
1617 inc
= qMax(inc
, oldInc
- incLimiter
);
1618 } else if (pos
> range
- autoScrollBorder
) {
1619 inc
= minSpeed
+ qAbs(pos
- range
+ autoScrollBorder
) * (pos
- range
+ autoScrollBorder
) / speedLimiter
;
1620 inc
= qMin(inc
, maxSpeed
);
1621 inc
= qMin(inc
, oldInc
+ incLimiter
);
1629 KItemListCreatorBase::~KItemListCreatorBase()
1631 qDeleteAll(m_recycleableWidgets
);
1632 qDeleteAll(m_createdWidgets
);
1635 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget
* widget
)
1637 m_createdWidgets
.insert(widget
);
1640 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget
* widget
)
1642 Q_ASSERT(m_createdWidgets
.contains(widget
));
1643 m_createdWidgets
.remove(widget
);
1645 if (m_recycleableWidgets
.count() < 100) {
1646 m_recycleableWidgets
.append(widget
);
1647 widget
->setVisible(false);
1653 QGraphicsWidget
* KItemListCreatorBase::popRecycleableWidget()
1655 if (m_recycleableWidgets
.isEmpty()) {
1659 QGraphicsWidget
* widget
= m_recycleableWidgets
.takeLast();
1660 m_createdWidgets
.insert(widget
);
1664 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
1668 void KItemListWidgetCreatorBase::recycle(KItemListWidget
* widget
)
1670 widget
->setOpacity(1.0);
1671 pushRecycleableWidget(widget
);
1674 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
1678 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader
* header
)
1680 header
->setOpacity(1.0);
1681 pushRecycleableWidget(header
);
1684 #include "kitemlistview.moc"