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