2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * Based on the Itemviews NG project from Trolltech Labs
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "kitemlistview.h"
11 #ifndef QT_NO_ACCESSIBILITY
12 #include "accessibility/kitemlistcontaineraccessible.h"
13 #include "accessibility/kitemlistdelegateaccessible.h"
14 #include "accessibility/kitemlistviewaccessible.h"
16 #include "dolphindebug.h"
17 #include "kitemlistcontainer.h"
18 #include "kitemlistcontroller.h"
19 #include "kitemlistheader.h"
20 #include "kitemlistselectionmanager.h"
21 #include "kstandarditemlistwidget.h"
23 #include "private/kitemlistheaderwidget.h"
24 #include "private/kitemlistrubberband.h"
25 #include "private/kitemlistsizehintresolver.h"
26 #include "private/kitemlistviewlayouter.h"
30 #include <QElapsedTimer>
31 #include <QGraphicsSceneMouseEvent>
32 #include <QGraphicsView>
33 #include <QPropertyAnimation>
34 #include <QStyleOptionRubberBand>
36 #include <QVariantAnimation>
40 // Time in ms until reaching the autoscroll margin triggers
41 // an initial autoscrolling
42 const int InitialAutoScrollDelay
= 700;
44 // Delay in ms for triggering the next autoscroll
45 const int RepeatingAutoScrollDelay
= 1000 / 60;
47 // Copied from the Kirigami.Units.shortDuration
48 const int RubberFadeSpeed
= 150;
50 const char *RubberPropertyName
= "_kitemviews_rubberBandPosition";
53 #ifndef QT_NO_ACCESSIBILITY
54 QAccessibleInterface
*accessibleInterfaceFactory(const QString
&key
, QObject
*object
)
58 if (KItemListContainer
*container
= qobject_cast
<KItemListContainer
*>(object
)) {
59 if (auto controller
= container
->controller(); controller
) {
60 if (KItemListView
*view
= controller
->view(); view
&& view
->accessibleParent()) {
61 return view
->accessibleParent();
64 return new KItemListContainerAccessible(container
);
65 } else if (KItemListView
*view
= qobject_cast
<KItemListView
*>(object
)) {
66 return new KItemListViewAccessible(view
, view
->accessibleParent());
73 KItemListView::KItemListView(QGraphicsWidget
*parent
)
74 : QGraphicsWidget(parent
)
75 , m_enabledSelectionToggles(false)
77 , m_highlightEntireRow(false)
78 , m_alternateBackgrounds(false)
79 , m_supportsItemExpanding(false)
80 , m_editingRole(false)
81 , m_activeTransactions(0)
82 , m_endTransactionAnimationHint(Animation
)
84 , m_controller(nullptr)
87 , m_widgetCreator(nullptr)
88 , m_groupHeaderCreator(nullptr)
93 , m_scrollBarExtent(0)
95 , m_animation(nullptr)
96 , m_oldScrollOffset(0)
97 , m_oldMaximumScrollOffset(0)
99 , m_oldMaximumItemOffset(0)
100 , m_skipAutoScrollForRubberBand(false)
101 , m_rubberBand(nullptr)
102 , m_tapAndHoldIndicator(nullptr)
104 , m_autoScrollIncrement(0)
105 , m_autoScrollTimer(nullptr)
107 , m_headerWidget(nullptr)
108 , m_indicatorAnimation(nullptr)
109 , m_statusBarOffset(0)
111 , m_sizeHintResolver(nullptr)
113 setAcceptHoverEvents(true);
114 setAcceptTouchEvents(true);
116 m_sizeHintResolver
= new KItemListSizeHintResolver(this);
118 m_layouter
= new KItemListViewLayouter(m_sizeHintResolver
, this);
120 m_animation
= new KItemListViewAnimation(this);
121 connect(m_animation
, &KItemListViewAnimation::finished
, this, &KItemListView::slotAnimationFinished
);
123 m_rubberBand
= new KItemListRubberBand(this);
124 connect(m_rubberBand
, &KItemListRubberBand::activationChanged
, this, &KItemListView::slotRubberBandActivationChanged
);
126 m_tapAndHoldIndicator
= new KItemListRubberBand(this);
127 m_indicatorAnimation
= new QPropertyAnimation(m_tapAndHoldIndicator
, "endPosition", this);
128 connect(m_tapAndHoldIndicator
, &KItemListRubberBand::activationChanged
, this, [this](bool active
) {
130 m_indicatorAnimation
->setDuration(150);
131 m_indicatorAnimation
->setStartValue(QPointF(1, 1));
132 m_indicatorAnimation
->setEndValue(QPointF(40, 40));
133 m_indicatorAnimation
->start();
137 connect(m_tapAndHoldIndicator
, &KItemListRubberBand::endPositionChanged
, this, [this]() {
138 if (m_tapAndHoldIndicator
->isActive()) {
143 m_headerWidget
= new KItemListHeaderWidget(this);
144 m_headerWidget
->setVisible(false);
146 m_header
= new KItemListHeader(this);
148 #ifndef QT_NO_ACCESSIBILITY
149 QAccessible::installFactory(accessibleInterfaceFactory
);
153 KItemListView::~KItemListView()
155 // The group headers are children of the widgets created by
156 // widgetCreator(). So it is mandatory to delete the group headers
158 delete m_groupHeaderCreator
;
159 m_groupHeaderCreator
= nullptr;
161 delete m_widgetCreator
;
162 m_widgetCreator
= nullptr;
164 delete m_sizeHintResolver
;
165 m_sizeHintResolver
= nullptr;
168 void KItemListView::setScrollOffset(qreal offset
)
174 const qreal previousOffset
= m_layouter
->scrollOffset();
175 if (offset
== previousOffset
) {
179 m_layouter
->setScrollOffset(offset
);
180 m_animation
->setScrollOffset(offset
);
182 // Don't check whether the m_layoutTimer is active: Changing the
183 // scroll offset must always trigger a synchronous layout, otherwise
184 // the smooth-scrolling might get jerky.
185 doLayout(NoAnimation
);
186 onScrollOffsetChanged(offset
, previousOffset
);
189 qreal
KItemListView::scrollOffset() const
191 return m_layouter
->scrollOffset();
194 qreal
KItemListView::maximumScrollOffset() const
196 return m_layouter
->maximumScrollOffset() + m_statusBarOffset
;
199 void KItemListView::setItemOffset(qreal offset
)
201 if (m_layouter
->itemOffset() == offset
) {
205 m_layouter
->setItemOffset(offset
);
206 if (m_headerWidget
->isVisible()) {
207 m_headerWidget
->setOffset(offset
);
210 // Don't check whether the m_layoutTimer is active: Changing the
211 // item offset must always trigger a synchronous layout, otherwise
212 // the smooth-scrolling might get jerky.
213 doLayout(NoAnimation
);
216 qreal
KItemListView::itemOffset() const
218 return m_layouter
->itemOffset();
221 qreal
KItemListView::maximumItemOffset() const
223 return m_layouter
->maximumItemOffset();
226 int KItemListView::maximumVisibleItems() const
228 return m_layouter
->maximumVisibleItems();
231 void KItemListView::setVisibleRoles(const QList
<QByteArray
> &roles
)
233 const QList
<QByteArray
> previousRoles
= m_visibleRoles
;
234 m_visibleRoles
= roles
;
235 onVisibleRolesChanged(roles
, previousRoles
);
237 m_sizeHintResolver
->clearCache();
238 m_layouter
->markAsDirty();
240 if (m_itemSize
.isEmpty()) {
241 m_headerWidget
->setColumns(roles
);
242 updatePreferredColumnWidths();
243 if (!m_headerWidget
->automaticColumnResizing()) {
244 // The column-width of new roles are still 0. Apply the preferred
245 // column-width as default with.
246 for (const QByteArray
&role
: std::as_const(m_visibleRoles
)) {
247 if (m_headerWidget
->columnWidth(role
) == 0) {
248 const qreal width
= m_headerWidget
->preferredColumnWidth(role
);
249 m_headerWidget
->setColumnWidth(role
, width
);
253 applyColumnWidthsFromHeader();
257 const bool alternateBackgroundsChanged
=
258 m_itemSize
.isEmpty() && ((roles
.count() > 1 && previousRoles
.count() <= 1) || (roles
.count() <= 1 && previousRoles
.count() > 1));
260 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
261 while (it
.hasNext()) {
263 KItemListWidget
*widget
= it
.value();
264 widget
->setVisibleRoles(roles
);
265 if (alternateBackgroundsChanged
) {
266 updateAlternateBackgroundForWidget(widget
);
270 doLayout(NoAnimation
);
273 QList
<QByteArray
> KItemListView::visibleRoles() const
275 return m_visibleRoles
;
278 void KItemListView::setAutoScroll(bool enabled
)
280 if (enabled
&& !m_autoScrollTimer
) {
281 m_autoScrollTimer
= new QTimer(this);
282 m_autoScrollTimer
->setSingleShot(true);
283 connect(m_autoScrollTimer
, &QTimer::timeout
, this, &KItemListView::triggerAutoScrolling
);
284 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
285 } else if (!enabled
&& m_autoScrollTimer
) {
286 delete m_autoScrollTimer
;
287 m_autoScrollTimer
= nullptr;
291 bool KItemListView::autoScroll() const
293 return m_autoScrollTimer
!= nullptr;
296 void KItemListView::setEnabledSelectionToggles(bool enabled
)
298 if (m_enabledSelectionToggles
!= enabled
) {
299 m_enabledSelectionToggles
= enabled
;
301 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
302 while (it
.hasNext()) {
304 it
.value()->setEnabledSelectionToggle(enabled
);
309 bool KItemListView::enabledSelectionToggles() const
311 return m_enabledSelectionToggles
;
314 KItemListController
*KItemListView::controller() const
319 KItemModelBase
*KItemListView::model() const
324 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase
*widgetCreator
)
326 delete m_widgetCreator
;
327 m_widgetCreator
= widgetCreator
;
330 KItemListWidgetCreatorBase
*KItemListView::widgetCreator() const
332 if (!m_widgetCreator
) {
333 m_widgetCreator
= defaultWidgetCreator();
335 return m_widgetCreator
;
338 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
*groupHeaderCreator
)
340 delete m_groupHeaderCreator
;
341 m_groupHeaderCreator
= groupHeaderCreator
;
344 KItemListGroupHeaderCreatorBase
*KItemListView::groupHeaderCreator() const
346 if (!m_groupHeaderCreator
) {
347 m_groupHeaderCreator
= defaultGroupHeaderCreator();
349 return m_groupHeaderCreator
;
352 #ifndef QT_NO_ACCESSIBILITY
353 void KItemListView::setAccessibleParentsObject(KItemListContainer
*accessibleParentsObject
)
355 Q_ASSERT(!m_accessibleParent
);
356 m_accessibleParent
= new KItemListContainerAccessible(accessibleParentsObject
);
358 KItemListContainerAccessible
*KItemListView::accessibleParent()
360 Q_CHECK_PTR(m_accessibleParent
); // We always want the accessibility tree/hierarchy to be complete.
361 return m_accessibleParent
;
365 QSizeF
KItemListView::itemSize() const
370 const KItemListStyleOption
&KItemListView::styleOption() const
372 return m_styleOption
;
375 void KItemListView::setGeometry(const QRectF
&rect
)
377 QGraphicsWidget::setGeometry(rect
);
383 const QSizeF newSize
= rect
.size();
384 if (m_itemSize
.isEmpty()) {
385 m_headerWidget
->resize(rect
.width(), m_headerWidget
->size().height());
386 if (m_headerWidget
->automaticColumnResizing()) {
387 applyAutomaticColumnWidths();
389 const qreal requiredWidth
= m_headerWidget
->leftPadding() + columnWidthsSum() + m_headerWidget
->rightPadding();
390 const QSizeF
dynamicItemSize(qMax(newSize
.width(), requiredWidth
), m_itemSize
.height());
391 m_layouter
->setItemSize(dynamicItemSize
);
395 m_layouter
->setSize(newSize
);
396 // We don't animate the moving of the items here because
397 // it would look like the items are slow to find their position.
398 doLayout(NoAnimation
);
401 qreal
KItemListView::scrollSingleStep() const
403 const QFontMetrics
metrics(font());
404 return metrics
.height();
407 qreal
KItemListView::verticalPageStep() const
409 qreal headerHeight
= 0;
410 if (m_headerWidget
->isVisible()) {
411 headerHeight
= m_headerWidget
->size().height();
413 return size().height() - headerHeight
;
416 std::optional
<int> KItemListView::itemAt(const QPointF
&pos
) const
418 if (headerBoundaries().contains(pos
)) {
422 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
423 while (it
.hasNext()) {
426 const KItemListWidget
*widget
= it
.value();
427 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
428 if (widget
->contains(mappedPos
)) {
436 bool KItemListView::isAboveSelectionToggle(int index
, const QPointF
&pos
) const
438 if (!m_enabledSelectionToggles
) {
442 const KItemListWidget
*widget
= m_visibleItems
.value(index
);
444 const QRectF selectionToggleRect
= widget
->selectionToggleRect();
445 if (!selectionToggleRect
.isEmpty()) {
446 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
447 return selectionToggleRect
.contains(mappedPos
);
453 bool KItemListView::isAboveExpansionToggle(int index
, const QPointF
&pos
) const
455 const KItemListWidget
*widget
= m_visibleItems
.value(index
);
457 const QRectF expansionToggleRect
= widget
->expansionToggleRect();
458 if (!expansionToggleRect
.isEmpty()) {
459 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
460 return expansionToggleRect
.contains(mappedPos
);
466 bool KItemListView::isAboveText(int index
, const QPointF
&pos
) const
468 const KItemListWidget
*widget
= m_visibleItems
.value(index
);
470 const QRectF
&textRect
= widget
->textRect();
471 if (!textRect
.isEmpty()) {
472 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
473 return textRect
.contains(mappedPos
);
479 int KItemListView::firstVisibleIndex() const
481 return m_layouter
->firstVisibleIndex();
484 int KItemListView::lastVisibleIndex() const
486 return m_layouter
->lastVisibleIndex();
489 void KItemListView::calculateItemSizeHints(QVector
<std::pair
<qreal
, bool>> &logicalHeightHints
, qreal
&logicalWidthHint
) const
491 widgetCreator()->calculateItemSizeHints(logicalHeightHints
, logicalWidthHint
, this);
494 void KItemListView::setSupportsItemExpanding(bool supportsExpanding
)
496 if (m_supportsItemExpanding
!= supportsExpanding
) {
497 m_supportsItemExpanding
= supportsExpanding
;
498 updateSiblingsInformation();
499 onSupportsItemExpandingChanged(supportsExpanding
);
503 bool KItemListView::supportsItemExpanding() const
505 return m_supportsItemExpanding
;
508 void KItemListView::setHighlightEntireRow(bool highlightEntireRow
)
510 if (m_highlightEntireRow
!= highlightEntireRow
) {
511 m_highlightEntireRow
= highlightEntireRow
;
512 onHighlightEntireRowChanged(highlightEntireRow
);
516 bool KItemListView::highlightEntireRow() const
518 return m_highlightEntireRow
;
521 void KItemListView::setAlternateBackgrounds(bool alternate
)
523 if (m_alternateBackgrounds
!= alternate
) {
524 m_alternateBackgrounds
= alternate
;
525 updateAlternateBackgrounds();
529 bool KItemListView::alternateBackgrounds() const
531 return m_alternateBackgrounds
;
534 QRectF
KItemListView::itemRect(int index
) const
536 return m_layouter
->itemRect(index
);
539 QRectF
KItemListView::itemContextRect(int index
) const
543 const KItemListWidget
*widget
= m_visibleItems
.value(index
);
545 contextRect
= widget
->selectionRectCore();
546 contextRect
.translate(itemRect(index
).topLeft());
552 bool KItemListView::isElided(int index
) const
554 return m_sizeHintResolver
->isElided(index
);
557 void KItemListView::scrollToItem(int index
, ViewItemPosition viewItemPosition
)
559 QRectF viewGeometry
= geometry();
560 if (m_headerWidget
->isVisible()) {
561 const qreal headerHeight
= m_headerWidget
->size().height();
562 viewGeometry
.adjust(0, headerHeight
, 0, 0);
564 if (m_statusBarOffset
!= 0) {
565 viewGeometry
.adjust(0, 0, 0, -m_statusBarOffset
);
567 QRectF currentRect
= itemRect(index
);
569 if (layoutDirection() == Qt::RightToLeft
&& scrollOrientation() == Qt::Horizontal
) {
570 currentRect
.moveLeft(m_layouter
->size().width() - currentRect
.right());
573 // Fix for Bug 311099 - View the underscore when using Ctrl + PageDown
574 currentRect
.adjust(-m_styleOption
.horizontalMargin
, -m_styleOption
.verticalMargin
, m_styleOption
.horizontalMargin
, m_styleOption
.verticalMargin
);
577 switch (scrollOrientation()) {
579 if (currentRect
.top() < viewGeometry
.top() || currentRect
.bottom() > viewGeometry
.bottom()) {
580 switch (viewItemPosition
) {
582 offset
= currentRect
.top() - viewGeometry
.top();
585 offset
= 0.5 * (currentRect
.top() + currentRect
.bottom() - (viewGeometry
.top() + viewGeometry
.bottom()));
588 offset
= currentRect
.bottom() - viewGeometry
.bottom();
591 if (currentRect
.top() < viewGeometry
.top()) {
592 offset
= currentRect
.top() - viewGeometry
.top();
594 if (currentRect
.bottom() > viewGeometry
.bottom() + offset
) {
595 offset
+= currentRect
.bottom() - viewGeometry
.bottom() - offset
;
604 if (currentRect
.left() < viewGeometry
.left() || currentRect
.right() > viewGeometry
.right()) {
605 switch (viewItemPosition
) {
607 if (layoutDirection() == Qt::RightToLeft
) {
608 offset
= currentRect
.right() - viewGeometry
.right();
610 offset
= currentRect
.left() - viewGeometry
.left();
614 offset
= 0.5 * (currentRect
.left() + currentRect
.right() - (viewGeometry
.left() + viewGeometry
.right()));
617 if (layoutDirection() == Qt::RightToLeft
) {
618 offset
= currentRect
.left() - viewGeometry
.left();
620 offset
= currentRect
.right() - viewGeometry
.right();
624 if (layoutDirection() == Qt::RightToLeft
) {
625 if (currentRect
.left() < viewGeometry
.left()) {
626 offset
= currentRect
.left() - viewGeometry
.left();
628 if (currentRect
.right() > viewGeometry
.right() + offset
) {
629 offset
+= currentRect
.right() - viewGeometry
.right() - offset
;
632 if (currentRect
.right() > viewGeometry
.right()) {
633 offset
= currentRect
.right() - viewGeometry
.right();
635 if (currentRect
.left() < viewGeometry
.left() + offset
) {
636 offset
+= currentRect
.left() - viewGeometry
.left() - offset
;
649 if (!qFuzzyIsNull(offset
)) {
650 Q_EMIT
scrollTo(scrollOffset() + offset
);
654 Q_EMIT
scrollingStopped();
657 void KItemListView::beginTransaction()
659 ++m_activeTransactions
;
660 if (m_activeTransactions
== 1) {
661 onTransactionBegin();
665 void KItemListView::endTransaction()
667 --m_activeTransactions
;
668 if (m_activeTransactions
< 0) {
669 m_activeTransactions
= 0;
670 qCWarning(DolphinDebug
) << "Mismatch between beginTransaction()/endTransaction()";
673 if (m_activeTransactions
== 0) {
675 doLayout(m_endTransactionAnimationHint
);
676 m_endTransactionAnimationHint
= Animation
;
680 bool KItemListView::isTransactionActive() const
682 return m_activeTransactions
> 0;
685 void KItemListView::setHeaderVisible(bool visible
)
687 if (visible
&& !m_headerWidget
->isVisible()) {
688 QStyleOptionHeader option
;
689 const QSize headerSize
= style()->sizeFromContents(QStyle::CT_HeaderSection
, &option
, QSize());
691 m_headerWidget
->setPos(0, 0);
692 m_headerWidget
->resize(size().width(), headerSize
.height());
693 m_headerWidget
->setModel(m_model
);
694 m_headerWidget
->setColumns(m_visibleRoles
);
695 m_headerWidget
->setZValue(1);
697 connect(m_headerWidget
, &KItemListHeaderWidget::columnWidthChanged
, this, &KItemListView::slotHeaderColumnWidthChanged
);
698 connect(m_headerWidget
, &KItemListHeaderWidget::sidePaddingChanged
, this, &KItemListView::slotSidePaddingChanged
);
699 connect(m_headerWidget
, &KItemListHeaderWidget::columnMoved
, this, &KItemListView::slotHeaderColumnMoved
);
700 connect(m_headerWidget
, &KItemListHeaderWidget::sortOrderChanged
, this, &KItemListView::sortOrderChanged
);
701 connect(m_headerWidget
, &KItemListHeaderWidget::sortRoleChanged
, this, &KItemListView::sortRoleChanged
);
702 connect(m_headerWidget
, &KItemListHeaderWidget::columnHovered
, this, &KItemListView::columnHovered
);
703 connect(m_headerWidget
, &KItemListHeaderWidget::columnUnHovered
, this, &KItemListView::columnUnHovered
);
705 m_layouter
->setHeaderHeight(headerSize
.height());
706 m_headerWidget
->setVisible(true);
707 } else if (!visible
&& m_headerWidget
->isVisible()) {
708 disconnect(m_headerWidget
, &KItemListHeaderWidget::columnWidthChanged
, this, &KItemListView::slotHeaderColumnWidthChanged
);
709 disconnect(m_headerWidget
, &KItemListHeaderWidget::sidePaddingChanged
, this, &KItemListView::slotSidePaddingChanged
);
710 disconnect(m_headerWidget
, &KItemListHeaderWidget::columnMoved
, this, &KItemListView::slotHeaderColumnMoved
);
711 disconnect(m_headerWidget
, &KItemListHeaderWidget::sortOrderChanged
, this, &KItemListView::sortOrderChanged
);
712 disconnect(m_headerWidget
, &KItemListHeaderWidget::sortRoleChanged
, this, &KItemListView::sortRoleChanged
);
713 disconnect(m_headerWidget
, &KItemListHeaderWidget::columnHovered
, this, &KItemListView::columnHovered
);
714 disconnect(m_headerWidget
, &KItemListHeaderWidget::columnUnHovered
, this, &KItemListView::columnUnHovered
);
716 m_layouter
->setHeaderHeight(0);
717 m_headerWidget
->setVisible(false);
721 bool KItemListView::isHeaderVisible() const
723 return m_headerWidget
->isVisible();
726 KItemListHeader
*KItemListView::header() const
731 QPixmap
KItemListView::createDragPixmap(const KItemSet
&indexes
) const
735 if (indexes
.count() == 1) {
736 KItemListWidget
*item
= m_visibleItems
.value(indexes
.first());
737 QGraphicsView
*graphicsView
= scene()->views()[0];
738 if (item
&& graphicsView
) {
739 pixmap
= item
->createDragPixmap(nullptr, graphicsView
);
742 // TODO: Not implemented yet. Probably extend the interface
743 // from KItemListWidget::createDragPixmap() to return a pixmap
744 // that can be used for multiple indexes.
750 void KItemListView::editRole(int index
, const QByteArray
&role
)
752 KStandardItemListWidget
*widget
= qobject_cast
<KStandardItemListWidget
*>(m_visibleItems
.value(index
));
756 if (m_editingRole
|| m_animation
->isStarted(widget
)) {
757 Q_EMIT widget
->roleEditingCanceled(index
, role
, QVariant());
761 m_editingRole
= true;
762 m_controller
->selectionManager()->setCurrentItem(index
);
763 widget
->setEditedRole(role
);
765 connect(widget
, &KItemListWidget::roleEditingCanceled
, this, &KItemListView::slotRoleEditingCanceled
);
766 connect(widget
, &KItemListWidget::roleEditingFinished
, this, &KItemListView::slotRoleEditingFinished
);
768 connect(this, &KItemListView::scrollOffsetChanged
, widget
, &KStandardItemListWidget::finishRoleEditing
);
771 void KItemListView::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
)
773 QGraphicsWidget::paint(painter
, option
, widget
);
775 for (auto animation
: std::as_const(m_rubberBandAnimations
)) {
776 QRectF rubberBandRect
= animation
->property(RubberPropertyName
).toRectF();
778 const QPointF topLeft
= rubberBandRect
.topLeft();
779 if (scrollOrientation() == Qt::Vertical
) {
780 rubberBandRect
.moveTo(topLeft
.x(), topLeft
.y() - scrollOffset());
782 rubberBandRect
.moveTo(topLeft
.x() - scrollOffset(), topLeft
.y());
785 QStyleOptionRubberBand opt
;
786 initStyleOption(&opt
);
787 opt
.shape
= QRubberBand::Rectangle
;
789 opt
.rect
= rubberBandRect
.toRect();
793 painter
->setOpacity(animation
->currentValue().toReal());
794 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
799 if (m_rubberBand
->isActive()) {
800 QRectF rubberBandRect
= QRectF(m_rubberBand
->startPosition(), m_rubberBand
->endPosition()).normalized();
802 const QPointF topLeft
= rubberBandRect
.topLeft();
803 if (scrollOrientation() == Qt::Vertical
) {
804 rubberBandRect
.moveTo(topLeft
.x(), topLeft
.y() - scrollOffset());
806 rubberBandRect
.moveTo(topLeft
.x() - scrollOffset(), topLeft
.y());
809 QStyleOptionRubberBand opt
;
810 initStyleOption(&opt
);
811 opt
.shape
= QRubberBand::Rectangle
;
813 opt
.rect
= rubberBandRect
.toRect();
814 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
817 if (m_tapAndHoldIndicator
->isActive()) {
818 const QPointF indicatorSize
= m_tapAndHoldIndicator
->endPosition();
819 const QRectF rubberBandRect
=
820 QRectF(m_tapAndHoldIndicator
->startPosition() - indicatorSize
, (m_tapAndHoldIndicator
->startPosition()) + indicatorSize
).normalized();
821 QStyleOptionRubberBand opt
;
822 initStyleOption(&opt
);
823 opt
.shape
= QRubberBand::Rectangle
;
825 opt
.rect
= rubberBandRect
.toRect();
826 style()->drawControl(QStyle::CE_RubberBand
, &opt
, painter
);
829 if (!m_dropIndicator
.isEmpty()) {
830 const QRectF r
= m_dropIndicator
.toRect();
832 QColor color
= palette().brush(QPalette::Normal
, QPalette::Text
).color();
833 painter
->setPen(color
);
835 // TODO: The following implementation works only for a vertical scroll-orientation
836 // and assumes a height of the m_draggingInsertIndicator of 1.
837 Q_ASSERT(r
.height() == 1);
838 painter
->drawLine(r
.left() + 1, r
.top(), r
.right() - 1, r
.top());
841 painter
->setPen(color
);
842 painter
->drawRect(r
.left(), r
.top() - 1, r
.width() - 1, 2);
846 void KItemListView::setStatusBarOffset(int offset
)
848 if (m_statusBarOffset
!= offset
) {
849 m_statusBarOffset
= offset
;
851 m_layouter
->setStatusBarOffset(offset
);
856 QVariant
KItemListView::itemChange(GraphicsItemChange change
, const QVariant
&value
)
858 if (change
== QGraphicsItem::ItemSceneHasChanged
&& scene()) {
859 if (!scene()->views().isEmpty()) {
860 m_styleOption
.palette
= scene()->views().at(0)->palette();
863 return QGraphicsItem::itemChange(change
, value
);
866 void KItemListView::setItemSize(const QSizeF
&size
)
868 const QSizeF previousSize
= m_itemSize
;
869 if (size
== previousSize
) {
873 // Skip animations when the number of rows or columns
874 // are changed in the grid layout. Although the animation
875 // engine can handle this usecase, it looks obtrusive.
876 const bool animate
= !changesItemGridLayout(m_layouter
->size(), size
, m_layouter
->itemMargin());
878 const bool alternateBackgroundsChanged
= m_alternateBackgrounds
&& ((m_itemSize
.isEmpty() && !size
.isEmpty()) || (!m_itemSize
.isEmpty() && size
.isEmpty()));
882 if (alternateBackgroundsChanged
) {
883 // For an empty item size alternate backgrounds are drawn if more than
884 // one role is shown. Assure that the backgrounds for visible items are
885 // updated when changing the size in this context.
886 updateAlternateBackgrounds();
889 if (size
.isEmpty()) {
890 if (m_headerWidget
->automaticColumnResizing()) {
891 updatePreferredColumnWidths();
893 // Only apply the changed height and respect the header widths
895 const qreal currentWidth
= m_layouter
->itemSize().width();
896 const QSizeF
newSize(currentWidth
, size
.height());
897 m_layouter
->setItemSize(newSize
);
900 m_layouter
->setItemSize(size
);
903 m_sizeHintResolver
->clearCache();
904 doLayout(animate
? Animation
: NoAnimation
);
905 onItemSizeChanged(size
, previousSize
);
908 void KItemListView::setStyleOption(const KItemListStyleOption
&option
)
910 if (m_styleOption
== option
) {
914 const KItemListStyleOption previousOption
= m_styleOption
;
915 m_styleOption
= option
;
918 const QSizeF
margin(option
.horizontalMargin
, option
.verticalMargin
);
919 if (margin
!= m_layouter
->itemMargin()) {
920 // Skip animations when the number of rows or columns
921 // are changed in the grid layout. Although the animation
922 // engine can handle this usecase, it looks obtrusive.
923 animate
= !changesItemGridLayout(m_layouter
->size(), m_layouter
->itemSize(), margin
);
924 m_layouter
->setItemMargin(margin
);
928 updateGroupHeaderHeight();
931 if (animate
&& (previousOption
.maxTextLines
!= option
.maxTextLines
|| previousOption
.maxTextWidth
!= option
.maxTextWidth
)) {
932 // Animating a change of the maximum text size just results in expensive
933 // temporary eliding and clipping operations and does not look good visually.
937 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
938 while (it
.hasNext()) {
940 it
.value()->setStyleOption(option
);
943 m_sizeHintResolver
->clearCache();
944 m_layouter
->markAsDirty();
945 doLayout(animate
? Animation
: NoAnimation
);
947 if (m_itemSize
.isEmpty()) {
948 updatePreferredColumnWidths();
951 onStyleOptionChanged(option
, previousOption
);
954 void KItemListView::setScrollOrientation(Qt::Orientation orientation
)
956 const Qt::Orientation previousOrientation
= m_layouter
->scrollOrientation();
957 if (orientation
== previousOrientation
) {
961 m_layouter
->setScrollOrientation(orientation
);
962 m_animation
->setScrollOrientation(orientation
);
963 m_sizeHintResolver
->clearCache();
966 QMutableHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_visibleGroups
);
967 while (it
.hasNext()) {
969 it
.value()->setScrollOrientation(orientation
);
971 updateGroupHeaderHeight();
974 doLayout(NoAnimation
);
976 onScrollOrientationChanged(orientation
, previousOrientation
);
977 Q_EMIT
scrollOrientationChanged(orientation
, previousOrientation
);
980 Qt::Orientation
KItemListView::scrollOrientation() const
982 return m_layouter
->scrollOrientation();
985 KItemListWidgetCreatorBase
*KItemListView::defaultWidgetCreator() const
990 KItemListGroupHeaderCreatorBase
*KItemListView::defaultGroupHeaderCreator() const
995 void KItemListView::initializeItemListWidget(KItemListWidget
*item
)
1000 bool KItemListView::itemSizeHintUpdateRequired(const QSet
<QByteArray
> &changedRoles
) const
1002 Q_UNUSED(changedRoles
)
1006 void KItemListView::onControllerChanged(KItemListController
*current
, KItemListController
*previous
)
1012 void KItemListView::onModelChanged(KItemModelBase
*current
, KItemModelBase
*previous
)
1018 void KItemListView::onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
1024 void KItemListView::onItemSizeChanged(const QSizeF
¤t
, const QSizeF
&previous
)
1030 void KItemListView::onScrollOffsetChanged(qreal current
, qreal previous
)
1036 void KItemListView::onVisibleRolesChanged(const QList
<QByteArray
> ¤t
, const QList
<QByteArray
> &previous
)
1042 void KItemListView::onStyleOptionChanged(const KItemListStyleOption
¤t
, const KItemListStyleOption
&previous
)
1048 void KItemListView::onHighlightEntireRowChanged(bool highlightEntireRow
)
1050 Q_UNUSED(highlightEntireRow
)
1053 void KItemListView::onSupportsItemExpandingChanged(bool supportsExpanding
)
1055 Q_UNUSED(supportsExpanding
)
1058 void KItemListView::onTransactionBegin()
1062 void KItemListView::onTransactionEnd()
1066 bool KItemListView::event(QEvent
*event
)
1068 switch (event
->type()) {
1069 case QEvent::PaletteChange
:
1073 case QEvent::FontChange
:
1077 case QEvent::FocusIn
:
1078 focusInEvent(static_cast<QFocusEvent
*>(event
));
1083 case QEvent::FocusOut
:
1084 focusOutEvent(static_cast<QFocusEvent
*>(event
));
1090 // Forward all other events to the controller and handle them there
1091 if (!m_editingRole
&& m_controller
&& m_controller
->processEvent(event
, transform())) {
1097 return QGraphicsWidget::event(event
);
1100 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent
*event
)
1102 m_mousePos
= transform().map(event
->pos());
1106 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent
*event
)
1108 QGraphicsWidget::mouseMoveEvent(event
);
1110 m_mousePos
= transform().map(event
->pos());
1111 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
1112 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
1116 void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent
*event
)
1118 event
->setAccepted(true);
1119 setAutoScroll(true);
1122 void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent
*event
)
1124 QGraphicsWidget::dragMoveEvent(event
);
1126 m_mousePos
= transform().map(event
->pos());
1127 if (m_autoScrollTimer
&& !m_autoScrollTimer
->isActive()) {
1128 m_autoScrollTimer
->start(InitialAutoScrollDelay
);
1132 void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent
*event
)
1134 QGraphicsWidget::dragLeaveEvent(event
);
1135 setAutoScroll(false);
1138 void KItemListView::dropEvent(QGraphicsSceneDragDropEvent
*event
)
1140 QGraphicsWidget::dropEvent(event
);
1141 setAutoScroll(false);
1144 QList
<KItemListWidget
*> KItemListView::visibleItemListWidgets() const
1146 return m_visibleItems
.values();
1149 void KItemListView::updateFont()
1151 if (scene() && !scene()->views().isEmpty()) {
1152 KItemListStyleOption option
= styleOption();
1153 option
.font
= scene()->views().first()->font();
1154 option
.fontMetrics
= QFontMetrics(option
.font
);
1156 setStyleOption(option
);
1160 void KItemListView::updatePalette()
1162 KItemListStyleOption option
= styleOption();
1163 option
.palette
= palette();
1164 setStyleOption(option
);
1167 void KItemListView::slotItemsInserted(const KItemRangeList
&itemRanges
)
1169 if (m_itemSize
.isEmpty()) {
1170 updatePreferredColumnWidths(itemRanges
);
1173 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
1174 if (hasMultipleRanges
) {
1178 m_layouter
->markAsDirty();
1180 m_sizeHintResolver
->itemsInserted(itemRanges
);
1182 int previouslyInsertedCount
= 0;
1183 for (const KItemRange
&range
: itemRanges
) {
1184 // range.index is related to the model before anything has been inserted.
1185 // As in each loop the current item-range gets inserted the index must
1186 // be increased by the already previously inserted items.
1187 const int index
= range
.index
+ previouslyInsertedCount
;
1188 const int count
= range
.count
;
1189 if (index
< 0 || count
<= 0) {
1190 qCWarning(DolphinDebug
) << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
1193 previouslyInsertedCount
+= count
;
1195 // Determine which visible items must be moved
1196 QList
<int> itemsToMove
;
1197 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
1198 while (it
.hasNext()) {
1200 const int visibleItemIndex
= it
.key();
1201 if (visibleItemIndex
>= index
) {
1202 itemsToMove
.append(visibleItemIndex
);
1206 // Update the indexes of all KItemListWidget instances that are located
1207 // after the inserted items. It is important to adjust the indexes in the order
1208 // from the highest index to the lowest index to prevent overlaps when setting the new index.
1209 std::sort(itemsToMove
.begin(), itemsToMove
.end());
1210 for (int i
= itemsToMove
.count() - 1; i
>= 0; --i
) {
1211 KItemListWidget
*widget
= m_visibleItems
.value(itemsToMove
[i
]);
1213 const int newIndex
= widget
->index() + count
;
1214 if (hasMultipleRanges
) {
1215 setWidgetIndex(widget
, newIndex
);
1217 // Try to animate the moving of the item
1218 moveWidgetToIndex(widget
, newIndex
);
1222 if (m_model
->count() == count
&& m_activeTransactions
== 0) {
1223 // Check whether a scrollbar is required to show the inserted items. In this case
1224 // the size of the layouter will be decreased before calling doLayout(): This prevents
1225 // an unnecessary temporary animation due to the geometry change of the inserted scrollbar.
1226 const bool verticalScrollOrientation
= (scrollOrientation() == Qt::Vertical
);
1227 const bool decreaseLayouterSize
= (verticalScrollOrientation
&& maximumScrollOffset() > size().height())
1228 || (!verticalScrollOrientation
&& maximumScrollOffset() > size().width());
1229 if (decreaseLayouterSize
) {
1230 const int scrollBarExtent
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
1232 int scrollbarSpacing
= 0;
1233 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
1234 scrollbarSpacing
= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
);
1237 QSizeF layouterSize
= m_layouter
->size();
1238 if (verticalScrollOrientation
) {
1239 layouterSize
.rwidth() -= scrollBarExtent
+ scrollbarSpacing
;
1241 layouterSize
.rheight() -= scrollBarExtent
+ scrollbarSpacing
;
1243 m_layouter
->setSize(layouterSize
);
1247 if (!hasMultipleRanges
) {
1248 doLayout(animateChangedItemCount(count
) ? Animation
: NoAnimation
, index
, count
);
1249 updateSiblingsInformation();
1254 m_controller
->selectionManager()->itemsInserted(itemRanges
);
1257 if (hasMultipleRanges
) {
1258 m_endTransactionAnimationHint
= NoAnimation
;
1261 updateSiblingsInformation();
1264 if (m_grouped
&& (hasMultipleRanges
|| itemRanges
.first().count
< m_model
->count())) {
1265 // In case if items of the same group have been inserted before an item that
1266 // currently represents the first item of the group, the group header of
1267 // this item must be removed.
1268 updateVisibleGroupHeaders();
1271 if (useAlternateBackgrounds()) {
1272 updateAlternateBackgrounds();
1276 void KItemListView::slotItemsRemoved(const KItemRangeList
&itemRanges
)
1278 if (m_itemSize
.isEmpty()) {
1279 // Don't pass the item-range: The preferred column-widths of
1280 // all items must be adjusted when removing items.
1281 updatePreferredColumnWidths();
1284 const bool hasMultipleRanges
= (itemRanges
.count() > 1);
1285 if (hasMultipleRanges
) {
1289 m_layouter
->markAsDirty();
1291 m_sizeHintResolver
->itemsRemoved(itemRanges
);
1293 for (int i
= itemRanges
.count() - 1; i
>= 0; --i
) {
1294 const KItemRange
&range
= itemRanges
[i
];
1295 const int index
= range
.index
;
1296 const int count
= range
.count
;
1297 if (index
< 0 || count
<= 0) {
1298 qCWarning(DolphinDebug
) << "Invalid item range (index:" << index
<< ", count:" << count
<< ")";
1302 const int firstRemovedIndex
= index
;
1303 const int lastRemovedIndex
= index
+ count
- 1;
1305 // Remember which items have to be moved because they are behind the removed range.
1306 QVector
<int> itemsToMove
;
1308 // Remove all KItemListWidget instances that got deleted
1309 // Iterate over a const copy because the container is mutated within the loop
1310 // directly and in `recycleWidget()` (https://bugs.kde.org/show_bug.cgi?id=428374)
1311 const auto visibleItems
= m_visibleItems
;
1312 for (KItemListWidget
*widget
: visibleItems
) {
1313 const int i
= widget
->index();
1314 if (i
< firstRemovedIndex
) {
1316 } else if (i
> lastRemovedIndex
) {
1317 itemsToMove
.append(i
);
1321 m_animation
->stop(widget
);
1322 // Stopping the animation might lead to recycling the widget if
1323 // it is invisible (see slotAnimationFinished()).
1324 // Check again whether it is still visible:
1325 if (!m_visibleItems
.contains(i
)) {
1329 if (m_model
->count() == 0 || hasMultipleRanges
|| !animateChangedItemCount(count
)) {
1330 // Remove the widget without animation
1331 recycleWidget(widget
);
1333 // Animate the removing of the items. Special case: When removing an item there
1334 // is no valid model index available anymore. For the
1335 // remove-animation the item gets removed from m_visibleItems but the widget
1336 // will stay alive until the animation has been finished and will
1337 // be recycled (deleted) in KItemListView::slotAnimationFinished().
1338 m_visibleItems
.remove(i
);
1339 widget
->setIndex(-1);
1340 m_animation
->start(widget
, KItemListViewAnimation::DeleteAnimation
);
1344 // Update the indexes of all KItemListWidget instances that are located
1345 // after the deleted items. It is important to update them in ascending
1346 // order to prevent overlaps when setting the new index.
1347 std::sort(itemsToMove
.begin(), itemsToMove
.end());
1348 for (int i
: std::as_const(itemsToMove
)) {
1349 KItemListWidget
*widget
= m_visibleItems
.value(i
);
1351 const int newIndex
= i
- count
;
1352 if (hasMultipleRanges
) {
1353 setWidgetIndex(widget
, newIndex
);
1355 // Try to animate the moving of the item
1356 moveWidgetToIndex(widget
, newIndex
);
1360 if (!hasMultipleRanges
) {
1361 // The decrease-layout-size optimization in KItemListView::slotItemsInserted()
1362 // assumes an updated geometry. If items are removed during an active transaction,
1363 // the transaction will be temporary deactivated so that doLayout() triggers a
1364 // geometry update if necessary.
1365 const int activeTransactions
= m_activeTransactions
;
1366 m_activeTransactions
= 0;
1367 doLayout(animateChangedItemCount(count
) ? Animation
: NoAnimation
, index
, -count
);
1368 m_activeTransactions
= activeTransactions
;
1369 updateSiblingsInformation();
1374 m_controller
->selectionManager()->itemsRemoved(itemRanges
);
1377 if (hasMultipleRanges
) {
1378 m_endTransactionAnimationHint
= NoAnimation
;
1380 updateSiblingsInformation();
1383 if (m_grouped
&& (hasMultipleRanges
|| m_model
->count() > 0)) {
1384 // In case if the first item of a group has been removed, the group header
1385 // must be applied to the next visible item.
1386 updateVisibleGroupHeaders();
1389 if (useAlternateBackgrounds()) {
1390 updateAlternateBackgrounds();
1394 void KItemListView::slotItemsMoved(const KItemRange
&itemRange
, const QList
<int> &movedToIndexes
)
1396 m_sizeHintResolver
->itemsMoved(itemRange
, movedToIndexes
);
1397 m_layouter
->markAsDirty();
1400 m_controller
->selectionManager()->itemsMoved(itemRange
, movedToIndexes
);
1403 const int firstVisibleMovedIndex
= qMax(firstVisibleIndex(), itemRange
.index
);
1404 const int lastVisibleMovedIndex
= qMin(lastVisibleIndex(), itemRange
.index
+ itemRange
.count
- 1);
1406 /// Represents an item that was moved while being edited.
1407 struct MovedEditedItem
{
1409 QByteArray editedRole
;
1411 std::optional
<MovedEditedItem
> movedEditedItem
;
1412 for (int index
= firstVisibleMovedIndex
; index
<= lastVisibleMovedIndex
; ++index
) {
1413 KItemListWidget
*widget
= m_visibleItems
.value(index
);
1415 if (m_editingRole
&& !widget
->editedRole().isEmpty()) {
1416 movedEditedItem
= {movedToIndexes
[index
- itemRange
.index
], widget
->editedRole()};
1417 disconnectRoleEditingSignals(index
);
1418 m_editingRole
= false;
1420 updateWidgetProperties(widget
, index
);
1421 initializeItemListWidget(widget
);
1425 doLayout(NoAnimation
);
1426 updateSiblingsInformation();
1428 if (movedEditedItem
) {
1429 editRole(movedEditedItem
->movedToIndex
, movedEditedItem
->editedRole
);
1433 void KItemListView::slotItemsChanged(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &roles
)
1435 const bool updateSizeHints
= itemSizeHintUpdateRequired(roles
);
1436 if (updateSizeHints
&& m_itemSize
.isEmpty()) {
1437 updatePreferredColumnWidths(itemRanges
);
1440 for (const KItemRange
&itemRange
: itemRanges
) {
1441 const int index
= itemRange
.index
;
1442 const int count
= itemRange
.count
;
1444 if (updateSizeHints
) {
1445 m_sizeHintResolver
->itemsChanged(index
, count
, roles
);
1446 m_layouter
->markAsDirty();
1449 // Apply the changed roles to the visible item-widgets
1450 const int lastIndex
= index
+ count
- 1;
1451 for (int i
= index
; i
<= lastIndex
; ++i
) {
1452 KItemListWidget
*widget
= m_visibleItems
.value(i
);
1454 widget
->setData(m_model
->data(i
), roles
);
1458 if (m_grouped
&& roles
.contains(m_model
->sortRole())) {
1459 // The sort-role has been changed which might result
1460 // in modified group headers
1461 updateVisibleGroupHeaders();
1462 doLayout(NoAnimation
);
1465 doLayout(NoAnimation
);
1468 void KItemListView::slotGroupsChanged()
1470 updateVisibleGroupHeaders();
1471 doLayout(NoAnimation
);
1472 updateSiblingsInformation();
1475 void KItemListView::slotGroupedSortingChanged(bool current
)
1477 m_grouped
= current
;
1478 m_layouter
->markAsDirty();
1481 updateGroupHeaderHeight();
1483 // Clear all visible headers. Note that the QHashIterator takes a copy of
1484 // m_visibleGroups. Therefore, it remains valid even if items are removed
1485 // from m_visibleGroups in recycleGroupHeaderForWidget().
1486 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_visibleGroups
);
1487 while (it
.hasNext()) {
1489 recycleGroupHeaderForWidget(it
.key());
1491 Q_ASSERT(m_visibleGroups
.isEmpty());
1494 if (useAlternateBackgrounds()) {
1495 // Changing the group mode requires to update the alternate backgrounds
1496 // as with the enabled group mode the altering is done on base of the first
1498 updateAlternateBackgrounds();
1500 updateSiblingsInformation();
1501 doLayout(NoAnimation
);
1504 void KItemListView::slotSortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
)
1509 updateVisibleGroupHeaders();
1510 doLayout(NoAnimation
);
1514 void KItemListView::slotSortRoleChanged(const QByteArray
¤t
, const QByteArray
&previous
)
1519 updateVisibleGroupHeaders();
1520 doLayout(NoAnimation
);
1524 void KItemListView::slotCurrentChanged(int current
, int previous
)
1526 // In SingleSelection mode (e.g., in the Places Panel), the current item is
1527 // always the selected item. It is not necessary to highlight the current item then.
1528 if (m_controller
->selectionBehavior() != KItemListController::SingleSelection
) {
1529 KItemListWidget
*previousWidget
= m_visibleItems
.value(previous
, nullptr);
1530 if (previousWidget
) {
1531 previousWidget
->setCurrent(false);
1534 KItemListWidget
*currentWidget
= m_visibleItems
.value(current
, nullptr);
1535 if (currentWidget
) {
1536 currentWidget
->setCurrent(true);
1539 #ifndef QT_NO_ACCESSIBILITY
1540 if (current
!= previous
&& QAccessible::isActive()) {
1541 static_cast<KItemListViewAccessible
*>(QAccessible::queryAccessibleInterface(this))->announceCurrentItem();
1546 void KItemListView::slotSelectionChanged(const KItemSet
¤t
, const KItemSet
&previous
)
1548 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
1549 while (it
.hasNext()) {
1551 const int index
= it
.key();
1552 KItemListWidget
*widget
= it
.value();
1553 const bool isSelected(current
.contains(index
));
1554 widget
->setSelected(isSelected
);
1556 #ifndef QT_NO_ACCESSIBILITY
1557 if (!QAccessible::isActive()) {
1560 // Let the screen reader announce "selected" or "not selected" for the active item.
1561 const bool wasSelected(previous
.contains(index
));
1562 if (isSelected
!= wasSelected
) {
1563 QAccessibleEvent
accessibleSelectionChangedEvent(this, QAccessible::SelectionAdd
);
1564 accessibleSelectionChangedEvent
.setChild(index
);
1565 QAccessible::updateAccessibility(&accessibleSelectionChangedEvent
);
1574 void KItemListView::slotAnimationFinished(QGraphicsWidget
*widget
, KItemListViewAnimation::AnimationType type
)
1576 KItemListWidget
*itemListWidget
= qobject_cast
<KItemListWidget
*>(widget
);
1577 Q_ASSERT(itemListWidget
);
1579 if (type
== KItemListViewAnimation::DeleteAnimation
) {
1580 // As we recycle the widget in this case it is important to assure that no
1581 // other animation has been started. This is a convention in KItemListView and
1582 // not a requirement defined by KItemListViewAnimation.
1583 Q_ASSERT(!m_animation
->isStarted(itemListWidget
));
1585 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
1586 // by m_visibleWidgets and must be deleted manually after the animation has
1588 recycleGroupHeaderForWidget(itemListWidget
);
1589 widgetCreator()->recycle(itemListWidget
);
1591 const int index
= itemListWidget
->index();
1592 const bool invisible
= (index
< m_layouter
->firstVisibleIndex()) || (index
> m_layouter
->lastVisibleIndex());
1593 if (invisible
&& !m_animation
->isStarted(itemListWidget
)) {
1594 recycleWidget(itemListWidget
);
1599 void KItemListView::slotRubberBandPosChanged()
1604 void KItemListView::slotRubberBandActivationChanged(bool active
)
1607 connect(m_rubberBand
, &KItemListRubberBand::startPositionChanged
, this, &KItemListView::slotRubberBandPosChanged
);
1608 connect(m_rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListView::slotRubberBandPosChanged
);
1609 m_skipAutoScrollForRubberBand
= true;
1611 QRectF rubberBandRect
= QRectF(m_rubberBand
->startPosition(), m_rubberBand
->endPosition()).normalized();
1613 auto animation
= new QVariantAnimation(this);
1614 animation
->setStartValue(1.0);
1615 animation
->setEndValue(0.0);
1616 animation
->setDuration(RubberFadeSpeed
);
1617 animation
->setProperty(RubberPropertyName
, rubberBandRect
);
1620 curve
.setType(QEasingCurve::BezierSpline
);
1621 curve
.addCubicBezierSegment(QPointF(0.4, 0.0), QPointF(1.0, 1.0), QPointF(1.0, 1.0));
1622 animation
->setEasingCurve(curve
);
1624 connect(animation
, &QVariantAnimation::valueChanged
, this, [=, this](const QVariant
&) {
1627 connect(animation
, &QVariantAnimation::finished
, this, [=, this]() {
1628 m_rubberBandAnimations
.removeAll(animation
);
1632 m_rubberBandAnimations
<< animation
;
1634 disconnect(m_rubberBand
, &KItemListRubberBand::startPositionChanged
, this, &KItemListView::slotRubberBandPosChanged
);
1635 disconnect(m_rubberBand
, &KItemListRubberBand::endPositionChanged
, this, &KItemListView::slotRubberBandPosChanged
);
1636 m_skipAutoScrollForRubberBand
= false;
1642 void KItemListView::slotHeaderColumnWidthChanged(const QByteArray
&role
, qreal currentWidth
, qreal previousWidth
)
1645 Q_UNUSED(currentWidth
)
1646 Q_UNUSED(previousWidth
)
1648 m_headerWidget
->setAutomaticColumnResizing(false);
1649 applyColumnWidthsFromHeader();
1650 doLayout(NoAnimation
);
1653 void KItemListView::slotSidePaddingChanged(qreal width
)
1656 if (m_headerWidget
->automaticColumnResizing()) {
1657 applyAutomaticColumnWidths();
1659 applyColumnWidthsFromHeader();
1660 doLayout(NoAnimation
);
1663 void KItemListView::slotHeaderColumnMoved(const QByteArray
&role
, int currentIndex
, int previousIndex
)
1665 Q_ASSERT(m_visibleRoles
[previousIndex
] == role
);
1667 const QList
<QByteArray
> previous
= m_visibleRoles
;
1669 QList
<QByteArray
> current
= m_visibleRoles
;
1670 current
.removeAt(previousIndex
);
1671 current
.insert(currentIndex
, role
);
1673 setVisibleRoles(current
);
1675 Q_EMIT
visibleRolesChanged(current
, previous
);
1678 void KItemListView::triggerAutoScrolling()
1680 if (!m_autoScrollTimer
) {
1685 int visibleSize
= 0;
1686 if (scrollOrientation() == Qt::Vertical
) {
1687 pos
= m_mousePos
.y();
1688 visibleSize
= size().height();
1690 pos
= m_mousePos
.x();
1691 visibleSize
= size().width();
1694 if (m_autoScrollTimer
->interval() == InitialAutoScrollDelay
) {
1695 m_autoScrollIncrement
= 0;
1698 m_autoScrollIncrement
= calculateAutoScrollingIncrement(pos
, visibleSize
, m_autoScrollIncrement
);
1699 if (m_autoScrollIncrement
== 0) {
1700 // The mouse position is not above an autoscroll margin (the autoscroll timer
1701 // will be restarted in mouseMoveEvent())
1702 m_autoScrollTimer
->stop();
1706 if (m_rubberBand
->isActive() && m_skipAutoScrollForRubberBand
) {
1707 // If a rubberband selection is ongoing the autoscrolling may only get triggered
1708 // if the direction of the rubberband is similar to the autoscroll direction. This
1709 // prevents that starting to create a rubberband within the autoscroll margins starts
1710 // an autoscrolling.
1712 const qreal minDiff
= 4; // Ignore any autoscrolling if the rubberband is very small
1713 const qreal diff
= (scrollOrientation() == Qt::Vertical
) ? m_rubberBand
->endPosition().y() - m_rubberBand
->startPosition().y()
1714 : m_rubberBand
->endPosition().x() - m_rubberBand
->startPosition().x();
1715 if (qAbs(diff
) < minDiff
|| (m_autoScrollIncrement
< 0 && diff
> 0) || (m_autoScrollIncrement
> 0 && diff
< 0)) {
1716 // The rubberband direction is different from the scroll direction (e.g. the rubberband has
1717 // been moved up although the autoscroll direction might be down)
1718 m_autoScrollTimer
->stop();
1723 // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
1724 // the autoscrolling may not get skipped anymore until a new rubberband is created
1725 m_skipAutoScrollForRubberBand
= false;
1727 const qreal maxVisibleOffset
= qMax(qreal(0), maximumScrollOffset() - visibleSize
);
1728 const qreal newScrollOffset
= qMin(scrollOffset() + m_autoScrollIncrement
, maxVisibleOffset
);
1729 setScrollOffset(newScrollOffset
);
1731 // Trigger the autoscroll timer which will periodically call
1732 // triggerAutoScrolling()
1733 m_autoScrollTimer
->start(RepeatingAutoScrollDelay
);
1736 void KItemListView::slotGeometryOfGroupHeaderParentChanged()
1738 KItemListWidget
*widget
= qobject_cast
<KItemListWidget
*>(sender());
1740 KItemListGroupHeader
*groupHeader
= m_visibleGroups
.value(widget
);
1741 Q_ASSERT(groupHeader
);
1742 updateGroupHeaderLayout(widget
);
1745 void KItemListView::slotRoleEditingCanceled(int index
, const QByteArray
&role
, const QVariant
&value
)
1747 disconnectRoleEditingSignals(index
);
1749 m_editingRole
= false;
1750 Q_EMIT
roleEditingCanceled(index
, role
, value
);
1753 void KItemListView::slotRoleEditingFinished(int index
, const QByteArray
&role
, const QVariant
&value
)
1755 disconnectRoleEditingSignals(index
);
1757 m_editingRole
= false;
1758 Q_EMIT
roleEditingFinished(index
, role
, value
);
1761 void KItemListView::setController(KItemListController
*controller
)
1763 if (m_controller
!= controller
) {
1764 KItemListController
*previous
= m_controller
;
1766 KItemListSelectionManager
*selectionManager
= previous
->selectionManager();
1767 disconnect(selectionManager
, &KItemListSelectionManager::currentChanged
, this, &KItemListView::slotCurrentChanged
);
1768 disconnect(selectionManager
, &KItemListSelectionManager::selectionChanged
, this, &KItemListView::slotSelectionChanged
);
1771 m_controller
= controller
;
1774 KItemListSelectionManager
*selectionManager
= controller
->selectionManager();
1775 connect(selectionManager
, &KItemListSelectionManager::currentChanged
, this, &KItemListView::slotCurrentChanged
);
1776 connect(selectionManager
, &KItemListSelectionManager::selectionChanged
, this, &KItemListView::slotSelectionChanged
);
1779 onControllerChanged(controller
, previous
);
1783 void KItemListView::setModel(KItemModelBase
*model
)
1785 if (m_model
== model
) {
1789 KItemModelBase
*previous
= m_model
;
1792 disconnect(m_model
, &KItemModelBase::itemsChanged
, this, &KItemListView::slotItemsChanged
);
1793 disconnect(m_model
, &KItemModelBase::itemsInserted
, this, &KItemListView::slotItemsInserted
);
1794 disconnect(m_model
, &KItemModelBase::itemsRemoved
, this, &KItemListView::slotItemsRemoved
);
1795 disconnect(m_model
, &KItemModelBase::itemsMoved
, this, &KItemListView::slotItemsMoved
);
1796 disconnect(m_model
, &KItemModelBase::groupsChanged
, this, &KItemListView::slotGroupsChanged
);
1797 disconnect(m_model
, &KItemModelBase::groupedSortingChanged
, this, &KItemListView::slotGroupedSortingChanged
);
1798 disconnect(m_model
, &KItemModelBase::sortOrderChanged
, this, &KItemListView::slotSortOrderChanged
);
1799 disconnect(m_model
, &KItemModelBase::sortRoleChanged
, this, &KItemListView::slotSortRoleChanged
);
1801 m_sizeHintResolver
->itemsRemoved(KItemRangeList() << KItemRange(0, m_model
->count()));
1805 m_layouter
->setModel(model
);
1806 m_grouped
= model
->groupedSorting();
1809 connect(m_model
, &KItemModelBase::itemsChanged
, this, &KItemListView::slotItemsChanged
);
1810 connect(m_model
, &KItemModelBase::itemsInserted
, this, &KItemListView::slotItemsInserted
);
1811 connect(m_model
, &KItemModelBase::itemsRemoved
, this, &KItemListView::slotItemsRemoved
);
1812 connect(m_model
, &KItemModelBase::itemsMoved
, this, &KItemListView::slotItemsMoved
);
1813 connect(m_model
, &KItemModelBase::groupsChanged
, this, &KItemListView::slotGroupsChanged
);
1814 connect(m_model
, &KItemModelBase::groupedSortingChanged
, this, &KItemListView::slotGroupedSortingChanged
);
1815 connect(m_model
, &KItemModelBase::sortOrderChanged
, this, &KItemListView::slotSortOrderChanged
);
1816 connect(m_model
, &KItemModelBase::sortRoleChanged
, this, &KItemListView::slotSortRoleChanged
);
1818 const int itemCount
= m_model
->count();
1819 if (itemCount
> 0) {
1820 slotItemsInserted(KItemRangeList() << KItemRange(0, itemCount
));
1824 onModelChanged(model
, previous
);
1827 KItemListRubberBand
*KItemListView::rubberBand() const
1829 return m_rubberBand
;
1832 void KItemListView::doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
)
1834 if (m_activeTransactions
> 0) {
1835 if (hint
== NoAnimation
) {
1836 // As soon as at least one property change should be done without animation,
1837 // the whole transaction will be marked as not animated.
1838 m_endTransactionAnimationHint
= NoAnimation
;
1843 if (!m_model
|| m_model
->count() < 0) {
1847 int firstVisibleIndex
= m_layouter
->firstVisibleIndex();
1848 if (firstVisibleIndex
< 0) {
1849 emitOffsetChanges();
1853 // Do a sanity check of the scroll-offset property: When properties of the itemlist-view have been changed
1854 // it might be possible that the maximum offset got changed too. Assure that the full visible range
1855 // is still shown if the maximum offset got decreased.
1856 const qreal visibleOffsetRange
= (scrollOrientation() == Qt::Horizontal
) ? size().width() : size().height();
1857 const qreal maxOffsetToShowFullRange
= maximumScrollOffset() - visibleOffsetRange
;
1858 if (scrollOffset() > maxOffsetToShowFullRange
) {
1859 m_layouter
->setScrollOffset(qMax(qreal(0), maxOffsetToShowFullRange
));
1860 firstVisibleIndex
= m_layouter
->firstVisibleIndex();
1863 const int lastVisibleIndex
= m_layouter
->lastVisibleIndex();
1865 int firstSibblingIndex
= -1;
1866 int lastSibblingIndex
= -1;
1867 const bool supportsExpanding
= supportsItemExpanding();
1869 QList
<int> reusableItems
= recycleInvisibleItems(firstVisibleIndex
, lastVisibleIndex
, hint
);
1871 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
1872 // instances from invisible items are reused. If no reusable items are
1873 // found then new KItemListWidget instances get created.
1874 const bool animate
= (hint
== Animation
);
1875 for (int i
= firstVisibleIndex
; i
<= lastVisibleIndex
; ++i
) {
1876 bool applyNewPos
= true;
1878 const QRectF itemBounds
= m_layouter
->itemRect(i
);
1879 const QPointF newPos
= itemBounds
.topLeft();
1880 KItemListWidget
*widget
= m_visibleItems
.value(i
);
1882 if (!reusableItems
.isEmpty()) {
1883 // Reuse a KItemListWidget instance from an invisible item
1884 const int oldIndex
= reusableItems
.takeLast();
1885 widget
= m_visibleItems
.value(oldIndex
);
1886 setWidgetIndex(widget
, i
);
1887 updateWidgetProperties(widget
, i
);
1888 initializeItemListWidget(widget
);
1890 // No reusable KItemListWidget instance is available, create a new one
1891 widget
= createWidget(i
);
1893 widget
->resize(itemBounds
.size());
1895 if (animate
&& changedCount
< 0) {
1896 // Items have been deleted.
1897 if (i
>= changedIndex
) {
1898 // The item is located behind the removed range. Move the
1899 // created item to the imaginary old position outside the
1900 // view. It will get animated to the new position later.
1901 const int previousIndex
= i
- changedCount
;
1902 const QRectF itemRect
= m_layouter
->itemRect(previousIndex
);
1903 if (itemRect
.isEmpty()) {
1904 const QPointF invisibleOldPos
= (scrollOrientation() == Qt::Vertical
) ? QPointF(0, size().height()) : QPointF(size().width(), 0);
1905 widget
->setPos(invisibleOldPos
);
1907 widget
->setPos(itemRect
.topLeft());
1909 applyNewPos
= false;
1913 if (supportsExpanding
&& changedCount
== 0) {
1914 if (firstSibblingIndex
< 0) {
1915 firstSibblingIndex
= i
;
1917 lastSibblingIndex
= i
;
1922 if (m_animation
->isStarted(widget
, KItemListViewAnimation::MovingAnimation
)) {
1923 if (m_editingRole
) {
1924 Q_EMIT widget
->roleEditingCanceled(widget
->index(), QByteArray(), QVariant());
1926 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
1927 applyNewPos
= false;
1930 const bool itemsRemoved
= (changedCount
< 0);
1931 const bool itemsInserted
= (changedCount
> 0);
1932 if (itemsRemoved
&& (i
>= changedIndex
)) {
1933 // The item is located after the removed items. Animate the moving of the position.
1934 applyNewPos
= !moveWidget(widget
, newPos
);
1935 } else if (itemsInserted
&& i
>= changedIndex
) {
1936 // The item is located after the first inserted item
1937 if (i
<= changedIndex
+ changedCount
- 1) {
1938 // The item is an inserted item. Animate the appearing of the item.
1939 // For performance reasons no animation is done when changedCount is equal
1940 // to all available items.
1941 if (changedCount
< m_model
->count()) {
1942 m_animation
->start(widget
, KItemListViewAnimation::CreateAnimation
);
1944 } else if (!m_animation
->isStarted(widget
, KItemListViewAnimation::CreateAnimation
)) {
1945 // The item was already there before, so animate the moving of the position.
1946 // No moving animation is done if the item is animated by a create animation: This
1947 // prevents a "move animation mess" when inserting several ranges in parallel.
1948 applyNewPos
= !moveWidget(widget
, newPos
);
1952 m_animation
->stop(widget
);
1956 widget
->setPos(newPos
);
1959 Q_ASSERT(widget
->index() == i
);
1960 widget
->setVisible(true);
1962 bool animateIconResizing
= animate
;
1964 if (widget
->size() != itemBounds
.size()) {
1965 // Resize the widget for the item to the changed size.
1967 // If a dynamic item size is used then no animation is done in the direction
1968 // of the dynamic size.
1969 if (m_itemSize
.width() <= 0) {
1970 // The width is dynamic, apply the new width without animation.
1971 widget
->resize(itemBounds
.width(), widget
->size().height());
1972 } else if (m_itemSize
.height() <= 0) {
1973 // The height is dynamic, apply the new height without animation.
1974 widget
->resize(widget
->size().width(), itemBounds
.height());
1976 m_animation
->start(widget
, KItemListViewAnimation::ResizeAnimation
, itemBounds
.size());
1978 widget
->resize(itemBounds
.size());
1981 animateIconResizing
= false;
1984 const int newIconSize
= widget
->styleOption().iconSize
;
1985 if (widget
->iconSize() != newIconSize
) {
1986 if (animateIconResizing
) {
1987 m_animation
->start(widget
, KItemListViewAnimation::IconResizeAnimation
, newIconSize
);
1989 widget
->setIconSize(newIconSize
);
1993 // Updating the cell-information must be done as last step: The decision whether the
1994 // moving-animation should be started at all is based on the previous cell-information.
1995 const Cell
cell(m_layouter
->itemColumn(i
), m_layouter
->itemRow(i
));
1996 m_visibleCells
.insert(i
, cell
);
1999 // Delete invisible KItemListWidget instances that have not been reused
2000 for (int index
: std::as_const(reusableItems
)) {
2001 recycleWidget(m_visibleItems
.value(index
));
2004 if (supportsExpanding
&& firstSibblingIndex
>= 0) {
2005 Q_ASSERT(lastSibblingIndex
>= 0);
2006 updateSiblingsInformation(firstSibblingIndex
, lastSibblingIndex
);
2010 // Update the layout of all visible group headers
2011 QHashIterator
<KItemListWidget
*, KItemListGroupHeader
*> it(m_visibleGroups
);
2012 while (it
.hasNext()) {
2014 updateGroupHeaderLayout(it
.key());
2018 emitOffsetChanges();
2021 QList
<int> KItemListView::recycleInvisibleItems(int firstVisibleIndex
, int lastVisibleIndex
, LayoutAnimationHint hint
)
2023 // Determine all items that are completely invisible and might be
2024 // reused for items that just got (at least partly) visible. If the
2025 // animation hint is set to 'Animation' items that do e.g. an animated
2026 // moving of their position are not marked as invisible: This assures
2027 // that a scrolling inside the view can be done without breaking an animation.
2031 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
2032 while (it
.hasNext()) {
2035 KItemListWidget
*widget
= it
.value();
2036 const int index
= widget
->index();
2037 const bool invisible
= (index
< firstVisibleIndex
) || (index
> lastVisibleIndex
);
2040 if (m_animation
->isStarted(widget
)) {
2041 if (hint
== NoAnimation
) {
2042 // Stopping the animation will call KItemListView::slotAnimationFinished()
2043 // and the widget will be recycled if necessary there.
2044 m_animation
->stop(widget
);
2047 widget
->setVisible(false);
2048 items
.append(index
);
2051 recycleGroupHeaderForWidget(widget
);
2060 bool KItemListView::moveWidget(KItemListWidget
*widget
, const QPointF
&newPos
)
2062 if (widget
->pos() == newPos
) {
2066 bool startMovingAnim
= false;
2068 if (m_itemSize
.isEmpty()) {
2069 // The items are not aligned in a grid but either as columns or rows.
2070 startMovingAnim
= true;
2072 // When having a grid the moving-animation should only be started, if it is done within
2073 // one row in the vertical scroll-orientation or one column in the horizontal scroll-orientation.
2074 // Otherwise instead of a moving-animation a create-animation on the new position will be used
2075 // instead. This is done to prevent overlapping (and confusing) moving-animations.
2076 const int index
= widget
->index();
2077 const Cell cell
= m_visibleCells
.value(index
);
2078 if (cell
.column
>= 0 && cell
.row
>= 0) {
2079 if (scrollOrientation() == Qt::Vertical
) {
2080 startMovingAnim
= (cell
.row
== m_layouter
->itemRow(index
));
2082 startMovingAnim
= (cell
.column
== m_layouter
->itemColumn(index
));
2087 if (startMovingAnim
) {
2088 m_animation
->start(widget
, KItemListViewAnimation::MovingAnimation
, newPos
);
2092 m_animation
->stop(widget
);
2093 m_animation
->start(widget
, KItemListViewAnimation::CreateAnimation
);
2097 void KItemListView::emitOffsetChanges()
2099 const qreal newScrollOffset
= m_layouter
->scrollOffset();
2100 if (m_oldScrollOffset
!= newScrollOffset
) {
2101 Q_EMIT
scrollOffsetChanged(newScrollOffset
, m_oldScrollOffset
);
2102 m_oldScrollOffset
= newScrollOffset
;
2105 const qreal newMaximumScrollOffset
= m_layouter
->maximumScrollOffset();
2106 if (m_oldMaximumScrollOffset
!= newMaximumScrollOffset
) {
2107 Q_EMIT
maximumScrollOffsetChanged(newMaximumScrollOffset
, m_oldMaximumScrollOffset
);
2108 m_oldMaximumScrollOffset
= newMaximumScrollOffset
;
2111 const qreal newItemOffset
= m_layouter
->itemOffset();
2112 if (m_oldItemOffset
!= newItemOffset
) {
2113 Q_EMIT
itemOffsetChanged(newItemOffset
, m_oldItemOffset
);
2114 m_oldItemOffset
= newItemOffset
;
2117 const qreal newMaximumItemOffset
= m_layouter
->maximumItemOffset();
2118 if (m_oldMaximumItemOffset
!= newMaximumItemOffset
) {
2119 Q_EMIT
maximumItemOffsetChanged(newMaximumItemOffset
, m_oldMaximumItemOffset
);
2120 m_oldMaximumItemOffset
= newMaximumItemOffset
;
2124 KItemListWidget
*KItemListView::createWidget(int index
)
2126 KItemListWidget
*widget
= widgetCreator()->create(this);
2127 widget
->setFlag(QGraphicsItem::ItemStacksBehindParent
);
2129 m_visibleItems
.insert(index
, widget
);
2130 m_visibleCells
.insert(index
, Cell());
2131 updateWidgetProperties(widget
, index
);
2132 initializeItemListWidget(widget
);
2136 void KItemListView::recycleWidget(KItemListWidget
*widget
)
2139 recycleGroupHeaderForWidget(widget
);
2142 const int index
= widget
->index();
2143 m_visibleItems
.remove(index
);
2144 m_visibleCells
.remove(index
);
2146 widgetCreator()->recycle(widget
);
2149 void KItemListView::setWidgetIndex(KItemListWidget
*widget
, int index
)
2151 const int oldIndex
= widget
->index();
2152 m_visibleItems
.remove(oldIndex
);
2153 m_visibleCells
.remove(oldIndex
);
2155 m_visibleItems
.insert(index
, widget
);
2156 m_visibleCells
.insert(index
, Cell());
2158 widget
->setIndex(index
);
2161 void KItemListView::moveWidgetToIndex(KItemListWidget
*widget
, int index
)
2163 const int oldIndex
= widget
->index();
2164 const Cell oldCell
= m_visibleCells
.value(oldIndex
);
2166 setWidgetIndex(widget
, index
);
2168 const Cell
newCell(m_layouter
->itemColumn(index
), m_layouter
->itemRow(index
));
2169 const bool vertical
= (scrollOrientation() == Qt::Vertical
);
2170 const bool updateCell
= (vertical
&& oldCell
.row
== newCell
.row
) || (!vertical
&& oldCell
.column
== newCell
.column
);
2172 m_visibleCells
.insert(index
, newCell
);
2176 void KItemListView::setLayouterSize(const QSizeF
&size
, SizeType sizeType
)
2180 m_layouter
->setSize(size
);
2183 m_layouter
->setItemSize(size
);
2190 void KItemListView::updateWidgetProperties(KItemListWidget
*widget
, int index
)
2192 widget
->setVisibleRoles(m_visibleRoles
);
2193 updateWidgetColumnWidths(widget
);
2194 widget
->setStyleOption(m_styleOption
);
2196 const KItemListSelectionManager
*selectionManager
= m_controller
->selectionManager();
2198 // In SingleSelection mode (e.g., in the Places Panel), the current item is
2199 // always the selected item. It is not necessary to highlight the current item then.
2200 if (m_controller
->selectionBehavior() != KItemListController::SingleSelection
) {
2201 widget
->setCurrent(index
== selectionManager
->currentItem());
2203 widget
->setSelected(selectionManager
->isSelected(index
));
2204 widget
->setHovered(false);
2205 widget
->setEnabledSelectionToggle(enabledSelectionToggles());
2206 widget
->setIndex(index
);
2207 widget
->setData(m_model
->data(index
));
2208 widget
->setSiblingsInformation(QBitArray());
2209 updateAlternateBackgroundForWidget(widget
);
2212 updateGroupHeaderForWidget(widget
);
2216 void KItemListView::updateGroupHeaderForWidget(KItemListWidget
*widget
)
2218 Q_ASSERT(m_grouped
);
2220 const int index
= widget
->index();
2221 if (!m_layouter
->isFirstGroupItem(index
)) {
2222 // The widget does not represent the first item of a group
2223 // and hence requires no header
2224 recycleGroupHeaderForWidget(widget
);
2228 const QList
<QPair
<int, QVariant
>> groups
= model()->groups();
2229 if (groups
.isEmpty() || !groupHeaderCreator()) {
2233 KItemListGroupHeader
*groupHeader
= m_visibleGroups
.value(widget
);
2235 groupHeader
= groupHeaderCreator()->create(this);
2236 groupHeader
->setParentItem(widget
);
2237 m_visibleGroups
.insert(widget
, groupHeader
);
2238 connect(widget
, &KItemListWidget::geometryChanged
, this, &KItemListView::slotGeometryOfGroupHeaderParentChanged
);
2240 Q_ASSERT(groupHeader
->parentItem() == widget
);
2242 const int groupIndex
= groupIndexForItem(index
);
2243 Q_ASSERT(groupIndex
>= 0);
2244 groupHeader
->setData(groups
.at(groupIndex
).second
);
2245 groupHeader
->setRole(model()->sortRole());
2246 groupHeader
->setStyleOption(m_styleOption
);
2247 groupHeader
->setScrollOrientation(scrollOrientation());
2248 groupHeader
->setItemIndex(index
);
2250 groupHeader
->show();
2253 void KItemListView::updateGroupHeaderLayout(KItemListWidget
*widget
)
2255 KItemListGroupHeader
*groupHeader
= m_visibleGroups
.value(widget
);
2256 Q_ASSERT(groupHeader
);
2258 const int index
= widget
->index();
2259 const QRectF groupHeaderRect
= m_layouter
->groupHeaderRect(index
);
2260 const QRectF itemRect
= m_layouter
->itemRect(index
);
2262 // The group-header is a child of the itemlist widget. Translate the
2263 // group header position to the relative position.
2264 if (scrollOrientation() == Qt::Vertical
) {
2265 // In the vertical scroll orientation the group header should always span
2266 // the whole width no matter which temporary position the parent widget
2267 // has. In this case the x-position and width will be adjusted manually.
2268 const qreal x
= -widget
->x() - itemOffset();
2269 const qreal width
= maximumItemOffset();
2270 groupHeader
->setPos(x
, -groupHeaderRect
.height());
2271 groupHeader
->resize(width
, groupHeaderRect
.size().height());
2273 groupHeader
->setPos(groupHeaderRect
.x() - itemRect
.x(), -widget
->y());
2274 groupHeader
->resize(groupHeaderRect
.size());
2278 void KItemListView::recycleGroupHeaderForWidget(KItemListWidget
*widget
)
2280 KItemListGroupHeader
*header
= m_visibleGroups
.value(widget
);
2282 header
->setParentItem(nullptr);
2283 groupHeaderCreator()->recycle(header
);
2284 m_visibleGroups
.remove(widget
);
2285 disconnect(widget
, &KItemListWidget::geometryChanged
, this, &KItemListView::slotGeometryOfGroupHeaderParentChanged
);
2289 void KItemListView::updateVisibleGroupHeaders()
2291 Q_ASSERT(m_grouped
);
2292 m_layouter
->markAsDirty();
2294 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
2295 while (it
.hasNext()) {
2297 updateGroupHeaderForWidget(it
.value());
2301 int KItemListView::groupIndexForItem(int index
) const
2303 Q_ASSERT(m_grouped
);
2305 const QList
<QPair
<int, QVariant
>> groups
= model()->groups();
2306 if (groups
.isEmpty()) {
2311 int max
= groups
.count() - 1;
2314 mid
= (min
+ max
) / 2;
2315 if (index
> groups
[mid
].first
) {
2320 } while (groups
[mid
].first
!= index
&& min
<= max
);
2323 while (groups
[mid
].first
> index
&& mid
> 0) {
2331 void KItemListView::updateAlternateBackgrounds()
2333 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
2334 while (it
.hasNext()) {
2336 updateAlternateBackgroundForWidget(it
.value());
2340 void KItemListView::updateAlternateBackgroundForWidget(KItemListWidget
*widget
)
2342 bool enabled
= useAlternateBackgrounds();
2344 const int index
= widget
->index();
2345 enabled
= (index
& 0x1) > 0;
2347 const int groupIndex
= groupIndexForItem(index
);
2348 if (groupIndex
>= 0) {
2349 const QList
<QPair
<int, QVariant
>> groups
= model()->groups();
2350 const int indexOfFirstGroupItem
= groups
[groupIndex
].first
;
2351 const int relativeIndex
= index
- indexOfFirstGroupItem
;
2352 enabled
= (relativeIndex
& 0x1) > 0;
2356 widget
->setAlternateBackground(enabled
);
2359 bool KItemListView::useAlternateBackgrounds() const
2361 return m_alternateBackgrounds
&& m_itemSize
.isEmpty();
2364 QHash
<QByteArray
, qreal
> KItemListView::preferredColumnWidths(const KItemRangeList
&itemRanges
) const
2366 QElapsedTimer timer
;
2369 QHash
<QByteArray
, qreal
> widths
;
2371 // Calculate the minimum width for each column that is required
2372 // to show the headline unclipped.
2373 const QFontMetricsF
fontMetrics(m_headerWidget
->font());
2374 const int gripMargin
= m_headerWidget
->style()->pixelMetric(QStyle::PM_HeaderGripMargin
);
2375 const int headerMargin
= m_headerWidget
->style()->pixelMetric(QStyle::PM_HeaderMargin
);
2376 for (const QByteArray
&visibleRole
: std::as_const(m_visibleRoles
)) {
2377 const QString headerText
= m_model
->roleDescription(visibleRole
);
2378 const qreal headerWidth
= fontMetrics
.horizontalAdvance(headerText
) + gripMargin
+ headerMargin
* 2;
2379 widths
.insert(visibleRole
, headerWidth
);
2382 // Calculate the preferred column widths for each item and ignore values
2383 // smaller than the width for showing the headline unclipped.
2384 const KItemListWidgetCreatorBase
*creator
= widgetCreator();
2385 int calculatedItemCount
= 0;
2386 bool maxTimeExceeded
= false;
2387 for (const KItemRange
&itemRange
: itemRanges
) {
2388 const int startIndex
= itemRange
.index
;
2389 const int endIndex
= startIndex
+ itemRange
.count
- 1;
2391 for (int i
= startIndex
; i
<= endIndex
; ++i
) {
2392 for (const QByteArray
&visibleRole
: std::as_const(m_visibleRoles
)) {
2393 qreal maxWidth
= widths
.value(visibleRole
, 0);
2394 const qreal width
= creator
->preferredRoleColumnWidth(visibleRole
, i
, this);
2395 maxWidth
= qMax(width
, maxWidth
);
2396 widths
.insert(visibleRole
, maxWidth
);
2399 if (calculatedItemCount
> 100 && timer
.elapsed() > 200) {
2400 // When having several thousands of items calculating the sizes can get
2401 // very expensive. We accept a possibly too small role-size in favour
2402 // of having no blocking user interface.
2403 maxTimeExceeded
= true;
2406 ++calculatedItemCount
;
2408 if (maxTimeExceeded
) {
2416 void KItemListView::applyColumnWidthsFromHeader()
2418 // Apply the new size to the layouter
2419 const qreal requiredWidth
= m_headerWidget
->leftPadding() + columnWidthsSum() + m_headerWidget
->rightPadding();
2420 const QSizeF
dynamicItemSize(qMax(size().width(), requiredWidth
), m_itemSize
.height());
2421 m_layouter
->setItemSize(dynamicItemSize
);
2423 // Update the role sizes for all visible widgets
2424 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
2425 while (it
.hasNext()) {
2427 updateWidgetColumnWidths(it
.value());
2431 void KItemListView::updateWidgetColumnWidths(KItemListWidget
*widget
)
2433 for (const QByteArray
&role
: std::as_const(m_visibleRoles
)) {
2434 widget
->setColumnWidth(role
, m_headerWidget
->columnWidth(role
));
2436 widget
->setSidePadding(m_headerWidget
->leftPadding(), m_headerWidget
->rightPadding());
2439 void KItemListView::updatePreferredColumnWidths(const KItemRangeList
&itemRanges
)
2441 Q_ASSERT(m_itemSize
.isEmpty());
2442 const int itemCount
= m_model
->count();
2443 int rangesItemCount
= 0;
2444 for (const KItemRange
&range
: itemRanges
) {
2445 rangesItemCount
+= range
.count
;
2448 if (itemCount
== rangesItemCount
) {
2449 const QHash
<QByteArray
, qreal
> preferredWidths
= preferredColumnWidths(itemRanges
);
2450 for (const QByteArray
&role
: std::as_const(m_visibleRoles
)) {
2451 m_headerWidget
->setPreferredColumnWidth(role
, preferredWidths
.value(role
));
2454 // Only a sub range of the roles need to be determined.
2455 // The chances are good that the widths of the sub ranges
2456 // already fit into the available widths and hence no
2457 // expensive update might be required.
2458 bool changed
= false;
2460 const QHash
<QByteArray
, qreal
> updatedWidths
= preferredColumnWidths(itemRanges
);
2461 QHashIterator
<QByteArray
, qreal
> it(updatedWidths
);
2462 while (it
.hasNext()) {
2464 const QByteArray
&role
= it
.key();
2465 const qreal updatedWidth
= it
.value();
2466 const qreal currentWidth
= m_headerWidget
->preferredColumnWidth(role
);
2467 if (updatedWidth
> currentWidth
) {
2468 m_headerWidget
->setPreferredColumnWidth(role
, updatedWidth
);
2474 // All the updated sizes are smaller than the current sizes and no change
2475 // of the stretched roles-widths is required
2480 if (m_headerWidget
->automaticColumnResizing()) {
2481 applyAutomaticColumnWidths();
2485 void KItemListView::updatePreferredColumnWidths()
2488 updatePreferredColumnWidths(KItemRangeList() << KItemRange(0, m_model
->count()));
2492 void KItemListView::applyAutomaticColumnWidths()
2494 Q_ASSERT(m_itemSize
.isEmpty());
2495 Q_ASSERT(m_headerWidget
->automaticColumnResizing());
2496 if (m_visibleRoles
.isEmpty()) {
2500 // Calculate the maximum size of an item by considering the
2501 // visible role sizes and apply them to the layouter. If the
2502 // size does not use the available view-size the size of the
2503 // first role will get stretched.
2505 for (const QByteArray
&role
: std::as_const(m_visibleRoles
)) {
2506 const qreal preferredWidth
= m_headerWidget
->preferredColumnWidth(role
);
2507 m_headerWidget
->setColumnWidth(role
, preferredWidth
);
2510 const QByteArray firstRole
= m_visibleRoles
.first();
2511 qreal firstColumnWidth
= m_headerWidget
->columnWidth(firstRole
);
2512 QSizeF dynamicItemSize
= m_itemSize
;
2514 qreal requiredWidth
= m_headerWidget
->leftPadding() + columnWidthsSum() + m_headerWidget
->rightPadding();
2515 // By default we want the same padding symmetrically on both sides of the view. This improves UX, looks better and increases the chances of users figuring
2516 // out that the padding area can be used for deselecting and dropping files.
2517 const qreal availableWidth
= size().width();
2518 if (requiredWidth
< availableWidth
) {
2519 // Stretch the first column to use the whole remaining width
2520 firstColumnWidth
+= availableWidth
- requiredWidth
;
2521 m_headerWidget
->setColumnWidth(firstRole
, firstColumnWidth
);
2522 } else if (requiredWidth
> availableWidth
&& m_visibleRoles
.count() > 1) {
2523 // Shrink the first column to be able to show as much other
2524 // columns as possible
2525 qreal shrinkedFirstColumnWidth
= firstColumnWidth
- requiredWidth
+ availableWidth
;
2527 // TODO: A proper calculation of the minimum width depends on the implementation
2528 // of KItemListWidget. Probably a kind of minimum size-hint should be introduced
2530 const qreal minWidth
= qMin(firstColumnWidth
, qreal(m_styleOption
.iconSize
* 2 + 200));
2531 if (shrinkedFirstColumnWidth
< minWidth
) {
2532 shrinkedFirstColumnWidth
= minWidth
;
2535 m_headerWidget
->setColumnWidth(firstRole
, shrinkedFirstColumnWidth
);
2536 requiredWidth
-= firstColumnWidth
- shrinkedFirstColumnWidth
;
2539 dynamicItemSize
.rwidth() = qMax(requiredWidth
, availableWidth
);
2541 m_layouter
->setItemSize(dynamicItemSize
);
2543 // Update the role sizes for all visible widgets
2544 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
2545 while (it
.hasNext()) {
2547 updateWidgetColumnWidths(it
.value());
2551 qreal
KItemListView::columnWidthsSum() const
2553 qreal widthsSum
= 0;
2554 for (const QByteArray
&role
: std::as_const(m_visibleRoles
)) {
2555 widthsSum
+= m_headerWidget
->columnWidth(role
);
2560 QRectF
KItemListView::headerBoundaries() const
2562 return m_headerWidget
->isVisible() ? m_headerWidget
->geometry() : QRectF();
2565 bool KItemListView::changesItemGridLayout(const QSizeF
&newGridSize
, const QSizeF
&newItemSize
, const QSizeF
&newItemMargin
) const
2567 if (newItemSize
.isEmpty() || newGridSize
.isEmpty()) {
2571 if (m_layouter
->scrollOrientation() == Qt::Vertical
) {
2572 const qreal itemWidth
= m_layouter
->itemSize().width();
2573 if (itemWidth
> 0) {
2574 const int newColumnCount
= itemsPerSize(newGridSize
.width(), newItemSize
.width(), newItemMargin
.width());
2575 if (m_model
->count() > newColumnCount
) {
2576 const int oldColumnCount
= itemsPerSize(m_layouter
->size().width(), itemWidth
, m_layouter
->itemMargin().width());
2577 return oldColumnCount
!= newColumnCount
;
2581 const qreal itemHeight
= m_layouter
->itemSize().height();
2582 if (itemHeight
> 0) {
2583 const int newRowCount
= itemsPerSize(newGridSize
.height(), newItemSize
.height(), newItemMargin
.height());
2584 if (m_model
->count() > newRowCount
) {
2585 const int oldRowCount
= itemsPerSize(m_layouter
->size().height(), itemHeight
, m_layouter
->itemMargin().height());
2586 return oldRowCount
!= newRowCount
;
2594 bool KItemListView::animateChangedItemCount(int changedItemCount
) const
2596 if (m_itemSize
.isEmpty()) {
2597 // We have only columns or only rows, but no grid: An animation is usually
2598 // welcome when inserting or removing items.
2599 return !supportsItemExpanding();
2602 if (m_layouter
->size().isEmpty() || m_layouter
->itemSize().isEmpty()) {
2606 const int maximum
= (scrollOrientation() == Qt::Vertical
) ? m_layouter
->size().width() / m_layouter
->itemSize().width()
2607 : m_layouter
->size().height() / m_layouter
->itemSize().height();
2608 // Only animate if up to 2/3 of a row or column are inserted or removed
2609 return changedItemCount
<= maximum
* 2 / 3;
2612 bool KItemListView::scrollBarRequired(const QSizeF
&size
) const
2614 const QSizeF oldSize
= m_layouter
->size();
2616 m_layouter
->setSize(size
);
2617 const qreal maxOffset
= m_layouter
->maximumScrollOffset();
2618 m_layouter
->setSize(oldSize
);
2620 return m_layouter
->scrollOrientation() == Qt::Vertical
? maxOffset
> size
.height() : maxOffset
> size
.width();
2623 int KItemListView::showDropIndicator(const QPointF
&pos
)
2625 QHashIterator
<int, KItemListWidget
*> it(m_visibleItems
);
2626 while (it
.hasNext()) {
2628 const KItemListWidget
*widget
= it
.value();
2630 const QPointF mappedPos
= widget
->mapFromItem(this, pos
);
2631 const QRectF rect
= itemRect(widget
->index());
2632 if (mappedPos
.y() >= 0 && mappedPos
.y() <= rect
.height()) {
2633 if (m_model
->supportsDropping(widget
->index())) {
2634 // Keep 30% of the rectangle as the gap instead of always having a fixed gap
2635 const int gap
= qMax(qreal(4.0), qreal(0.3) * rect
.height());
2636 if (mappedPos
.y() >= gap
&& mappedPos
.y() <= rect
.height() - gap
) {
2641 const bool isAboveItem
= (mappedPos
.y() < rect
.height() / 2);
2642 const qreal y
= isAboveItem
? rect
.top() : rect
.bottom();
2644 const QRectF
draggingInsertIndicator(rect
.left(), y
, rect
.width(), 1);
2645 if (m_dropIndicator
!= draggingInsertIndicator
) {
2646 m_dropIndicator
= draggingInsertIndicator
;
2650 int index
= widget
->index();
2658 const QRectF firstItemRect
= itemRect(firstVisibleIndex());
2659 return (pos
.y() <= firstItemRect
.top()) ? 0 : -1;
2662 void KItemListView::hideDropIndicator()
2664 if (!m_dropIndicator
.isNull()) {
2665 m_dropIndicator
= QRectF();
2670 void KItemListView::updateGroupHeaderHeight()
2672 qreal groupHeaderHeight
= m_styleOption
.fontMetrics
.height();
2673 qreal groupHeaderMargin
= 0;
2675 if (scrollOrientation() == Qt::Horizontal
) {
2676 // The vertical margin above and below the header should be
2677 // equal to the horizontal margin, not the vertical margin
2678 // from m_styleOption.
2679 groupHeaderHeight
+= 2 * m_styleOption
.horizontalMargin
;
2680 groupHeaderMargin
= m_styleOption
.horizontalMargin
;
2681 } else if (m_itemSize
.isEmpty()) {
2682 groupHeaderHeight
+= 4 * m_styleOption
.padding
;
2683 groupHeaderMargin
= m_styleOption
.iconSize
/ 2;
2685 groupHeaderHeight
+= 2 * m_styleOption
.padding
+ m_styleOption
.verticalMargin
;
2686 groupHeaderMargin
= m_styleOption
.iconSize
/ 4;
2688 m_layouter
->setGroupHeaderHeight(groupHeaderHeight
);
2689 m_layouter
->setGroupHeaderMargin(groupHeaderMargin
);
2691 updateVisibleGroupHeaders();
2694 void KItemListView::updateSiblingsInformation(int firstIndex
, int lastIndex
)
2696 if (!supportsItemExpanding() || !m_model
) {
2700 if (firstIndex
< 0 || lastIndex
< 0) {
2701 firstIndex
= m_layouter
->firstVisibleIndex();
2702 lastIndex
= m_layouter
->lastVisibleIndex();
2704 const bool isRangeVisible
= (firstIndex
<= m_layouter
->lastVisibleIndex() && lastIndex
>= m_layouter
->firstVisibleIndex());
2705 if (!isRangeVisible
) {
2710 int previousParents
= 0;
2711 QBitArray previousSiblings
;
2713 // The rootIndex describes the first index where the siblings get
2714 // calculated from. For the calculation the upper most parent item
2715 // is required. For performance reasons it is checked first whether
2716 // the visible items before or after the current range already
2717 // contain a siblings information which can be used as base.
2718 int rootIndex
= firstIndex
;
2720 KItemListWidget
*widget
= m_visibleItems
.value(firstIndex
- 1);
2722 // There is no visible widget before the range, check whether there
2723 // is one after the range:
2724 widget
= m_visibleItems
.value(lastIndex
+ 1);
2726 // The sibling information of the widget may only be used if
2727 // all items of the range have the same number of parents.
2728 const int parents
= m_model
->expandedParentsCount(lastIndex
+ 1);
2729 for (int i
= lastIndex
; i
>= firstIndex
; --i
) {
2730 if (m_model
->expandedParentsCount(i
) != parents
) {
2739 // Performance optimization: Use the sibling information of the visible
2740 // widget beside the given range.
2741 previousSiblings
= widget
->siblingsInformation();
2742 if (previousSiblings
.isEmpty()) {
2745 previousParents
= previousSiblings
.count() - 1;
2746 previousSiblings
.truncate(previousParents
);
2748 // Potentially slow path: Go back to the upper most parent of firstIndex
2749 // to be able to calculate the initial value for the siblings.
2750 while (rootIndex
> 0 && m_model
->expandedParentsCount(rootIndex
) > 0) {
2755 Q_ASSERT(previousParents
>= 0);
2756 for (int i
= rootIndex
; i
<= lastIndex
; ++i
) {
2757 // Update the parent-siblings in case if the current item represents
2758 // a child or an upper parent.
2759 const int currentParents
= m_model
->expandedParentsCount(i
);
2760 Q_ASSERT(currentParents
>= 0);
2761 if (previousParents
< currentParents
) {
2762 previousParents
= currentParents
;
2763 previousSiblings
.resize(currentParents
);
2764 previousSiblings
.setBit(currentParents
- 1, hasSiblingSuccessor(i
- 1));
2765 } else if (previousParents
> currentParents
) {
2766 previousParents
= currentParents
;
2767 previousSiblings
.truncate(currentParents
);
2770 if (i
>= firstIndex
) {
2771 // The index represents a visible item. Apply the parent-siblings
2772 // and update the sibling of the current item.
2773 KItemListWidget
*widget
= m_visibleItems
.value(i
);
2778 QBitArray siblings
= previousSiblings
;
2779 siblings
.resize(siblings
.count() + 1);
2780 siblings
.setBit(siblings
.count() - 1, hasSiblingSuccessor(i
));
2782 widget
->setSiblingsInformation(siblings
);
2787 bool KItemListView::hasSiblingSuccessor(int index
) const
2789 bool hasSuccessor
= false;
2790 const int parentsCount
= m_model
->expandedParentsCount(index
);
2791 int successorIndex
= index
+ 1;
2793 // Search the next sibling
2794 const int itemCount
= m_model
->count();
2795 while (successorIndex
< itemCount
) {
2796 const int currentParentsCount
= m_model
->expandedParentsCount(successorIndex
);
2797 if (currentParentsCount
== parentsCount
) {
2798 hasSuccessor
= true;
2800 } else if (currentParentsCount
< parentsCount
) {
2806 if (m_grouped
&& hasSuccessor
) {
2807 // If the sibling is part of another group, don't mark it as
2808 // successor as the group header is between the sibling connections.
2809 for (int i
= index
+ 1; i
<= successorIndex
; ++i
) {
2810 if (m_layouter
->isFirstGroupItem(i
)) {
2811 hasSuccessor
= false;
2817 return hasSuccessor
;
2820 void KItemListView::disconnectRoleEditingSignals(int index
)
2822 KStandardItemListWidget
*widget
= qobject_cast
<KStandardItemListWidget
*>(m_visibleItems
.value(index
));
2827 disconnect(widget
, &KItemListWidget::roleEditingCanceled
, this, nullptr);
2828 disconnect(widget
, &KItemListWidget::roleEditingFinished
, this, nullptr);
2829 disconnect(this, &KItemListView::scrollOffsetChanged
, widget
, nullptr);
2832 int KItemListView::calculateAutoScrollingIncrement(int pos
, int range
, int oldInc
)
2836 const int minSpeed
= 4;
2837 const int maxSpeed
= 128;
2838 const int speedLimiter
= 96;
2839 const int autoScrollBorder
= 64;
2841 // Limit the increment that is allowed to be added in comparison to 'oldInc'.
2842 // This assures that the autoscrolling speed grows gradually.
2843 const int incLimiter
= 1;
2845 if (pos
< autoScrollBorder
) {
2846 inc
= -minSpeed
+ qAbs(pos
- autoScrollBorder
) * (pos
- autoScrollBorder
) / speedLimiter
;
2847 inc
= qMax(inc
, -maxSpeed
);
2848 inc
= qMax(inc
, oldInc
- incLimiter
);
2849 } else if (pos
> range
- autoScrollBorder
) {
2850 inc
= minSpeed
+ qAbs(pos
- range
+ autoScrollBorder
) * (pos
- range
+ autoScrollBorder
) / speedLimiter
;
2851 inc
= qMin(inc
, maxSpeed
);
2852 inc
= qMin(inc
, oldInc
+ incLimiter
);
2858 int KItemListView::itemsPerSize(qreal size
, qreal itemSize
, qreal itemMargin
)
2860 const qreal availableSize
= size
- itemMargin
;
2861 const int count
= availableSize
/ (itemSize
+ itemMargin
);
2865 KItemListCreatorBase::~KItemListCreatorBase()
2867 qDeleteAll(m_recycleableWidgets
);
2868 qDeleteAll(m_createdWidgets
);
2871 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget
*widget
)
2873 m_createdWidgets
.insert(widget
);
2876 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget
*widget
)
2878 Q_ASSERT(m_createdWidgets
.contains(widget
));
2879 m_createdWidgets
.remove(widget
);
2881 if (m_recycleableWidgets
.count() < 100) {
2882 m_recycleableWidgets
.append(widget
);
2883 widget
->setVisible(false);
2889 QGraphicsWidget
*KItemListCreatorBase::popRecycleableWidget()
2891 if (m_recycleableWidgets
.isEmpty()) {
2895 QGraphicsWidget
*widget
= m_recycleableWidgets
.takeLast();
2896 m_createdWidgets
.insert(widget
);
2900 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
2904 void KItemListWidgetCreatorBase::recycle(KItemListWidget
*widget
)
2906 widget
->setParentItem(nullptr);
2907 widget
->setOpacity(1.0);
2908 pushRecycleableWidget(widget
);
2911 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
2915 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader
*header
)
2917 header
->setOpacity(1.0);
2918 pushRecycleableWidget(header
);
2921 #include "moc_kitemlistview.cpp"