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 offset READ offset WRITE setOffset
)
71 KItemListView(QGraphicsWidget
* parent
= 0);
72 virtual ~KItemListView();
74 void setScrollOrientation(Qt::Orientation orientation
);
75 Qt::Orientation
scrollOrientation() const;
77 void setItemSize(const QSizeF
& size
);
78 QSizeF
itemSize() const;
80 // TODO: add note that offset is not checked against maximumOffset, only against 0.
81 void setOffset(qreal offset
);
84 qreal
maximumOffset() const;
87 * Sets the visible roles to \p roles. The integer-value defines
88 * the order of the visible role: Smaller values are ordered first.
90 void setVisibleRoles(const QHash
<QByteArray
, int>& roles
);
91 QHash
<QByteArray
, int> visibleRoles() const;
94 * If set to true an automatic scrolling is done as soon as the
95 * mouse is moved near the borders of the view. Per default
96 * the automatic scrolling is turned off.
98 void setAutoScroll(bool enabled
);
99 bool autoScroll() const;
102 * Turns on the header if \p show is true. Per default the
103 * header is not shown.
105 void setHeaderShown(bool show
);
106 bool isHeaderShown() const;
109 * @return Controller of the item-list. The controller gets
110 * initialized by KItemListController::setView() and will
111 * result in calling KItemListController::onControllerChanged().
113 KItemListController
* controller() const;
116 * @return Model of the item-list. The model gets
117 * initialized by KItemListController::setView() and will
118 * result in calling KItemListController::onModelChanged().
120 KItemModelBase
* model() const;
123 * Sets the creator that creates a widget showing the
124 * content of one model-item. Usually it is sufficient
125 * to implement a custom widget X derived from KItemListWidget and
126 * set the creator by:
128 * itemListView->setWidgetCreator(new KItemListWidgetCreator<X>());
130 * Note that the ownership of the widget creator is not transferred to
131 * the item-list view: One instance of a widget creator might get shared
132 * by several item-list view instances.
134 void setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
);
135 KItemListWidgetCreatorBase
* widgetCreator() const;
137 void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
);
138 KItemListGroupHeaderCreatorBase
* groupHeaderCreator() const;
140 void setStyleOption(const KItemListStyleOption
& option
);
141 const KItemListStyleOption
& styleOption() const;
144 virtual void setGeometry(const QRectF
& rect
);
146 int itemAt(const QPointF
& pos
) const;
147 bool isAboveSelectionToggle(int index
, const QPointF
& pos
) const;
148 bool isAboveExpansionToggle(int index
, const QPointF
& pos
) const;
150 int firstVisibleIndex() const;
151 int lastVisibleIndex() const;
153 virtual QSizeF
itemSizeHint(int index
) const;
154 virtual QHash
<QByteArray
, QSizeF
> visibleRoleSizes() const;
157 * @return The bounding rectangle of the item relative to the top/left of
158 * the currently visible area (see KItemListView::offset()).
160 QRectF
itemBoundingRect(int index
) const;
163 * @return The number of items that can be shown in parallel for one offset.
164 * Assuming the scrolldirection is vertical then a value of 4 means
165 * that 4 columns for items are available. Assuming the scrolldirection
166 * is horizontal then a value of 4 means that 4 lines for items are
169 int itemsPerOffset() const;
171 void beginTransaction();
172 void endTransaction();
173 bool isTransactionActive() const;
176 * @return Pixmap that is used for a drag operation based on the
177 * items given by \a indexes. The default implementation returns
180 virtual QPixmap
createDragPixmap(const QSet
<int>& indexes
) const;
185 virtual void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= 0);
188 void offsetChanged(qreal current
, qreal previous
);
189 void maximumOffsetChanged(qreal current
, qreal previous
);
190 void scrollTo(qreal newOffset
);
193 virtual void initializeItemListWidget(KItemListWidget
* item
);
195 virtual void onControllerChanged(KItemListController
* current
, KItemListController
* previous
);
196 virtual void onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
198 virtual void onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
);
199 virtual void onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
);
200 virtual void onOffsetChanged(qreal current
, qreal previous
);
201 virtual void onVisibleRolesChanged(const QHash
<QByteArray
, int>& current
, const QHash
<QByteArray
, int>& previous
);
202 virtual void onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
204 virtual void onTransactionBegin();
205 virtual void onTransactionEnd();
207 virtual bool event(QEvent
* event
);
208 virtual void mousePressEvent(QGraphicsSceneMouseEvent
* event
);
209 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent
* event
);
210 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent
* event
);
211 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent
* event
);
212 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent
* event
);
213 virtual void dropEvent(QGraphicsSceneDragDropEvent
* event
);
215 QList
<KItemListWidget
*> visibleItemListWidgets() const;
217 virtual void resizeEvent(QGraphicsSceneResizeEvent
* event
);
220 virtual void slotItemsInserted(const KItemRangeList
& itemRanges
);
221 virtual void slotItemsRemoved(const KItemRangeList
& itemRanges
);
222 virtual void slotItemsChanged(const KItemRangeList
& itemRanges
,
223 const QSet
<QByteArray
>& roles
);
226 void slotCurrentChanged(int current
, int previous
);
227 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
228 void slotAnimationFinished(QGraphicsWidget
* widget
,
229 KItemListViewAnimation::AnimationType type
);
230 void slotLayoutTimerFinished();
232 void slotRubberBandPosChanged();
233 void slotRubberBandActivationChanged(bool active
);
236 * Triggers the autoscrolling if autoScroll() is enabled by checking the
237 * current mouse position. If the mouse position is within the autoscroll
238 * margins a timer will be started that periodically triggers the autoscrolling.
240 void triggerAutoScrolling();
243 enum LayoutAnimationHint
255 void setController(KItemListController
* controller
);
256 void setModel(KItemModelBase
* model
);
258 KItemListRubberBand
* rubberBand() const;
261 void doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
);
262 void doGroupHeadersLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
);
263 void emitOffsetChanges();
265 KItemListWidget
* createWidget(int index
);
266 void recycleWidget(KItemListWidget
* widget
);
267 void setWidgetIndex(KItemListWidget
* widget
, int index
);
270 * Helper method for setGeometry() and setItemSize(): Calling both methods might result
271 * in a changed number of visible items. To assure that currently invisible items can
272 * get animated from the old position to the new position prepareLayoutForIncreasedItemCount()
273 * takes care to create all item widgets that are visible with the old or the new size.
274 * @param size Size of the layouter or the item dependent on \p sizeType.
275 * @param sizeType LayouterSize: KItemListLayouter::setSize() is used.
276 * ItemSize: KItemListLayouter::setItemSize() is used.
278 void prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
);
281 * Helper method for prepareLayoutForIncreasedItemCount().
283 void setLayouterSize(const QSizeF
& size
, SizeType sizeType
);
286 * Marks the visible roles as dirty so that they will get updated when doing the next
287 * layout. The visible roles will only get marked as dirty if an empty item-size is
289 * @return True if the visible roles have been marked as dirty.
291 bool markVisibleRolesSizesAsDirty();
294 * Updates the m_visibleRoleSizes property and applies the dynamic
295 * size to the layouter.
297 void applyDynamicItemSize();
300 * Helper method for createWidget() and setWidgetIndex() to update the properties
301 * of the itemlist widget.
303 void updateWidgetProperties(KItemListWidget
* widget
, int index
);
306 * Updates the width of the KItemListHeader corresponding to the required width of
309 void updateHeaderWidth();
312 * Helper function for triggerAutoScrolling().
313 * @param pos Logical position of the mouse relative to the range.
314 * @param range Range of the visible area.
315 * @param oldInc Previous increment. Is used to assure that the increment
316 * increases only gradually.
317 * @return Scroll increment that should be added to the offset().
318 * As soon as \a pos is inside the autoscroll-margin a
319 * value != 0 will be returned.
321 static int calculateAutoScrollingIncrement(int pos
, int range
, int oldInc
);
325 int m_activeTransactions
; // Counter for beginTransaction()/endTransaction()
328 KItemListController
* m_controller
;
329 KItemModelBase
* m_model
;
330 QHash
<QByteArray
, int> m_visibleRoles
;
331 QHash
<QByteArray
, QSizeF
> m_visibleRolesSizes
;
332 KItemListWidgetCreatorBase
* m_widgetCreator
;
333 KItemListGroupHeaderCreatorBase
* m_groupHeaderCreator
;
334 KItemListStyleOption m_styleOption
;
336 QHash
<int, KItemListWidget
*> m_visibleItems
;
337 QHash
<KItemListWidget
*, KItemListGroupHeader
*> m_visibleGroups
;
339 int m_scrollBarExtent
;
340 KItemListSizeHintResolver
* m_sizeHintResolver
;
341 KItemListViewLayouter
* m_layouter
;
342 KItemListViewAnimation
* m_animation
;
344 QTimer
* m_layoutTimer
; // Triggers an asynchronous doLayout() call.
346 qreal m_oldMaximumOffset
;
348 bool m_skipAutoScrollForRubberBand
;
349 KItemListRubberBand
* m_rubberBand
;
352 int m_autoScrollIncrement
;
353 QTimer
* m_autoScrollTimer
;
355 KItemListHeader
* m_header
;
357 friend class KItemListController
;
361 * Allows to do a fast logical creation and deletion of QGraphicsWidgets
362 * by recycling existing QGraphicsWidgets instances. Is used by
363 * KItemListWidgetCreatorBase and KItemListGroupHeaderCreatorBase.
366 class LIBDOLPHINPRIVATE_EXPORT KItemListCreatorBase
369 virtual ~KItemListCreatorBase();
372 void addCreatedWidget(QGraphicsWidget
* widget
);
373 void pushRecycleableWidget(QGraphicsWidget
* widget
);
374 QGraphicsWidget
* popRecycleableWidget();
377 QSet
<QGraphicsWidget
*> m_createdWidgets
;
378 QList
<QGraphicsWidget
*> m_recycleableWidgets
;
382 * @brief Base class for creating KItemListWidgets.
384 * It is recommended that applications simply use the KItemListWidgetCreator-template class.
385 * For a custom implementation the methods create() and recyle() must be reimplemented.
386 * The intention of the widget creator is to prevent repetitive and expensive instantiations and
387 * deletions of KItemListWidgets by recycling existing widget instances.
389 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreatorBase
: public KItemListCreatorBase
392 virtual ~KItemListWidgetCreatorBase();
393 virtual KItemListWidget
* create(KItemListView
* view
) = 0;
394 virtual void recycle(KItemListWidget
* widget
);
398 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreator
: public KItemListWidgetCreatorBase
401 virtual ~KItemListWidgetCreator();
402 virtual KItemListWidget
* create(KItemListView
* view
);
406 KItemListWidgetCreator
<T
>::~KItemListWidgetCreator()
411 KItemListWidget
* KItemListWidgetCreator
<T
>::create(KItemListView
* view
)
413 KItemListWidget
* widget
= static_cast<KItemListWidget
*>(popRecycleableWidget());
415 widget
= new T(view
);
416 addCreatedWidget(widget
);
422 * @brief Base class for creating KItemListGroupHeaders.
424 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
425 * For a custom implementation the methods create() and recyle() must be reimplemented.
426 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
427 * deletions of KItemListGroupHeaders by recycling existing header instances.
429 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreatorBase
: public KItemListCreatorBase
432 virtual ~KItemListGroupHeaderCreatorBase();
433 virtual KItemListGroupHeader
* create(QGraphicsWidget
* parent
) = 0;
434 virtual void recycle(KItemListGroupHeader
* header
);
438 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreator
: public KItemListGroupHeaderCreatorBase
441 virtual ~KItemListGroupHeaderCreator();
442 virtual KItemListGroupHeader
* create(QGraphicsWidget
* parent
);
446 KItemListGroupHeaderCreator
<T
>::~KItemListGroupHeaderCreator()
451 KItemListGroupHeader
* KItemListGroupHeaderCreator
<T
>::create(QGraphicsWidget
* parent
)
453 KItemListGroupHeader
* widget
= static_cast<KItemListGroupHeader
*>(popRecycleableWidget());
455 widget
= new T(parent
);
456 addCreatedWidget(widget
);