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();
122 onScrollOrientationChanged(orientation
, previousOrientation
);
125 Qt::Orientation
KItemListView::scrollOrientation() const
127 return m_layouter
->scrollOrientation();
130 void KItemListView::setItemSize(const QSizeF
& itemSize
)
132 const QSizeF previousSize
= m_itemSize
;
133 if (itemSize
== previousSize
) {
137 m_itemSize
= itemSize
;
139 if (itemSize
.isEmpty()) {
140 updateVisibleRolesSizes();
143 if (itemSize
.width() < previousSize
.width() || itemSize
.height() < previousSize
.height()) {
144 prepareLayoutForIncreasedItemCount(itemSize
, ItemSize
);
146 m_layouter
->setItemSize(itemSize
);
149 m_sizeHintResolver
->clearCache();
151 onItemSizeChanged(itemSize
, previousSize
);
154 QSizeF
KItemListView::itemSize() const
159 void KItemListView::setScrollOffset(qreal offset
)
165 const qreal previousOffset
= m_layouter
->scrollOffset();
166 if (offset
== previousOffset
) {
170 m_layouter
->setScrollOffset(offset
);
171 m_animation
->setScrollOffset(offset
);
172 if (!m_layoutTimer
->isActive()) {
173 doLayout(NoAnimation
, 0, 0);
176 onScrollOffsetChanged(offset
, previousOffset
);
179 qreal
KItemListView::scrollOffset() const
181 return m_layouter
->scrollOffset();
184 qreal
KItemListView::maximumScrollOffset() const
186 return m_layouter
->maximumScrollOffset();
189 void KItemListView::setItemOffset(qreal offset
)
191 m_layouter
->setItemOffset(offset
);
194 qreal
KItemListView::itemOffset() const
196 return m_layouter
->itemOffset();
199 qreal
KItemListView::maximumItemOffset() const
201 return m_layouter
->maximumItemOffset();
204 void KItemListView::setVisibleRoles(const QList
<QByteArray
>& roles
)
206 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
207 m_visibleRoles
= roles
;
209 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
210 while (it
.hasNext()) {
212 KItemListWidget
* widget
= it
.value();
213 widget
->setVisibleRoles(roles
);
214 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
217 m_sizeHintResolver
->clearCache();
218 m_layouter
->markAsDirty();
219 onVisibleRolesChanged(roles
, previousRoles
);
221 updateVisibleRolesSizes();
225 m_header
->setVisibleRoles(roles
);
226 m_header
->setVisibleRolesWidths(headerRolesWidths());
227 m_useHeaderWidths
= false;
231 QList
<QByteArray
> KItemListView::visibleRoles() const
233 return m_visibleRoles
;
236 void KItemListView::setAutoScroll(bool enabled
)
238 if (enabled
&& !m_autoScrollTimer
) {
239 m_autoScrollTimer
= new QTimer(this);
240 m_autoScrollTimer
->setSingleShot(false);
241 connect(m_autoScrollTimer
, SIGNAL(timeout()), this, SLOT(triggerAutoScrolling()));
242 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
243 } else if (!enabled
&& m_autoScrollTimer
) {
244 delete m_autoScrollTimer
;
245 m_autoScrollTimer
= 0;
250 bool KItemListView::autoScroll() const
252 return m_autoScrollTimer
!= 0;
255 void KItemListView::setHeaderShown(bool show
)
257 if (show
&& !m_header
) {
258 m_header
= new KItemListHeader(this);
259 m_header
->setPos(0, 0);
260 m_header
->setModel(m_model
);
261 m_header
->setVisibleRoles(m_visibleRoles
);
262 m_header
->setVisibleRolesWidths(headerRolesWidths());
263 m_header
->setZValue(1);
265 m_useHeaderWidths
= false;
268 connect(m_header
, SIGNAL(visibleRoleWidthChanged(QByteArray
,qreal
,qreal
)),
269 this, SLOT(slotVisibleRoleWidthChanged(QByteArray
,qreal
,qreal
)));
271 m_layouter
->setHeaderHeight(m_header
->size().height());
272 } else if (!show
&& m_header
) {
275 m_useHeaderWidths
= false;
276 m_layouter
->setHeaderHeight(0);
280 bool KItemListView::isHeaderShown() const
282 return m_header
!= 0;
285 KItemListController
* KItemListView::controller() const
290 KItemModelBase
* KItemListView::model() const
295 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
)
297 m_widgetCreator
= widgetCreator
;
300 KItemListWidgetCreatorBase
* KItemListView::widgetCreator() const
302 return m_widgetCreator
;
305 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
)
307 m_groupHeaderCreator
= groupHeaderCreator
;
310 KItemListGroupHeaderCreatorBase
* KItemListView::groupHeaderCreator() const
312 return m_groupHeaderCreator
;
315 void KItemListView::setStyleOption(const KItemListStyleOption
& option
)
317 const KItemListStyleOption previousOption
= m_styleOption
;
318 m_styleOption
= option
;
320 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
321 while (it
.hasNext()) {
323 it
.value()->setStyleOption(option
);
326 m_sizeHintResolver
->clearCache();
328 onStyleOptionChanged(option
, previousOption
);
331 const KItemListStyleOption
& KItemListView::styleOption() const
333 return m_styleOption
;
336 void KItemListView::setGeometry(const QRectF
& rect
)
338 QGraphicsWidget::setGeometry(rect
);
344 if (m_model
->count() > 0) {
345 prepareLayoutForIncreasedItemCount(rect
.size(), LayouterSize
);
347 m_layouter
->setSize(rect
.size());
350 if (!m_layoutTimer
->isActive()) {
351 m_layoutTimer
->start();
354 // Changing the geometry does not require to do an expensive
355 // update of the visible-roles sizes, only the stretched sizes
356 // need to be adjusted to the new size.
357 updateStretchedVisibleRolesSizes();
360 int KItemListView::itemAt(const QPointF
& pos
) const
362 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
363 while (it
.hasNext()) {
366 const KItemListWidget
* widget
= it
.value();
367 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
368 if (widget
->contains(mappedPos
)) {
376 bool KItemListView::isAboveSelectionToggle(int index
, const QPointF
& pos
) const
383 bool KItemListView::isAboveExpansionToggle(int index
, const QPointF
& pos
) const
385 const KItemListWidget
* widget
= m_visibleItems
.value(index
);
387 const QRectF expansionToggleRect
= widget
->expansionToggleRect();
388 if (!expansionToggleRect
.isEmpty()) {
389 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
390 return expansionToggleRect
.contains(mappedPos
);
396 int KItemListView::firstVisibleIndex() const
398 return m_layouter
->firstVisibleIndex();
401 int KItemListView::lastVisibleIndex() const
403 return m_layouter
->lastVisibleIndex();
406 QSizeF
KItemListView::itemSizeHint(int index
) const
412 QHash
<QByteArray
, QSizeF
> KItemListView::visibleRolesSizes(const KItemRangeList
& itemRanges
) const
414 Q_UNUSED(itemRanges
);
415 return QHash
<QByteArray
, QSizeF
>();
418 QRectF
KItemListView::itemBoundingRect(int index
) const
420 return m_layouter
->itemBoundingRect(index
);
423 int KItemListView::itemsPerOffset() const
425 return m_layouter
->itemsPerOffset();
428 void KItemListView::beginTransaction()
430 ++m_activeTransactions
;
431 if (m_activeTransactions
== 1) {
432 onTransactionBegin();
436 void KItemListView::endTransaction()
438 --m_activeTransactions
;
439 if (m_activeTransactions
< 0) {
440 m_activeTransactions
= 0;
441 kWarning() << "Mismatch between beginTransaction()/endTransaction()";
444 if (m_activeTransactions
== 0) {
450 bool KItemListView::isTransactionActive() const
452 return m_activeTransactions
> 0;
455 QPixmap
KItemListView::createDragPixmap(const QSet
<int>& indexes
) const
461 void KItemListView::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
463 QGraphicsWidget::paint(painter
, option
, widget
);
465 if (m_rubberBand
->isActive()) {
466 QRectF rubberBandRect
= QRectF(m_rubberBand
->startPosition(),
467 m_rubberBand
->endPosition()).normalized();
469 const QPointF topLeft
= rubberBandRect
.topLeft();
470 if (scrollOrientation() == Qt::Vertical
) {
471 rubberBandRect
.moveTo(topLeft
.x(), topLeft
.y() - scrollOffset());
473 rubberBandRect
.moveTo(topLeft
.x() - scrollOffset(), topLeft
.y());
476 QStyleOptionRubberBand opt
;
477 opt
.initFrom(widget
);
478 opt
.shape
= QRubberBand::Rectangle
;
480 opt
.rect
= rubberBandRect
.toRect();
481 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
485 void KItemListView::initializeItemListWidget(KItemListWidget
* item
)
490 bool KItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
492 Q_UNUSED(changedRoles
);
496 void KItemListView::onControllerChanged(KItemListController
* current
, KItemListController
* previous
)
502 void KItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
508 void KItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
514 void KItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
520 void KItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
526 void KItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
532 void KItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
538 void KItemListView::onTransactionBegin()
542 void KItemListView::onTransactionEnd()
546 bool KItemListView::event(QEvent
* event
)
548 // Forward all events to the controller and handle them there
549 if (m_controller
&& m_controller
->processEvent(event
, transform())) {
553 return QGraphicsWidget::event(event
);
556 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent
* event
)
558 m_mousePos
= transform().map(event
->pos());
562 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
)
564 QGraphicsWidget::mouseMoveEvent(event
);
566 m_mousePos
= transform().map(event
->pos());
567 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
568 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
572 void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
)
574 event
->setAccepted(true);
578 void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent
*event
)
580 QGraphicsWidget::dragMoveEvent(event
);
582 m_mousePos
= transform().map(event
->pos());
583 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
584 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
588 void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
)
590 QGraphicsWidget::dragLeaveEvent(event
);
591 setAutoScroll(false);
594 void KItemListView::dropEvent(QGraphicsSceneDragDropEvent
* event
)
596 QGraphicsWidget::dropEvent(event
);
597 setAutoScroll(false);
600 QList
<KItemListWidget
*> KItemListView::visibleItemListWidgets() const
602 return m_visibleItems
.values();
605 void KItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
607 QGraphicsWidget::resizeEvent(event
);
611 void KItemListView::slotItemsInserted(const KItemRangeList
& itemRanges
)
613 updateVisibleRolesSizes(itemRanges
);
615 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
616 if (hasMultipleRanges
) {
620 int previouslyInsertedCount
= 0;
621 foreach (const KItemRange
& range
, itemRanges
) {
622 // range.index is related to the model before anything has been inserted.
623 // As in each loop the current item-range gets inserted the index must
624 // be increased by the already previously inserted items.
625 const int index
= range
.index
+ previouslyInsertedCount
;
626 const int count
= range
.count
;
627 if (index
< 0 || count
<= 0) {
628 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
631 previouslyInsertedCount
+= count
;
633 m_sizeHintResolver
->itemsInserted(index
, count
);
635 // Determine which visible items must be moved
636 QList
<int> itemsToMove
;
637 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
638 while (it
.hasNext()) {
640 const int visibleItemIndex
= it
.key();
641 if (visibleItemIndex
>= index
) {
642 itemsToMove
.append(visibleItemIndex
);
646 // Update the indexes of all KItemListWidget instances that are located
647 // after the inserted items. It is important to adjust the indexes in the order
648 // from the highest index to the lowest index to prevent overlaps when setting the new index.
650 for (int i
= itemsToMove
.count() - 1; i
>= 0; --i
) {
651 KItemListWidget
* widget
= m_visibleItems
.value(itemsToMove
[i
]);
653 setWidgetIndex(widget
, widget
->index() + count
);
656 m_layouter
->markAsDirty();
657 if (m_model
->count() == count
&& maximumScrollOffset() > size().height()) {
658 kDebug() << "Scrollbar required, skipping layout";
659 const int scrollBarExtent
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
660 QSizeF layouterSize
= m_layouter
->size();
661 if (scrollOrientation() == Qt::Vertical
) {
662 layouterSize
.rwidth() -= scrollBarExtent
;
664 layouterSize
.rheight() -= scrollBarExtent
;
666 m_layouter
->setSize(layouterSize
);
669 if (!hasMultipleRanges
) {
670 doLayout(Animation
, index
, count
);
676 m_controller
->selectionManager()->itemsInserted(itemRanges
);
679 if (hasMultipleRanges
) {
684 void KItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
686 updateVisibleRolesSizes();
688 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
689 if (hasMultipleRanges
) {
693 for (int i
= itemRanges
.count() - 1; i
>= 0; --i
) {
694 const KItemRange
& range
= itemRanges
.at(i
);
695 const int index
= range
.index
;
696 const int count
= range
.count
;
697 if (index
< 0 || count
<= 0) {
698 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
702 m_sizeHintResolver
->itemsRemoved(index
, count
);
704 const int firstRemovedIndex
= index
;
705 const int lastRemovedIndex
= index
+ count
- 1;
706 const int lastIndex
= m_model
->count() + count
- 1;
708 // Remove all KItemListWidget instances that got deleted
709 for (int i
= firstRemovedIndex
; i
<= lastRemovedIndex
; ++i
) {
710 KItemListWidget
* widget
= m_visibleItems
.value(i
);
715 m_animation
->stop(widget
);
716 // Stopping the animation might lead to recycling the widget if
717 // it is invisible (see slotAnimationFinished()).
718 // Check again whether it is still visible:
719 if (!m_visibleItems
.contains(i
)) {
723 if (m_model
->count() == 0) {
724 // For performance reasons no animation is done when all items have
726 recycleWidget(widget
);
728 // Animate the removing of the items. Special case: When removing an item there
729 // is no valid model index available anymore. For the
730 // remove-animation the item gets removed from m_visibleItems but the widget
731 // will stay alive until the animation has been finished and will
732 // be recycled (deleted) in KItemListView::slotAnimationFinished().
733 m_visibleItems
.remove(i
);
734 widget
->setIndex(-1);
735 m_animation
->start(widget
, KItemListViewAnimation::DeleteAnimation
);
739 // Update the indexes of all KItemListWidget instances that are located
740 // after the deleted items
741 for (int i
= lastRemovedIndex
+ 1; i
<= lastIndex
; ++i
) {
742 KItemListWidget
* widget
= m_visibleItems
.value(i
);
744 const int newIndex
= i
- count
;
745 setWidgetIndex(widget
, newIndex
);
749 m_layouter
->markAsDirty();
750 if (!hasMultipleRanges
) {
751 doLayout(Animation
, index
, -count
);
757 m_controller
->selectionManager()->itemsRemoved(itemRanges
);
760 if (hasMultipleRanges
) {
765 void KItemListView::slotItemsMoved(const KItemRange
& itemRange
, const QList
<int>& movedToIndexes
)
768 // * Implement KItemListView::slotItemsMoved() (which should call KItemListSelectionManager::itemsMoved())
769 // * Do not emit itemsRemoved()/itemsInserted() in KFileItemModel::resortAllItems()
771 Q_UNUSED(movedToIndexes
);
774 void KItemListView::slotItemsChanged(const KItemRangeList
& itemRanges
,
775 const QSet
<QByteArray
>& roles
)
777 const bool updateSizeHints
= itemSizeHintUpdateRequired(roles
);
778 if (updateSizeHints
) {
779 updateVisibleRolesSizes(itemRanges
);
782 foreach (const KItemRange
& itemRange
, itemRanges
) {
783 const int index
= itemRange
.index
;
784 const int count
= itemRange
.count
;
786 if (updateSizeHints
) {
787 m_sizeHintResolver
->itemsChanged(index
, count
, roles
);
788 m_layouter
->markAsDirty();
789 if (!m_layoutTimer
->isActive()) {
790 m_layoutTimer
->start();
794 // Apply the changed roles to the visible item-widgets
795 const int lastIndex
= index
+ count
- 1;
796 for (int i
= index
; i
<= lastIndex
; ++i
) {
797 KItemListWidget
* widget
= m_visibleItems
.value(i
);
799 widget
->setData(m_model
->data(i
), roles
);
806 void KItemListView::slotCurrentChanged(int current
, int previous
)
810 KItemListWidget
* previousWidget
= m_visibleItems
.value(previous
, 0);
811 if (previousWidget
) {
812 Q_ASSERT(previousWidget
->isCurrent());
813 previousWidget
->setCurrent(false);
816 KItemListWidget
* currentWidget
= m_visibleItems
.value(current
, 0);
818 Q_ASSERT(!currentWidget
->isCurrent());
819 currentWidget
->setCurrent(true);
822 const QRectF viewGeometry
= geometry();
823 const QRectF currentBoundingRect
= itemBoundingRect(current
);
825 if (!viewGeometry
.contains(currentBoundingRect
)) {
826 // Make sure that the new current item is fully visible in the view.
827 qreal newOffset
= scrollOffset();
828 if (currentBoundingRect
.top() < viewGeometry
.top()) {
829 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
830 newOffset
+= currentBoundingRect
.top() - viewGeometry
.top();
831 } else if ((currentBoundingRect
.bottom() > viewGeometry
.bottom())) {
832 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
833 newOffset
+= currentBoundingRect
.bottom() - viewGeometry
.bottom();
834 } else if (currentBoundingRect
.left() < viewGeometry
.left()) {
835 if (scrollOrientation() == Qt::Horizontal
) {
836 newOffset
+= currentBoundingRect
.left() - viewGeometry
.left();
838 } else if ((currentBoundingRect
.right() > viewGeometry
.right())) {
839 if (scrollOrientation() == Qt::Horizontal
) {
840 newOffset
+= currentBoundingRect
.right() - viewGeometry
.right();
844 if (newOffset
!= scrollOffset()) {
845 emit
scrollTo(newOffset
);
850 void KItemListView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
854 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
855 while (it
.hasNext()) {
857 const int index
= it
.key();
858 KItemListWidget
* widget
= it
.value();
859 widget
->setSelected(current
.contains(index
));
863 void KItemListView::slotAnimationFinished(QGraphicsWidget
* widget
,
864 KItemListViewAnimation::AnimationType type
)
866 KItemListWidget
* itemListWidget
= qobject_cast
<KItemListWidget
*>(widget
);
867 Q_ASSERT(itemListWidget
);
870 case KItemListViewAnimation::DeleteAnimation
: {
871 // As we recycle the widget in this case it is important to assure that no
872 // other animation has been started. This is a convention in KItemListView and
873 // not a requirement defined by KItemListViewAnimation.
874 Q_ASSERT(!m_animation
->isStarted(itemListWidget
));
876 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
877 // by m_visibleWidgets and must be deleted manually after the animation has
879 KItemListGroupHeader
* header
= m_visibleGroups
.value(itemListWidget
);
881 m_groupHeaderCreator
->recycle(header
);
882 m_visibleGroups
.remove(itemListWidget
);
884 m_widgetCreator
->recycle(itemListWidget
);
888 case KItemListViewAnimation::CreateAnimation
:
889 case KItemListViewAnimation::MovingAnimation
:
890 case KItemListViewAnimation::ResizeAnimation
: {
891 const int index
= itemListWidget
->index();
892 const bool invisible
= (index
< m_layouter
->firstVisibleIndex()) ||
893 (index
> m_layouter
->lastVisibleIndex());
894 if (invisible
&& !m_animation
->isStarted(itemListWidget
)) {
895 recycleWidget(itemListWidget
);
904 void KItemListView::slotLayoutTimerFinished()
906 m_layouter
->setSize(geometry().size());
907 doLayout(Animation
, 0, 0);
910 void KItemListView::slotRubberBandPosChanged()
915 void KItemListView::slotRubberBandActivationChanged(bool active
)
918 connect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
919 connect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
920 m_skipAutoScrollForRubberBand
= true;
922 disconnect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
923 disconnect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
924 m_skipAutoScrollForRubberBand
= false;
930 void KItemListView::slotVisibleRoleWidthChanged(const QByteArray
& role
,
934 Q_UNUSED(previousWidth
);
936 m_useHeaderWidths
= true;
938 if (m_visibleRolesSizes
.contains(role
)) {
939 QSizeF roleSize
= m_visibleRolesSizes
.value(role
);
940 roleSize
.setWidth(currentWidth
);
941 m_visibleRolesSizes
.insert(role
, roleSize
);
942 m_stretchedVisibleRolesSizes
.insert(role
, roleSize
);
944 updateVisibleRolesSizes();
949 void KItemListView::triggerAutoScrolling()
951 if (!m_autoScrollTimer
) {
957 if (scrollOrientation() == Qt::Vertical
) {
958 pos
= m_mousePos
.y();
959 visibleSize
= size().height();
961 pos
= m_mousePos
.x();
962 visibleSize
= size().width();
965 if (m_autoScrollTimer
->interval() == InitialAutoScrollDelay
) {
966 m_autoScrollIncrement
= 0;
969 m_autoScrollIncrement
= calculateAutoScrollingIncrement(pos
, visibleSize
, m_autoScrollIncrement
);
970 if (m_autoScrollIncrement
== 0) {
971 // The mouse position is not above an autoscroll margin (the autoscroll timer
972 // will be restarted in mouseMoveEvent())
973 m_autoScrollTimer
->stop();
977 if (m_rubberBand
->isActive() && m_skipAutoScrollForRubberBand
) {
978 // If a rubberband selection is ongoing the autoscrolling may only get triggered
979 // if the direction of the rubberband is similar to the autoscroll direction. This
980 // prevents that starting to create a rubberband within the autoscroll margins starts
983 const qreal minDiff
= 4; // Ignore any autoscrolling if the rubberband is very small
984 const qreal diff
= (scrollOrientation() == Qt::Vertical
)
985 ? m_rubberBand
->endPosition().y() - m_rubberBand
->startPosition().y()
986 : m_rubberBand
->endPosition().x() - m_rubberBand
->startPosition().x();
987 if (qAbs(diff
) < minDiff
|| (m_autoScrollIncrement
< 0 && diff
> 0) || (m_autoScrollIncrement
> 0 && diff
< 0)) {
988 // The rubberband direction is different from the scroll direction (e.g. the rubberband has
989 // been moved up although the autoscroll direction might be down)
990 m_autoScrollTimer
->stop();
995 // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
996 // the autoscrolling may not get skipped anymore until a new rubberband is created
997 m_skipAutoScrollForRubberBand
= false;
999 setScrollOffset(scrollOffset() + m_autoScrollIncrement
);
1001 // Trigger the autoscroll timer which will periodically call
1002 // triggerAutoScrolling()
1003 m_autoScrollTimer
->start(RepeatingAutoScrollDelay
);
1006 void KItemListView::setController(KItemListController
* controller
)
1008 if (m_controller
!= controller
) {
1009 KItemListController
* previous
= m_controller
;
1011 KItemListSelectionManager
* selectionManager
= previous
->selectionManager();
1012 disconnect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1013 disconnect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
1016 m_controller
= controller
;
1019 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
1020 connect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1021 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
1024 onControllerChanged(controller
, previous
);
1028 void KItemListView::setModel(KItemModelBase
* model
)
1030 if (m_model
== model
) {
1034 KItemModelBase
* previous
= m_model
;
1037 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1038 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1039 disconnect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
1040 this, SLOT(slotItemsInserted(KItemRangeList
)));
1041 disconnect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
1042 this, SLOT(slotItemsRemoved(KItemRangeList
)));
1043 disconnect(m_model
, SIGNAL(itemsMoved(KItemRangeList
,QList
<int>)),
1044 this, SLOT(slotItemsMoved(KItemRangeList
,QList
<int>)));
1048 m_layouter
->setModel(model
);
1049 m_grouped
= !model
->groupRole().isEmpty();
1052 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1053 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1054 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
1055 this, SLOT(slotItemsInserted(KItemRangeList
)));
1056 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
1057 this, SLOT(slotItemsRemoved(KItemRangeList
)));
1058 connect(m_model
, SIGNAL(itemsMoved(KItemRangeList
,QList
<int>)),
1059 this, SLOT(slotItemsMoved(KItemRangeList
,QList
<int>)));
1062 onModelChanged(model
, previous
);
1065 KItemListRubberBand
* KItemListView::rubberBand() const
1067 return m_rubberBand
;
1070 void KItemListView::updateLayout()
1072 doLayout(Animation
, 0, 0);
1076 void KItemListView::doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
)
1078 if (m_layoutTimer
->isActive()) {
1079 kDebug() << "Stopping layout timer, synchronous layout requested";
1080 m_layoutTimer
->stop();
1083 if (m_model
->count() < 0 || m_activeTransactions
> 0) {
1087 //markVisibleRolesSizesAsDirty();
1089 const int firstVisibleIndex
= m_layouter
->firstVisibleIndex();
1090 const int lastVisibleIndex
= m_layouter
->lastVisibleIndex();
1091 if (firstVisibleIndex
< 0) {
1092 emitOffsetChanges();
1096 // Do a sanity check of the scroll-offset property: When properties of the itemlist-view have been changed
1097 // it might be possible that the maximum offset got changed too. Assure that the full visible range
1098 // is still shown if the maximum offset got decreased.
1099 const qreal visibleOffsetRange
= (scrollOrientation() == Qt::Horizontal
) ? size().width() : size().height();
1100 const qreal maxOffsetToShowFullRange
= maximumScrollOffset() - visibleOffsetRange
;
1101 if (scrollOffset() > maxOffsetToShowFullRange
) {
1102 m_layouter
->setScrollOffset(qMax(qreal(0), maxOffsetToShowFullRange
));
1105 // Determine all items that are completely invisible and might be
1106 // reused for items that just got (at least partly) visible.
1107 // Items that do e.g. an animated moving of their position are not
1108 // marked as invisible: This assures that a scrolling inside the view
1109 // can be done without breaking an animation.
1110 QList
<int> reusableItems
;
1111 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
1112 while (it
.hasNext()) {
1114 KItemListWidget
* widget
= it
.value();
1115 const int index
= widget
->index();
1116 const bool invisible
= (index
< firstVisibleIndex
) || (index
> lastVisibleIndex
);
1117 if (invisible
&& !m_animation
->isStarted(widget
)) {
1118 widget
->setVisible(false);
1119 reusableItems
.append(index
);
1123 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
1124 // instances from invisible items are reused. If no reusable items are
1125 // found then new KItemListWidget instances get created.
1126 const bool animate
= (hint
== Animation
);
1127 for (int i
= firstVisibleIndex
; i
<= lastVisibleIndex
; ++i
) {
1128 bool applyNewPos
= true;
1129 bool wasHidden
= false;
1131 const QRectF itemBounds
= m_layouter
->itemBoundingRect(i
);
1132 const QPointF newPos
= itemBounds
.topLeft();
1133 KItemListWidget
* widget
= m_visibleItems
.value(i
);
1136 if (!reusableItems
.isEmpty()) {
1137 // Reuse a KItemListWidget instance from an invisible item
1138 const int oldIndex
= reusableItems
.takeLast();
1139 widget
= m_visibleItems
.value(oldIndex
);
1140 setWidgetIndex(widget
, i
);
1142 // No reusable KItemListWidget instance is available, create a new one
1143 widget
= createWidget(i
);
1145 widget
->resize(itemBounds
.size());
1147 if (animate
&& changedCount
< 0) {
1148 // Items have been deleted, move the created item to the
1149 // imaginary old position.
1150 const QRectF itemBoundingRect
= m_layouter
->itemBoundingRect(i
- changedCount
);
1151 if (itemBoundingRect
.isEmpty()) {
1152 const QPointF invisibleOldPos
= (scrollOrientation() == Qt::Vertical
)
1153 ? QPointF(0, size().height()) : QPointF(size().width(), 0);
1154 widget
->setPos(invisibleOldPos
);
1156 widget
->setPos(itemBoundingRect
.topLeft());
1158 applyNewPos
= false;
1160 } else if (m_animation
->isStarted(widget
, KItemListViewAnimation::MovingAnimation
)) {
1161 applyNewPos
= false;
1165 const bool itemsRemoved
= (changedCount
< 0);
1166 const bool itemsInserted
= (changedCount
> 0);
1168 if (itemsRemoved
&& (i
>= changedIndex
+ changedCount
+ 1)) {
1169 // The item is located after the removed items. Animate the moving of the position.
1170 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1171 applyNewPos
= false;
1172 } else if (itemsInserted
&& i
>= changedIndex
) {
1173 // The item is located after the first inserted item
1174 if (i
<= changedIndex
+ changedCount
- 1) {
1175 // The item is an inserted item. Animate the appearing of the item.
1176 // For performance reasons no animation is done when changedCount is equal
1177 // to all available items.
1178 if (changedCount
< m_model
->count()) {
1179 m_animation
->start(widget
, KItemListViewAnimation::CreateAnimation
);
1181 } else if (!m_animation
->isStarted(widget
, KItemListViewAnimation::CreateAnimation
)) {
1182 // The item was already there before, so animate the moving of the position.
1183 // No moving animation is done if the item is animated by a create animation: This
1184 // prevents a "move animation mess" when inserting several ranges in parallel.
1185 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1186 applyNewPos
= false;
1188 } else if (!itemsRemoved
&& !itemsInserted
&& !wasHidden
) {
1189 // The size of the view might have been changed. Animate the moving of the position.
1190 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1191 applyNewPos
= false;
1196 widget
->setPos(newPos
);
1199 Q_ASSERT(widget
->index() == i
);
1200 widget
->setVisible(true);
1202 if (widget
->size() != itemBounds
.size()) {
1203 m_animation
->start(widget
, KItemListViewAnimation::ResizeAnimation
, itemBounds
.size());
1207 // Delete invisible KItemListWidget instances that have not been reused
1208 foreach (int index
, reusableItems
) {
1209 recycleWidget(m_visibleItems
.value(index
));
1212 emitOffsetChanges();
1215 void KItemListView::emitOffsetChanges()
1217 const qreal newScrollOffset
= m_layouter
->scrollOffset();
1218 if (m_oldScrollOffset
!= newScrollOffset
) {
1219 emit
scrollOffsetChanged(newScrollOffset
, m_oldScrollOffset
);
1220 m_oldScrollOffset
= newScrollOffset
;
1223 const qreal newMaximumScrollOffset
= m_layouter
->maximumScrollOffset();
1224 if (m_oldMaximumScrollOffset
!= newMaximumScrollOffset
) {
1225 emit
maximumScrollOffsetChanged(newMaximumScrollOffset
, m_oldMaximumScrollOffset
);
1226 m_oldMaximumScrollOffset
= newMaximumScrollOffset
;
1229 const qreal newItemOffset
= m_layouter
->itemOffset();
1230 if (m_oldItemOffset
!= newItemOffset
) {
1231 emit
itemOffsetChanged(newItemOffset
, m_oldItemOffset
);
1232 m_oldItemOffset
= newItemOffset
;
1235 const qreal newMaximumItemOffset
= m_layouter
->maximumItemOffset();
1236 if (m_oldMaximumItemOffset
!= newMaximumItemOffset
) {
1237 emit
maximumItemOffsetChanged(newMaximumItemOffset
, m_oldMaximumItemOffset
);
1238 m_oldMaximumItemOffset
= newMaximumItemOffset
;
1242 KItemListWidget
* KItemListView::createWidget(int index
)
1244 KItemListWidget
* widget
= m_widgetCreator
->create(this);
1245 updateWidgetProperties(widget
, index
);
1246 m_visibleItems
.insert(index
, widget
);
1249 if (m_layouter
->isFirstGroupItem(index
)) {
1250 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1251 header
->setPos(0, -50);
1252 header
->resize(50, 50);
1253 m_visibleGroups
.insert(widget
, header
);
1257 initializeItemListWidget(widget
);
1261 void KItemListView::recycleWidget(KItemListWidget
* widget
)
1264 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1266 m_groupHeaderCreator
->recycle(header
);
1267 m_visibleGroups
.remove(widget
);
1271 m_visibleItems
.remove(widget
->index());
1272 m_widgetCreator
->recycle(widget
);
1275 void KItemListView::setWidgetIndex(KItemListWidget
* widget
, int index
)
1278 bool createHeader
= m_layouter
->isFirstGroupItem(index
);
1279 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1282 createHeader
= false;
1284 m_groupHeaderCreator
->recycle(header
);
1285 m_visibleGroups
.remove(widget
);
1290 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1291 header
->setPos(0, -50);
1292 header
->resize(50, 50);
1293 m_visibleGroups
.insert(widget
, header
);
1297 const int oldIndex
= widget
->index();
1298 m_visibleItems
.remove(oldIndex
);
1299 updateWidgetProperties(widget
, index
);
1300 m_visibleItems
.insert(index
, widget
);
1302 initializeItemListWidget(widget
);
1305 void KItemListView::prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
)
1307 // Calculate the first visible index and last visible index for the current size
1308 const int currentFirst
= m_layouter
->firstVisibleIndex();
1309 const int currentLast
= m_layouter
->lastVisibleIndex();
1311 const QSizeF currentSize
= (sizeType
== LayouterSize
) ? m_layouter
->size() : m_layouter
->itemSize();
1313 // Calculate the first visible index and last visible index for the new size
1314 setLayouterSize(size
, sizeType
);
1315 const int newFirst
= m_layouter
->firstVisibleIndex();
1316 const int newLast
= m_layouter
->lastVisibleIndex();
1318 if ((currentFirst
!= newFirst
) || (currentLast
!= newLast
)) {
1319 // At least one index has been changed. Assure that widgets for all possible
1320 // visible items get created so that a move-animation can be started later.
1321 const int maxVisibleItems
= m_layouter
->maximumVisibleItems();
1322 int minFirst
= qMin(newFirst
, currentFirst
);
1323 const int maxLast
= qMax(newLast
, currentLast
);
1325 if (maxLast
- minFirst
+ 1 < maxVisibleItems
) {
1326 // Increasing the size might result in a smaller KItemListView::offset().
1327 // Decrease the first visible index in a way that at least the maximum
1328 // visible items are shown.
1329 minFirst
= qMax(0, maxLast
- maxVisibleItems
+ 1);
1332 if (maxLast
- minFirst
> maxVisibleItems
+ maxVisibleItems
/ 2) {
1333 // The creating of widgets is quite expensive. Assure that never more
1334 // than 50 % of the maximum visible items get created for the animations.
1338 setLayouterSize(currentSize
, sizeType
);
1339 for (int i
= minFirst
; i
<= maxLast
; ++i
) {
1340 if (!m_visibleItems
.contains(i
)) {
1341 KItemListWidget
* widget
= createWidget(i
);
1342 const QPointF pos
= m_layouter
->itemBoundingRect(i
).topLeft();
1343 widget
->setPos(pos
);
1346 setLayouterSize(size
, sizeType
);
1350 void KItemListView::setLayouterSize(const QSizeF
& size
, SizeType sizeType
)
1353 case LayouterSize
: m_layouter
->setSize(size
); break;
1354 case ItemSize
: m_layouter
->setItemSize(size
); break;
1359 void KItemListView::updateWidgetProperties(KItemListWidget
* widget
, int index
)
1361 widget
->setVisibleRoles(m_visibleRoles
);
1362 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1363 widget
->setStyleOption(m_styleOption
);
1365 const KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
1366 widget
->setCurrent(index
== selectionManager
->currentItem());
1368 if (selectionManager
->hasSelection()) {
1369 const QSet
<int> selectedItems
= selectionManager
->selectedItems();
1370 widget
->setSelected(selectedItems
.contains(index
));
1372 widget
->setSelected(false);
1375 widget
->setHovered(false);
1377 widget
->setIndex(index
);
1378 widget
->setData(m_model
->data(index
));
1381 void KItemListView::updateHeaderWidth()
1387 // TODO 1: Use the required width of all roles
1388 m_header
->resize(size().width(), m_header
->size().height());
1391 QHash
<QByteArray
, qreal
> KItemListView::headerRolesWidths() const
1393 QHash
<QByteArray
, qreal
> rolesWidths
;
1395 QHashIterator
<QByteArray
, QSizeF
> it(m_stretchedVisibleRolesSizes
);
1396 while (it
.hasNext()) {
1398 rolesWidths
.insert(it
.key(), it
.value().width());
1404 void KItemListView::updateVisibleRolesSizes(const KItemRangeList
& itemRanges
)
1406 if (!m_itemSize
.isEmpty() || m_useHeaderWidths
) {
1410 const int itemCount
= m_model
->count();
1411 int rangesItemCount
= 0;
1412 foreach (const KItemRange
& range
, itemRanges
) {
1413 rangesItemCount
+= range
.count
;
1416 if (itemCount
== rangesItemCount
) {
1417 // The sizes of all roles need to be determined
1418 m_visibleRolesSizes
= visibleRolesSizes(itemRanges
);
1420 // Only a sub range of the roles need to be determined.
1421 // The chances are good that the sizes of the sub ranges
1422 // already fit into the available sizes and hence no
1423 // expensive update might be required.
1424 bool updateRequired
= false;
1426 const QHash
<QByteArray
, QSizeF
> updatedSizes
= visibleRolesSizes(itemRanges
);
1427 QHashIterator
<QByteArray
, QSizeF
> it(updatedSizes
);
1428 while (it
.hasNext()) {
1430 const QByteArray
& role
= it
.key();
1431 const QSizeF
& updatedSize
= it
.value();
1432 const QSizeF currentSize
= m_visibleRolesSizes
.value(role
);
1433 if (updatedSize
.width() > currentSize
.width() || updatedSize
.height() > currentSize
.height()) {
1434 m_visibleRolesSizes
.insert(role
, updatedSize
);
1435 updateRequired
= true;
1439 if (!updateRequired
) {
1440 // All the updated sizes are smaller than the current sizes and no change
1441 // of the roles-widths is required
1446 updateStretchedVisibleRolesSizes();
1449 void KItemListView::updateVisibleRolesSizes()
1451 const int itemCount
= m_model
->count();
1452 if (itemCount
> 0) {
1453 updateVisibleRolesSizes(KItemRangeList() << KItemRange(0, itemCount
));
1457 void KItemListView::updateStretchedVisibleRolesSizes()
1459 if (!m_itemSize
.isEmpty() || m_useHeaderWidths
) {
1463 // Calculate the maximum size of an item by considering the
1464 // visible role sizes and apply them to the layouter. If the
1465 // size does not use the available view-size it the size of the
1466 // first role will get stretched.
1467 qreal requiredWidth
= 0;
1468 qreal requiredHeight
= 0;
1470 QHashIterator
<QByteArray
, QSizeF
> it(m_visibleRolesSizes
);
1471 while (it
.hasNext()) {
1473 const QSizeF
& visibleRoleSize
= it
.value();
1474 requiredWidth
+= visibleRoleSize
.width();
1475 requiredHeight
+= visibleRoleSize
.height();
1478 m_stretchedVisibleRolesSizes
= m_visibleRolesSizes
;
1479 const QByteArray role
= visibleRoles().first();
1480 QSizeF firstRoleSize
= m_stretchedVisibleRolesSizes
.value(role
);
1482 QSizeF dynamicItemSize
= m_itemSize
;
1484 if (dynamicItemSize
.width() <= 0) {
1485 const qreal availableWidth
= size().width();
1486 if (requiredWidth
< availableWidth
) {
1487 // Stretch the first role to use the whole width for the item
1488 firstRoleSize
.rwidth() += availableWidth
- requiredWidth
;
1489 m_stretchedVisibleRolesSizes
.insert(role
, firstRoleSize
);
1491 dynamicItemSize
.setWidth(qMax(requiredWidth
, availableWidth
));
1494 if (dynamicItemSize
.height() <= 0) {
1495 const qreal availableHeight
= size().height();
1496 if (requiredHeight
< availableHeight
) {
1497 // Stretch the first role to use the whole height for the item
1498 firstRoleSize
.rheight() += availableHeight
- requiredHeight
;
1499 m_stretchedVisibleRolesSizes
.insert(role
, firstRoleSize
);
1501 dynamicItemSize
.setHeight(qMax(requiredHeight
, availableHeight
));
1504 m_layouter
->setItemSize(dynamicItemSize
);
1507 m_header
->setVisibleRolesWidths(headerRolesWidths());
1510 // Update the role sizes for all visible widgets
1511 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
1512 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1516 int KItemListView::calculateAutoScrollingIncrement(int pos
, int range
, int oldInc
)
1520 const int minSpeed
= 4;
1521 const int maxSpeed
= 128;
1522 const int speedLimiter
= 96;
1523 const int autoScrollBorder
= 64;
1525 // Limit the increment that is allowed to be added in comparison to 'oldInc'.
1526 // This assures that the autoscrolling speed grows gradually.
1527 const int incLimiter
= 1;
1529 if (pos
< autoScrollBorder
) {
1530 inc
= -minSpeed
+ qAbs(pos
- autoScrollBorder
) * (pos
- autoScrollBorder
) / speedLimiter
;
1531 inc
= qMax(inc
, -maxSpeed
);
1532 inc
= qMax(inc
, oldInc
- incLimiter
);
1533 } else if (pos
> range
- autoScrollBorder
) {
1534 inc
= minSpeed
+ qAbs(pos
- range
+ autoScrollBorder
) * (pos
- range
+ autoScrollBorder
) / speedLimiter
;
1535 inc
= qMin(inc
, maxSpeed
);
1536 inc
= qMin(inc
, oldInc
+ incLimiter
);
1544 KItemListCreatorBase::~KItemListCreatorBase()
1546 qDeleteAll(m_recycleableWidgets
);
1547 qDeleteAll(m_createdWidgets
);
1550 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget
* widget
)
1552 m_createdWidgets
.insert(widget
);
1555 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget
* widget
)
1557 Q_ASSERT(m_createdWidgets
.contains(widget
));
1558 m_createdWidgets
.remove(widget
);
1560 if (m_recycleableWidgets
.count() < 100) {
1561 m_recycleableWidgets
.append(widget
);
1562 widget
->setVisible(false);
1568 QGraphicsWidget
* KItemListCreatorBase::popRecycleableWidget()
1570 if (m_recycleableWidgets
.isEmpty()) {
1574 QGraphicsWidget
* widget
= m_recycleableWidgets
.takeLast();
1575 m_createdWidgets
.insert(widget
);
1579 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
1583 void KItemListWidgetCreatorBase::recycle(KItemListWidget
* widget
)
1585 widget
->setOpacity(1.0);
1586 pushRecycleableWidget(widget
);
1589 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
1593 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader
* header
)
1595 header
->setOpacity(1.0);
1596 pushRecycleableWidget(header
);
1599 #include "kitemlistview.moc"