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