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