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 KItemListWidgetCreatorBase
;
37 class KItemListGroupHeader
;
38 class KItemListGroupHeaderCreatorBase
;
39 class KItemListSizeHintResolver
;
40 class KItemListRubberBand
;
41 class KItemListViewAnimation
;
42 class KItemListViewLayouter
;
43 class KItemListWidget
;
44 class KItemListViewCreatorBase
;
48 * @brief Represents the view of an item-list.
50 * The view is responsible for showing the items of the model within
51 * a GraphicsItem. Each visible item is represented by a KItemListWidget.
53 * The created view must be applied to the KItemListController with
54 * KItemListController::setView(). For showing a custom model it is not
55 * mandatory to derive from KItemListView, all that is necessary is
56 * to set a widget-creator that is capable to create KItemListWidgets
57 * showing the model items. A widget-creator can be set with
58 * KItemListView::setWidgetCreator().
60 * @see KItemListWidget
63 class LIBDOLPHINPRIVATE_EXPORT KItemListView
: public QGraphicsWidget
67 Q_PROPERTY(qreal offset READ offset WRITE setOffset
)
70 KItemListView(QGraphicsWidget
* parent
= 0);
71 virtual ~KItemListView();
73 void setScrollOrientation(Qt::Orientation orientation
);
74 Qt::Orientation
scrollOrientation() const;
76 void setItemSize(const QSizeF
& size
);
77 QSizeF
itemSize() const;
79 // TODO: add note that offset is not checked against maximumOffset, only against 0.
80 void setOffset(qreal offset
);
83 qreal
maximumOffset() const;
86 * Sets the visible roles to \p roles. The integer-value defines
87 * the order of the visible role: Smaller values are ordered first.
89 void setVisibleRoles(const QHash
<QByteArray
, int>& roles
);
90 QHash
<QByteArray
, int> visibleRoles() const;
93 * @return Controller of the item-list. The controller gets
94 * initialized by KItemListController::setView() and will
95 * result in calling KItemListController::onControllerChanged().
97 KItemListController
* controller() const;
100 * @return Model of the item-list. The model gets
101 * initialized by KItemListController::setView() and will
102 * result in calling KItemListController::onModelChanged().
104 KItemModelBase
* model() const;
107 * Sets the creator that creates a widget showing the
108 * content of one model-item. Usually it is sufficient
109 * to implement a custom widget X derived from KItemListWidget and
110 * set the creator by:
112 * itemListView->setWidgetCreator(new KItemListWidgetCreator<X>());
114 * Note that the ownership of the widget creator is not transferred to
115 * the item-list view: One instance of a widget creator might get shared
116 * by several item-list view instances.
118 void setWidgetCreator(KItemListWidgetCreatorBase
* widgetCreator
);
119 KItemListWidgetCreatorBase
* widgetCreator() const;
121 void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase
* groupHeaderCreator
);
122 KItemListGroupHeaderCreatorBase
* groupHeaderCreator() const;
124 void setStyleOption(const KItemListStyleOption
& option
);
125 const KItemListStyleOption
& styleOption() const;
127 virtual void setGeometry(const QRectF
& rect
);
129 int itemAt(const QPointF
& pos
) const;
130 bool isAboveSelectionToggle(int index
, const QPointF
& pos
) const;
131 bool isAboveExpansionToggle(int index
, const QPointF
& pos
) const;
133 int firstVisibleIndex() const;
134 int lastVisibleIndex() const;
136 virtual QSizeF
itemSizeHint(int index
) const;
137 virtual QHash
<QByteArray
, QSizeF
> visibleRoleSizes() const;
140 * @return The bounding rectangle of the item relative to the top/left of
141 * the currently visible area (see KItemListView::offset()).
143 QRectF
itemBoundingRect(int index
) const;
146 * @return The number of items that can be shown in parallel for one offset.
147 * Assuming the scrolldirection is vertical then a value of 4 means
148 * that 4 columns for items are available. Assuming the scrolldirection
149 * is horizontal then a value of 4 means that 4 lines for items are
152 int itemsPerOffset() const;
154 void beginTransaction();
155 void endTransaction();
156 bool isTransactionActive() const;
161 virtual void paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
= 0);
164 void offsetChanged(qreal current
, qreal previous
);
165 void maximumOffsetChanged(qreal current
, qreal previous
);
166 void scrollTo(qreal newOffset
);
169 virtual void initializeItemListWidget(KItemListWidget
* item
);
171 virtual void onControllerChanged(KItemListController
* current
, KItemListController
* previous
);
172 virtual void onModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
174 virtual void onScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
);
175 virtual void onItemSizeChanged(const QSizeF
& current
, const QSizeF
& previous
);
176 virtual void onOffsetChanged(qreal current
, qreal previous
);
177 virtual void onVisibleRolesChanged(const QHash
<QByteArray
, int>& current
, const QHash
<QByteArray
, int>& previous
);
178 virtual void onStyleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
);
180 virtual void onTransactionBegin();
181 virtual void onTransactionEnd();
183 virtual bool event(QEvent
* event
);
184 virtual void mousePressEvent(QGraphicsSceneMouseEvent
* event
);
185 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent
* event
);
186 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent
* event
);
188 QList
<KItemListWidget
*> visibleItemListWidgets() const;
191 virtual void slotItemsInserted(const KItemRangeList
& itemRanges
);
192 virtual void slotItemsRemoved(const KItemRangeList
& itemRanges
);
193 virtual void slotItemsChanged(const KItemRangeList
& itemRanges
,
194 const QSet
<QByteArray
>& roles
);
197 void slotCurrentChanged(int current
, int previous
);
198 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
199 void slotAnimationFinished(QGraphicsWidget
* widget
,
200 KItemListViewAnimation::AnimationType type
);
201 void slotLayoutTimerFinished();
203 void slotRubberBandStartPosChanged();
204 void slotRubberBandEndPosChanged();
205 void slotRubberBandActivationChanged(bool active
);
208 * Emits the signal scrollTo() with the corresponding target offset if the current
209 * mouse position is above the autoscroll-margin.
211 void triggerAutoScrolling();
214 enum LayoutAnimationHint
226 void setController(KItemListController
* controller
);
227 void setModel(KItemModelBase
* model
);
229 KItemListRubberBand
* rubberBand() const;
232 void doLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
);
233 void doGroupHeadersLayout(LayoutAnimationHint hint
, int changedIndex
, int changedCount
);
234 void emitOffsetChanges();
236 KItemListWidget
* createWidget(int index
);
237 void recycleWidget(KItemListWidget
* widget
);
238 void setWidgetIndex(KItemListWidget
* widget
, int index
);
241 * Helper method for setGeometry() and setItemSize(): Calling both methods might result
242 * in a changed number of visible items. To assure that currently invisible items can
243 * get animated from the old position to the new position prepareLayoutForIncreasedItemCount()
244 * takes care to create all item widgets that are visible with the old or the new size.
245 * @param size Size of the layouter or the item dependent on \p sizeType.
246 * @param sizeType LayouterSize: KItemListLayouter::setSize() is used.
247 * ItemSize: KItemListLayouter::setItemSize() is used.
249 void prepareLayoutForIncreasedItemCount(const QSizeF
& size
, SizeType sizeType
);
252 * Helper method for prepareLayoutForIncreasedItemCount().
254 void setLayouterSize(const QSizeF
& size
, SizeType sizeType
);
257 * Marks the visible roles as dirty so that they will get updated when doing the next
258 * layout. The visible roles will only get marked as dirty if an empty item-size is
260 * @return True if the visible roles have been marked as dirty.
262 bool markVisibleRolesSizesAsDirty();
265 * Updates the m_visibleRoleSizes property and applies the dynamic
266 * size to the layouter.
268 void applyDynamicItemSize();
271 * Helper method for createWidget() and setWidgetIndex() to update the properties
272 * of the itemlist widget.
274 void updateWidgetProperties(KItemListWidget
* widget
, int index
);
277 * Helper function for triggerAutoScrolling(). Returns the scroll increment
278 * that should be added to the offset() based on the available size \a size
279 * and the current mouse position \a pos. As soon as \a pos is inside
280 * the autoscroll-margin a value != 0 will be returned.
282 static int calculateAutoScrollingIncrement(int pos
, int size
);
285 bool m_autoScrollMarginEnabled
;
287 int m_activeTransactions
; // Counter for beginTransaction()/endTransaction()
290 KItemListController
* m_controller
;
291 KItemModelBase
* m_model
;
292 QHash
<QByteArray
, int> m_visibleRoles
;
293 QHash
<QByteArray
, QSizeF
> m_visibleRolesSizes
;
294 KItemListWidgetCreatorBase
* m_widgetCreator
;
295 KItemListGroupHeaderCreatorBase
* m_groupHeaderCreator
;
296 KItemListStyleOption m_styleOption
;
298 QHash
<int, KItemListWidget
*> m_visibleItems
;
299 QHash
<KItemListWidget
*, KItemListGroupHeader
*> m_visibleGroups
;
301 int m_scrollBarExtent
;
302 KItemListSizeHintResolver
* m_sizeHintResolver
;
303 KItemListViewLayouter
* m_layouter
;
304 KItemListViewAnimation
* m_animation
;
306 QTimer
* m_layoutTimer
; // Triggers an asynchronous doLayout() call.
308 qreal m_oldMaximumOffset
;
310 KItemListRubberBand
* m_rubberBand
;
314 friend class KItemListController
;
318 * Allows to do a fast logical creation and deletion of QGraphicsWidgets
319 * by recycling existing QGraphicsWidgets instances. Is used by
320 * KItemListWidgetCreatorBase and KItemListGroupHeaderCreatorBase.
323 class LIBDOLPHINPRIVATE_EXPORT KItemListCreatorBase
326 virtual ~KItemListCreatorBase();
329 void addCreatedWidget(QGraphicsWidget
* widget
);
330 void pushRecycleableWidget(QGraphicsWidget
* widget
);
331 QGraphicsWidget
* popRecycleableWidget();
334 QSet
<QGraphicsWidget
*> m_createdWidgets
;
335 QList
<QGraphicsWidget
*> m_recycleableWidgets
;
339 * @brief Base class for creating KItemListWidgets.
341 * It is recommended that applications simply use the KItemListWidgetCreator-template class.
342 * For a custom implementation the methods create() and recyle() must be reimplemented.
343 * The intention of the widget creator is to prevent repetitive and expensive instantiations and
344 * deletions of KItemListWidgets by recycling existing widget instances.
346 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreatorBase
: public KItemListCreatorBase
349 virtual ~KItemListWidgetCreatorBase();
350 virtual KItemListWidget
* create(KItemListView
* view
) = 0;
351 virtual void recycle(KItemListWidget
* widget
);
355 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreator
: public KItemListWidgetCreatorBase
358 virtual ~KItemListWidgetCreator();
359 virtual KItemListWidget
* create(KItemListView
* view
);
363 KItemListWidgetCreator
<T
>::~KItemListWidgetCreator()
368 KItemListWidget
* KItemListWidgetCreator
<T
>::create(KItemListView
* view
)
370 KItemListWidget
* widget
= static_cast<KItemListWidget
*>(popRecycleableWidget());
372 widget
= new T(view
);
373 addCreatedWidget(widget
);
379 * @brief Base class for creating KItemListGroupHeaders.
381 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
382 * For a custom implementation the methods create() and recyle() must be reimplemented.
383 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
384 * deletions of KItemListGroupHeaders by recycling existing header instances.
386 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreatorBase
: public KItemListCreatorBase
389 virtual ~KItemListGroupHeaderCreatorBase();
390 virtual KItemListGroupHeader
* create(QGraphicsWidget
* parent
) = 0;
391 virtual void recycle(KItemListGroupHeader
* header
);
395 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreator
: public KItemListGroupHeaderCreatorBase
398 virtual ~KItemListGroupHeaderCreator();
399 virtual KItemListGroupHeader
* create(QGraphicsWidget
* parent
);
403 KItemListGroupHeaderCreator
<T
>::~KItemListGroupHeaderCreator()
408 KItemListGroupHeader
* KItemListGroupHeaderCreator
<T
>::create(QGraphicsWidget
* parent
)
410 KItemListGroupHeader
* widget
= static_cast<KItemListGroupHeader
*>(popRecycleableWidget());
412 widget
= new T(parent
);
413 addCreatedWidget(widget
);