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