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