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 "kitemlistrubberband_p.h"
28 #include "kitemlistselectionmanager.h"
29 #include "kitemlistsizehintresolver_p.h"
30 #include "kitemlistviewlayouter_p.h"
31 #include "kitemlistviewanimation_p.h"
32 #include "kitemlistwidget.h"
37 #include <QGraphicsSceneMouseEvent>
39 #include <QPropertyAnimation>
41 #include <QStyleOptionRubberBand>
44 KItemListView::KItemListView(QGraphicsWidget
* parent
) :
45 QGraphicsWidget(parent
),
46 m_autoScrollMarginEnabled(false),
48 m_activeTransactions(0),
53 m_visibleRolesSizes(),
55 m_groupHeaderCreator(0),
59 m_sizeHintResolver(0),
64 m_oldMaximumOffset(0),
68 setAcceptHoverEvents(true);
70 m_sizeHintResolver
= new KItemListSizeHintResolver(this);
72 m_layouter
= new KItemListViewLayouter(this);
73 m_layouter
->setSizeHintResolver(m_sizeHintResolver
);
75 m_animation
= new KItemListViewAnimation(this);
76 connect(m_animation
, SIGNAL(finished(QGraphicsWidget
*,KItemListViewAnimation::AnimationType
)),
77 this, SLOT(slotAnimationFinished(QGraphicsWidget
*,KItemListViewAnimation::AnimationType
)));
79 m_layoutTimer
= new QTimer(this);
80 m_layoutTimer
->setInterval(300);
81 m_layoutTimer
->setSingleShot(true);
82 connect(m_layoutTimer
, SIGNAL(timeout()), this, SLOT(slotLayoutTimerFinished()));
84 m_rubberBand
= new KItemListRubberBand(this);
85 connect(m_rubberBand
, SIGNAL(activationChanged(bool)), this, SLOT(slotRubberBandActivationChanged(bool)));
88 KItemListView::~KItemListView()
90 delete m_sizeHintResolver
;
91 m_sizeHintResolver
= 0;
94 void KItemListView::setScrollOrientation(Qt::Orientation orientation
)
96 const Qt::Orientation previousOrientation
= m_layouter
->scrollOrientation();
97 if (orientation
== previousOrientation
) {
101 m_layouter
->setScrollOrientation(orientation
);
102 m_animation
->setScrollOrientation(orientation
);
103 m_sizeHintResolver
->clearCache();
105 onScrollOrientationChanged(orientation
, previousOrientation
);
108 Qt::Orientation
KItemListView::scrollOrientation() const
110 return m_layouter
->scrollOrientation();
113 void KItemListView::setItemSize(const QSizeF
& itemSize
)
115 const QSizeF previousSize
= m_itemSize
;
116 if (itemSize
== previousSize
) {
120 m_itemSize
= itemSize
;
122 if (!markVisibleRolesSizesAsDirty()) {
123 if (itemSize
.width() < previousSize
.width() || itemSize
.height() < previousSize
.height()) {
124 prepareLayoutForIncreasedItemCount(itemSize
, ItemSize
);
126 m_layouter
->setItemSize(itemSize
);
130 m_sizeHintResolver
->clearCache();
132 onItemSizeChanged(itemSize
, previousSize
);
135 QSizeF
KItemListView::itemSize() const
140 void KItemListView::setOffset(qreal offset
)
146 const qreal previousOffset
= m_layouter
->offset();
147 if (offset
== previousOffset
) {
151 m_layouter
->setOffset(offset
);
152 m_animation
->setOffset(offset
);
153 if (!m_layoutTimer
->isActive()) {
154 doLayout(NoAnimation
, 0, 0);
157 onOffsetChanged(offset
, previousOffset
);
160 qreal
KItemListView::offset() const
162 return m_layouter
->offset();
165 qreal
KItemListView::maximumOffset() const
167 return m_layouter
->maximumOffset();
170 void KItemListView::setVisibleRoles(const QHash
<QByteArray
, int>& roles
)
172 const QHash
<QByteArray
, int> previousRoles
= m_visibleRoles
;
173 m_visibleRoles
= roles
;
175 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
176 while (it
.hasNext()) {
178 KItemListWidget
* widget
= it
.value();
179 widget
->setVisibleRoles(roles
);
180 widget
->setVisibleRolesSizes(m_visibleRolesSizes
);
183 m_sizeHintResolver
->clearCache();
184 m_layouter
->markAsDirty();
185 onVisibleRolesChanged(roles
, previousRoles
);
187 markVisibleRolesSizesAsDirty();
191 QHash
<QByteArray
, int> KItemListView::visibleRoles() const
193 return m_visibleRoles
;
196 KItemListController
* KItemListView::controller() const
201 KItemModelBase
* KItemListView::model() const
206 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
)
208 m_widgetCreator
= widgetCreator
;
211 KItemListWidgetCreatorBase
* KItemListView::widgetCreator() const
213 return m_widgetCreator
;
216 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
)
218 m_groupHeaderCreator
= groupHeaderCreator
;
221 KItemListGroupHeaderCreatorBase
* KItemListView::groupHeaderCreator() const
223 return m_groupHeaderCreator
;
226 void KItemListView::setStyleOption(const KItemListStyleOption
& option
)
228 const KItemListStyleOption previousOption
= m_styleOption
;
229 m_styleOption
= option
;
231 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
232 while (it
.hasNext()) {
234 it
.value()->setStyleOption(option
);
237 m_sizeHintResolver
->clearCache();
239 onStyleOptionChanged(option
, previousOption
);
242 const KItemListStyleOption
& KItemListView::styleOption() const
244 return m_styleOption
;
247 void KItemListView::setGeometry(const QRectF
& rect
)
249 QGraphicsWidget::setGeometry(rect
);
254 if (m_itemSize
.isEmpty()) {
255 m_layouter
->setItemSize(QSizeF());
258 if (m_model
->count() > 0) {
259 prepareLayoutForIncreasedItemCount(rect
.size(), LayouterSize
);
261 m_layouter
->setSize(rect
.size());
264 m_layoutTimer
->start();
267 int KItemListView::itemAt(const QPointF
& pos
) const
269 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
270 while (it
.hasNext()) {
273 const KItemListWidget
* widget
= it
.value();
274 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
275 if (widget
->contains(mappedPos
)) {
283 bool KItemListView::isAboveSelectionToggle(int index
, const QPointF
& pos
) const
290 bool KItemListView::isAboveExpansionToggle(int index
, const QPointF
& pos
) const
292 const KItemListWidget
* widget
= m_visibleItems
.value(index
);
294 const QRectF expansionToggleRect
= widget
->expansionToggleRect();
295 if (!expansionToggleRect
.isEmpty()) {
296 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
297 return expansionToggleRect
.contains(mappedPos
);
303 int KItemListView::firstVisibleIndex() const
305 return m_layouter
->firstVisibleIndex();
308 int KItemListView::lastVisibleIndex() const
310 return m_layouter
->lastVisibleIndex();
313 QSizeF
KItemListView::itemSizeHint(int index
) const
319 QHash
<QByteArray
, QSizeF
> KItemListView::visibleRoleSizes() const
321 return QHash
<QByteArray
, QSizeF
>();
324 QRectF
KItemListView::itemBoundingRect(int index
) const
326 return m_layouter
->itemBoundingRect(index
);
329 int KItemListView::itemsPerOffset() const
331 return m_layouter
->itemsPerOffset();
334 void KItemListView::beginTransaction()
336 ++m_activeTransactions
;
337 if (m_activeTransactions
== 1) {
338 onTransactionBegin();
342 void KItemListView::endTransaction()
344 --m_activeTransactions
;
345 if (m_activeTransactions
< 0) {
346 m_activeTransactions
= 0;
347 kWarning() << "Mismatch between beginTransaction()/endTransaction()";
350 if (m_activeTransactions
== 0) {
356 bool KItemListView::isTransactionActive() const
358 return m_activeTransactions
> 0;
361 void KItemListView::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
363 QGraphicsWidget::paint(painter
, option
, widget
);
365 if (m_rubberBand
->isActive()) {
366 QRectF rubberBandRect
= QRectF(m_rubberBand
->startPosition(),
367 m_rubberBand
->endPosition()).normalized();
369 const QPointF topLeft
= rubberBandRect
.topLeft();
370 if (scrollOrientation() == Qt::Vertical
) {
371 rubberBandRect
.moveTo(topLeft
.x(), topLeft
.y() - offset());
373 rubberBandRect
.moveTo(topLeft
.x() - offset(), topLeft
.y());
376 QStyleOptionRubberBand opt
;
377 opt
.initFrom(widget
);
378 opt
.shape
= QRubberBand::Rectangle
;
380 opt
.rect
= rubberBandRect
.toRect();
381 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
385 void KItemListView::initializeItemListWidget(KItemListWidget
* item
)
390 void KItemListView::onControllerChanged(KItemListController
* current
, KItemListController
* previous
)
396 void KItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
402 void KItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
408 void KItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
414 void KItemListView::onOffsetChanged(qreal current
, qreal previous
)
420 void KItemListView::onVisibleRolesChanged(const QHash
<QByteArray
, int>& current
, const QHash
<QByteArray
, int>& previous
)
426 void KItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
432 void KItemListView::onTransactionBegin()
436 void KItemListView::onTransactionEnd()
440 bool KItemListView::event(QEvent
* event
)
442 // Forward all events to the controller and handle them there
443 if (m_controller
&& m_controller
->processEvent(event
, transform())) {
447 return QGraphicsWidget::event(event
);
450 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent
* event
)
452 m_mousePos
= transform().map(event
->pos());
456 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
)
458 m_mousePos
= transform().map(event
->pos());
459 QGraphicsWidget::mouseMoveEvent(event
);
462 QList
<KItemListWidget
*> KItemListView::visibleItemListWidgets() const
464 return m_visibleItems
.values();
467 void KItemListView::slotItemsInserted(const KItemRangeList
& itemRanges
)
469 markVisibleRolesSizesAsDirty();
471 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
472 if (hasMultipleRanges
) {
476 int previouslyInsertedCount
= 0;
477 foreach (const KItemRange
& range
, itemRanges
) {
478 // range.index is related to the model before anything has been inserted.
479 // As in each loop the current item-range gets inserted the index must
480 // be increased by the already previously inserted items.
481 const int index
= range
.index
+ previouslyInsertedCount
;
482 const int count
= range
.count
;
483 if (index
< 0 || count
<= 0) {
484 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
487 previouslyInsertedCount
+= count
;
489 m_sizeHintResolver
->itemsInserted(index
, count
);
491 // Determine which visible items must be moved
492 QList
<int> itemsToMove
;
493 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
494 while (it
.hasNext()) {
496 const int visibleItemIndex
= it
.key();
497 if (visibleItemIndex
>= index
) {
498 itemsToMove
.append(visibleItemIndex
);
502 // Update the indexes of all KItemListWidget instances that are located
503 // after the inserted items. It is important to adjust the indexes in the order
504 // from the highest index to the lowest index to prevent overlaps when setting the new index.
506 for (int i
= itemsToMove
.count() - 1; i
>= 0; --i
) {
507 KItemListWidget
* widget
= m_visibleItems
.value(itemsToMove
[i
]);
509 setWidgetIndex(widget
, widget
->index() + count
);
512 m_layouter
->markAsDirty();
513 if (m_model
->count() == count
&& maximumOffset() > size().height()) {
514 kDebug() << "Scrollbar required, skipping layout";
515 const int scrollBarExtent
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
516 QSizeF layouterSize
= m_layouter
->size();
517 if (scrollOrientation() == Qt::Vertical
) {
518 layouterSize
.rwidth() -= scrollBarExtent
;
520 layouterSize
.rheight() -= scrollBarExtent
;
522 m_layouter
->setSize(layouterSize
);
525 if (!hasMultipleRanges
) {
526 doLayout(Animation
, index
, count
);
532 m_controller
->selectionManager()->itemsInserted(itemRanges
);
535 if (hasMultipleRanges
) {
540 void KItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
542 markVisibleRolesSizesAsDirty();
544 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
545 if (hasMultipleRanges
) {
549 for (int i
= itemRanges
.count() - 1; i
>= 0; --i
) {
550 const KItemRange
& range
= itemRanges
.at(i
);
551 const int index
= range
.index
;
552 const int count
= range
.count
;
553 if (index
< 0 || count
<= 0) {
554 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
558 m_sizeHintResolver
->itemsRemoved(index
, count
);
560 const int firstRemovedIndex
= index
;
561 const int lastRemovedIndex
= index
+ count
- 1;
562 const int lastIndex
= m_model
->count() + count
- 1;
564 // Remove all KItemListWidget instances that got deleted
565 for (int i
= firstRemovedIndex
; i
<= lastRemovedIndex
; ++i
) {
566 KItemListWidget
* widget
= m_visibleItems
.value(i
);
571 m_animation
->stop(widget
);
572 // Stopping the animation might lead to recycling the widget if
573 // it is invisible (see slotAnimationFinished()).
574 // Check again whether it is still visible:
575 if (!m_visibleItems
.contains(i
)) {
579 if (m_model
->count() == 0) {
580 // For performance reasons no animation is done when all items have
582 recycleWidget(widget
);
584 // Animate the removing of the items. Special case: When removing an item there
585 // is no valid model index available anymore. For the
586 // remove-animation the item gets removed from m_visibleItems but the widget
587 // will stay alive until the animation has been finished and will
588 // be recycled (deleted) in KItemListView::slotAnimationFinished().
589 m_visibleItems
.remove(i
);
590 widget
->setIndex(-1);
591 m_animation
->start(widget
, KItemListViewAnimation::DeleteAnimation
);
595 // Update the indexes of all KItemListWidget instances that are located
596 // after the deleted items
597 for (int i
= lastRemovedIndex
+ 1; i
<= lastIndex
; ++i
) {
598 KItemListWidget
* widget
= m_visibleItems
.value(i
);
600 const int newIndex
= i
- count
;
601 setWidgetIndex(widget
, newIndex
);
605 m_layouter
->markAsDirty();
606 if (!hasMultipleRanges
) {
607 doLayout(Animation
, index
, -count
);
613 m_controller
->selectionManager()->itemsRemoved(itemRanges
);
616 if (hasMultipleRanges
) {
621 void KItemListView::slotItemsChanged(const KItemRangeList
& itemRanges
,
622 const QSet
<QByteArray
>& roles
)
624 foreach (const KItemRange
& itemRange
, itemRanges
) {
625 const int index
= itemRange
.index
;
626 const int count
= itemRange
.count
;
628 m_sizeHintResolver
->itemsChanged(index
, count
, roles
);
630 const int lastIndex
= index
+ count
- 1;
631 for (int i
= index
; i
<= lastIndex
; ++i
) {
632 KItemListWidget
* widget
= m_visibleItems
.value(i
);
634 widget
->setData(m_model
->data(i
), roles
);
640 void KItemListView::slotCurrentChanged(int current
, int previous
)
644 KItemListWidget
* previousWidget
= m_visibleItems
.value(previous
, 0);
645 if (previousWidget
) {
646 Q_ASSERT(previousWidget
->isCurrent());
647 previousWidget
->setCurrent(false);
650 KItemListWidget
* currentWidget
= m_visibleItems
.value(current
, 0);
652 Q_ASSERT(!currentWidget
->isCurrent());
653 currentWidget
->setCurrent(true);
656 const QRectF viewGeometry
= geometry();
657 const QRectF currentBoundingRect
= itemBoundingRect(current
);
659 if (!viewGeometry
.contains(currentBoundingRect
)) {
660 // Make sure that the new current item is fully visible in the view.
661 qreal newOffset
= offset();
662 if (currentBoundingRect
.top() < viewGeometry
.top()) {
663 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
664 newOffset
+= currentBoundingRect
.top() - viewGeometry
.top();
666 else if ((currentBoundingRect
.bottom() > viewGeometry
.bottom())) {
667 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
668 newOffset
+= currentBoundingRect
.bottom() - viewGeometry
.bottom();
670 else if (currentBoundingRect
.left() < viewGeometry
.left()) {
671 if (scrollOrientation() == Qt::Horizontal
) {
672 newOffset
+= currentBoundingRect
.left() - viewGeometry
.left();
675 else if ((currentBoundingRect
.right() > viewGeometry
.right())) {
676 if (scrollOrientation() == Qt::Horizontal
) {
677 newOffset
+= currentBoundingRect
.right() - viewGeometry
.right();
681 emit
scrollTo(newOffset
);
685 void KItemListView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
689 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
690 while (it
.hasNext()) {
692 const int index
= it
.key();
693 KItemListWidget
* widget
= it
.value();
694 widget
->setSelected(current
.contains(index
));
698 void KItemListView::slotAnimationFinished(QGraphicsWidget
* widget
,
699 KItemListViewAnimation::AnimationType type
)
701 KItemListWidget
* itemListWidget
= qobject_cast
<KItemListWidget
*>(widget
);
702 Q_ASSERT(itemListWidget
);
705 case KItemListViewAnimation::DeleteAnimation
: {
706 // As we recycle the widget in this case it is important to assure that no
707 // other animation has been started. This is a convention in KItemListView and
708 // not a requirement defined by KItemListViewAnimation.
709 Q_ASSERT(!m_animation
->isStarted(itemListWidget
));
711 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
712 // by m_visibleWidgets and must be deleted manually after the animation has
714 KItemListGroupHeader
* header
= m_visibleGroups
.value(itemListWidget
);
716 m_groupHeaderCreator
->recycle(header
);
717 m_visibleGroups
.remove(itemListWidget
);
719 m_widgetCreator
->recycle(itemListWidget
);
723 case KItemListViewAnimation::CreateAnimation
:
724 case KItemListViewAnimation::MovingAnimation
:
725 case KItemListViewAnimation::ResizeAnimation
: {
726 const int index
= itemListWidget
->index();
727 const bool invisible
= (index
< m_layouter
->firstVisibleIndex()) ||
728 (index
> m_layouter
->lastVisibleIndex());
729 if (invisible
&& !m_animation
->isStarted(itemListWidget
)) {
730 recycleWidget(itemListWidget
);
739 void KItemListView::slotLayoutTimerFinished()
741 m_layouter
->setSize(geometry().size());
742 doLayout(Animation
, 0, 0);
745 void KItemListView::slotRubberBandStartPosChanged()
750 void KItemListView::slotRubberBandEndPosChanged()
752 triggerAutoScrolling();
756 void KItemListView::slotRubberBandActivationChanged(bool active
)
759 connect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandStartPosChanged()));
760 connect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandEndPosChanged()));
762 disconnect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandStartPosChanged()));
763 disconnect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandEndPosChanged()));
769 void KItemListView::setController(KItemListController
* controller
)
771 if (m_controller
!= controller
) {
772 KItemListController
* previous
= m_controller
;
774 KItemListSelectionManager
* selectionManager
= previous
->selectionManager();
775 disconnect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
776 disconnect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
779 m_controller
= controller
;
782 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
783 connect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
784 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
787 onControllerChanged(controller
, previous
);
791 void KItemListView::setModel(KItemModelBase
* model
)
793 if (m_model
== model
) {
797 KItemModelBase
* previous
= m_model
;
800 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
801 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
802 disconnect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
803 this, SLOT(slotItemsInserted(KItemRangeList
)));
804 disconnect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
805 this, SLOT(slotItemsRemoved(KItemRangeList
)));
809 m_layouter
->setModel(model
);
810 m_grouped
= !model
->groupRole().isEmpty();
813 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
814 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
815 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
816 this, SLOT(slotItemsInserted(KItemRangeList
)));
817 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
818 this, SLOT(slotItemsRemoved(KItemRangeList
)));
821 onModelChanged(model
, previous
);
824 KItemListRubberBand
* KItemListView::rubberBand() const
829 void KItemListView::updateLayout()
831 doLayout(Animation
, 0, 0);
835 void KItemListView::doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
)
837 if (m_layoutTimer
->isActive()) {
838 kDebug() << "Stopping layout timer, synchronous layout requested";
839 m_layoutTimer
->stop();
842 if (m_model
->count() < 0 || m_activeTransactions
> 0) {
846 applyDynamicItemSize();
848 const int firstVisibleIndex
= m_layouter
->firstVisibleIndex();
849 const int lastVisibleIndex
= m_layouter
->lastVisibleIndex();
850 if (firstVisibleIndex
< 0) {
855 // Do a sanity check of the offset-property: When properties of the itemlist-view have been changed
856 // it might be possible that the maximum offset got changed too. Assure that the full visible range
857 // is still shown if the maximum offset got decreased.
858 const qreal visibleOffsetRange
= (scrollOrientation() == Qt::Horizontal
) ? size().width() : size().height();
859 const qreal maxOffsetToShowFullRange
= maximumOffset() - visibleOffsetRange
;
860 if (offset() > maxOffsetToShowFullRange
) {
861 m_layouter
->setOffset(qMax(qreal(0), maxOffsetToShowFullRange
));
864 // Determine all items that are completely invisible and might be
865 // reused for items that just got (at least partly) visible.
866 // Items that do e.g. an animated moving of their position are not
867 // marked as invisible: This assures that a scrolling inside the view
868 // can be done without breaking an animation.
869 QList
<int> reusableItems
;
870 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
871 while (it
.hasNext()) {
873 KItemListWidget
* widget
= it
.value();
874 const int index
= widget
->index();
875 const bool invisible
= (index
< firstVisibleIndex
) || (index
> lastVisibleIndex
);
876 if (invisible
&& !m_animation
->isStarted(widget
)) {
877 widget
->setVisible(false);
878 reusableItems
.append(index
);
882 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
883 // instances from invisible items are reused. If no reusable items are
884 // found then new KItemListWidget instances get created.
885 const bool animate
= (hint
== Animation
);
886 for (int i
= firstVisibleIndex
; i
<= lastVisibleIndex
; ++i
) {
887 bool applyNewPos
= true;
888 bool wasHidden
= false;
890 const QRectF itemBounds
= m_layouter
->itemBoundingRect(i
);
891 const QPointF newPos
= itemBounds
.topLeft();
892 KItemListWidget
* widget
= m_visibleItems
.value(i
);
895 if (!reusableItems
.isEmpty()) {
896 // Reuse a KItemListWidget instance from an invisible item
897 const int oldIndex
= reusableItems
.takeLast();
898 widget
= m_visibleItems
.value(oldIndex
);
899 setWidgetIndex(widget
, i
);
901 // No reusable KItemListWidget instance is available, create a new one
902 widget
= createWidget(i
);
904 widget
->resize(itemBounds
.size());
906 if (animate
&& changedCount
< 0) {
907 // Items have been deleted, move the created item to the
908 // imaginary old position.
909 const QRectF itemBoundingRect
= m_layouter
->itemBoundingRect(i
- changedCount
);
910 if (itemBoundingRect
.isEmpty()) {
911 const QPointF invisibleOldPos
= (scrollOrientation() == Qt::Vertical
)
912 ? QPointF(0, size().height()) : QPointF(size().width(), 0);
913 widget
->setPos(invisibleOldPos
);
915 widget
->setPos(itemBoundingRect
.topLeft());
919 } else if (m_animation
->isStarted(widget
, KItemListViewAnimation::MovingAnimation
)) {
924 const bool itemsRemoved
= (changedCount
< 0);
925 const bool itemsInserted
= (changedCount
> 0);
927 if (itemsRemoved
&& (i
>= changedIndex
+ changedCount
+ 1)) {
928 // The item is located after the removed items. Animate the moving of the position.
929 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
931 } else if (itemsInserted
&& i
>= changedIndex
) {
932 // The item is located after the first inserted item
933 if (i
<= changedIndex
+ changedCount
- 1) {
934 // The item is an inserted item. Animate the appearing of the item.
935 // For performance reasons no animation is done when changedCount is equal
936 // to all available items.
937 if (changedCount
< m_model
->count()) {
938 m_animation
->start(widget
, KItemListViewAnimation::CreateAnimation
);
940 } else if (!m_animation
->isStarted(widget
, KItemListViewAnimation::CreateAnimation
)) {
941 // The item was already there before, so animate the moving of the position.
942 // No moving animation is done if the item is animated by a create animation: This
943 // prevents a "move animation mess" when inserting several ranges in parallel.
944 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
947 } else if (!itemsRemoved
&& !itemsInserted
&& !wasHidden
) {
948 // The size of the view might have been changed. Animate the moving of the position.
949 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
955 widget
->setPos(newPos
);
958 Q_ASSERT(widget
->index() == i
);
959 widget
->setVisible(true);
961 if (widget
->size() != itemBounds
.size()) {
962 m_animation
->start(widget
, KItemListViewAnimation::ResizeAnimation
, itemBounds
.size());
966 // Delete invisible KItemListWidget instances that have not been reused
967 foreach (int index
, reusableItems
) {
968 recycleWidget(m_visibleItems
.value(index
));
974 void KItemListView::emitOffsetChanges()
976 const qreal newOffset
= m_layouter
->offset();
977 if (m_oldOffset
!= newOffset
) {
978 emit
offsetChanged(newOffset
, m_oldOffset
);
979 m_oldOffset
= newOffset
;
982 const qreal newMaximumOffset
= m_layouter
->maximumOffset();
983 if (m_oldMaximumOffset
!= newMaximumOffset
) {
984 emit
maximumOffsetChanged(newMaximumOffset
, m_oldMaximumOffset
);
985 m_oldMaximumOffset
= newMaximumOffset
;
989 KItemListWidget
* KItemListView::createWidget(int index
)
991 KItemListWidget
* widget
= m_widgetCreator
->create(this);
992 updateWidgetProperties(widget
, index
);
993 m_visibleItems
.insert(index
, widget
);
996 if (m_layouter
->isFirstGroupItem(index
)) {
997 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
998 header
->setPos(0, -50);
999 header
->resize(50, 50);
1000 m_visibleGroups
.insert(widget
, header
);
1004 initializeItemListWidget(widget
);
1008 void KItemListView::recycleWidget(KItemListWidget
* widget
)
1011 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1013 m_groupHeaderCreator
->recycle(header
);
1014 m_visibleGroups
.remove(widget
);
1018 m_visibleItems
.remove(widget
->index());
1019 m_widgetCreator
->recycle(widget
);
1022 void KItemListView::setWidgetIndex(KItemListWidget
* widget
, int index
)
1025 bool createHeader
= m_layouter
->isFirstGroupItem(index
);
1026 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1029 createHeader
= false;
1031 m_groupHeaderCreator
->recycle(header
);
1032 m_visibleGroups
.remove(widget
);
1037 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1038 header
->setPos(0, -50);
1039 header
->resize(50, 50);
1040 m_visibleGroups
.insert(widget
, header
);
1044 const int oldIndex
= widget
->index();
1045 m_visibleItems
.remove(oldIndex
);
1046 updateWidgetProperties(widget
, index
);
1047 m_visibleItems
.insert(index
, widget
);
1049 initializeItemListWidget(widget
);
1052 void KItemListView::prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
)
1054 // Calculate the first visible index and last visible index for the current size
1055 const int currentFirst
= m_layouter
->firstVisibleIndex();
1056 const int currentLast
= m_layouter
->lastVisibleIndex();
1058 const QSizeF currentSize
= (sizeType
== LayouterSize
) ? m_layouter
->size() : m_layouter
->itemSize();
1060 // Calculate the first visible index and last visible index for the new size
1061 setLayouterSize(size
, sizeType
);
1062 const int newFirst
= m_layouter
->firstVisibleIndex();
1063 const int newLast
= m_layouter
->lastVisibleIndex();
1065 if ((currentFirst
!= newFirst
) || (currentLast
!= newLast
)) {
1066 // At least one index has been changed. Assure that widgets for all possible
1067 // visible items get created so that a move-animation can be started later.
1068 const int maxVisibleItems
= m_layouter
->maximumVisibleItems();
1069 int minFirst
= qMin(newFirst
, currentFirst
);
1070 const int maxLast
= qMax(newLast
, currentLast
);
1072 if (maxLast
- minFirst
+ 1 < maxVisibleItems
) {
1073 // Increasing the size might result in a smaller KItemListView::offset().
1074 // Decrease the first visible index in a way that at least the maximum
1075 // visible items are shown.
1076 minFirst
= qMax(0, maxLast
- maxVisibleItems
+ 1);
1079 if (maxLast
- minFirst
> maxVisibleItems
+ maxVisibleItems
/ 2) {
1080 // The creating of widgets is quite expensive. Assure that never more
1081 // than 50 % of the maximum visible items get created for the animations.
1085 setLayouterSize(currentSize
, sizeType
);
1086 for (int i
= minFirst
; i
<= maxLast
; ++i
) {
1087 if (!m_visibleItems
.contains(i
)) {
1088 KItemListWidget
* widget
= createWidget(i
);
1089 const QPointF pos
= m_layouter
->itemBoundingRect(i
).topLeft();
1090 widget
->setPos(pos
);
1093 setLayouterSize(size
, sizeType
);
1097 void KItemListView::setLayouterSize(const QSizeF
& size
, SizeType sizeType
)
1100 case LayouterSize
: m_layouter
->setSize(size
); break;
1101 case ItemSize
: m_layouter
->setItemSize(size
); break;
1106 bool KItemListView::markVisibleRolesSizesAsDirty()
1108 const bool dirty
= m_itemSize
.isEmpty();
1110 m_visibleRolesSizes
.clear();
1111 m_layouter
->setItemSize(QSizeF());
1116 void KItemListView::applyDynamicItemSize()
1118 if (!m_itemSize
.isEmpty()) {
1122 if (m_visibleRolesSizes
.isEmpty()) {
1123 m_visibleRolesSizes
= visibleRoleSizes();
1124 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
1125 widget
->setVisibleRolesSizes(m_visibleRolesSizes
);
1129 if (m_layouter
->itemSize().isEmpty()) {
1130 qreal requiredWidth
= 0;
1131 qreal requiredHeight
= 0;
1133 QHashIterator
<QByteArray
, QSizeF
> it(m_visibleRolesSizes
);
1134 while (it
.hasNext()) {
1136 const QSizeF
& visibleRoleSize
= it
.value();
1137 requiredWidth
+= visibleRoleSize
.width();
1138 requiredHeight
+= visibleRoleSize
.height();
1141 QSizeF dynamicItemSize
= m_itemSize
;
1142 if (dynamicItemSize
.width() <= 0) {
1143 dynamicItemSize
.setWidth(qMax(requiredWidth
, size().width()));
1145 if (dynamicItemSize
.height() <= 0) {
1146 dynamicItemSize
.setHeight(qMax(requiredHeight
, size().height()));
1149 m_layouter
->setItemSize(dynamicItemSize
);
1153 void KItemListView::updateWidgetProperties(KItemListWidget
* widget
, int index
)
1155 widget
->setVisibleRoles(m_visibleRoles
);
1156 widget
->setVisibleRolesSizes(m_visibleRolesSizes
);
1157 widget
->setStyleOption(m_styleOption
);
1159 const KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
1160 widget
->setCurrent(index
== selectionManager
->currentItem());
1162 if (selectionManager
->hasSelection()) {
1163 const QSet
<int> selectedItems
= selectionManager
->selectedItems();
1164 widget
->setSelected(selectedItems
.contains(index
));
1166 widget
->setSelected(false);
1169 widget
->setHovered(false);
1171 widget
->setIndex(index
);
1172 widget
->setData(m_model
->data(index
));
1175 void KItemListView::triggerAutoScrolling()
1177 const int pos
= (scrollOrientation() == Qt::Vertical
) ? m_mousePos
.y() : m_mousePos
.x();
1178 const int inc
= calculateAutoScrollingIncrement(pos
, size().height());
1180 emit
scrollTo(offset() + inc
);
1184 int KItemListView::calculateAutoScrollingIncrement(int pos
, int size
)
1188 const int minSpeed
= 4;
1189 const int maxSpeed
= 768;
1190 const int speedLimiter
= 48;
1191 const int autoScrollBorder
= 64;
1193 if (pos
< autoScrollBorder
) {
1194 inc
= -minSpeed
+ qAbs(pos
- autoScrollBorder
) * (pos
- autoScrollBorder
) / speedLimiter
;
1195 if (inc
< -maxSpeed
) {
1198 } else if (pos
> size
- autoScrollBorder
) {
1199 inc
= minSpeed
+ qAbs(pos
- size
+ autoScrollBorder
) * (pos
- size
+ autoScrollBorder
) / speedLimiter
;
1200 if (inc
> maxSpeed
) {
1209 KItemListCreatorBase::~KItemListCreatorBase()
1211 qDeleteAll(m_recycleableWidgets
);
1212 qDeleteAll(m_createdWidgets
);
1215 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget
* widget
)
1217 m_createdWidgets
.insert(widget
);
1220 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget
* widget
)
1222 Q_ASSERT(m_createdWidgets
.contains(widget
));
1223 m_createdWidgets
.remove(widget
);
1225 if (m_recycleableWidgets
.count() < 100) {
1226 m_recycleableWidgets
.append(widget
);
1227 widget
->setVisible(false);
1233 QGraphicsWidget
* KItemListCreatorBase::popRecycleableWidget()
1235 if (m_recycleableWidgets
.isEmpty()) {
1239 QGraphicsWidget
* widget
= m_recycleableWidgets
.takeLast();
1240 m_createdWidgets
.insert(widget
);
1244 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
1248 void KItemListWidgetCreatorBase::recycle(KItemListWidget
* widget
)
1250 widget
->setOpacity(1.0);
1251 pushRecycleableWidget(widget
);
1254 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
1258 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader
* header
)
1260 header
->setOpacity(1.0);
1261 pushRecycleableWidget(header
);
1264 #include "kitemlistview.moc"