1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #ifndef KITEMLISTVIEW_H
24 #define KITEMLISTVIEW_H
26 #include <libdolphin_export.h>
28 #include <kitemviews/kitemliststyleoption.h>
29 #include <kitemviews/kitemlistviewanimation_p.h>
30 #include <kitemviews/kitemlistwidget.h>
31 #include <kitemviews/kitemmodelbase.h>
32 #include <QGraphicsWidget>
35 class KItemListController
;
36 class KItemListGroupHeader
;
37 class KItemListGroupHeaderCreatorBase
;
38 class KItemListHeader
;
39 class KItemListSizeHintResolver
;
40 class KItemListRubberBand
;
41 class KItemListViewAnimation
;
42 class KItemListViewLayouter
;
43 class KItemListWidget
;
44 class KItemListWidgetCreatorBase
;
45 class KItemListViewCreatorBase
;
49 * @brief Represents the view of an item-list.
51 * The view is responsible for showing the items of the model within
52 * a GraphicsItem. Each visible item is represented by a KItemListWidget.
54 * The created view must be applied to the KItemListController with
55 * KItemListController::setView(). For showing a custom model it is not
56 * mandatory to derive from KItemListView, all that is necessary is
57 * to set a widget-creator that is capable to create KItemListWidgets
58 * showing the model items. A widget-creator can be set with
59 * KItemListView::setWidgetCreator().
61 * @see KItemListWidget
64 class LIBDOLPHINPRIVATE_EXPORT KItemListView
: public QGraphicsWidget
68 Q_PROPERTY(qreal scrollOffset READ scrollOffset WRITE setScrollOffset
)
69 Q_PROPERTY(qreal itemOffset READ itemOffset WRITE setItemOffset
)
72 KItemListView(QGraphicsWidget
* parent
= 0);
73 virtual ~KItemListView();
75 void setScrollOrientation(Qt::Orientation orientation
);
76 Qt::Orientation
scrollOrientation() const;
78 void setItemSize(const QSizeF
& size
);
79 QSizeF
itemSize() const;
81 // TODO: add note that offset is not checked against maximumOffset, only against 0.
82 void setScrollOffset(qreal offset
);
83 qreal
scrollOffset() const;
85 qreal
maximumScrollOffset() const;
87 void setItemOffset(qreal scrollOffset
);
88 qreal
itemOffset() const;
90 qreal
maximumItemOffset() const;
92 void setVisibleRoles(const QList
<QByteArray
>& roles
);
93 QList
<QByteArray
> visibleRoles() const;
96 * If set to true an automatic scrolling is done as soon as the
97 * mouse is moved near the borders of the view. Per default
98 * the automatic scrolling is turned off.
100 void setAutoScroll(bool enabled
);
101 bool autoScroll() const;
104 * Turns on the header if \p show is true. Per default the
105 * header is not shown.
107 void setHeaderShown(bool show
);
108 bool isHeaderShown() const;
111 * @return Controller of the item-list. The controller gets
112 * initialized by KItemListController::setView() and will
113 * result in calling KItemListController::onControllerChanged().
115 KItemListController
* controller() const;
118 * @return Model of the item-list. The model gets
119 * initialized by KItemListController::setView() and will
120 * result in calling KItemListController::onModelChanged().
122 KItemModelBase
* model() const;
125 * Sets the creator that creates a widget showing the
126 * content of one model-item. Usually it is sufficient
127 * to implement a custom widget X derived from KItemListWidget and
128 * set the creator by:
130 * itemListView->setWidgetCreator(new KItemListWidgetCreator<X>());
132 * Note that the ownership of the widget creator is not transferred to
133 * the item-list view: One instance of a widget creator might get shared
134 * by several item-list view instances.
136 void setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
);
137 KItemListWidgetCreatorBase
* widgetCreator() const;
139 void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
);
140 KItemListGroupHeaderCreatorBase
* groupHeaderCreator() const;
142 void setStyleOption(const KItemListStyleOption
& option
);
143 const KItemListStyleOption
& styleOption() const;
146 virtual void setGeometry(const QRectF
& rect
);
148 int itemAt(const QPointF
& pos
) const;
149 bool isAboveSelectionToggle(int index
, const QPointF
& pos
) const;
150 bool isAboveExpansionToggle(int index
, const QPointF
& pos
) const;
152 int firstVisibleIndex() const;
153 int lastVisibleIndex() const;
156 * @return Required size for the item with the index \p index.
157 * Per default KItemListView::itemSize() is returned.
158 * When reimplementing this method it is recommended to
159 * also reimplement KItemListView::itemSizeHintUpdateRequired().
161 virtual QSizeF
itemSizeHint(int index
) const;
164 * @param itemRanges Items that must be checked for getting the visible roles sizes.
165 * @return The size of each visible role in case if KItemListView::itemSize()
166 * is empty. This allows to have dynamic but equal role sizes between
167 * all items. Per default an empty hash is returned.
169 virtual QHash
<QByteArray
, QSizeF
> visibleRolesSizes(const KItemRangeList
& itemRanges
) const;
172 * @return The bounding rectangle of the item relative to the top/left of
173 * the currently visible area (see KItemListView::offset()).
175 QRectF
itemBoundingRect(int index
) const;
178 * @return The number of items that can be shown in parallel for one offset.
179 * Assuming the scrolldirection is vertical then a value of 4 means
180 * that 4 columns for items are available. Assuming the scrolldirection
181 * is horizontal then a value of 4 means that 4 lines for items are
184 int itemsPerOffset() const;
186 void beginTransaction();
187 void endTransaction();
188 bool isTransactionActive() const;
191 * @return Pixmap that is used for a drag operation based on the
192 * items given by \a indexes. The default implementation returns
195 virtual QPixmap
createDragPixmap(const QSet
<int>& indexes
) const;
200 virtual void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= 0);
203 void scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
);
204 void scrollOffsetChanged(qreal current
, qreal previous
);
205 void maximumScrollOffsetChanged(qreal current
, qreal previous
);
206 void itemOffsetChanged(qreal current
, qreal previous
);
207 void maximumItemOffsetChanged(qreal current
, qreal previous
);
208 void scrollTo(qreal newOffset
);
211 virtual void initializeItemListWidget(KItemListWidget
* item
);
214 * @return True if at least one of the changed roles \p changedRoles might result
215 * in the need to update the item-size hint (see KItemListView::itemSizeHint()).
216 * Per default true is returned which means on each role-change of existing items
217 * the item-size hints are recalculated. For performance reasons it is recommended
218 * to return false in case if a role-change will not result in a changed
221 virtual bool itemSizeHintUpdateRequired(const QSet
<QByteArray
>& changedRoles
) const;
223 virtual void onControllerChanged(KItemListController
* current
, KItemListController
* previous
);
224 virtual void onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
226 virtual void onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
);
227 virtual void onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
);
228 virtual void onScrollOffsetChanged(qreal current
, qreal previous
);
229 virtual void onVisibleRolesChanged(const QList
<QByteArray
>& current
, const QList
<QByteArray
>& previous
);
230 virtual void onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
232 virtual void onTransactionBegin();
233 virtual void onTransactionEnd();
235 virtual bool event(QEvent
* event
);
236 virtual void mousePressEvent(QGraphicsSceneMouseEvent
* event
);
237 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent
* event
);
238 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent
* event
);
239 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent
* event
);
240 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
);
241 virtual void dropEvent(QGraphicsSceneDragDropEvent
* event
);
243 QList
<KItemListWidget
*> visibleItemListWidgets() const;
246 virtual void resizeEvent(QGraphicsSceneResizeEvent
* event
);
249 virtual void slotItemsInserted(const KItemRangeList
& itemRanges
);
250 virtual void slotItemsRemoved(const KItemRangeList
& itemRanges
);
251 virtual void slotItemsMoved(const KItemRange
& itemRange
, const QList
<int>& movedToIndexes
);
252 virtual void slotItemsChanged(const KItemRangeList
& itemRanges
,
253 const QSet
<QByteArray
>& roles
);
256 void slotCurrentChanged(int current
, int previous
);
257 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
258 void slotAnimationFinished(QGraphicsWidget
* widget
,
259 KItemListViewAnimation::AnimationType type
);
260 void slotLayoutTimerFinished();
262 void slotRubberBandPosChanged();
263 void slotRubberBandActivationChanged(bool active
);
266 * Is invoked if the visible role-width of one role in the header has
267 * been changed by the user. It is remembered that the user has modified
268 * the role-width, so that it won't be changed anymore automatically to
269 * calculate an optimized width.
271 void slotVisibleRoleWidthChanged(const QByteArray
& role
,
273 qreal previousWidth
);
276 * Triggers the autoscrolling if autoScroll() is enabled by checking the
277 * current mouse position. If the mouse position is within the autoscroll
278 * margins a timer will be started that periodically triggers the autoscrolling.
280 void triggerAutoScrolling();
283 enum LayoutAnimationHint
295 void setController(KItemListController
* controller
);
296 void setModel(KItemModelBase
* model
);
298 KItemListRubberBand
* rubberBand() const;
301 void doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
);
302 void doGroupHeadersLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
);
303 void emitOffsetChanges();
305 KItemListWidget
* createWidget(int index
);
306 void recycleWidget(KItemListWidget
* widget
);
307 void setWidgetIndex(KItemListWidget
* widget
, int index
);
310 * Helper method for setGeometry() and setItemSize(): Calling both methods might result
311 * in a changed number of visible items. To assure that currently invisible items can
312 * get animated from the old position to the new position prepareLayoutForIncreasedItemCount()
313 * takes care to create all item widgets that are visible with the old or the new size.
314 * @param size Size of the layouter or the item dependent on \p sizeType.
315 * @param sizeType LayouterSize: KItemListLayouter::setSize() is used.
316 * ItemSize: KItemListLayouter::setItemSize() is used.
318 void prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
);
321 * Helper method for prepareLayoutForIncreasedItemCount().
323 void setLayouterSize(const QSizeF
& size
, SizeType sizeType
);
326 * Helper method for createWidget() and setWidgetIndex() to update the properties
327 * of the itemlist widget.
329 void updateWidgetProperties(KItemListWidget
* widget
, int index
);
332 * Updates the width of the KItemListHeader corresponding to the required width of
335 void updateHeaderWidth();
338 * @return The widths of each visible role that is shown in the KItemListHeader.
340 QHash
<QByteArray
, qreal
> headerRolesWidths() const;
343 * Updates m_visibleRolesSizes by calling KItemListView::visibleRolesSizes().
344 * Nothing will be done if m_itemRect is not empty or custom header-widths
345 * are used (see m_useHeaderWidths). Also m_strechedVisibleRolesSizes will be adjusted
346 * to respect the available view-size.
348 void updateVisibleRolesSizes(const KItemRangeList
& itemRanges
);
351 * Convenience method for updateVisibleRoleSizes(KItemRangeList() << KItemRange(0, m_model->count()).
353 void updateVisibleRolesSizes();
356 * Updates m_stretchedVisibleRolesSizes based on m_visibleRolesSizes and the available
357 * view-size. Nothing will be done if m_itemRect is not empty or custom header-widths
358 * are used (see m_useHeaderWidths).
360 void updateStretchedVisibleRolesSizes();
363 * Helper function for triggerAutoScrolling().
364 * @param pos Logical position of the mouse relative to the range.
365 * @param range Range of the visible area.
366 * @param oldInc Previous increment. Is used to assure that the increment
367 * increases only gradually.
368 * @return Scroll increment that should be added to the offset().
369 * As soon as \a pos is inside the autoscroll-margin a
370 * value != 0 will be returned.
372 static int calculateAutoScrollingIncrement(int pos
, int range
, int oldInc
);
376 int m_activeTransactions
; // Counter for beginTransaction()/endTransaction()
379 KItemListController
* m_controller
;
380 KItemModelBase
* m_model
;
381 QList
<QByteArray
> m_visibleRoles
;
382 QHash
<QByteArray
, QSizeF
> m_visibleRolesSizes
;
383 QHash
<QByteArray
, QSizeF
> m_stretchedVisibleRolesSizes
;
384 KItemListWidgetCreatorBase
* m_widgetCreator
;
385 KItemListGroupHeaderCreatorBase
* m_groupHeaderCreator
;
386 KItemListStyleOption m_styleOption
;
388 QHash
<int, KItemListWidget
*> m_visibleItems
;
389 QHash
<KItemListWidget
*, KItemListGroupHeader
*> m_visibleGroups
;
391 int m_scrollBarExtent
;
392 KItemListSizeHintResolver
* m_sizeHintResolver
;
393 KItemListViewLayouter
* m_layouter
;
394 KItemListViewAnimation
* m_animation
;
396 QTimer
* m_layoutTimer
; // Triggers an asynchronous doLayout() call.
397 qreal m_oldScrollOffset
;
398 qreal m_oldMaximumScrollOffset
;
399 qreal m_oldItemOffset
;
400 qreal m_oldMaximumItemOffset
;
402 bool m_skipAutoScrollForRubberBand
;
403 KItemListRubberBand
* m_rubberBand
;
406 int m_autoScrollIncrement
;
407 QTimer
* m_autoScrollTimer
;
409 KItemListHeader
* m_header
;
410 bool m_useHeaderWidths
;
412 friend class KItemListController
;
416 * Allows to do a fast logical creation and deletion of QGraphicsWidgets
417 * by recycling existing QGraphicsWidgets instances. Is used by
418 * KItemListWidgetCreatorBase and KItemListGroupHeaderCreatorBase.
421 class LIBDOLPHINPRIVATE_EXPORT KItemListCreatorBase
424 virtual ~KItemListCreatorBase();
427 void addCreatedWidget(QGraphicsWidget
* widget
);
428 void pushRecycleableWidget(QGraphicsWidget
* widget
);
429 QGraphicsWidget
* popRecycleableWidget();
432 QSet
<QGraphicsWidget
*> m_createdWidgets
;
433 QList
<QGraphicsWidget
*> m_recycleableWidgets
;
437 * @brief Base class for creating KItemListWidgets.
439 * It is recommended that applications simply use the KItemListWidgetCreator-template class.
440 * For a custom implementation the methods create() and recyle() must be reimplemented.
441 * The intention of the widget creator is to prevent repetitive and expensive instantiations and
442 * deletions of KItemListWidgets by recycling existing widget instances.
444 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreatorBase
: public KItemListCreatorBase
447 virtual ~KItemListWidgetCreatorBase();
448 virtual KItemListWidget
* create(KItemListView
* view
) = 0;
449 virtual void recycle(KItemListWidget
* widget
);
453 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreator
: public KItemListWidgetCreatorBase
456 virtual ~KItemListWidgetCreator();
457 virtual KItemListWidget
* create(KItemListView
* view
);
461 KItemListWidgetCreator
<T
>::~KItemListWidgetCreator()
466 KItemListWidget
* KItemListWidgetCreator
<T
>::create(KItemListView
* view
)
468 KItemListWidget
* widget
= static_cast<KItemListWidget
*>(popRecycleableWidget());
470 widget
= new T(view
);
471 addCreatedWidget(widget
);
477 * @brief Base class for creating KItemListGroupHeaders.
479 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
480 * For a custom implementation the methods create() and recyle() must be reimplemented.
481 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
482 * deletions of KItemListGroupHeaders by recycling existing header instances.
484 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreatorBase
: public KItemListCreatorBase
487 virtual ~KItemListGroupHeaderCreatorBase();
488 virtual KItemListGroupHeader
* create(QGraphicsWidget
* parent
) = 0;
489 virtual void recycle(KItemListGroupHeader
* header
);
493 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreator
: public KItemListGroupHeaderCreatorBase
496 virtual ~KItemListGroupHeaderCreator();
497 virtual KItemListGroupHeader
* create(QGraphicsWidget
* parent
);
501 KItemListGroupHeaderCreator
<T
>::~KItemListGroupHeaderCreator()
506 KItemListGroupHeader
* KItemListGroupHeaderCreator
<T
>::create(QGraphicsWidget
* parent
)
508 KItemListGroupHeader
* widget
= static_cast<KItemListGroupHeader
*>(popRecycleableWidget());
510 widget
= new T(parent
);
511 addCreatedWidget(widget
);