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