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
);
151 setHeaderShown(emptySize
);
153 m_sizeHintResolver
->clearCache();
155 onItemSizeChanged(itemSize
, previousSize
);
158 QSizeF
KItemListView::itemSize() const
163 void KItemListView::setScrollOffset(qreal offset
)
169 const qreal previousOffset
= m_layouter
->scrollOffset();
170 if (offset
== previousOffset
) {
174 m_layouter
->setScrollOffset(offset
);
175 m_animation
->setScrollOffset(offset
);
176 if (!m_layoutTimer
->isActive()) {
177 doLayout(NoAnimation
, 0, 0);
180 onScrollOffsetChanged(offset
, previousOffset
);
183 qreal
KItemListView::scrollOffset() const
185 return m_layouter
->scrollOffset();
188 qreal
KItemListView::maximumScrollOffset() const
190 return m_layouter
->maximumScrollOffset();
193 void KItemListView::setItemOffset(qreal offset
)
195 m_layouter
->setItemOffset(offset
);
197 m_header
->setPos(-offset
, 0);
199 if (!m_layoutTimer
->isActive()) {
200 doLayout(NoAnimation
, 0, 0);
205 qreal
KItemListView::itemOffset() const
207 return m_layouter
->itemOffset();
210 qreal
KItemListView::maximumItemOffset() const
212 return m_layouter
->maximumItemOffset();
215 void KItemListView::setVisibleRoles(const QList
<QByteArray
>& roles
)
217 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
218 m_visibleRoles
= roles
;
220 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
221 while (it
.hasNext()) {
223 KItemListWidget
* widget
= it
.value();
224 widget
->setVisibleRoles(roles
);
225 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
228 m_sizeHintResolver
->clearCache();
229 m_layouter
->markAsDirty();
232 m_header
->setVisibleRoles(roles
);
233 m_header
->setVisibleRolesWidths(headerRolesWidths());
234 m_useHeaderWidths
= false;
237 updateVisibleRolesSizes();
240 onVisibleRolesChanged(roles
, previousRoles
);
243 QList
<QByteArray
> KItemListView::visibleRoles() const
245 return m_visibleRoles
;
248 void KItemListView::setAutoScroll(bool enabled
)
250 if (enabled
&& !m_autoScrollTimer
) {
251 m_autoScrollTimer
= new QTimer(this);
252 m_autoScrollTimer
->setSingleShot(false);
253 connect(m_autoScrollTimer
, SIGNAL(timeout()), this, SLOT(triggerAutoScrolling()));
254 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
255 } else if (!enabled
&& m_autoScrollTimer
) {
256 delete m_autoScrollTimer
;
257 m_autoScrollTimer
= 0;
262 bool KItemListView::autoScroll() const
264 return m_autoScrollTimer
!= 0;
267 KItemListController
* KItemListView::controller() const
272 KItemModelBase
* KItemListView::model() const
277 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
)
279 m_widgetCreator
= widgetCreator
;
282 KItemListWidgetCreatorBase
* KItemListView::widgetCreator() const
284 return m_widgetCreator
;
287 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
)
289 m_groupHeaderCreator
= groupHeaderCreator
;
292 KItemListGroupHeaderCreatorBase
* KItemListView::groupHeaderCreator() const
294 return m_groupHeaderCreator
;
297 void KItemListView::setStyleOption(const KItemListStyleOption
& option
)
299 const KItemListStyleOption previousOption
= m_styleOption
;
300 m_styleOption
= option
;
302 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
303 while (it
.hasNext()) {
305 it
.value()->setStyleOption(option
);
308 m_sizeHintResolver
->clearCache();
310 onStyleOptionChanged(option
, previousOption
);
313 const KItemListStyleOption
& KItemListView::styleOption() const
315 return m_styleOption
;
318 void KItemListView::setGeometry(const QRectF
& rect
)
320 QGraphicsWidget::setGeometry(rect
);
326 if (m_model
->count() > 0) {
327 prepareLayoutForIncreasedItemCount(rect
.size(), LayouterSize
);
329 m_layouter
->setSize(rect
.size());
332 if (!m_layoutTimer
->isActive()) {
333 m_layoutTimer
->start();
336 // Changing the geometry does not require to do an expensive
337 // update of the visible-roles sizes, only the stretched sizes
338 // need to be adjusted to the new size.
339 updateStretchedVisibleRolesSizes();
342 int KItemListView::itemAt(const QPointF
& pos
) const
344 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
345 while (it
.hasNext()) {
348 const KItemListWidget
* widget
= it
.value();
349 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
350 if (widget
->contains(mappedPos
)) {
358 bool KItemListView::isAboveSelectionToggle(int index
, const QPointF
& pos
) const
365 bool KItemListView::isAboveExpansionToggle(int index
, const QPointF
& pos
) const
367 const KItemListWidget
* widget
= m_visibleItems
.value(index
);
369 const QRectF expansionToggleRect
= widget
->expansionToggleRect();
370 if (!expansionToggleRect
.isEmpty()) {
371 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
372 return expansionToggleRect
.contains(mappedPos
);
378 int KItemListView::firstVisibleIndex() const
380 return m_layouter
->firstVisibleIndex();
383 int KItemListView::lastVisibleIndex() const
385 return m_layouter
->lastVisibleIndex();
388 QSizeF
KItemListView::itemSizeHint(int index
) const
394 QHash
<QByteArray
, QSizeF
> KItemListView::visibleRolesSizes(const KItemRangeList
& itemRanges
) const
396 Q_UNUSED(itemRanges
);
397 return QHash
<QByteArray
, QSizeF
>();
400 QRectF
KItemListView::itemBoundingRect(int index
) const
402 return m_layouter
->itemBoundingRect(index
);
405 int KItemListView::itemsPerOffset() const
407 return m_layouter
->itemsPerOffset();
410 void KItemListView::beginTransaction()
412 ++m_activeTransactions
;
413 if (m_activeTransactions
== 1) {
414 onTransactionBegin();
418 void KItemListView::endTransaction()
420 --m_activeTransactions
;
421 if (m_activeTransactions
< 0) {
422 m_activeTransactions
= 0;
423 kWarning() << "Mismatch between beginTransaction()/endTransaction()";
426 if (m_activeTransactions
== 0) {
432 bool KItemListView::isTransactionActive() const
434 return m_activeTransactions
> 0;
437 QPixmap
KItemListView::createDragPixmap(const QSet
<int>& indexes
) const
443 void KItemListView::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
445 QGraphicsWidget::paint(painter
, option
, widget
);
447 if (m_rubberBand
->isActive()) {
448 QRectF rubberBandRect
= QRectF(m_rubberBand
->startPosition(),
449 m_rubberBand
->endPosition()).normalized();
451 const QPointF topLeft
= rubberBandRect
.topLeft();
452 if (scrollOrientation() == Qt::Vertical
) {
453 rubberBandRect
.moveTo(topLeft
.x(), topLeft
.y() - scrollOffset());
455 rubberBandRect
.moveTo(topLeft
.x() - scrollOffset(), topLeft
.y());
458 QStyleOptionRubberBand opt
;
459 opt
.initFrom(widget
);
460 opt
.shape
= QRubberBand::Rectangle
;
462 opt
.rect
= rubberBandRect
.toRect();
463 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
467 void KItemListView::initializeItemListWidget(KItemListWidget
* item
)
472 bool KItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const
474 Q_UNUSED(changedRoles
);
478 void KItemListView::onControllerChanged(KItemListController
* current
, KItemListController
* previous
)
484 void KItemListView::onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
490 void KItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
496 void KItemListView::onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
)
502 void KItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
508 void KItemListView::onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
)
514 void KItemListView::onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
520 void KItemListView::onTransactionBegin()
524 void KItemListView::onTransactionEnd()
528 bool KItemListView::event(QEvent
* event
)
530 // Forward all events to the controller and handle them there
531 if (m_controller
&& m_controller
->processEvent(event
, transform())) {
535 return QGraphicsWidget::event(event
);
538 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent
* event
)
540 m_mousePos
= transform().map(event
->pos());
544 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent
* event
)
546 QGraphicsWidget::mouseMoveEvent(event
);
548 m_mousePos
= transform().map(event
->pos());
549 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
550 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
554 void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent
* event
)
556 event
->setAccepted(true);
560 void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent
*event
)
562 QGraphicsWidget::dragMoveEvent(event
);
564 m_mousePos
= transform().map(event
->pos());
565 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
566 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
570 void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
)
572 QGraphicsWidget::dragLeaveEvent(event
);
573 setAutoScroll(false);
576 void KItemListView::dropEvent(QGraphicsSceneDragDropEvent
* event
)
578 QGraphicsWidget::dropEvent(event
);
579 setAutoScroll(false);
582 QList
<KItemListWidget
*> KItemListView::visibleItemListWidgets() const
584 return m_visibleItems
.values();
587 void KItemListView::resizeEvent(QGraphicsSceneResizeEvent
* event
)
589 QGraphicsWidget::resizeEvent(event
);
590 if (m_itemSize
.isEmpty() && m_useHeaderWidths
) {
591 QSizeF dynamicItemSize
= m_layouter
->itemSize();
592 const QSizeF newSize
= event
->newSize();
594 if (m_itemSize
.width() < 0) {
595 const qreal requiredWidth
= visibleRolesSizesWidthSum();
596 if (newSize
.width() > requiredWidth
) {
597 dynamicItemSize
.setWidth(newSize
.width());
599 const qreal headerWidth
= qMax(newSize
.width(), requiredWidth
);
600 m_header
->resize(headerWidth
, m_header
->size().height());
603 if (m_itemSize
.height() < 0) {
604 const qreal requiredHeight
= visibleRolesSizesHeightSum();
605 if (newSize
.height() > requiredHeight
) {
606 dynamicItemSize
.setHeight(newSize
.height());
608 // TODO: KItemListHeader is not prepared for vertical alignment
611 m_layouter
->setItemSize(dynamicItemSize
);
615 void KItemListView::slotItemsInserted(const KItemRangeList
& itemRanges
)
617 updateVisibleRolesSizes(itemRanges
);
619 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
620 if (hasMultipleRanges
) {
624 int previouslyInsertedCount
= 0;
625 foreach (const KItemRange
& range
, itemRanges
) {
626 // range.index is related to the model before anything has been inserted.
627 // As in each loop the current item-range gets inserted the index must
628 // be increased by the already previously inserted items.
629 const int index
= range
.index
+ previouslyInsertedCount
;
630 const int count
= range
.count
;
631 if (index
< 0 || count
<= 0) {
632 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
635 previouslyInsertedCount
+= count
;
637 m_sizeHintResolver
->itemsInserted(index
, count
);
639 // Determine which visible items must be moved
640 QList
<int> itemsToMove
;
641 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
642 while (it
.hasNext()) {
644 const int visibleItemIndex
= it
.key();
645 if (visibleItemIndex
>= index
) {
646 itemsToMove
.append(visibleItemIndex
);
650 // Update the indexes of all KItemListWidget instances that are located
651 // after the inserted items. It is important to adjust the indexes in the order
652 // from the highest index to the lowest index to prevent overlaps when setting the new index.
654 for (int i
= itemsToMove
.count() - 1; i
>= 0; --i
) {
655 KItemListWidget
* widget
= m_visibleItems
.value(itemsToMove
[i
]);
657 setWidgetIndex(widget
, widget
->index() + count
);
660 m_layouter
->markAsDirty();
661 if (m_model
->count() == count
&& maximumScrollOffset() > size().height()) {
662 kDebug() << "Scrollbar required, skipping layout";
663 const int scrollBarExtent
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
664 QSizeF layouterSize
= m_layouter
->size();
665 if (scrollOrientation() == Qt::Vertical
) {
666 layouterSize
.rwidth() -= scrollBarExtent
;
668 layouterSize
.rheight() -= scrollBarExtent
;
670 m_layouter
->setSize(layouterSize
);
673 if (!hasMultipleRanges
) {
674 doLayout(Animation
, index
, count
);
680 m_controller
->selectionManager()->itemsInserted(itemRanges
);
683 if (hasMultipleRanges
) {
688 void KItemListView::slotItemsRemoved(const KItemRangeList
& itemRanges
)
690 updateVisibleRolesSizes();
692 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
693 if (hasMultipleRanges
) {
697 for (int i
= itemRanges
.count() - 1; i
>= 0; --i
) {
698 const KItemRange
& range
= itemRanges
.at(i
);
699 const int index
= range
.index
;
700 const int count
= range
.count
;
701 if (index
< 0 || count
<= 0) {
702 kWarning() << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
706 m_sizeHintResolver
->itemsRemoved(index
, count
);
708 const int firstRemovedIndex
= index
;
709 const int lastRemovedIndex
= index
+ count
- 1;
710 const int lastIndex
= m_model
->count() + count
- 1;
712 // Remove all KItemListWidget instances that got deleted
713 for (int i
= firstRemovedIndex
; i
<= lastRemovedIndex
; ++i
) {
714 KItemListWidget
* widget
= m_visibleItems
.value(i
);
719 m_animation
->stop(widget
);
720 // Stopping the animation might lead to recycling the widget if
721 // it is invisible (see slotAnimationFinished()).
722 // Check again whether it is still visible:
723 if (!m_visibleItems
.contains(i
)) {
727 if (m_model
->count() == 0) {
728 // For performance reasons no animation is done when all items have
730 recycleWidget(widget
);
732 // Animate the removing of the items. Special case: When removing an item there
733 // is no valid model index available anymore. For the
734 // remove-animation the item gets removed from m_visibleItems but the widget
735 // will stay alive until the animation has been finished and will
736 // be recycled (deleted) in KItemListView::slotAnimationFinished().
737 m_visibleItems
.remove(i
);
738 widget
->setIndex(-1);
739 m_animation
->start(widget
, KItemListViewAnimation::DeleteAnimation
);
743 // Update the indexes of all KItemListWidget instances that are located
744 // after the deleted items
745 for (int i
= lastRemovedIndex
+ 1; i
<= lastIndex
; ++i
) {
746 KItemListWidget
* widget
= m_visibleItems
.value(i
);
748 const int newIndex
= i
- count
;
749 setWidgetIndex(widget
, newIndex
);
753 m_layouter
->markAsDirty();
754 if (!hasMultipleRanges
) {
755 doLayout(Animation
, index
, -count
);
761 m_controller
->selectionManager()->itemsRemoved(itemRanges
);
764 if (hasMultipleRanges
) {
769 void KItemListView::slotItemsMoved(const KItemRange
& itemRange
, const QList
<int>& movedToIndexes
)
771 const int firstVisibleMovedIndex
= qMax(firstVisibleIndex(), itemRange
.index
);
772 const int lastVisibleMovedIndex
= qMin(lastVisibleIndex(), itemRange
.index
+ itemRange
.count
- 1);
774 for (int index
= firstVisibleMovedIndex
; index
<= lastVisibleMovedIndex
; ++index
) {
775 KItemListWidget
* widget
= m_visibleItems
.value(index
);
777 updateWidgetProperties(widget
, index
);
782 m_controller
->selectionManager()->itemsMoved(itemRange
, movedToIndexes
);
786 void KItemListView::slotItemsChanged(const KItemRangeList
& itemRanges
,
787 const QSet
<QByteArray
>& roles
)
789 const bool updateSizeHints
= itemSizeHintUpdateRequired(roles
);
790 if (updateSizeHints
) {
791 updateVisibleRolesSizes(itemRanges
);
794 foreach (const KItemRange
& itemRange
, itemRanges
) {
795 const int index
= itemRange
.index
;
796 const int count
= itemRange
.count
;
798 if (updateSizeHints
) {
799 m_sizeHintResolver
->itemsChanged(index
, count
, roles
);
800 m_layouter
->markAsDirty();
801 if (!m_layoutTimer
->isActive()) {
802 m_layoutTimer
->start();
806 // Apply the changed roles to the visible item-widgets
807 const int lastIndex
= index
+ count
- 1;
808 for (int i
= index
; i
<= lastIndex
; ++i
) {
809 KItemListWidget
* widget
= m_visibleItems
.value(i
);
811 widget
->setData(m_model
->data(i
), roles
);
818 void KItemListView::slotCurrentChanged(int current
, int previous
)
822 KItemListWidget
* previousWidget
= m_visibleItems
.value(previous
, 0);
823 if (previousWidget
) {
824 Q_ASSERT(previousWidget
->isCurrent());
825 previousWidget
->setCurrent(false);
828 KItemListWidget
* currentWidget
= m_visibleItems
.value(current
, 0);
830 Q_ASSERT(!currentWidget
->isCurrent());
831 currentWidget
->setCurrent(true);
834 const QRectF viewGeometry
= geometry();
835 const QRectF currentBoundingRect
= itemBoundingRect(current
);
837 if (!viewGeometry
.contains(currentBoundingRect
)) {
838 // Make sure that the new current item is fully visible in the view.
839 qreal newOffset
= scrollOffset();
840 if (currentBoundingRect
.top() < viewGeometry
.top()) {
841 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
842 newOffset
+= currentBoundingRect
.top() - viewGeometry
.top();
843 } else if ((currentBoundingRect
.bottom() > viewGeometry
.bottom())) {
844 Q_ASSERT(scrollOrientation() == Qt::Vertical
);
845 newOffset
+= currentBoundingRect
.bottom() - viewGeometry
.bottom();
846 } else if (currentBoundingRect
.left() < viewGeometry
.left()) {
847 if (scrollOrientation() == Qt::Horizontal
) {
848 newOffset
+= currentBoundingRect
.left() - viewGeometry
.left();
850 } else if ((currentBoundingRect
.right() > viewGeometry
.right())) {
851 if (scrollOrientation() == Qt::Horizontal
) {
852 newOffset
+= currentBoundingRect
.right() - viewGeometry
.right();
856 if (newOffset
!= scrollOffset()) {
857 emit
scrollTo(newOffset
);
862 void KItemListView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
866 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
867 while (it
.hasNext()) {
869 const int index
= it
.key();
870 KItemListWidget
* widget
= it
.value();
871 widget
->setSelected(current
.contains(index
));
875 void KItemListView::slotAnimationFinished(QGraphicsWidget
* widget
,
876 KItemListViewAnimation::AnimationType type
)
878 KItemListWidget
* itemListWidget
= qobject_cast
<KItemListWidget
*>(widget
);
879 Q_ASSERT(itemListWidget
);
882 case KItemListViewAnimation::DeleteAnimation
: {
883 // As we recycle the widget in this case it is important to assure that no
884 // other animation has been started. This is a convention in KItemListView and
885 // not a requirement defined by KItemListViewAnimation.
886 Q_ASSERT(!m_animation
->isStarted(itemListWidget
));
888 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
889 // by m_visibleWidgets and must be deleted manually after the animation has
891 KItemListGroupHeader
* header
= m_visibleGroups
.value(itemListWidget
);
893 m_groupHeaderCreator
->recycle(header
);
894 m_visibleGroups
.remove(itemListWidget
);
896 m_widgetCreator
->recycle(itemListWidget
);
900 case KItemListViewAnimation::CreateAnimation
:
901 case KItemListViewAnimation::MovingAnimation
:
902 case KItemListViewAnimation::ResizeAnimation
: {
903 const int index
= itemListWidget
->index();
904 const bool invisible
= (index
< m_layouter
->firstVisibleIndex()) ||
905 (index
> m_layouter
->lastVisibleIndex());
906 if (invisible
&& !m_animation
->isStarted(itemListWidget
)) {
907 recycleWidget(itemListWidget
);
916 void KItemListView::slotLayoutTimerFinished()
918 m_layouter
->setSize(geometry().size());
919 doLayout(Animation
, 0, 0);
922 void KItemListView::slotRubberBandPosChanged()
927 void KItemListView::slotRubberBandActivationChanged(bool active
)
930 connect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
931 connect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
932 m_skipAutoScrollForRubberBand
= true;
934 disconnect(m_rubberBand
, SIGNAL(startPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
935 disconnect(m_rubberBand
, SIGNAL(endPositionChanged(QPointF
,QPointF
)), this, SLOT(slotRubberBandPosChanged()));
936 m_skipAutoScrollForRubberBand
= false;
942 void KItemListView::slotVisibleRoleWidthChanged(const QByteArray
& role
,
946 Q_UNUSED(previousWidth
);
948 m_useHeaderWidths
= true;
950 if (m_visibleRolesSizes
.contains(role
)) {
951 QSizeF roleSize
= m_visibleRolesSizes
.value(role
);
952 roleSize
.setWidth(currentWidth
);
953 m_visibleRolesSizes
.insert(role
, roleSize
);
954 m_stretchedVisibleRolesSizes
.insert(role
, roleSize
);
956 // Apply the new size to the layouter
957 QSizeF dynamicItemSize
= m_itemSize
;
958 if (dynamicItemSize
.width() < 0) {
959 const qreal requiredWidth
= visibleRolesSizesWidthSum();
960 dynamicItemSize
.setWidth(qMax(size().width(), requiredWidth
));
962 if (dynamicItemSize
.height() < 0) {
963 const qreal requiredHeight
= visibleRolesSizesHeightSum();
964 dynamicItemSize
.setHeight(qMax(size().height(), requiredHeight
));
967 m_layouter
->setItemSize(dynamicItemSize
);
969 // Update the role sizes for all visible widgets
970 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
971 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
978 void KItemListView::triggerAutoScrolling()
980 if (!m_autoScrollTimer
) {
986 if (scrollOrientation() == Qt::Vertical
) {
987 pos
= m_mousePos
.y();
988 visibleSize
= size().height();
990 pos
= m_mousePos
.x();
991 visibleSize
= size().width();
994 if (m_autoScrollTimer
->interval() == InitialAutoScrollDelay
) {
995 m_autoScrollIncrement
= 0;
998 m_autoScrollIncrement
= calculateAutoScrollingIncrement(pos
, visibleSize
, m_autoScrollIncrement
);
999 if (m_autoScrollIncrement
== 0) {
1000 // The mouse position is not above an autoscroll margin (the autoscroll timer
1001 // will be restarted in mouseMoveEvent())
1002 m_autoScrollTimer
->stop();
1006 if (m_rubberBand
->isActive() && m_skipAutoScrollForRubberBand
) {
1007 // If a rubberband selection is ongoing the autoscrolling may only get triggered
1008 // if the direction of the rubberband is similar to the autoscroll direction. This
1009 // prevents that starting to create a rubberband within the autoscroll margins starts
1010 // an autoscrolling.
1012 const qreal minDiff
= 4; // Ignore any autoscrolling if the rubberband is very small
1013 const qreal diff
= (scrollOrientation() == Qt::Vertical
)
1014 ? m_rubberBand
->endPosition().y() - m_rubberBand
->startPosition().y()
1015 : m_rubberBand
->endPosition().x() - m_rubberBand
->startPosition().x();
1016 if (qAbs(diff
) < minDiff
|| (m_autoScrollIncrement
< 0 && diff
> 0) || (m_autoScrollIncrement
> 0 && diff
< 0)) {
1017 // The rubberband direction is different from the scroll direction (e.g. the rubberband has
1018 // been moved up although the autoscroll direction might be down)
1019 m_autoScrollTimer
->stop();
1024 // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
1025 // the autoscrolling may not get skipped anymore until a new rubberband is created
1026 m_skipAutoScrollForRubberBand
= false;
1028 setScrollOffset(scrollOffset() + m_autoScrollIncrement
);
1030 // Trigger the autoscroll timer which will periodically call
1031 // triggerAutoScrolling()
1032 m_autoScrollTimer
->start(RepeatingAutoScrollDelay
);
1035 void KItemListView::setController(KItemListController
* controller
)
1037 if (m_controller
!= controller
) {
1038 KItemListController
* previous
= m_controller
;
1040 KItemListSelectionManager
* selectionManager
= previous
->selectionManager();
1041 disconnect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1042 disconnect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
1045 m_controller
= controller
;
1048 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
1049 connect(selectionManager
, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1050 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)), this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
1053 onControllerChanged(controller
, previous
);
1057 void KItemListView::setModel(KItemModelBase
* model
)
1059 if (m_model
== model
) {
1063 KItemModelBase
* previous
= m_model
;
1066 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1067 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1068 disconnect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
1069 this, SLOT(slotItemsInserted(KItemRangeList
)));
1070 disconnect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
1071 this, SLOT(slotItemsRemoved(KItemRangeList
)));
1072 disconnect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
1073 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
1077 m_layouter
->setModel(model
);
1078 m_grouped
= !model
->groupRole().isEmpty();
1081 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1082 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1083 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
1084 this, SLOT(slotItemsInserted(KItemRangeList
)));
1085 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
1086 this, SLOT(slotItemsRemoved(KItemRangeList
)));
1087 connect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
1088 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
1091 onModelChanged(model
, previous
);
1094 KItemListRubberBand
* KItemListView::rubberBand() const
1096 return m_rubberBand
;
1099 void KItemListView::updateLayout()
1101 doLayout(Animation
, 0, 0);
1105 void KItemListView::doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
)
1107 if (m_layoutTimer
->isActive()) {
1108 kDebug() << "Stopping layout timer, synchronous layout requested";
1109 m_layoutTimer
->stop();
1112 if (m_model
->count() < 0 || m_activeTransactions
> 0) {
1116 //markVisibleRolesSizesAsDirty();
1118 const int firstVisibleIndex
= m_layouter
->firstVisibleIndex();
1119 const int lastVisibleIndex
= m_layouter
->lastVisibleIndex();
1120 if (firstVisibleIndex
< 0) {
1121 emitOffsetChanges();
1125 // Do a sanity check of the scroll-offset property: When properties of the itemlist-view have been changed
1126 // it might be possible that the maximum offset got changed too. Assure that the full visible range
1127 // is still shown if the maximum offset got decreased.
1128 const qreal visibleOffsetRange
= (scrollOrientation() == Qt::Horizontal
) ? size().width() : size().height();
1129 const qreal maxOffsetToShowFullRange
= maximumScrollOffset() - visibleOffsetRange
;
1130 if (scrollOffset() > maxOffsetToShowFullRange
) {
1131 m_layouter
->setScrollOffset(qMax(qreal(0), maxOffsetToShowFullRange
));
1134 // Determine all items that are completely invisible and might be
1135 // reused for items that just got (at least partly) visible.
1136 // Items that do e.g. an animated moving of their position are not
1137 // marked as invisible: This assures that a scrolling inside the view
1138 // can be done without breaking an animation.
1139 QList
<int> reusableItems
;
1140 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
1141 while (it
.hasNext()) {
1143 KItemListWidget
* widget
= it
.value();
1144 const int index
= widget
->index();
1145 const bool invisible
= (index
< firstVisibleIndex
) || (index
> lastVisibleIndex
);
1146 if (invisible
&& !m_animation
->isStarted(widget
)) {
1147 widget
->setVisible(false);
1148 reusableItems
.append(index
);
1152 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
1153 // instances from invisible items are reused. If no reusable items are
1154 // found then new KItemListWidget instances get created.
1155 const bool animate
= (hint
== Animation
);
1156 for (int i
= firstVisibleIndex
; i
<= lastVisibleIndex
; ++i
) {
1157 bool applyNewPos
= true;
1158 bool wasHidden
= false;
1160 const QRectF itemBounds
= m_layouter
->itemBoundingRect(i
);
1161 const QPointF newPos
= itemBounds
.topLeft();
1162 KItemListWidget
* widget
= m_visibleItems
.value(i
);
1165 if (!reusableItems
.isEmpty()) {
1166 // Reuse a KItemListWidget instance from an invisible item
1167 const int oldIndex
= reusableItems
.takeLast();
1168 widget
= m_visibleItems
.value(oldIndex
);
1169 setWidgetIndex(widget
, i
);
1171 // No reusable KItemListWidget instance is available, create a new one
1172 widget
= createWidget(i
);
1174 widget
->resize(itemBounds
.size());
1176 if (animate
&& changedCount
< 0) {
1177 // Items have been deleted, move the created item to the
1178 // imaginary old position.
1179 const QRectF itemBoundingRect
= m_layouter
->itemBoundingRect(i
- changedCount
);
1180 if (itemBoundingRect
.isEmpty()) {
1181 const QPointF invisibleOldPos
= (scrollOrientation() == Qt::Vertical
)
1182 ? QPointF(0, size().height()) : QPointF(size().width(), 0);
1183 widget
->setPos(invisibleOldPos
);
1185 widget
->setPos(itemBoundingRect
.topLeft());
1187 applyNewPos
= false;
1189 } else if (m_animation
->isStarted(widget
, KItemListViewAnimation::MovingAnimation
)) {
1190 applyNewPos
= false;
1194 const bool itemsRemoved
= (changedCount
< 0);
1195 const bool itemsInserted
= (changedCount
> 0);
1197 if (itemsRemoved
&& (i
>= changedIndex
+ changedCount
+ 1)) {
1198 // The item is located after the removed items. Animate the moving of the position.
1199 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1200 applyNewPos
= false;
1201 } else if (itemsInserted
&& i
>= changedIndex
) {
1202 // The item is located after the first inserted item
1203 if (i
<= changedIndex
+ changedCount
- 1) {
1204 // The item is an inserted item. Animate the appearing of the item.
1205 // For performance reasons no animation is done when changedCount is equal
1206 // to all available items.
1207 if (changedCount
< m_model
->count()) {
1208 m_animation
->start(widget
, KItemListViewAnimation::CreateAnimation
);
1210 } else if (!m_animation
->isStarted(widget
, KItemListViewAnimation::CreateAnimation
)) {
1211 // The item was already there before, so animate the moving of the position.
1212 // No moving animation is done if the item is animated by a create animation: This
1213 // prevents a "move animation mess" when inserting several ranges in parallel.
1214 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1215 applyNewPos
= false;
1217 } else if (!itemsRemoved
&& !itemsInserted
&& !wasHidden
) {
1218 // The size of the view might have been changed. Animate the moving of the position.
1219 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1220 applyNewPos
= false;
1225 widget
->setPos(newPos
);
1228 Q_ASSERT(widget
->index() == i
);
1229 widget
->setVisible(true);
1231 if (widget
->size() != itemBounds
.size()) {
1232 m_animation
->start(widget
, KItemListViewAnimation::ResizeAnimation
, itemBounds
.size());
1236 // Delete invisible KItemListWidget instances that have not been reused
1237 foreach (int index
, reusableItems
) {
1238 recycleWidget(m_visibleItems
.value(index
));
1241 emitOffsetChanges();
1244 void KItemListView::emitOffsetChanges()
1246 const qreal newScrollOffset
= m_layouter
->scrollOffset();
1247 if (m_oldScrollOffset
!= newScrollOffset
) {
1248 emit
scrollOffsetChanged(newScrollOffset
, m_oldScrollOffset
);
1249 m_oldScrollOffset
= newScrollOffset
;
1252 const qreal newMaximumScrollOffset
= m_layouter
->maximumScrollOffset();
1253 if (m_oldMaximumScrollOffset
!= newMaximumScrollOffset
) {
1254 emit
maximumScrollOffsetChanged(newMaximumScrollOffset
, m_oldMaximumScrollOffset
);
1255 m_oldMaximumScrollOffset
= newMaximumScrollOffset
;
1258 const qreal newItemOffset
= m_layouter
->itemOffset();
1259 if (m_oldItemOffset
!= newItemOffset
) {
1260 emit
itemOffsetChanged(newItemOffset
, m_oldItemOffset
);
1261 m_oldItemOffset
= newItemOffset
;
1264 const qreal newMaximumItemOffset
= m_layouter
->maximumItemOffset();
1265 if (m_oldMaximumItemOffset
!= newMaximumItemOffset
) {
1266 emit
maximumItemOffsetChanged(newMaximumItemOffset
, m_oldMaximumItemOffset
);
1267 m_oldMaximumItemOffset
= newMaximumItemOffset
;
1271 KItemListWidget
* KItemListView::createWidget(int index
)
1273 KItemListWidget
* widget
= m_widgetCreator
->create(this);
1274 updateWidgetProperties(widget
, index
);
1275 m_visibleItems
.insert(index
, widget
);
1278 if (m_layouter
->isFirstGroupItem(index
)) {
1279 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1280 header
->setPos(0, -50);
1281 header
->resize(50, 50);
1282 m_visibleGroups
.insert(widget
, header
);
1286 initializeItemListWidget(widget
);
1290 void KItemListView::recycleWidget(KItemListWidget
* widget
)
1293 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1295 m_groupHeaderCreator
->recycle(header
);
1296 m_visibleGroups
.remove(widget
);
1300 m_visibleItems
.remove(widget
->index());
1301 m_widgetCreator
->recycle(widget
);
1304 void KItemListView::setWidgetIndex(KItemListWidget
* widget
, int index
)
1307 bool createHeader
= m_layouter
->isFirstGroupItem(index
);
1308 KItemListGroupHeader
* header
= m_visibleGroups
.value(widget
);
1311 createHeader
= false;
1313 m_groupHeaderCreator
->recycle(header
);
1314 m_visibleGroups
.remove(widget
);
1319 KItemListGroupHeader
* header
= m_groupHeaderCreator
->create(widget
);
1320 header
->setPos(0, -50);
1321 header
->resize(50, 50);
1322 m_visibleGroups
.insert(widget
, header
);
1326 const int oldIndex
= widget
->index();
1327 m_visibleItems
.remove(oldIndex
);
1328 updateWidgetProperties(widget
, index
);
1329 m_visibleItems
.insert(index
, widget
);
1331 initializeItemListWidget(widget
);
1334 void KItemListView::prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
)
1336 // Calculate the first visible index and last visible index for the current size
1337 const int currentFirst
= m_layouter
->firstVisibleIndex();
1338 const int currentLast
= m_layouter
->lastVisibleIndex();
1340 const QSizeF currentSize
= (sizeType
== LayouterSize
) ? m_layouter
->size() : m_layouter
->itemSize();
1342 // Calculate the first visible index and last visible index for the new size
1343 setLayouterSize(size
, sizeType
);
1344 const int newFirst
= m_layouter
->firstVisibleIndex();
1345 const int newLast
= m_layouter
->lastVisibleIndex();
1347 if ((currentFirst
!= newFirst
) || (currentLast
!= newLast
)) {
1348 // At least one index has been changed. Assure that widgets for all possible
1349 // visible items get created so that a move-animation can be started later.
1350 const int maxVisibleItems
= m_layouter
->maximumVisibleItems();
1351 int minFirst
= qMin(newFirst
, currentFirst
);
1352 const int maxLast
= qMax(newLast
, currentLast
);
1354 if (maxLast
- minFirst
+ 1 < maxVisibleItems
) {
1355 // Increasing the size might result in a smaller KItemListView::offset().
1356 // Decrease the first visible index in a way that at least the maximum
1357 // visible items are shown.
1358 minFirst
= qMax(0, maxLast
- maxVisibleItems
+ 1);
1361 if (maxLast
- minFirst
> maxVisibleItems
+ maxVisibleItems
/ 2) {
1362 // The creating of widgets is quite expensive. Assure that never more
1363 // than 50 % of the maximum visible items get created for the animations.
1367 setLayouterSize(currentSize
, sizeType
);
1368 for (int i
= minFirst
; i
<= maxLast
; ++i
) {
1369 if (!m_visibleItems
.contains(i
)) {
1370 KItemListWidget
* widget
= createWidget(i
);
1371 const QPointF pos
= m_layouter
->itemBoundingRect(i
).topLeft();
1372 widget
->setPos(pos
);
1375 setLayouterSize(size
, sizeType
);
1379 void KItemListView::setLayouterSize(const QSizeF
& size
, SizeType sizeType
)
1382 case LayouterSize
: m_layouter
->setSize(size
); break;
1383 case ItemSize
: m_layouter
->setItemSize(size
); break;
1388 void KItemListView::updateWidgetProperties(KItemListWidget
* widget
, int index
)
1390 widget
->setVisibleRoles(m_visibleRoles
);
1391 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1392 widget
->setStyleOption(m_styleOption
);
1394 const KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
1395 widget
->setCurrent(index
== selectionManager
->currentItem());
1396 widget
->setSelected(selectionManager
->isSelected(index
));
1397 widget
->setHovered(false);
1398 widget
->setAlternatingBackgroundColors(false);
1399 widget
->setIndex(index
);
1400 widget
->setData(m_model
->data(index
));
1403 QHash
<QByteArray
, qreal
> KItemListView::headerRolesWidths() const
1405 QHash
<QByteArray
, qreal
> rolesWidths
;
1407 QHashIterator
<QByteArray
, QSizeF
> it(m_stretchedVisibleRolesSizes
);
1408 while (it
.hasNext()) {
1410 rolesWidths
.insert(it
.key(), it
.value().width());
1416 void KItemListView::updateVisibleRolesSizes(const KItemRangeList
& itemRanges
)
1418 if (!m_itemSize
.isEmpty() || m_useHeaderWidths
) {
1422 const int itemCount
= m_model
->count();
1423 int rangesItemCount
= 0;
1424 foreach (const KItemRange
& range
, itemRanges
) {
1425 rangesItemCount
+= range
.count
;
1428 if (itemCount
== rangesItemCount
) {
1429 // The sizes of all roles need to be determined
1430 m_visibleRolesSizes
= visibleRolesSizes(itemRanges
);
1432 // Only a sub range of the roles need to be determined.
1433 // The chances are good that the sizes of the sub ranges
1434 // already fit into the available sizes and hence no
1435 // expensive update might be required.
1436 bool updateRequired
= false;
1438 const QHash
<QByteArray
, QSizeF
> updatedSizes
= visibleRolesSizes(itemRanges
);
1439 QHashIterator
<QByteArray
, QSizeF
> it(updatedSizes
);
1440 while (it
.hasNext()) {
1442 const QByteArray
& role
= it
.key();
1443 const QSizeF
& updatedSize
= it
.value();
1444 const QSizeF currentSize
= m_visibleRolesSizes
.value(role
);
1445 if (updatedSize
.width() > currentSize
.width() || updatedSize
.height() > currentSize
.height()) {
1446 m_visibleRolesSizes
.insert(role
, updatedSize
);
1447 updateRequired
= true;
1451 if (!updateRequired
) {
1452 // All the updated sizes are smaller than the current sizes and no change
1453 // of the roles-widths is required
1458 updateStretchedVisibleRolesSizes();
1461 void KItemListView::updateVisibleRolesSizes()
1463 const int itemCount
= m_model
->count();
1464 if (itemCount
> 0) {
1465 updateVisibleRolesSizes(KItemRangeList() << KItemRange(0, itemCount
));
1469 void KItemListView::updateStretchedVisibleRolesSizes()
1471 if (!m_itemSize
.isEmpty() || m_useHeaderWidths
) {
1475 // Calculate the maximum size of an item by considering the
1476 // visible role sizes and apply them to the layouter. If the
1477 // size does not use the available view-size it the size of the
1478 // first role will get stretched.
1479 m_stretchedVisibleRolesSizes
= m_visibleRolesSizes
;
1480 const QByteArray role
= visibleRoles().first();
1481 QSizeF firstRoleSize
= m_stretchedVisibleRolesSizes
.value(role
);
1483 QSizeF dynamicItemSize
= m_itemSize
;
1485 if (dynamicItemSize
.width() <= 0) {
1486 const qreal requiredWidth
= visibleRolesSizesWidthSum();
1487 const qreal availableWidth
= size().width();
1488 if (requiredWidth
< availableWidth
) {
1489 // Stretch the first role to use the whole width for the item
1490 firstRoleSize
.rwidth() += availableWidth
- requiredWidth
;
1491 m_stretchedVisibleRolesSizes
.insert(role
, firstRoleSize
);
1493 dynamicItemSize
.setWidth(qMax(requiredWidth
, availableWidth
));
1496 if (dynamicItemSize
.height() <= 0) {
1497 const qreal requiredHeight
= visibleRolesSizesHeightSum();
1498 const qreal availableHeight
= size().height();
1499 if (requiredHeight
< availableHeight
) {
1500 // Stretch the first role to use the whole height for the item
1501 firstRoleSize
.rheight() += availableHeight
- requiredHeight
;
1502 m_stretchedVisibleRolesSizes
.insert(role
, firstRoleSize
);
1504 dynamicItemSize
.setHeight(qMax(requiredHeight
, availableHeight
));
1507 m_layouter
->setItemSize(dynamicItemSize
);
1510 m_header
->setVisibleRolesWidths(headerRolesWidths());
1511 m_header
->resize(dynamicItemSize
.width(), m_header
->size().height());
1514 // Update the role sizes for all visible widgets
1515 foreach (KItemListWidget
* widget
, visibleItemListWidgets()) {
1516 widget
->setVisibleRolesSizes(m_stretchedVisibleRolesSizes
);
1520 void KItemListView::setHeaderShown(bool show
)
1522 if (show
&& !m_header
) {
1523 m_header
= new KItemListHeader(this);
1524 m_header
->setPos(0, 0);
1525 m_header
->setModel(m_model
);
1526 m_header
->setVisibleRoles(m_visibleRoles
);
1527 m_header
->setVisibleRolesWidths(headerRolesWidths());
1528 m_header
->setZValue(1);
1530 m_useHeaderWidths
= false;
1532 connect(m_header
, SIGNAL(visibleRoleWidthChanged(QByteArray
,qreal
,qreal
)),
1533 this, SLOT(slotVisibleRoleWidthChanged(QByteArray
,qreal
,qreal
)));
1535 m_layouter
->setHeaderHeight(m_header
->size().height());
1536 } else if (!show
&& m_header
) {
1539 m_useHeaderWidths
= false;
1540 m_layouter
->setHeaderHeight(0);
1544 qreal
KItemListView::visibleRolesSizesWidthSum() const
1547 QHashIterator
<QByteArray
, QSizeF
> it(m_visibleRolesSizes
);
1548 while (it
.hasNext()) {
1550 widthSum
+= it
.value().width();
1555 qreal
KItemListView::visibleRolesSizesHeightSum() const
1557 qreal heightSum
= 0;
1558 QHashIterator
<QByteArray
, QSizeF
> it(m_visibleRolesSizes
);
1559 while (it
.hasNext()) {
1561 heightSum
+= it
.value().height();
1566 int KItemListView::calculateAutoScrollingIncrement(int pos
, int range
, int oldInc
)
1570 const int minSpeed
= 4;
1571 const int maxSpeed
= 128;
1572 const int speedLimiter
= 96;
1573 const int autoScrollBorder
= 64;
1575 // Limit the increment that is allowed to be added in comparison to 'oldInc'.
1576 // This assures that the autoscrolling speed grows gradually.
1577 const int incLimiter
= 1;
1579 if (pos
< autoScrollBorder
) {
1580 inc
= -minSpeed
+ qAbs(pos
- autoScrollBorder
) * (pos
- autoScrollBorder
) / speedLimiter
;
1581 inc
= qMax(inc
, -maxSpeed
);
1582 inc
= qMax(inc
, oldInc
- incLimiter
);
1583 } else if (pos
> range
- autoScrollBorder
) {
1584 inc
= minSpeed
+ qAbs(pos
- range
+ autoScrollBorder
) * (pos
- range
+ autoScrollBorder
) / speedLimiter
;
1585 inc
= qMin(inc
, maxSpeed
);
1586 inc
= qMin(inc
, oldInc
+ incLimiter
);
1594 KItemListCreatorBase::~KItemListCreatorBase()
1596 qDeleteAll(m_recycleableWidgets
);
1597 qDeleteAll(m_createdWidgets
);
1600 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget
* widget
)
1602 m_createdWidgets
.insert(widget
);
1605 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget
* widget
)
1607 Q_ASSERT(m_createdWidgets
.contains(widget
));
1608 m_createdWidgets
.remove(widget
);
1610 if (m_recycleableWidgets
.count() < 100) {
1611 m_recycleableWidgets
.append(widget
);
1612 widget
->setVisible(false);
1618 QGraphicsWidget
* KItemListCreatorBase::popRecycleableWidget()
1620 if (m_recycleableWidgets
.isEmpty()) {
1624 QGraphicsWidget
* widget
= m_recycleableWidgets
.takeLast();
1625 m_createdWidgets
.insert(widget
);
1629 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
1633 void KItemListWidgetCreatorBase::recycle(KItemListWidget
* widget
)
1635 widget
->setOpacity(1.0);
1636 pushRecycleableWidget(widget
);
1639 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
1643 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader
* header
)
1645 header
->setOpacity(1.0);
1646 pushRecycleableWidget(header
);
1649 #include "kitemlistview.moc"