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