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