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