]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.h
Merge branch 'master' into frameworks
[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::itemSizeHint() and KItemListView::itemRect()).
158 */
159 QSizeF itemSize() const;
160
161 const KItemListStyleOption& styleOption() const;
162
163 virtual void setGeometry(const QRectF& rect) Q_DECL_OVERRIDE;
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 Calculates the required size for all items in the model.
196 * It might be larger than KItemListView::itemSize().
197 * In this case the layout grid will be stretched to assure an
198 * unclipped item.
199 * NOTE: the logical height (width) is actually the
200 * width (height) if the scroll orientation is Qt::Vertical!
201 */
202 void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint) 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 KItemSet& 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 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0) Q_DECL_OVERRIDE;
282
283 signals:
284 void scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous);
285 void scrollOffsetChanged(qreal current, qreal previous);
286 void maximumScrollOffsetChanged(qreal current, qreal previous);
287 void itemOffsetChanged(qreal current, qreal previous);
288 void maximumItemOffsetChanged(qreal current, qreal previous);
289 void scrollTo(qreal newOffset);
290
291 /**
292 * Is emitted if the user has changed the sort order by clicking on a
293 * header item (see KItemListView::setHeaderShown()). The sort order
294 * of the model has already been adjusted to
295 * the current sort order. Note that no signal will be emitted if the
296 * sort order of the model has been changed without user interaction.
297 */
298 void sortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
299
300 /**
301 * Is emitted if the user has changed the sort role by clicking on a
302 * header item (see KItemListView::setHeaderShown()). The sort role
303 * of the model has already been adjusted to
304 * the current sort role. Note that no signal will be emitted if the
305 * sort role of the model has been changed without user interaction.
306 */
307 void sortRoleChanged(const QByteArray& current, const QByteArray& previous);
308
309 /**
310 * Is emitted if the user has changed the visible roles by moving a header
311 * item (see KItemListView::setHeaderShown()). Note that no signal will be
312 * emitted if the roles have been changed without user interaction by
313 * KItemListView::setVisibleRoles().
314 */
315 void visibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous);
316
317 void roleEditingCanceled(int index, const QByteArray& role, const QVariant& value);
318 void roleEditingFinished(int index, const QByteArray& role, const QVariant& value);
319
320 protected:
321 virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE;
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) Q_DECL_OVERRIDE;
379 virtual void mousePressEvent(QGraphicsSceneMouseEvent* event) Q_DECL_OVERRIDE;
380 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event) Q_DECL_OVERRIDE;
381 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent* event) Q_DECL_OVERRIDE;
382 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent* event) Q_DECL_OVERRIDE;
383 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent* event) Q_DECL_OVERRIDE;
384 virtual void dropEvent(QGraphicsSceneDragDropEvent* event) Q_DECL_OVERRIDE;
385
386 QList<KItemListWidget*> visibleItemListWidgets() const;
387
388 virtual void updateFont();
389 virtual void updatePalette();
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 virtual void slotGroupsChanged();
398
399 virtual void slotGroupedSortingChanged(bool current);
400 virtual void slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous);
401 virtual void slotSortRoleChanged(const QByteArray& current, const QByteArray& previous);
402 virtual void slotCurrentChanged(int current, int previous);
403 virtual void slotSelectionChanged(const KItemSet& current, const KItemSet& previous);
404
405 private slots:
406 void slotAnimationFinished(QGraphicsWidget* widget,
407 KItemListViewAnimation::AnimationType type);
408 void slotLayoutTimerFinished();
409
410 void slotRubberBandPosChanged();
411 void slotRubberBandActivationChanged(bool active);
412
413 /**
414 * Is invoked if the column-width of one role in the header has
415 * been changed by the user. The automatic resizing of columns
416 * will be turned off as soon as this method has been called at
417 * least once.
418 */
419 void slotHeaderColumnWidthChanged(const QByteArray& role,
420 qreal currentWidth,
421 qreal previousWidth);
422
423 /**
424 * Is invoked if a column has been moved by the user. Applies
425 * the moved role to the view.
426 */
427 void slotHeaderColumnMoved(const QByteArray& role,
428 int currentIndex,
429 int previousIndex);
430
431 /**
432 * Triggers the autoscrolling if autoScroll() is enabled by checking the
433 * current mouse position. If the mouse position is within the autoscroll
434 * margins a timer will be started that periodically triggers the autoscrolling.
435 */
436 void triggerAutoScrolling();
437
438 /**
439 * Is invoked if the geometry of the parent-widget from a group-header has been
440 * changed. The x-position and width of the group-header gets adjusted to assure
441 * that it always spans the whole width even during temporary transitions of the
442 * parent widget.
443 */
444 void slotGeometryOfGroupHeaderParentChanged();
445
446 void slotRoleEditingCanceled(int index, const QByteArray& role, const QVariant& value);
447 void slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value);
448
449 private:
450 enum LayoutAnimationHint
451 {
452 NoAnimation,
453 Animation
454 };
455
456 enum SizeType
457 {
458 LayouterSize,
459 ItemSize
460 };
461
462 void setController(KItemListController* controller);
463 void setModel(KItemModelBase* model);
464
465 KItemListRubberBand* rubberBand() const;
466
467 void doLayout(LayoutAnimationHint hint, int changedIndex = 0, int changedCount = 0);
468
469 /**
470 * Helper method for doLayout: Returns a list of items that can be reused for the visible
471 * area. Invisible group headers get recycled. The reusable items are items that are
472 * invisible. If the animation hint is 'Animation' then items that are currently animated
473 * won't be reused. Reusing items is faster in comparison to deleting invisible
474 * items and creating a new instance for visible items.
475 */
476 QList<int> recycleInvisibleItems(int firstVisibleIndex,
477 int lastVisibleIndex,
478 LayoutAnimationHint hint);
479
480 /**
481 * Helper method for doLayout: Starts a moving-animation for the widget to the given
482 * new position. The moving-animation is only started if the new position is within
483 * the same row or column, otherwise the create-animation is used instead.
484 * @return True if the moving-animation has been applied.
485 */
486 bool moveWidget(KItemListWidget* widget, const QPointF& newPos);
487
488 void emitOffsetChanges();
489
490 KItemListWidget* createWidget(int index);
491 void recycleWidget(KItemListWidget* widget);
492
493 /**
494 * Changes the index of the widget to \a index and assures a consistent
495 * update for m_visibleItems and m_visibleCells. The cell-information
496 * for the new index will not be updated and be initialized as empty cell.
497 */
498 void setWidgetIndex(KItemListWidget* widget, int index);
499
500 /**
501 * Changes the index of the widget to \a index. In opposite to
502 * setWidgetIndex() the cell-information for the widget gets updated.
503 * This update gives doLayout() the chance to animate the moving
504 * of the item visually (see moveWidget()).
505 */
506 void moveWidgetToIndex(KItemListWidget* widget, int index);
507
508 /**
509 * Helper method for prepareLayoutForIncreasedItemCount().
510 */
511 void setLayouterSize(const QSizeF& size, SizeType sizeType);
512
513 /**
514 * Helper method for createWidget() and setWidgetIndex() to update the properties
515 * of the itemlist widget.
516 */
517 void updateWidgetProperties(KItemListWidget* widget, int index);
518
519 /**
520 * Helper method for updateWidgetPropertes() to create or update
521 * the itemlist group-header.
522 */
523 void updateGroupHeaderForWidget(KItemListWidget* widget);
524
525 /**
526 * Updates the position and size of the group-header that belongs
527 * to the itemlist widget \a widget. The given widget must represent
528 * the first item of a group.
529 */
530 void updateGroupHeaderLayout(KItemListWidget* widget);
531
532 /**
533 * Recycles the group-header for the widget.
534 */
535 void recycleGroupHeaderForWidget(KItemListWidget* widget);
536
537 /**
538 * Helper method for slotGroupedSortingChanged(), slotSortOrderChanged()
539 * and slotSortRoleChanged(): Iterates through all visible items and updates
540 * the group-header widgets.
541 */
542 void updateVisibleGroupHeaders();
543
544 /**
545 * @return Index for the item in the list returned by KItemModelBase::groups()
546 * that represents the group where the item with the index \a index
547 * belongs to. -1 is returned if no groups are available.
548 */
549 int groupIndexForItem(int index) const;
550
551 /**
552 * Updates the alternate background for all visible items.
553 * @see updateAlternateBackgroundForWidget()
554 */
555 void updateAlternateBackgrounds();
556
557 /**
558 * Updates the alternateBackground-property of the widget dependent
559 * on the state of useAlternateBackgrounds() and the grouping state.
560 */
561 void updateAlternateBackgroundForWidget(KItemListWidget* widget);
562
563 /**
564 * @return True if alternate backgrounds should be used for the items.
565 * This is the case if an empty item-size is given and if there
566 * is more than one visible role.
567 */
568 bool useAlternateBackgrounds() const;
569
570 /**
571 * @param itemRanges Items that must be checked for getting the widths of columns.
572 * @return The preferred width of the column of each visible role. The width will
573 * be respected if the width of the item size is <= 0 (see
574 * KItemListView::setItemSize()). Per default an empty hash
575 * is returned.
576 */
577 QHash<QByteArray, qreal> preferredColumnWidths(const KItemRangeList& itemRanges) const;
578
579 /**
580 * Applies the column-widths from m_headerWidget to the layout
581 * of the view.
582 */
583 void applyColumnWidthsFromHeader();
584
585 /**
586 * Applies the column-widths from m_headerWidget to \a widget.
587 */
588 void updateWidgetColumnWidths(KItemListWidget* widget);
589
590 /**
591 * Updates the preferred column-widths of m_groupHeaderWidget by
592 * invoking KItemListView::columnWidths().
593 */
594 void updatePreferredColumnWidths(const KItemRangeList& itemRanges);
595
596 /**
597 * Convenience method for
598 * updatePreferredColumnWidths(KItemRangeList() << KItemRange(0, m_model->count()).
599 */
600 void updatePreferredColumnWidths();
601
602 /**
603 * Resizes the column-widths of m_headerWidget based on the preferred widths
604 * and the vailable view-size.
605 */
606 void applyAutomaticColumnWidths();
607
608 /**
609 * @return Sum of the widths of all columns.
610 */
611 qreal columnWidthsSum() const;
612
613 /**
614 * @return Boundaries of the header. An empty rectangle is returned
615 * if no header is shown.
616 */
617 QRectF headerBoundaries() const;
618
619 /**
620 * @return True if the number of columns or rows will be changed when applying
621 * the new grid- and item-size. Used to determine whether an animation
622 * should be done when applying the new layout.
623 */
624 bool changesItemGridLayout(const QSizeF& newGridSize,
625 const QSizeF& newItemSize,
626 const QSizeF& newItemMargin) const;
627
628 /**
629 * @param changedItemCount Number of inserted or removed items.
630 * @return True if the inserting or removing of items should be animated.
631 * No animation should be done if the number of items is too large
632 * to provide a pleasant animation.
633 */
634 bool animateChangedItemCount(int changedItemCount) const;
635
636 /**
637 * @return True if a scrollbar for the given scroll-orientation is required
638 * when using a size of \p size for the view. Calling the method is rather
639 * expansive as a temporary relayout needs to be done.
640 */
641 bool scrollBarRequired(const QSizeF& size) const;
642
643 /**
644 * Shows a drop-indicator between items dependent on the given
645 * cursor position. The cursor position is relative the the upper left
646 * edge of the view.
647 * @return Index of the item where the dropping is done. An index of -1
648 * indicates that the item has been dropped after the last item.
649 */
650 int showDropIndicator(const QPointF& pos);
651 void hideDropIndicator();
652
653 /**
654 * Applies the height of the group header to the layouter. The height
655 * depends on the used scroll orientation.
656 */
657 void updateGroupHeaderHeight();
658
659 /**
660 * Updates the siblings-information for all visible items that are inside
661 * the range of \p firstIndex and \p lastIndex. If firstIndex or lastIndex
662 * is smaller than 0, the siblings-information for all visible items gets
663 * updated.
664 * @see KItemListWidget::setSiblingsInformation()
665 */
666 void updateSiblingsInformation(int firstIndex = -1, int lastIndex = -1);
667
668 /**
669 * Helper method for updateExpansionIndicators().
670 * @return True if the item with the index \a index has a sibling successor
671 * (= the item is not the last item of the current hierarchy).
672 */
673 bool hasSiblingSuccessor(int index) const;
674
675 /**
676 * Helper method for slotRoleEditingCanceled() and slotRoleEditingFinished().
677 * Disconnects the two Signals "roleEditingCanceled" and
678 * "roleEditingFinished"
679 */
680 void disconnectRoleEditingSignals(int index);
681
682 /**
683 * Helper function for triggerAutoScrolling().
684 * @param pos Logical position of the mouse relative to the range.
685 * @param range Range of the visible area.
686 * @param oldInc Previous increment. Is used to assure that the increment
687 * increases only gradually.
688 * @return Scroll increment that should be added to the offset().
689 * As soon as \a pos is inside the autoscroll-margin a
690 * value != 0 will be returned.
691 */
692 static int calculateAutoScrollingIncrement(int pos, int range, int oldInc);
693
694 /**
695 * Helper functions for changesItemCount().
696 * @return The number of items that fit into the available size by
697 * respecting the size of the item and the margin between the items.
698 */
699 static int itemsPerSize(qreal size, qreal itemSize, qreal itemMargin);
700
701 private:
702 bool m_enabledSelectionToggles;
703 bool m_grouped;
704 bool m_supportsItemExpanding;
705 bool m_editingRole;
706 int m_activeTransactions; // Counter for beginTransaction()/endTransaction()
707 LayoutAnimationHint m_endTransactionAnimationHint;
708
709 QSizeF m_itemSize;
710 KItemListController* m_controller;
711 KItemModelBase* m_model;
712 QList<QByteArray> m_visibleRoles;
713 mutable KItemListWidgetCreatorBase* m_widgetCreator;
714 mutable KItemListGroupHeaderCreatorBase* m_groupHeaderCreator;
715 KItemListStyleOption m_styleOption;
716
717 QHash<int, KItemListWidget*> m_visibleItems;
718 QHash<KItemListWidget*, KItemListGroupHeader*> m_visibleGroups;
719
720 struct Cell
721 {
722 Cell() : column(-1), row(-1) {}
723 Cell(int c, int r) : column(c), row(r) {}
724 int column;
725 int row;
726 };
727 QHash<int, Cell> m_visibleCells;
728
729 int m_scrollBarExtent;
730 KItemListSizeHintResolver* m_sizeHintResolver;
731 KItemListViewLayouter* m_layouter;
732 KItemListViewAnimation* m_animation;
733
734 QTimer* m_layoutTimer; // Triggers an asynchronous doLayout() call.
735 qreal m_oldScrollOffset;
736 qreal m_oldMaximumScrollOffset;
737 qreal m_oldItemOffset;
738 qreal m_oldMaximumItemOffset;
739
740 bool m_skipAutoScrollForRubberBand;
741 KItemListRubberBand* m_rubberBand;
742
743 QPointF m_mousePos;
744 int m_autoScrollIncrement;
745 QTimer* m_autoScrollTimer;
746
747 KItemListHeader* m_header;
748 KItemListHeaderWidget* m_headerWidget;
749
750 // When dragging items into the view where the sort-role of the model
751 // is empty, a visual indicator should be shown during dragging where
752 // the dropping will happen. This indicator is specified by an index
753 // of the item. -1 means that no indicator will be shown at all.
754 // The m_dropIndicator is set by the KItemListController
755 // by KItemListView::showDropIndicator() and KItemListView::hideDropIndicator().
756 QRectF m_dropIndicator;
757
758 friend class KItemListContainer; // Accesses scrollBarRequired()
759 friend class KItemListHeader; // Accesses m_headerWidget
760 friend class KItemListController;
761 friend class KItemListControllerTest;
762 friend class KItemListViewAccessible;
763 friend class KItemListAccessibleCell;
764 };
765
766 /**
767 * Allows to do a fast logical creation and deletion of QGraphicsWidgets
768 * by recycling existing QGraphicsWidgets instances. Is used by
769 * KItemListWidgetCreatorBase and KItemListGroupHeaderCreatorBase.
770 * @internal
771 */
772 class DOLPHIN_EXPORT KItemListCreatorBase
773 {
774 public:
775 virtual ~KItemListCreatorBase();
776
777 protected:
778 void addCreatedWidget(QGraphicsWidget* widget);
779 void pushRecycleableWidget(QGraphicsWidget* widget);
780 QGraphicsWidget* popRecycleableWidget();
781
782 private:
783 QSet<QGraphicsWidget*> m_createdWidgets;
784 QList<QGraphicsWidget*> m_recycleableWidgets;
785 };
786
787 /**
788 * @brief Base class for creating KItemListWidgets.
789 *
790 * It is recommended that applications simply use the KItemListWidgetCreator-template class.
791 * For a custom implementation the methods create(), itemSizeHint() and preferredColumnWith()
792 * must be reimplemented. The intention of the widget creator is to prevent repetitive and
793 * expensive instantiations and deletions of KItemListWidgets by recycling existing widget
794 * instances.
795 */
796 class DOLPHIN_EXPORT KItemListWidgetCreatorBase : public KItemListCreatorBase
797 {
798 public:
799 virtual ~KItemListWidgetCreatorBase();
800
801 virtual KItemListWidget* create(KItemListView* view) = 0;
802
803 virtual void recycle(KItemListWidget* widget);
804
805 virtual void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const = 0;
806
807 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
808 int index,
809 const KItemListView* view) const = 0;
810 };
811
812 /**
813 * @brief Template class for creating KItemListWidgets.
814 */
815 template <class T>
816 class KItemListWidgetCreator : public KItemListWidgetCreatorBase
817 {
818 public:
819 KItemListWidgetCreator();
820 virtual ~KItemListWidgetCreator();
821
822 virtual KItemListWidget* create(KItemListView* view);
823
824 virtual void calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const;
825
826 virtual qreal preferredRoleColumnWidth(const QByteArray& role,
827 int index,
828 const KItemListView* view) const;
829 private:
830 KItemListWidgetInformant* m_informant;
831 };
832
833 template <class T>
834 KItemListWidgetCreator<T>::KItemListWidgetCreator() :
835 m_informant(T::createInformant())
836 {
837 }
838
839 template <class T>
840 KItemListWidgetCreator<T>::~KItemListWidgetCreator()
841 {
842 delete m_informant;
843 }
844
845 template <class T>
846 KItemListWidget* KItemListWidgetCreator<T>::create(KItemListView* view)
847 {
848 KItemListWidget* widget = static_cast<KItemListWidget*>(popRecycleableWidget());
849 if (!widget) {
850 widget = new T(m_informant, view);
851 addCreatedWidget(widget);
852 }
853 return widget;
854 }
855
856 template<class T>
857 void KItemListWidgetCreator<T>::calculateItemSizeHints(QVector<qreal>& logicalHeightHints, qreal& logicalWidthHint, const KItemListView* view) const
858 {
859 return m_informant->calculateItemSizeHints(logicalHeightHints, logicalWidthHint, view);
860 }
861
862 template<class T>
863 qreal KItemListWidgetCreator<T>::preferredRoleColumnWidth(const QByteArray& role,
864 int index,
865 const KItemListView* view) const
866 {
867 return m_informant->preferredRoleColumnWidth(role, index, view);
868 }
869
870 /**
871 * @brief Base class for creating KItemListGroupHeaders.
872 *
873 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
874 * For a custom implementation the methods create() and recyle() must be reimplemented.
875 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
876 * deletions of KItemListGroupHeaders by recycling existing header instances.
877 */
878 class DOLPHIN_EXPORT KItemListGroupHeaderCreatorBase : public KItemListCreatorBase
879 {
880 public:
881 virtual ~KItemListGroupHeaderCreatorBase();
882 virtual KItemListGroupHeader* create(KItemListView* view) = 0;
883 virtual void recycle(KItemListGroupHeader* header);
884 };
885
886 template <class T>
887 class KItemListGroupHeaderCreator : public KItemListGroupHeaderCreatorBase
888 {
889 public:
890 virtual ~KItemListGroupHeaderCreator();
891 virtual KItemListGroupHeader* create(KItemListView* view);
892 };
893
894 template <class T>
895 KItemListGroupHeaderCreator<T>::~KItemListGroupHeaderCreator()
896 {
897 }
898
899 template <class T>
900 KItemListGroupHeader* KItemListGroupHeaderCreator<T>::create(KItemListView* view)
901 {
902 KItemListGroupHeader* widget = static_cast<KItemListGroupHeader*>(popRecycleableWidget());
903 if (!widget) {
904 widget = new T(view);
905 addCreatedWidget(widget);
906 }
907 return widget;
908 }
909
910 #endif