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