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