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