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