]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.h
Revert the 2.0 decision to always use KB for file-sizes
[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 * The returned value might be larger than KItemListView::itemSize().
190 * In this case the layout grid will be stretched to assure an
191 * unclipped item.
192 */
193 QSizeF itemSizeHint(int index) const;
194
195 /**
196 * If set to true, items having child-items can be expanded to show the child-items as
197 * part of the view. Per default the expanding of items is is disabled. If expanding of
198 * items is enabled, the methods KItemModelBase::setExpanded(), KItemModelBase::isExpanded(),
199 * KItemModelBase::isExpandable() and KItemModelBase::expandedParentsCount()
200 * must be reimplemented. The view-implementation
201 * has to take care itself how to visually represent the expanded items provided
202 * by the model.
203 */
204 void setSupportsItemExpanding(bool supportsExpanding);
205 bool supportsItemExpanding() const;
206
207 /**
208 * @return The rectangle of the item relative to the top/left of
209 * the currently visible area (see KItemListView::offset()).
210 */
211 QRectF itemRect(int index) const;
212
213 /**
214 * @return The context rectangle of the item relative to the top/left of
215 * the currently visible area (see KItemListView::offset()). The
216 * context rectangle is defined by by the united rectangle of
217 * the icon rectangle and the text rectangle (see KItemListWidget::iconRect()
218 * and KItemListWidget::textRect()) and is useful as reference for e.g. aligning
219 * a tooltip or a context-menu for an item. Note that a context rectangle will
220 * only be returned for (at least partly) visible items. An empty rectangle will
221 * be returned for fully invisible items.
222 */
223 QRectF itemContextRect(int index) const;
224
225 /**
226 * Scrolls to the item with the index \a index so that the item
227 * will be fully visible.
228 */
229 void scrollToItem(int index);
230
231 /**
232 * If several properties of KItemListView are changed synchronously, it is
233 * recommended to encapsulate the calls between beginTransaction() and endTransaction().
234 * This prevents unnecessary and expensive layout-calculations.
235 */
236 void beginTransaction();
237
238 /**
239 * Counterpart to beginTransaction(). The layout changes will only be animated if
240 * all property changes between beginTransaction() and endTransaction() support
241 * animations.
242 */
243 void endTransaction();
244
245 bool isTransactionActive() const;
246
247 /**
248 * Turns on the header if \p visible is true. Per default the
249 * header is not visible. Usually the header is turned on when
250 * showing a classic "table-view" to describe the shown columns.
251 */
252 void setHeaderVisible(bool visible);
253 bool isHeaderVisible() const;
254
255 /**
256 * @return Header of the list. The header is also available if it is not shown
257 * (see KItemListView::setHeaderShown()).
258 */
259 KItemListHeader* header() const;
260
261 /**
262 * @return Pixmap that is used for a drag operation based on the
263 * items given by \a indexes. The default implementation returns
264 * a null-pixmap.
265 */
266 virtual QPixmap createDragPixmap(const QSet<int>& indexes) const;
267
268 /**
269 * @reimp
270 */
271 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
272
273 signals:
274 void scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous);
275 void scrollOffsetChanged(qreal current, qreal previous);
276 void maximumScrollOffsetChanged(qreal current, qreal previous);
277 void itemOffsetChanged(qreal current, qreal previous);
278 void maximumItemOffsetChanged(qreal current, qreal previous);
279 void scrollTo(qreal newOffset);
280
281 /**
282 * Is emitted if the user has changed the sort order by clicking on a
283 * header item (see KItemListView::setHeaderShown()). The sort order
284 * of the model has already been adjusted to
285 * the current sort order. Note that no signal will be emitted if the
286 * sort order of the model has been changed without user interaction.
287 */
288 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
289
290 /**
291 * Is emitted if the user has changed the sort role by clicking on a
292 * header item (see KItemListView::setHeaderShown()). The sort role
293 * of the model has already been adjusted to
294 * the current sort role. Note that no signal will be emitted if the
295 * sort role of the model has been changed without user interaction.
296 */
297 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
298
299 /**
300 * Is emitted if the user has changed the visible roles by moving a header
301 * item (see KItemListView::setHeaderShown()). Note that no signal will be
302 * emitted if the roles have been changed without user interaction by
303 * KItemListView::setVisibleRoles().
304 */
305 void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
306
307 protected:
308 virtual void initializeItemListWidget(KItemListWidget* item);
309
310 /**
311 * @return True if at least one of the changed roles \p changedRoles might result
312 * in the need to update the item-size hint (see KItemListView::itemSizeHint()).
313 * Per default true is returned which means on each role-change of existing items
314 * the item-size hints are recalculated. For performance reasons it is recommended
315 * to return false in case if a role-change will not result in a changed
316 * item-size hint.
317 */
318 virtual bool itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const;
319
320 virtual void onControllerChanged(KItemListController* current, KItemListController* previous);
321 virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous);
322
323 virtual void onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous);
324 virtual void onItemSizeChanged(const QSizeF& current, const QSizeF& previous);
325 virtual void onScrollOffsetChanged(qreal current, qreal previous);
326 virtual void onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
327 virtual void onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
328 virtual void onSupportsItemExpandingChanged(bool supportsExpanding);
329
330 virtual void onTransactionBegin();
331 virtual void onTransactionEnd();
332
333 virtual bool event(QEvent* event);
334 virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
335 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
336 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent* event);
337 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent* event);
338 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent* event);
339 virtual void dropEvent(QGraphicsSceneDragDropEvent* event);
340
341 QList<KItemListWidget*> visibleItemListWidgets() const;
342
343 protected slots:
344 virtual void slotItemsInserted(const KItemRangeList& itemRanges);
345 virtual void slotItemsRemoved(const KItemRangeList& itemRanges);
346 virtual void slotItemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes);
347 virtual void slotItemsChanged(const KItemRangeList& itemRanges,
348 const QSet<QByteArray>& roles);
349
350 virtual void slotGroupedSortingChanged(bool current);
351 virtual void slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
352 virtual void slotSortRoleChanged(const QByteArray& current, const QByteArray& previous);
353 virtual void slotCurrentChanged(int current, int previous);
354 virtual void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
355
356 private slots:
357 void slotAnimationFinished(QGraphicsWidget* widget,
358 KItemListViewAnimation::AnimationType type);
359 void slotLayoutTimerFinished();
360
361 void slotRubberBandPosChanged();
362 void slotRubberBandActivationChanged(bool active);
363
364 /**
365 * Is invoked if the column-width of one role in the header has
366 * been changed by the user. The automatic resizing of columns
367 * will be turned off as soon as this method has been called at
368 * least once.
369 */
370 void slotHeaderColumnWidthChanged(const QByteArray& role,
371 qreal currentWidth,
372 qreal previousWidth);
373
374 /**
375 * Is invoked if a column has been moved by the user. Applies
376 * the moved role to the view.
377 */
378 void slotHeaderColumnMoved(const QByteArray& role,
379 int currentIndex,
380 int previousIndex);
381
382 /**
383 * Triggers the autoscrolling if autoScroll() is enabled by checking the
384 * current mouse position. If the mouse position is within the autoscroll
385 * margins a timer will be started that periodically triggers the autoscrolling.
386 */
387 void triggerAutoScrolling();
388
389 /**
390 * Is invoked if the geometry of the parent-widget from a group-header has been
391 * changed. The x-position and width of the group-header gets adjusted to assure
392 * that it always spans the whole width even during temporary transitions of the
393 * parent widget.
394 */
395 void slotGeometryOfGroupHeaderParentChanged();
396
397 private:
398 enum LayoutAnimationHint
399 {
400 NoAnimation,
401 Animation
402 };
403
404 enum SizeType
405 {
406 LayouterSize,
407 ItemSize
408 };
409
410 void setController(KItemListController* controller);
411 void setModel(KItemModelBase* model);
412
413 KItemListRubberBand* rubberBand() const;
414
415 void doLayout(LayoutAnimationHint hint, int changedIndex = 0, int changedCount = 0);
416
417 /**
418 * Helper method for doLayout: Returns a list of items that can be reused for the visible
419 * area. Invisible group headers get recycled. The reusable items are items that are
420 * invisible. If the animation hint is 'Animation' then items that are currently animated
421 * won't be reused. Reusing items is faster in comparison to deleting invisible
422 * items and creating a new instance for visible items.
423 */
424 QList<int> recycleInvisibleItems(int firstVisibleIndex,
425 int lastVisibleIndex,
426 LayoutAnimationHint hint);
427
428 /**
429 * Helper method for doLayout: Starts a moving-animation for the widget to the given
430 * new position. The moving-animation is only started if the new position is within
431 * the same row or column, otherwise the create-animation is used instead.
432 * @return True if the moving-animation has been applied.
433 */
434 bool moveWidget(KItemListWidget* widget, const QPointF& newPos);
435
436 void emitOffsetChanges();
437
438 KItemListWidget* createWidget(int index);
439 void recycleWidget(KItemListWidget* widget);
440
441 /**
442 * Changes the index of the widget to \a index and assures a consistent
443 * update for m_visibleItems and m_visibleCells. The cell-information
444 * for the new index will not be updated and be initialized as empty cell.
445 */
446 void setWidgetIndex(KItemListWidget* widget, int index);
447
448 /**
449 * Changes the index of the widget to \a index. In opposite to
450 * setWidgetIndex() the cell-information for the widget gets updated.
451 * This update gives doLayout() the chance to animate the moving
452 * of the item visually (see moveWidget()).
453 */
454 void moveWidgetToIndex(KItemListWidget* widget, int index);
455
456 /**
457 * Helper method for prepareLayoutForIncreasedItemCount().
458 */
459 void setLayouterSize(const QSizeF& size, SizeType sizeType);
460
461 /**
462 * Helper method for createWidget() and setWidgetIndex() to update the properties
463 * of the itemlist widget.
464 */
465 void updateWidgetProperties(KItemListWidget* widget, int index);
466
467 /**
468 * Helper method for updateWidgetPropertes() to create or update
469 * the itemlist group-header.
470 */
471 void updateGroupHeaderForWidget(KItemListWidget* widget);
472
473 /**
474 * Updates the position and size of the group-header that belongs
475 * to the itemlist widget \a widget. The given widget must represent
476 * the first item of a group.
477 */
478 void updateGroupHeaderLayout(KItemListWidget* widget);
479
480 /**
481 * Recycles the group-header for the widget.
482 */
483 void recycleGroupHeaderForWidget(KItemListWidget* widget);
484
485 /**
486 * Helper method for slotGroupedSortingChanged(), slotSortOrderChanged()
487 * and slotSortRoleChanged(): Iterates through all visible items and updates
488 * the group-header widgets.
489 */
490 void updateVisibleGroupHeaders();
491
492 /**
493 * @return Index for the item in the list returned by KItemModelBase::groups()
494 * that represents the group where the item with the index \a index
495 * belongs to. -1 is returned if no groups are available.
496 */
497 int groupIndexForItem(int index) const;
498
499 /**
500 * Updates the alternate background for all visible items.
501 * @see updateAlternateBackgroundForWidget()
502 */
503 void updateAlternateBackgrounds();
504
505 /**
506 * Updates the alternateBackground-property of the widget dependent
507 * on the state of useAlternateBackgrounds() and the grouping state.
508 */
509 void updateAlternateBackgroundForWidget(KItemListWidget* widget);
510
511 /**
512 * @return True if alternate backgrounds should be used for the items.
513 * This is the case if an empty item-size is given and if there
514 * is more than one visible role.
515 */
516 bool useAlternateBackgrounds() const;
517
518 /**
519 * @param itemRanges Items that must be checked for getting the widths of columns.
520 * @return The preferred width of the column of each visible role. The width will
521 * be respected if the width of the item size is <= 0 (see
522 * KItemListView::setItemSize()). Per default an empty hash
523 * is returned.
524 */
525 QHash<QByteArray, qreal> preferredColumnWidths(const KItemRangeList& itemRanges) const;
526
527 /**
528 * Applies the column-widths from m_headerWidget to the layout
529 * of the view.
530 */
531 void applyColumnWidthsFromHeader();
532
533 /**
534 * Applies the column-widths from m_headerWidget to \a widget.
535 */
536 void updateWidgetColumnWidths(KItemListWidget* widget);
537
538 /**
539 * Updates the preferred column-widths of m_groupHeaderWidget by
540 * invoking KItemListView::columnWidths().
541 */
542 void updatePreferredColumnWidths(const KItemRangeList& itemRanges);
543
544 /**
545 * Convenience method for
546 * updatePreferredColumnWidths(KItemRangeList() << KItemRange(0, m_model->count()).
547 */
548 void updatePreferredColumnWidths();
549
550 /**
551 * Resizes the column-widths of m_headerWidget based on the preferred widths
552 * and the vailable view-size.
553 */
554 void applyAutomaticColumnWidths();
555
556 /**
557 * @return Sum of the widths of all columns.
558 */
559 qreal columnWidthsSum() const;
560
561 /**
562 * @return Boundaries of the header. An empty rectangle is returned
563 * if no header is shown.
564 */
565 QRectF headerBoundaries() const;
566
567 /**
568 * @return True if the number of columns or rows will be changed when applying
569 * the new grid- and item-size. Used to determine whether an animation
570 * should be done when applying the new layout.
571 */
572 bool changesItemGridLayout(const QSizeF& newGridSize,
573 const QSizeF& newItemSize,
574 const QSizeF& newItemMargin) const;
575
576 /**
577 * @param changedItemCount Number of inserted or removed items.
578 * @return True if the inserting or removing of items should be animated.
579 * No animation should be done if the number of items is too large
580 * to provide a pleasant animation.
581 */
582 bool animateChangedItemCount(int changedItemCount) const;
583
584 /**
585 * @return True if a scrollbar for the given scroll-orientation is required
586 * when using a size of \p size for the view. Calling the method is rather
587 * expansive as a temporary relayout needs to be done.
588 */
589 bool scrollBarRequired(const QSizeF& size) const;
590
591 /**
592 * Applies the height of the group header to the layouter. The height
593 * depends on the used scroll orientation.
594 */
595 void updateGroupHeaderHeight();
596
597 /**
598 * Updates the siblings-information for all visible items that are inside
599 * the range of \p firstIndex and \p lastIndex. If firstIndex or lastIndex
600 * is smaller than 0, the siblings-information for all visible items gets
601 * updated.
602 * @see KItemListWidget::setSiblingsInformation()
603 */
604 void updateSiblingsInformation(int firstIndex = -1, int lastIndex = -1);
605
606 /**
607 * Helper method for updateExpansionIndicators().
608 * @return True if the item with the index \a index has a sibling successor
609 * (= the item is not the last item of the current hierarchy).
610 */
611 bool hasSiblingSuccessor(int index) const;
612
613 /**
614 * Helper function for triggerAutoScrolling().
615 * @param pos Logical position of the mouse relative to the range.
616 * @param range Range of the visible area.
617 * @param oldInc Previous increment. Is used to assure that the increment
618 * increases only gradually.
619 * @return Scroll increment that should be added to the offset().
620 * As soon as \a pos is inside the autoscroll-margin a
621 * value != 0 will be returned.
622 */
623 static int calculateAutoScrollingIncrement(int pos, int range, int oldInc);
624
625 /**
626 * Helper functions for changesItemCount().
627 * @return The number of items that fit into the available size by
628 * respecting the size of the item and the margin between the items.
629 */
630 static int itemsPerSize(qreal size, qreal itemSize, qreal itemMargin);
631
632 private:
633 bool m_enabledSelectionToggles;
634 bool m_grouped;
635 bool m_supportsItemExpanding;
636 int m_activeTransactions; // Counter for beginTransaction()/endTransaction()
637 LayoutAnimationHint m_endTransactionAnimationHint;
638
639 QSizeF m_itemSize;
640 KItemListController* m_controller;
641 KItemModelBase* m_model;
642 QList<QByteArray> m_visibleRoles;
643 KItemListWidgetCreatorBase* m_widgetCreator;
644 KItemListGroupHeaderCreatorBase* m_groupHeaderCreator;
645 KItemListStyleOption m_styleOption;
646
647 QHash<int, KItemListWidget*> m_visibleItems;
648 QHash<KItemListWidget*, KItemListGroupHeader*> m_visibleGroups;
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(), itemSizeHint() and preferredColumnWith()
712 * must be reimplemented. The intention of the widget creator is to prevent repetitive and
713 * expensive instantiations and deletions of KItemListWidgets by recycling existing widget
714 * instances.
715 */
716 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreatorBase : public KItemListCreatorBase
717 {
718 public:
719 virtual ~KItemListWidgetCreatorBase();
720
721 virtual KItemListWidget* create(KItemListView* view) = 0;
722
723 virtual void recycle(KItemListWidget* widget);
724
725 virtual QSizeF itemSizeHint(int index, const KItemListView* view) const = 0;
726
727 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
728 int index,
729 const KItemListView* view) const = 0;
730 };
731
732 /**
733 * @brief Template class for creating KItemListWidgets.
734 *
735 * The template class must provide the following two static methods:
736 * - QSizeF itemSizeHint(int index, const KItemListView* view)
737 * - preferredRoleColumnWidth(const QByteArray& role, int index, const KItemListView* view)
738 * Those static methods are used as implementation for
739 * KItemListWidgetCreatorBase::itemSizeHint() and
740 * KItemListWidgetCreatorBase::preferedRoleColumnWidth().
741 */
742 template <class T>
743 class KItemListWidgetCreator : public KItemListWidgetCreatorBase
744 {
745 public:
746 virtual ~KItemListWidgetCreator();
747
748 virtual KItemListWidget* create(KItemListView* view);
749
750 virtual QSizeF itemSizeHint(int index, const KItemListView* view) const;
751
752 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
753 int index,
754 const KItemListView* view) const;
755 };
756
757 template <class T>
758 KItemListWidgetCreator<T>::~KItemListWidgetCreator()
759 {
760 }
761
762 template <class T>
763 KItemListWidget* KItemListWidgetCreator<T>::create(KItemListView* view)
764 {
765 KItemListWidget* widget = static_cast<KItemListWidget*>(popRecycleableWidget());
766 if (!widget) {
767 widget = new T(view);
768 addCreatedWidget(widget);
769 }
770 return widget;
771 }
772
773 template<class T>
774 QSizeF KItemListWidgetCreator<T>::itemSizeHint(int index, const KItemListView* view) const
775 {
776 return T::itemSizeHint(index, view);
777 }
778
779 template<class T>
780 qreal KItemListWidgetCreator<T>::preferredRoleColumnWidth(const QByteArray& role,
781 int index,
782 const KItemListView* view) const
783 {
784 return T::preferredRoleColumnWidth(role, index, view);
785 }
786
787 /**
788 * @brief Base class for creating KItemListGroupHeaders.
789 *
790 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
791 * For a custom implementation the methods create() and recyle() must be reimplemented.
792 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
793 * deletions of KItemListGroupHeaders by recycling existing header instances.
794 */
795 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreatorBase : public KItemListCreatorBase
796 {
797 public:
798 virtual ~KItemListGroupHeaderCreatorBase();
799 virtual KItemListGroupHeader* create(KItemListView* view) = 0;
800 virtual void recycle(KItemListGroupHeader* header);
801 };
802
803 template <class T>
804 class KItemListGroupHeaderCreator : public KItemListGroupHeaderCreatorBase
805 {
806 public:
807 virtual ~KItemListGroupHeaderCreator();
808 virtual KItemListGroupHeader* create(KItemListView* view);
809 };
810
811 template <class T>
812 KItemListGroupHeaderCreator<T>::~KItemListGroupHeaderCreator()
813 {
814 }
815
816 template <class T>
817 KItemListGroupHeader* KItemListGroupHeaderCreator<T>::create(KItemListView* view)
818 {
819 KItemListGroupHeader* widget = static_cast<KItemListGroupHeader*>(popRecycleableWidget());
820 if (!widget) {
821 widget = new T(view);
822 addCreatedWidget(widget);
823 }
824 return widget;
825 }
826
827 #endif