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