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