]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.h
Merge branch 'master' into kf6
[dolphin.git] / src / kitemviews / kitemlistview.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #ifndef KITEMLISTVIEW_H
10 #define KITEMLISTVIEW_H
11
12 #include <optional>
13
14 #include "dolphin_export.h"
15 #include "kitemviews/kitemliststyleoption.h"
16 #include "kitemviews/kitemlistwidget.h"
17 #include "kitemviews/kitemmodelbase.h"
18 #include "kitemviews/kstandarditemlistgroupheader.h"
19 #include "kitemviews/private/kitemlistviewanimation.h"
20
21 #include <QGraphicsWidget>
22 #include <QSet>
23
24 class KItemListContainer;
25 class KItemListContainerAccessible;
26 class KItemListController;
27 class KItemListGroupHeaderCreatorBase;
28 class KItemListHeader;
29 class KItemListHeaderWidget;
30 class KItemListSizeHintResolver;
31 class KItemListRubberBand;
32 class KItemListViewAnimation;
33 class KItemListViewLayouter;
34 class KItemListWidget;
35 class KItemListWidgetInformant;
36 class KItemListWidgetCreatorBase;
37 class QTimer;
38 class QPropertyAnimation;
39 class QVariantAnimation;
40
41 /**
42 * @brief Represents the view of an item-list.
43 *
44 * The view is responsible for showing the items of the model within
45 * a GraphicsItem. Each visible item is represented by a KItemListWidget.
46 *
47 * The created view must be applied to the KItemListController with
48 * KItemListController::setView() or with the constructor of
49 * KItemListController.
50 *
51 * @see KItemListWidget
52 * @see KItemModelBase
53 */
54 class DOLPHIN_EXPORT KItemListView : public QGraphicsWidget
55 {
56 Q_OBJECT
57
58 Q_PROPERTY(qreal scrollOffset READ scrollOffset WRITE setScrollOffset NOTIFY scrollOffsetChanged)
59 Q_PROPERTY(qreal itemOffset READ itemOffset WRITE setItemOffset NOTIFY itemOffsetChanged)
60
61 public:
62 explicit KItemListView(QGraphicsWidget *parent = nullptr);
63 ~KItemListView() override;
64
65 /**
66 * Offset of the scrollbar that represents the scroll-orientation
67 * (see setScrollOrientation()).
68 */
69 void setScrollOffset(qreal offset);
70 qreal scrollOffset() const;
71
72 qreal maximumScrollOffset() const;
73
74 /**
75 * Offset related to an item, that does not fit into the available
76 * size of the listview. If the scroll-orientation is vertical
77 * the item-offset describes the offset of the horizontal axe, if
78 * the scroll-orientation is horizontal the item-offset describes
79 * the offset of the vertical axe.
80 */
81 void setItemOffset(qreal scrollOffset);
82 qreal itemOffset() const;
83
84 qreal maximumItemOffset() const;
85
86 int maximumVisibleItems() const;
87
88 void setVisibleRoles(const QList<QByteArray> &roles);
89 QList<QByteArray> visibleRoles() const;
90
91 /**
92 * If set to true an automatic scrolling is done as soon as the
93 * mouse is moved near the borders of the view. Per default
94 * the automatic scrolling is turned off.
95 */
96 void setAutoScroll(bool enabled);
97 bool autoScroll() const;
98
99 /**
100 * If set to true selection-toggles will be shown when hovering
101 * an item. Per default the selection-toggles are disabled.
102 */
103 void setEnabledSelectionToggles(bool enabled);
104 bool enabledSelectionToggles() const;
105
106 /**
107 * @return Controller of the item-list. The controller gets
108 * initialized by KItemListController::setView() and will
109 * result in calling KItemListController::onControllerChanged().
110 */
111 KItemListController *controller() const;
112
113 /**
114 * @return Model of the item-list. The model gets
115 * initialized by KItemListController::setModel() and will
116 * result in calling KItemListController::onModelChanged().
117 */
118 KItemModelBase *model() const;
119
120 /**
121 * Sets the creator that creates a widget showing the
122 * content of one model-item. Usually it is sufficient
123 * to implement a custom widget X derived from KItemListWidget and
124 * set the creator by:
125 * <code>
126 * itemListView->setWidgetCreator(new KItemListWidgetCreator<X>());
127 * </code>
128 * The ownership of the widget creator is transferred to
129 * the item-list view.
130 **/
131 void setWidgetCreator(KItemListWidgetCreatorBase *widgetCreator);
132 KItemListWidgetCreatorBase *widgetCreator() const;
133
134 /**
135 * Sets the creator that creates a group header. Usually it is sufficient
136 * to implement a custom header widget X derived from KItemListGroupHeader and
137 * set the creator by:
138 * <code>
139 * itemListView->setGroupHeaderCreator(new KItemListGroupHeaderCreator<X>());
140 * </code>
141 * The ownership of the gropup header creator is transferred to
142 * the item-list view.
143 **/
144 void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase *groupHeaderCreator);
145 KItemListGroupHeaderCreatorBase *groupHeaderCreator() const;
146
147 #ifndef QT_NO_ACCESSIBILITY
148 /**
149 * Uses \a parent to create an accessible object for \a parent. That accessible object will
150 * then be used as the accessible parent of the accessible object for this KItemListView.
151 * Make sure \a parent is the container which contains this specific KItemListView.
152 * This method must be called once before the accessible interface is queried for this class.
153 */
154 void setAccessibleParentsObject(KItemListContainer *accessibleParentsObject);
155 /** The parent of the QAccessibilityInterface of this class. */
156 KItemListContainerAccessible *accessibleParent();
157 #endif
158
159 /**
160 * @return The basic size of all items. The size of an item may be larger than
161 * the basic size (see KItemListView::itemRect()).
162 */
163 QSizeF itemSize() const;
164
165 const KItemListStyleOption &styleOption() const;
166
167 void setGeometry(const QRectF &rect) override;
168
169 /**
170 * @return The page step which should be used by the vertical scroll bar.
171 * This is the height of the view except for the header widget.
172 */
173 qreal verticalPageStep() const;
174
175 /**
176 * @return Index of the item that is below the point \a pos.
177 * The position is relative to the upper right of
178 * the visible area. Only (at least partly) visible
179 * items are considered. std::nullopt is returned if
180 * no item is below the position.
181 */
182 std::optional<int> itemAt(const QPointF &pos) const;
183 bool isAboveSelectionToggle(int index, const QPointF &pos) const;
184 bool isAboveExpansionToggle(int index, const QPointF &pos) const;
185 bool isAboveText(int index, const QPointF &pos) const;
186
187 /**
188 * @return Index of the first item that is at least partly visible.
189 * -1 is returned if the model contains no items.
190 */
191 int firstVisibleIndex() const;
192
193 /**
194 * @return Index of the last item that is at least partly visible.
195 * -1 is returned if the model contains no items.
196 */
197 int lastVisibleIndex() const;
198
199 /**
200 * Calculates the required size for all items in the model.
201 * It might be larger than KItemListView::itemSize().
202 * In this case the layout grid will be stretched to assure an
203 * unclipped item.
204 *
205 * @note the logical height (width) is actually the
206 * width (height) if the scroll orientation is Qt::Vertical!
207 */
208 void calculateItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint) const;
209
210 /**
211 * If set to true, items having child-items can be expanded to show the child-items as
212 * part of the view. Per default the expanding of items is disabled. If expanding of
213 * items is enabled, the methods KItemModelBase::setExpanded(), KItemModelBase::isExpanded(),
214 * KItemModelBase::isExpandable() and KItemModelBase::expandedParentsCount()
215 * must be reimplemented. The view-implementation
216 * has to take care itself how to visually represent the expanded items provided
217 * by the model.
218 */
219 void setSupportsItemExpanding(bool supportsExpanding);
220 bool supportsItemExpanding() const;
221
222 void setHighlightEntireRow(bool highlightEntireRow);
223 bool highlightEntireRow() const;
224
225 void setAlternateBackgrounds(bool alternate);
226 bool alternateBackgrounds() const;
227
228 /**
229 * @return The rectangle of the item relative to the top/left of
230 * the currently visible area (see KItemListView::offset()).
231 */
232 QRectF itemRect(int index) const;
233
234 /**
235 * @return The context rectangle of the item relative to the top/left of
236 * the currently visible area (see KItemListView::offset()). The
237 * context rectangle is defined by the united rectangle of
238 * the icon rectangle and the text rectangle (see KItemListWidget::iconRect()
239 * and KItemListWidget::textRect()) and is useful as reference for e.g. aligning
240 * a tooltip or a context-menu for an item. Note that a context rectangle will
241 * only be returned for (at least partly) visible items. An empty rectangle will
242 * be returned for fully invisible items.
243 */
244 QRectF itemContextRect(int index) const;
245
246 /**
247 * @return Whether or not the name of the file has been elided. At present this will
248 * only ever be true when in icons view.
249 */
250 bool isElided(int index) const;
251
252 /**
253 * Scrolls to the item with the index \a index so that the item
254 * will be fully visible.
255 */
256 void scrollToItem(int index);
257
258 /**
259 * If several properties of KItemListView are changed synchronously, it is
260 * recommended to encapsulate the calls between beginTransaction() and endTransaction().
261 * This prevents unnecessary and expensive layout-calculations.
262 */
263 void beginTransaction();
264
265 /**
266 * Counterpart to beginTransaction(). The layout changes will only be animated if
267 * all property changes between beginTransaction() and endTransaction() support
268 * animations.
269 */
270 void endTransaction();
271
272 bool isTransactionActive() const;
273
274 /**
275 * Turns on the header if \p visible is true. Per default the
276 * header is not visible. Usually the header is turned on when
277 * showing a classic "table-view" to describe the shown columns.
278 */
279 void setHeaderVisible(bool visible);
280 bool isHeaderVisible() const;
281
282 /**
283 * @return Header of the list. The header is also available if it is not shown
284 * (see KItemListView::setHeaderShown()).
285 */
286 KItemListHeader *header() const;
287
288 /**
289 * @return Pixmap that is used for a drag operation based on the
290 * items given by \a indexes.
291 */
292 virtual QPixmap createDragPixmap(const KItemSet &indexes) const;
293
294 /**
295 * Lets the user edit the role \a role for item with the index \a index.
296 */
297 void editRole(int index, const QByteArray &role);
298
299 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
300
301 Q_SIGNALS:
302 void scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous);
303 void scrollOffsetChanged(qreal current, qreal previous);
304 void maximumScrollOffsetChanged(qreal current, qreal previous);
305 void itemOffsetChanged(qreal current, qreal previous);
306 void maximumItemOffsetChanged(qreal current, qreal previous);
307 void scrollTo(qreal newOffset);
308
309 /**
310 * Is emitted if the user has changed the sort order by clicking on a
311 * header item (see KItemListView::setHeaderShown()). The sort order
312 * of the model has already been adjusted to
313 * the current sort order. Note that no signal will be emitted if the
314 * sort order of the model has been changed without user interaction.
315 */
316 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
317
318 /**
319 * Is emitted if the user has changed the sort role by clicking on a
320 * header item (see KItemListView::setHeaderShown()). The sort role
321 * of the model has already been adjusted to
322 * the current sort role. Note that no signal will be emitted if the
323 * sort role of the model has been changed without user interaction.
324 */
325 void sortRoleChanged(const QByteArray &current, const QByteArray &previous);
326
327 /**
328 * Is emitted if the user has changed the visible roles by moving a header
329 * item (see KItemListView::setHeaderShown()). Note that no signal will be
330 * emitted if the roles have been changed without user interaction by
331 * KItemListView::setVisibleRoles().
332 */
333 void visibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous);
334
335 void roleEditingCanceled(int index, const QByteArray &role, const QVariant &value);
336 void roleEditingFinished(int index, const QByteArray &role, const QVariant &value);
337
338 /**
339 * Emitted once scrolling has finished, or immediately if no scrolling was necessary
340 * to get item in view in scrollToItem.
341 */
342 void scrollingStopped();
343
344 void columnHovered(int roleIndex);
345 void columnUnHovered(int roleIndex);
346
347 protected:
348 QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
349 void setItemSize(const QSizeF &size);
350 void setStyleOption(const KItemListStyleOption &option);
351
352 /**
353 * If the scroll-orientation is vertical, the items are ordered
354 * from top to bottom (= default setting). If the scroll-orientation
355 * is horizontal, the items are ordered from left to right.
356 */
357 void setScrollOrientation(Qt::Orientation orientation);
358 Qt::Orientation scrollOrientation() const;
359
360 /**
361 * Factory method for creating a default widget-creator. The method will be used
362 * in case if setWidgetCreator() has not been set by the application.
363 * @return New instance of the widget-creator that should be used per
364 * default.
365 */
366 virtual KItemListWidgetCreatorBase *defaultWidgetCreator() const;
367
368 /**
369 * Factory method for creating a default group-header-creator. The method will be used
370 * in case if setGroupHeaderCreator() has not been set by the application.
371 * @return New instance of the group-header-creator that should be used per
372 * default.
373 */
374 virtual KItemListGroupHeaderCreatorBase *defaultGroupHeaderCreator() const;
375
376 /**
377 * Is called when creating a new KItemListWidget instance and allows derived
378 * classes to do a custom initialization.
379 */
380 virtual void initializeItemListWidget(KItemListWidget *item);
381
382 /**
383 * @return True if at least one of the changed roles \p changedRoles might result
384 * in the need to update the item-size hint (see KItemListView::itemSizeHint()).
385 * Per default true is returned which means on each role-change of existing items
386 * the item-size hints are recalculated. For performance reasons it is recommended
387 * to return false in case if a role-change will not result in a changed
388 * item-size hint.
389 */
390 virtual bool itemSizeHintUpdateRequired(const QSet<QByteArray> &changedRoles) const;
391
392 virtual void onControllerChanged(KItemListController *current, KItemListController *previous);
393 virtual void onModelChanged(KItemModelBase *current, KItemModelBase *previous);
394
395 virtual void onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous);
396 virtual void onItemSizeChanged(const QSizeF &current, const QSizeF &previous);
397 virtual void onScrollOffsetChanged(qreal current, qreal previous);
398 virtual void onVisibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous);
399 virtual void onStyleOptionChanged(const KItemListStyleOption &current, const KItemListStyleOption &previous);
400 virtual void onHighlightEntireRowChanged(bool highlightEntireRow);
401 virtual void onSupportsItemExpandingChanged(bool supportsExpanding);
402
403 virtual void onTransactionBegin();
404 virtual void onTransactionEnd();
405
406 bool event(QEvent *event) override;
407 void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
408 void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
409 void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
410 void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
411 void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override;
412 void dropEvent(QGraphicsSceneDragDropEvent *event) override;
413
414 QList<KItemListWidget *> visibleItemListWidgets() const;
415
416 virtual void updateFont();
417 virtual void updatePalette();
418
419 protected Q_SLOTS:
420 virtual void slotItemsInserted(const KItemRangeList &itemRanges);
421 virtual void slotItemsRemoved(const KItemRangeList &itemRanges);
422 virtual void slotItemsMoved(const KItemRange &itemRange, const QList<int> &movedToIndexes);
423 virtual void slotItemsChanged(const KItemRangeList &itemRanges, const QSet<QByteArray> &roles);
424 virtual void slotGroupsChanged();
425
426 virtual void slotGroupedSortingChanged(bool current);
427 virtual void slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
428 virtual void slotSortRoleChanged(const QByteArray &current, const QByteArray &previous);
429 virtual void slotCurrentChanged(int current, int previous);
430 virtual void slotSelectionChanged(const KItemSet &current, const KItemSet &previous);
431
432 private Q_SLOTS:
433 void slotAnimationFinished(QGraphicsWidget *widget, KItemListViewAnimation::AnimationType type);
434
435 void slotRubberBandPosChanged();
436 void slotRubberBandActivationChanged(bool active);
437
438 /**
439 * Is invoked if the column-width of one role in the header has
440 * been changed by the user. The automatic resizing of columns
441 * will be turned off as soon as this method has been called at
442 * least once.
443 */
444 void slotHeaderColumnWidthChanged(const QByteArray &role, qreal currentWidth, qreal previousWidth);
445
446 void slotSidePaddingChanged(qreal width);
447
448 /**
449 * Is invoked if a column has been moved by the user. Applies
450 * the moved role to the view.
451 */
452 void slotHeaderColumnMoved(const QByteArray &role, int currentIndex, int previousIndex);
453
454 /**
455 * Triggers the autoscrolling if autoScroll() is enabled by checking the
456 * current mouse position. If the mouse position is within the autoscroll
457 * margins a timer will be started that periodically triggers the autoscrolling.
458 */
459 void triggerAutoScrolling();
460
461 /**
462 * Is invoked if the geometry of the parent-widget from a group-header has been
463 * changed. The x-position and width of the group-header gets adjusted to assure
464 * that it always spans the whole width even during temporary transitions of the
465 * parent widget.
466 */
467 void slotGeometryOfGroupHeaderParentChanged();
468
469 void slotRoleEditingCanceled(int index, const QByteArray &role, const QVariant &value);
470 void slotRoleEditingFinished(int index, const QByteArray &role, const QVariant &value);
471
472 private:
473 enum LayoutAnimationHint { NoAnimation, Animation };
474
475 enum SizeType { LayouterSize, ItemSize };
476
477 void setController(KItemListController *controller);
478 void setModel(KItemModelBase *model);
479
480 KItemListRubberBand *rubberBand() const;
481
482 void doLayout(LayoutAnimationHint hint, int changedIndex = 0, int changedCount = 0);
483
484 /**
485 * Helper method for doLayout: Returns a list of items that can be reused for the visible
486 * area. Invisible group headers get recycled. The reusable items are items that are
487 * invisible. If the animation hint is 'Animation' then items that are currently animated
488 * won't be reused. Reusing items is faster in comparison to deleting invisible
489 * items and creating a new instance for visible items.
490 */
491 QList<int> recycleInvisibleItems(int firstVisibleIndex, int lastVisibleIndex, LayoutAnimationHint hint);
492
493 /**
494 * Helper method for doLayout: Starts a moving-animation for the widget to the given
495 * new position. The moving-animation is only started if the new position is within
496 * the same row or column, otherwise the create-animation is used instead.
497 * @return True if the moving-animation has been applied.
498 */
499 bool moveWidget(KItemListWidget *widget, const QPointF &newPos);
500
501 void emitOffsetChanges();
502
503 KItemListWidget *createWidget(int index);
504 void recycleWidget(KItemListWidget *widget);
505
506 /**
507 * Changes the index of the widget to \a index and assures a consistent
508 * update for m_visibleItems and m_visibleCells. The cell-information
509 * for the new index will not be updated and be initialized as empty cell.
510 */
511 void setWidgetIndex(KItemListWidget *widget, int index);
512
513 /**
514 * Changes the index of the widget to \a index. In opposite to
515 * setWidgetIndex() the cell-information for the widget gets updated.
516 * This update gives doLayout() the chance to animate the moving
517 * of the item visually (see moveWidget()).
518 */
519 void moveWidgetToIndex(KItemListWidget *widget, int index);
520
521 /**
522 * Helper method for prepareLayoutForIncreasedItemCount().
523 */
524 void setLayouterSize(const QSizeF &size, SizeType sizeType);
525
526 /**
527 * Helper method for createWidget() and setWidgetIndex() to update the properties
528 * of the itemlist widget.
529 */
530 void updateWidgetProperties(KItemListWidget *widget, int index);
531
532 /**
533 * Helper method for updateWidgetPropertes() to create or update
534 * the itemlist group-header.
535 */
536 void updateGroupHeaderForWidget(KItemListWidget *widget);
537
538 /**
539 * Updates the position and size of the group-header that belongs
540 * to the itemlist widget \a widget. The given widget must represent
541 * the first item of a group.
542 */
543 void updateGroupHeaderLayout(KItemListWidget *widget);
544
545 /**
546 * Recycles the group-header for the widget.
547 */
548 void recycleGroupHeaderForWidget(KItemListWidget *widget);
549
550 /**
551 * Helper method for slotGroupedSortingChanged(), slotSortOrderChanged()
552 * and slotSortRoleChanged(): Iterates through all visible items and updates
553 * the group-header widgets.
554 */
555 void updateVisibleGroupHeaders();
556
557 /**
558 * @return Index for the item in the list returned by KItemModelBase::groups()
559 * that represents the group where the item with the index \a index
560 * belongs to. -1 is returned if no groups are available.
561 */
562 int groupIndexForItem(int index) const;
563
564 /**
565 * Updates the alternate background for all visible items.
566 * @see updateAlternateBackgroundForWidget()
567 */
568 void updateAlternateBackgrounds();
569
570 /**
571 * Updates the alternateBackground-property of the widget dependent
572 * on the state of useAlternateBackgrounds() and the grouping state.
573 */
574 void updateAlternateBackgroundForWidget(KItemListWidget *widget);
575
576 /**
577 * @return True if alternate backgrounds should be used for the items.
578 * This is the case if an empty item-size is given and if there
579 * is more than one visible role.
580 */
581 bool useAlternateBackgrounds() const;
582
583 /**
584 * @param itemRanges Items that must be checked for getting the widths of columns.
585 * @return The preferred width of the column of each visible role. The width will
586 * be respected if the width of the item size is <= 0 (see
587 * KItemListView::setItemSize()). Per default an empty hash
588 * is returned.
589 */
590 QHash<QByteArray, qreal> preferredColumnWidths(const KItemRangeList &itemRanges) const;
591
592 /**
593 * Applies the column-widths from m_headerWidget to the layout
594 * of the view.
595 */
596 void applyColumnWidthsFromHeader();
597
598 /**
599 * Applies the column-widths from m_headerWidget to \a widget.
600 */
601 void updateWidgetColumnWidths(KItemListWidget *widget);
602
603 /**
604 * Updates the preferred column-widths of m_groupHeaderWidget by
605 * invoking KItemListView::columnWidths().
606 */
607 void updatePreferredColumnWidths(const KItemRangeList &itemRanges);
608
609 /**
610 * Convenience method for
611 * updatePreferredColumnWidths(KItemRangeList() << KItemRange(0, m_model->count()).
612 */
613 void updatePreferredColumnWidths();
614
615 /**
616 * Resizes the column-widths of m_headerWidget based on the preferred widths
617 * and the available view-size.
618 */
619 void applyAutomaticColumnWidths();
620
621 /**
622 * @return Sum of the widths of all columns.
623 */
624 qreal columnWidthsSum() const;
625
626 /**
627 * @return Boundaries of the header. An empty rectangle is returned
628 * if no header is shown.
629 */
630 QRectF headerBoundaries() const;
631
632 /**
633 * @return True if the number of columns or rows will be changed when applying
634 * the new grid- and item-size. Used to determine whether an animation
635 * should be done when applying the new layout.
636 */
637 bool changesItemGridLayout(const QSizeF &newGridSize, const QSizeF &newItemSize, const QSizeF &newItemMargin) const;
638
639 /**
640 * @param changedItemCount Number of inserted or removed items.
641 * @return True if the inserting or removing of items should be animated.
642 * No animation should be done if the number of items is too large
643 * to provide a pleasant animation.
644 */
645 bool animateChangedItemCount(int changedItemCount) const;
646
647 /**
648 * @return True if a scrollbar for the given scroll-orientation is required
649 * when using a size of \p size for the view. Calling the method is rather
650 * expansive as a temporary relayout needs to be done.
651 */
652 bool scrollBarRequired(const QSizeF &size) const;
653
654 /**
655 * Shows a drop-indicator between items dependent on the given
656 * cursor position. The cursor position is relative to the upper left
657 * edge of the view.
658 * @return Index of the item where the dropping is done. An index of -1
659 * indicates that the item has been dropped after the last item.
660 */
661 int showDropIndicator(const QPointF &pos);
662 void hideDropIndicator();
663
664 /**
665 * Applies the height of the group header to the layouter. The height
666 * depends on the used scroll orientation.
667 */
668 void updateGroupHeaderHeight();
669
670 /**
671 * Updates the siblings-information for all visible items that are inside
672 * the range of \p firstIndex and \p lastIndex. If firstIndex or lastIndex
673 * is smaller than 0, the siblings-information for all visible items gets
674 * updated.
675 * @see KItemListWidget::setSiblingsInformation()
676 */
677 void updateSiblingsInformation(int firstIndex = -1, int lastIndex = -1);
678
679 /**
680 * Helper method for updateExpansionIndicators().
681 * @return True if the item with the index \a index has a sibling successor
682 * (= the item is not the last item of the current hierarchy).
683 */
684 bool hasSiblingSuccessor(int index) const;
685
686 /**
687 * Helper method for slotRoleEditingCanceled() and slotRoleEditingFinished().
688 * Disconnects the two Signals "roleEditingCanceled" and
689 * "roleEditingFinished"
690 */
691 void disconnectRoleEditingSignals(int index);
692
693 /**
694 * Helper function for triggerAutoScrolling().
695 * @param pos Logical position of the mouse relative to the range.
696 * @param range Range of the visible area.
697 * @param oldInc Previous increment. Is used to assure that the increment
698 * increases only gradually.
699 * @return Scroll increment that should be added to the offset().
700 * As soon as \a pos is inside the autoscroll-margin a
701 * value != 0 will be returned.
702 */
703 static int calculateAutoScrollingIncrement(int pos, int range, int oldInc);
704
705 /**
706 * Helper functions for changesItemCount().
707 * @return The number of items that fit into the available size by
708 * respecting the size of the item and the margin between the items.
709 */
710 static int itemsPerSize(qreal size, qreal itemSize, qreal itemMargin);
711
712 private:
713 bool m_enabledSelectionToggles;
714 bool m_grouped;
715 bool m_highlightEntireRow;
716 bool m_alternateBackgrounds;
717 bool m_supportsItemExpanding;
718 bool m_editingRole;
719 int m_activeTransactions; // Counter for beginTransaction()/endTransaction()
720 LayoutAnimationHint m_endTransactionAnimationHint;
721
722 QSizeF m_itemSize;
723 KItemListController *m_controller;
724 KItemModelBase *m_model;
725 QList<QByteArray> m_visibleRoles;
726 mutable KItemListWidgetCreatorBase *m_widgetCreator;
727 mutable KItemListGroupHeaderCreatorBase *m_groupHeaderCreator;
728 #ifndef QT_NO_ACCESSIBILITY
729 /** The object that will be the parent of this classes QAccessibleInterface. */
730 KItemListContainerAccessible *m_accessibleParent = nullptr;
731 #endif
732 KItemListStyleOption m_styleOption;
733
734 QHash<int, KItemListWidget *> m_visibleItems;
735 QHash<KItemListWidget *, KItemListGroupHeader *> m_visibleGroups;
736
737 struct Cell {
738 Cell()
739 : column(-1)
740 , row(-1)
741 {
742 }
743 Cell(int c, int r)
744 : column(c)
745 , row(r)
746 {
747 }
748 int column;
749 int row;
750 };
751 QHash<int, Cell> m_visibleCells;
752
753 int m_scrollBarExtent;
754 KItemListViewLayouter *m_layouter;
755 KItemListViewAnimation *m_animation;
756
757 qreal m_oldScrollOffset;
758 qreal m_oldMaximumScrollOffset;
759 qreal m_oldItemOffset;
760 qreal m_oldMaximumItemOffset;
761
762 bool m_skipAutoScrollForRubberBand;
763 KItemListRubberBand *m_rubberBand;
764 KItemListRubberBand *m_tapAndHoldIndicator;
765
766 QPointF m_mousePos;
767 int m_autoScrollIncrement;
768 QTimer *m_autoScrollTimer;
769
770 KItemListHeader *m_header;
771 KItemListHeaderWidget *m_headerWidget;
772
773 QPropertyAnimation *m_indicatorAnimation;
774
775 // When dragging items into the view where the sort-role of the model
776 // is empty, a visual indicator should be shown during dragging where
777 // the dropping will happen. This indicator is specified by an index
778 // of the item. -1 means that no indicator will be shown at all.
779 // The m_dropIndicator is set by the KItemListController
780 // by KItemListView::showDropIndicator() and KItemListView::hideDropIndicator().
781 QRectF m_dropIndicator;
782
783 QList<QVariantAnimation *> m_rubberBandAnimations;
784
785 KItemListSizeHintResolver *m_sizeHintResolver;
786
787 friend class KItemListContainer; // Accesses scrollBarRequired()
788 friend class KItemListHeader; // Accesses m_headerWidget
789 friend class KItemListController;
790 friend class KItemListControllerTest;
791 friend class KItemListViewAccessible;
792 friend class KItemListAccessibleCell;
793 };
794
795 /**
796 * Allows to do a fast logical creation and deletion of QGraphicsWidgets
797 * by recycling existing QGraphicsWidgets instances. Is used by
798 * KItemListWidgetCreatorBase and KItemListGroupHeaderCreatorBase.
799 * @internal
800 */
801 class DOLPHIN_EXPORT KItemListCreatorBase
802 {
803 public:
804 virtual ~KItemListCreatorBase();
805
806 protected:
807 void addCreatedWidget(QGraphicsWidget *widget);
808 void pushRecycleableWidget(QGraphicsWidget *widget);
809 QGraphicsWidget *popRecycleableWidget();
810
811 private:
812 QSet<QGraphicsWidget *> m_createdWidgets;
813 QList<QGraphicsWidget *> m_recycleableWidgets;
814 };
815
816 /**
817 * @brief Base class for creating KItemListWidgets.
818 *
819 * It is recommended that applications simply use the KItemListWidgetCreator-template class.
820 * For a custom implementation the methods create(), itemSizeHint() and preferredColumnWith()
821 * must be reimplemented. The intention of the widget creator is to prevent repetitive and
822 * expensive instantiations and deletions of KItemListWidgets by recycling existing widget
823 * instances.
824 */
825 class DOLPHIN_EXPORT KItemListWidgetCreatorBase : public KItemListCreatorBase
826 {
827 public:
828 ~KItemListWidgetCreatorBase() override;
829
830 virtual KItemListWidget *create(KItemListView *view) = 0;
831
832 virtual void recycle(KItemListWidget *widget);
833
834 virtual void calculateItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint, const KItemListView *view) const = 0;
835
836 virtual qreal preferredRoleColumnWidth(const QByteArray &role, int index, const KItemListView *view) const = 0;
837 };
838
839 /**
840 * @brief Template class for creating KItemListWidgets.
841 */
842 template<class T>
843 class KItemListWidgetCreator : public KItemListWidgetCreatorBase
844 {
845 public:
846 KItemListWidgetCreator();
847 ~KItemListWidgetCreator() override;
848
849 KItemListWidget *create(KItemListView *view) override;
850
851 void calculateItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints, qreal &logicalWidthHint, const KItemListView *view) const override;
852
853 qreal preferredRoleColumnWidth(const QByteArray &role, int index, const KItemListView *view) const override;
854
855 private:
856 KItemListWidgetInformant *m_informant;
857 };
858
859 template<class T>
860 KItemListWidgetCreator<T>::KItemListWidgetCreator()
861 : m_informant(T::createInformant())
862 {
863 }
864
865 template<class T>
866 KItemListWidgetCreator<T>::~KItemListWidgetCreator()
867 {
868 delete m_informant;
869 }
870
871 template<class T>
872 KItemListWidget *KItemListWidgetCreator<T>::create(KItemListView *view)
873 {
874 KItemListWidget *widget = static_cast<KItemListWidget *>(popRecycleableWidget());
875 if (!widget) {
876 widget = new T(m_informant, view);
877 addCreatedWidget(widget);
878 }
879 widget->setParentItem(view);
880 return widget;
881 }
882
883 template<class T>
884 void KItemListWidgetCreator<T>::calculateItemSizeHints(QVector<std::pair<qreal, bool>> &logicalHeightHints,
885 qreal &logicalWidthHint,
886 const KItemListView *view) const
887 {
888 return m_informant->calculateItemSizeHints(logicalHeightHints, logicalWidthHint, view);
889 }
890
891 template<class T>
892 qreal KItemListWidgetCreator<T>::preferredRoleColumnWidth(const QByteArray &role, int index, const KItemListView *view) const
893 {
894 return m_informant->preferredRoleColumnWidth(role, index, view);
895 }
896
897 /**
898 * @brief Base class for creating KItemListGroupHeaders.
899 *
900 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
901 * For a custom implementation the methods create() and recycle() must be reimplemented.
902 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
903 * deletions of KItemListGroupHeaders by recycling existing header instances.
904 */
905 class DOLPHIN_EXPORT KItemListGroupHeaderCreatorBase : public KItemListCreatorBase
906 {
907 public:
908 ~KItemListGroupHeaderCreatorBase() override;
909 virtual KItemListGroupHeader *create(KItemListView *view) = 0;
910 virtual void recycle(KItemListGroupHeader *header);
911 };
912
913 template<class T>
914 class KItemListGroupHeaderCreator : public KItemListGroupHeaderCreatorBase
915 {
916 public:
917 ~KItemListGroupHeaderCreator() override;
918 KItemListGroupHeader *create(KItemListView *view) override;
919 };
920
921 template<class T>
922 KItemListGroupHeaderCreator<T>::~KItemListGroupHeaderCreator()
923 {
924 }
925
926 template<class T>
927 KItemListGroupHeader *KItemListGroupHeaderCreator<T>::create(KItemListView *view)
928 {
929 KItemListGroupHeader *widget = static_cast<KItemListGroupHeader *>(popRecycleableWidget());
930 if (!widget) {
931 widget = new T(view);
932 addCreatedWidget(widget);
933 }
934 return widget;
935 }
936
937 #endif