]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.h
Rough draft for getting back the header for the details-view
[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/kitemliststyleoption.h>
29 #include <kitemviews/kitemlistviewanimation_p.h>
30 #include <kitemviews/kitemlistwidget.h>
31 #include <kitemviews/kitemmodelbase.h>
32 #include <QGraphicsWidget>
33 #include <QSet>
34
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;
46 class QTimer;
47
48 /**
49 * @brief Represents the view of an item-list.
50 *
51 * The view is responsible for showing the items of the model within
52 * a GraphicsItem. Each visible item is represented by a KItemListWidget.
53 *
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().
60 *
61 * @see KItemListWidget
62 * @see KItemModelBase
63 */
64 class LIBDOLPHINPRIVATE_EXPORT KItemListView : public QGraphicsWidget
65 {
66 Q_OBJECT
67
68 Q_PROPERTY(qreal offset READ offset WRITE setOffset)
69
70 public:
71 KItemListView(QGraphicsWidget* parent = 0);
72 virtual ~KItemListView();
73
74 void setScrollOrientation(Qt::Orientation orientation);
75 Qt::Orientation scrollOrientation() const;
76
77 void setItemSize(const QSizeF& size);
78 QSizeF itemSize() const;
79
80 // TODO: add note that offset is not checked against maximumOffset, only against 0.
81 void setOffset(qreal offset);
82 qreal offset() const;
83
84 qreal maximumOffset() const;
85
86 /**
87 * Sets the visible roles to \p roles. The integer-value defines
88 * the order of the visible role: Smaller values are ordered first.
89 */
90 void setVisibleRoles(const QHash<QByteArray, int>& roles);
91 QHash<QByteArray, int> visibleRoles() const;
92
93 /**
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.
97 */
98 void setAutoScroll(bool enabled);
99 bool autoScroll() const;
100
101 /**
102 * Turns on the header if \p show is true. Per default the
103 * header is not shown.
104 */
105 void setHeaderShown(bool show);
106 bool isHeaderShown() const;
107
108 /**
109 * @return Controller of the item-list. The controller gets
110 * initialized by KItemListController::setView() and will
111 * result in calling KItemListController::onControllerChanged().
112 */
113 KItemListController* controller() const;
114
115 /**
116 * @return Model of the item-list. The model gets
117 * initialized by KItemListController::setView() and will
118 * result in calling KItemListController::onModelChanged().
119 */
120 KItemModelBase* model() const;
121
122 /**
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:
127 * <code>
128 * itemListView->setWidgetCreator(new KItemListWidgetCreator<X>());
129 * </code>
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.
133 **/
134 void setWidgetCreator(KItemListWidgetCreatorBase* widgetCreator);
135 KItemListWidgetCreatorBase* widgetCreator() const;
136
137 void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase* groupHeaderCreator);
138 KItemListGroupHeaderCreatorBase* groupHeaderCreator() const;
139
140 void setStyleOption(const KItemListStyleOption& option);
141 const KItemListStyleOption& styleOption() const;
142
143 /** @reimp */
144 virtual void setGeometry(const QRectF& rect);
145
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;
149
150 int firstVisibleIndex() const;
151 int lastVisibleIndex() const;
152
153 virtual QSizeF itemSizeHint(int index) const;
154 virtual QHash<QByteArray, QSizeF> visibleRoleSizes() const;
155
156 /**
157 * @return The bounding rectangle of the item relative to the top/left of
158 * the currently visible area (see KItemListView::offset()).
159 */
160 QRectF itemBoundingRect(int index) const;
161
162 /**
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
167 * available.
168 */
169 int itemsPerOffset() const;
170
171 void beginTransaction();
172 void endTransaction();
173 bool isTransactionActive() const;
174
175 /**
176 * @return Pixmap that is used for a drag operation based on the
177 * items given by \a indexes. The default implementation returns
178 * a null-pixmap.
179 */
180 virtual QPixmap createDragPixmap(const QSet<int>& indexes) const;
181
182 /**
183 * @reimp
184 */
185 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
186
187 signals:
188 void offsetChanged(qreal current, qreal previous);
189 void maximumOffsetChanged(qreal current, qreal previous);
190 void scrollTo(qreal newOffset);
191
192 protected:
193 virtual void initializeItemListWidget(KItemListWidget* item);
194
195 virtual void onControllerChanged(KItemListController* current, KItemListController* previous);
196 virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous);
197
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);
203
204 virtual void onTransactionBegin();
205 virtual void onTransactionEnd();
206
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);
214
215 QList<KItemListWidget*> visibleItemListWidgets() const;
216
217 virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
218
219 protected slots:
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);
224
225 private slots:
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();
231
232 void slotRubberBandPosChanged();
233 void slotRubberBandActivationChanged(bool active);
234
235 /**
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.
239 */
240 void triggerAutoScrolling();
241
242 private:
243 enum LayoutAnimationHint
244 {
245 NoAnimation,
246 Animation
247 };
248
249 enum SizeType
250 {
251 LayouterSize,
252 ItemSize
253 };
254
255 void setController(KItemListController* controller);
256 void setModel(KItemModelBase* model);
257
258 KItemListRubberBand* rubberBand() const;
259
260 void updateLayout();
261 void doLayout(LayoutAnimationHint hint, int changedIndex, int changedCount);
262 void doGroupHeadersLayout(LayoutAnimationHint hint, int changedIndex, int changedCount);
263 void emitOffsetChanges();
264
265 KItemListWidget* createWidget(int index);
266 void recycleWidget(KItemListWidget* widget);
267 void setWidgetIndex(KItemListWidget* widget, int index);
268
269 /**
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.
277 */
278 void prepareLayoutForIncreasedItemCount(const QSizeF& size, SizeType sizeType);
279
280 /**
281 * Helper method for prepareLayoutForIncreasedItemCount().
282 */
283 void setLayouterSize(const QSizeF& size, SizeType sizeType);
284
285 /**
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
288 * given.
289 * @return True if the visible roles have been marked as dirty.
290 */
291 bool markVisibleRolesSizesAsDirty();
292
293 /**
294 * Updates the m_visibleRoleSizes property and applies the dynamic
295 * size to the layouter.
296 */
297 void applyDynamicItemSize();
298
299 /**
300 * Helper method for createWidget() and setWidgetIndex() to update the properties
301 * of the itemlist widget.
302 */
303 void updateWidgetProperties(KItemListWidget* widget, int index);
304
305 /**
306 * Updates the width of the KItemListHeader corresponding to the required width of
307 * the roles.
308 */
309 void updateHeaderWidth();
310
311 /**
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.
320 */
321 static int calculateAutoScrollingIncrement(int pos, int range, int oldInc);
322
323 private:
324 bool m_grouped;
325 int m_activeTransactions; // Counter for beginTransaction()/endTransaction()
326
327 QSizeF m_itemSize;
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;
335
336 QHash<int, KItemListWidget*> m_visibleItems;
337 QHash<KItemListWidget*, KItemListGroupHeader*> m_visibleGroups;
338
339 int m_scrollBarExtent;
340 KItemListSizeHintResolver* m_sizeHintResolver;
341 KItemListViewLayouter* m_layouter;
342 KItemListViewAnimation* m_animation;
343
344 QTimer* m_layoutTimer; // Triggers an asynchronous doLayout() call.
345 qreal m_oldOffset;
346 qreal m_oldMaximumOffset;
347
348 bool m_skipAutoScrollForRubberBand;
349 KItemListRubberBand* m_rubberBand;
350
351 QPointF m_mousePos;
352 int m_autoScrollIncrement;
353 QTimer* m_autoScrollTimer;
354
355 KItemListHeader* m_header;
356
357 friend class KItemListController;
358 };
359
360 /**
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.
364 * @internal
365 */
366 class LIBDOLPHINPRIVATE_EXPORT KItemListCreatorBase
367 {
368 public:
369 virtual ~KItemListCreatorBase();
370
371 protected:
372 void addCreatedWidget(QGraphicsWidget* widget);
373 void pushRecycleableWidget(QGraphicsWidget* widget);
374 QGraphicsWidget* popRecycleableWidget();
375
376 private:
377 QSet<QGraphicsWidget*> m_createdWidgets;
378 QList<QGraphicsWidget*> m_recycleableWidgets;
379 };
380
381 /**
382 * @brief Base class for creating KItemListWidgets.
383 *
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.
388 */
389 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreatorBase : public KItemListCreatorBase
390 {
391 public:
392 virtual ~KItemListWidgetCreatorBase();
393 virtual KItemListWidget* create(KItemListView* view) = 0;
394 virtual void recycle(KItemListWidget* widget);
395 };
396
397 template <class T>
398 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreator : public KItemListWidgetCreatorBase
399 {
400 public:
401 virtual ~KItemListWidgetCreator();
402 virtual KItemListWidget* create(KItemListView* view);
403 };
404
405 template <class T>
406 KItemListWidgetCreator<T>::~KItemListWidgetCreator()
407 {
408 }
409
410 template <class T>
411 KItemListWidget* KItemListWidgetCreator<T>::create(KItemListView* view)
412 {
413 KItemListWidget* widget = static_cast<KItemListWidget*>(popRecycleableWidget());
414 if (!widget) {
415 widget = new T(view);
416 addCreatedWidget(widget);
417 }
418 return widget;
419 }
420
421 /**
422 * @brief Base class for creating KItemListGroupHeaders.
423 *
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.
428 */
429 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreatorBase : public KItemListCreatorBase
430 {
431 public:
432 virtual ~KItemListGroupHeaderCreatorBase();
433 virtual KItemListGroupHeader* create(QGraphicsWidget* parent) = 0;
434 virtual void recycle(KItemListGroupHeader* header);
435 };
436
437 template <class T>
438 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreator : public KItemListGroupHeaderCreatorBase
439 {
440 public:
441 virtual ~KItemListGroupHeaderCreator();
442 virtual KItemListGroupHeader* create(QGraphicsWidget* parent);
443 };
444
445 template <class T>
446 KItemListGroupHeaderCreator<T>::~KItemListGroupHeaderCreator()
447 {
448 }
449
450 template <class T>
451 KItemListGroupHeader* KItemListGroupHeaderCreator<T>::create(QGraphicsWidget* parent)
452 {
453 KItemListGroupHeader* widget = static_cast<KItemListGroupHeader*>(popRecycleableWidget());
454 if (!widget) {
455 widget = new T(parent);
456 addCreatedWidget(widget);
457 }
458 return widget;
459 }
460
461 #endif