]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.h
Implement grouping for names
[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/kitemlistgroupheader.h>
29 #include <kitemviews/kitemliststyleoption.h>
30 #include <kitemviews/kitemlistviewanimation_p.h>
31 #include <kitemviews/kitemlistwidget.h>
32 #include <kitemviews/kitemmodelbase.h>
33 #include <QGraphicsWidget>
34 #include <QSet>
35
36 class KItemListController;
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 scrollOffset READ scrollOffset WRITE setScrollOffset)
69 Q_PROPERTY(qreal itemOffset READ itemOffset WRITE setItemOffset)
70
71 public:
72 KItemListView(QGraphicsWidget* parent = 0);
73 virtual ~KItemListView();
74
75 void setScrollOrientation(Qt::Orientation orientation);
76 Qt::Orientation scrollOrientation() const;
77
78 void setItemSize(const QSizeF& size);
79 QSizeF itemSize() const;
80
81 // TODO: add note that offset is not checked against maximumOffset, only against 0.
82 void setScrollOffset(qreal offset);
83 qreal scrollOffset() const;
84
85 qreal maximumScrollOffset() const;
86
87 void setItemOffset(qreal scrollOffset);
88 qreal itemOffset() const;
89
90 qreal maximumItemOffset() const;
91
92 void setVisibleRoles(const QList<QByteArray>& roles);
93 QList<QByteArray> visibleRoles() const;
94
95 /**
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.
99 */
100 void setAutoScroll(bool enabled);
101 bool autoScroll() const;
102
103 /**
104 * @return Controller of the item-list. The controller gets
105 * initialized by KItemListController::setView() and will
106 * result in calling KItemListController::onControllerChanged().
107 */
108 KItemListController* controller() const;
109
110 /**
111 * @return Model of the item-list. The model gets
112 * initialized by KItemListController::setView() and will
113 * result in calling KItemListController::onModelChanged().
114 */
115 KItemModelBase* model() const;
116
117 /**
118 * Sets the creator that creates a widget showing the
119 * content of one model-item. Usually it is sufficient
120 * to implement a custom widget X derived from KItemListWidget and
121 * set the creator by:
122 * <code>
123 * itemListView->setWidgetCreator(new KItemListWidgetCreator<X>());
124 * </code>
125 * Note that the ownership of the widget creator is not transferred to
126 * the item-list view: One instance of a widget creator might get shared
127 * by several item-list view instances.
128 **/
129 void setWidgetCreator(KItemListWidgetCreatorBase* widgetCreator);
130 KItemListWidgetCreatorBase* widgetCreator() const;
131
132 void setGroupHeaderCreator(KItemListGroupHeaderCreatorBase* groupHeaderCreator);
133 KItemListGroupHeaderCreatorBase* groupHeaderCreator() const;
134
135 void setStyleOption(const KItemListStyleOption& option);
136 const KItemListStyleOption& styleOption() const;
137
138 /** @reimp */
139 virtual void setGeometry(const QRectF& rect);
140
141 int itemAt(const QPointF& pos) const;
142 bool isAboveSelectionToggle(int index, const QPointF& pos) const;
143 bool isAboveExpansionToggle(int index, const QPointF& pos) const;
144
145 int firstVisibleIndex() const;
146 int lastVisibleIndex() const;
147
148 /**
149 * @return Required size for the item with the index \p index.
150 * Per default KItemListView::itemSize() is returned.
151 * When reimplementing this method it is recommended to
152 * also reimplement KItemListView::itemSizeHintUpdateRequired().
153 */
154 virtual QSizeF itemSizeHint(int index) const;
155
156 /**
157 * @param itemRanges Items that must be checked for getting the visible roles sizes.
158 * @return The size of each visible role in case if KItemListView::itemSize()
159 * is empty. This allows to have dynamic but equal role sizes between
160 * all items. Per default an empty hash is returned.
161 */
162 virtual QHash<QByteArray, QSizeF> visibleRolesSizes(const KItemRangeList& itemRanges) const;
163
164 /**
165 * @return The bounding rectangle of the item relative to the top/left of
166 * the currently visible area (see KItemListView::offset()).
167 */
168 QRectF itemBoundingRect(int index) const;
169
170 /**
171 * @return The number of items that can be shown in parallel for one offset.
172 * Assuming the scrolldirection is vertical then a value of 4 means
173 * that 4 columns for items are available. Assuming the scrolldirection
174 * is horizontal then a value of 4 means that 4 lines for items are
175 * available.
176 */
177 int itemsPerOffset() const;
178
179 void beginTransaction();
180 void endTransaction();
181 bool isTransactionActive() const;
182
183 /**
184 * Turns on the header if \p show is true. Per default the
185 * header is not shown.
186 */
187 void setHeaderShown(bool show);
188 bool isHeaderShown() const;
189
190 /**
191 * @return Pixmap that is used for a drag operation based on the
192 * items given by \a indexes. The default implementation returns
193 * a null-pixmap.
194 */
195 virtual QPixmap createDragPixmap(const QSet<int>& indexes) const;
196
197 /**
198 * @reimp
199 */
200 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
201
202 signals:
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);
209
210 protected:
211 virtual void initializeItemListWidget(KItemListWidget* item);
212
213 /**
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
219 * item-size hint.
220 */
221 virtual bool itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const;
222
223 virtual void onControllerChanged(KItemListController* current, KItemListController* previous);
224 virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous);
225
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);
231
232 virtual void onTransactionBegin();
233 virtual void onTransactionEnd();
234
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);
242
243 QList<KItemListWidget*> visibleItemListWidgets() const;
244
245 /** @reimp */
246 virtual void resizeEvent(QGraphicsSceneResizeEvent* event);
247
248 protected slots:
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);
254
255 private slots:
256 void slotGroupedSortingChanged(bool current);
257 void slotCurrentChanged(int current, int previous);
258 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
259 void slotAnimationFinished(QGraphicsWidget* widget,
260 KItemListViewAnimation::AnimationType type);
261 void slotLayoutTimerFinished();
262
263 void slotRubberBandPosChanged();
264 void slotRubberBandActivationChanged(bool active);
265
266 /**
267 * Is invoked if the visible role-width of one role in the header has
268 * been changed by the user. It is remembered that the user has modified
269 * the role-width, so that it won't be changed anymore automatically to
270 * calculate an optimized width.
271 */
272 void slotVisibleRoleWidthChanged(const QByteArray& role,
273 qreal currentWidth,
274 qreal previousWidth);
275
276 /**
277 * Triggers the autoscrolling if autoScroll() is enabled by checking the
278 * current mouse position. If the mouse position is within the autoscroll
279 * margins a timer will be started that periodically triggers the autoscrolling.
280 */
281 void triggerAutoScrolling();
282
283 private:
284 enum LayoutAnimationHint
285 {
286 NoAnimation,
287 Animation
288 };
289
290 enum SizeType
291 {
292 LayouterSize,
293 ItemSize
294 };
295
296 void setController(KItemListController* controller);
297 void setModel(KItemModelBase* model);
298
299 KItemListRubberBand* rubberBand() const;
300
301 void updateLayout();
302 void doLayout(LayoutAnimationHint hint, int changedIndex, int changedCount);
303 void doGroupHeadersLayout(LayoutAnimationHint hint, int changedIndex, int changedCount);
304 void emitOffsetChanges();
305
306 KItemListWidget* createWidget(int index);
307 void recycleWidget(KItemListWidget* widget);
308 void setWidgetIndex(KItemListWidget* widget, int index);
309
310 /**
311 * Helper method for setGeometry() and setItemSize(): Calling both methods might result
312 * in a changed number of visible items. To assure that currently invisible items can
313 * get animated from the old position to the new position prepareLayoutForIncreasedItemCount()
314 * takes care to create all item widgets that are visible with the old or the new size.
315 * @param size Size of the layouter or the item dependent on \p sizeType.
316 * @param sizeType LayouterSize: KItemListLayouter::setSize() is used.
317 * ItemSize: KItemListLayouter::setItemSize() is used.
318 */
319 void prepareLayoutForIncreasedItemCount(const QSizeF& size, SizeType sizeType);
320
321 /**
322 * Helper method for prepareLayoutForIncreasedItemCount().
323 */
324 void setLayouterSize(const QSizeF& size, SizeType sizeType);
325
326 /**
327 * Helper method for createWidget() and setWidgetIndex() to update the properties
328 * of the itemlist widget.
329 */
330 void updateWidgetProperties(KItemListWidget* widget, int index);
331
332 /**
333 * Helper method for createWidget() and setWidgetIndex() to create or update
334 * the itemlist groupheader.
335 */
336 void updateGroupHeaderForWidget(KItemListWidget* widget);
337
338 /**
339 * @return The widths of each visible role that is shown in the KItemListHeader.
340 */
341 QHash<QByteArray, qreal> headerRolesWidths() const;
342
343 /**
344 * Updates m_visibleRolesSizes by calling KItemListView::visibleRolesSizes().
345 * Nothing will be done if m_itemRect is not empty or custom header-widths
346 * are used (see m_useHeaderWidths). Also m_strechedVisibleRolesSizes will be adjusted
347 * to respect the available view-size.
348 */
349 void updateVisibleRolesSizes(const KItemRangeList& itemRanges);
350
351 /**
352 * Convenience method for updateVisibleRoleSizes(KItemRangeList() << KItemRange(0, m_model->count()).
353 */
354 void updateVisibleRolesSizes();
355
356 /**
357 * Updates m_stretchedVisibleRolesSizes based on m_visibleRolesSizes and the available
358 * view-size. Nothing will be done if m_itemRect is not empty or custom header-widths
359 * are used (see m_useHeaderWidths).
360 */
361 void updateStretchedVisibleRolesSizes();
362
363 /**
364 * @return Sum of the widths of all visible roles.
365 */
366 qreal visibleRolesSizesWidthSum() const;
367
368 /**
369 * @return Sum of the heights of all visible roles.
370 */
371 qreal visibleRolesSizesHeightSum() const;
372
373 /**
374 * @return Boundaries of the header. An empty rectangle is returned
375 * if no header is shown.
376 */
377 QRectF headerBoundaries() const;
378
379 /**
380 * Helper function for triggerAutoScrolling().
381 * @param pos Logical position of the mouse relative to the range.
382 * @param range Range of the visible area.
383 * @param oldInc Previous increment. Is used to assure that the increment
384 * increases only gradually.
385 * @return Scroll increment that should be added to the offset().
386 * As soon as \a pos is inside the autoscroll-margin a
387 * value != 0 will be returned.
388 */
389 static int calculateAutoScrollingIncrement(int pos, int range, int oldInc);
390
391 private:
392 bool m_grouped;
393 int m_activeTransactions; // Counter for beginTransaction()/endTransaction()
394
395 QSizeF m_itemSize;
396 KItemListController* m_controller;
397 KItemModelBase* m_model;
398 QList<QByteArray> m_visibleRoles;
399 QHash<QByteArray, QSizeF> m_visibleRolesSizes;
400 QHash<QByteArray, QSizeF> m_stretchedVisibleRolesSizes;
401 KItemListWidgetCreatorBase* m_widgetCreator;
402 KItemListGroupHeaderCreatorBase* m_groupHeaderCreator;
403 KItemListStyleOption m_styleOption;
404
405 QHash<int, KItemListWidget*> m_visibleItems;
406 QHash<KItemListWidget*, KItemListGroupHeader*> m_visibleGroups;
407
408 int m_scrollBarExtent;
409 KItemListSizeHintResolver* m_sizeHintResolver;
410 KItemListViewLayouter* m_layouter;
411 KItemListViewAnimation* m_animation;
412
413 QTimer* m_layoutTimer; // Triggers an asynchronous doLayout() call.
414 qreal m_oldScrollOffset;
415 qreal m_oldMaximumScrollOffset;
416 qreal m_oldItemOffset;
417 qreal m_oldMaximumItemOffset;
418
419 bool m_skipAutoScrollForRubberBand;
420 KItemListRubberBand* m_rubberBand;
421
422 QPointF m_mousePos;
423 int m_autoScrollIncrement;
424 QTimer* m_autoScrollTimer;
425
426 KItemListHeader* m_header;
427 bool m_useHeaderWidths;
428
429 friend class KItemListController;
430 };
431
432 /**
433 * Allows to do a fast logical creation and deletion of QGraphicsWidgets
434 * by recycling existing QGraphicsWidgets instances. Is used by
435 * KItemListWidgetCreatorBase and KItemListGroupHeaderCreatorBase.
436 * @internal
437 */
438 class LIBDOLPHINPRIVATE_EXPORT KItemListCreatorBase
439 {
440 public:
441 virtual ~KItemListCreatorBase();
442
443 protected:
444 void addCreatedWidget(QGraphicsWidget* widget);
445 void pushRecycleableWidget(QGraphicsWidget* widget);
446 QGraphicsWidget* popRecycleableWidget();
447
448 private:
449 QSet<QGraphicsWidget*> m_createdWidgets;
450 QList<QGraphicsWidget*> m_recycleableWidgets;
451 };
452
453 /**
454 * @brief Base class for creating KItemListWidgets.
455 *
456 * It is recommended that applications simply use the KItemListWidgetCreator-template class.
457 * For a custom implementation the methods create() and recyle() must be reimplemented.
458 * The intention of the widget creator is to prevent repetitive and expensive instantiations and
459 * deletions of KItemListWidgets by recycling existing widget instances.
460 */
461 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreatorBase : public KItemListCreatorBase
462 {
463 public:
464 virtual ~KItemListWidgetCreatorBase();
465 virtual KItemListWidget* create(KItemListView* view) = 0;
466 virtual void recycle(KItemListWidget* widget);
467 };
468
469 template <class T>
470 class LIBDOLPHINPRIVATE_EXPORT KItemListWidgetCreator : public KItemListWidgetCreatorBase
471 {
472 public:
473 virtual ~KItemListWidgetCreator();
474 virtual KItemListWidget* create(KItemListView* view);
475 };
476
477 template <class T>
478 KItemListWidgetCreator<T>::~KItemListWidgetCreator()
479 {
480 }
481
482 template <class T>
483 KItemListWidget* KItemListWidgetCreator<T>::create(KItemListView* view)
484 {
485 KItemListWidget* widget = static_cast<KItemListWidget*>(popRecycleableWidget());
486 if (widget) {
487 widget->setParentItem(view);
488 } else {
489 widget = new T(view);
490 addCreatedWidget(widget);
491 }
492 return widget;
493 }
494
495 /**
496 * @brief Base class for creating KItemListGroupHeaders.
497 *
498 * It is recommended that applications simply use the KItemListGroupHeaderCreator-template class.
499 * For a custom implementation the methods create() and recyle() must be reimplemented.
500 * The intention of the group-header creator is to prevent repetitive and expensive instantiations and
501 * deletions of KItemListGroupHeaders by recycling existing header instances.
502 */
503 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreatorBase : public KItemListCreatorBase
504 {
505 public:
506 virtual ~KItemListGroupHeaderCreatorBase();
507 virtual KItemListGroupHeader* create(QGraphicsWidget* parent) = 0;
508 virtual void recycle(KItemListGroupHeader* header);
509 };
510
511 template <class T>
512 class LIBDOLPHINPRIVATE_EXPORT KItemListGroupHeaderCreator : public KItemListGroupHeaderCreatorBase
513 {
514 public:
515 virtual ~KItemListGroupHeaderCreator();
516 virtual KItemListGroupHeader* create(QGraphicsWidget* parent);
517 };
518
519 template <class T>
520 KItemListGroupHeaderCreator<T>::~KItemListGroupHeaderCreator()
521 {
522 }
523
524 template <class T>
525 KItemListGroupHeader* KItemListGroupHeaderCreator<T>::create(QGraphicsWidget* parent)
526 {
527 KItemListGroupHeader* widget = static_cast<KItemListGroupHeader*>(popRecycleableWidget());
528 if (widget) {
529 widget->setParentItem(parent);
530 } else {
531 widget = new T(parent);
532 addCreatedWidget(widget);
533 }
534 return widget;
535 }
536
537 #endif