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