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