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