|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef QLISTVIEW_P_H |
|
43 #define QLISTVIEW_P_H |
|
44 |
|
45 // |
|
46 // W A R N I N G |
|
47 // ------------- |
|
48 // |
|
49 // This file is not part of the Qt API. It exists for the convenience |
|
50 // of other Qt classes. This header file may change from version to |
|
51 // version without notice, or even be removed. |
|
52 // |
|
53 // We mean it. |
|
54 // |
|
55 |
|
56 #include "private/qabstractitemview_p.h" |
|
57 #include "qrubberband.h" |
|
58 #include "qbitarray.h" |
|
59 #include "qbsptree_p.h" |
|
60 #include <limits.h> |
|
61 #include <qscrollbar.h> |
|
62 |
|
63 #ifndef QT_NO_LISTVIEW |
|
64 |
|
65 QT_BEGIN_NAMESPACE |
|
66 |
|
67 class QListViewItem |
|
68 { |
|
69 friend class QListViewPrivate; |
|
70 friend class QListModeViewBase; |
|
71 friend class QIconModeViewBase; |
|
72 public: |
|
73 inline QListViewItem() |
|
74 : x(-1), y(-1), w(0), h(0), indexHint(-1), visited(0xffff) {} |
|
75 inline QListViewItem(const QListViewItem &other) |
|
76 : x(other.x), y(other.y), w(other.w), h(other.h), |
|
77 indexHint(other.indexHint), visited(other.visited) {} |
|
78 inline QListViewItem(QRect r, int i) |
|
79 : x(r.x()), y(r.y()), w(qMin(r.width(), SHRT_MAX)), h(qMin(r.height(), SHRT_MAX)), |
|
80 indexHint(i), visited(0xffff) {} |
|
81 inline bool operator==(const QListViewItem &other) const { |
|
82 return (x == other.x && y == other.y && w == other.w && h == other.h && |
|
83 indexHint == other.indexHint); } |
|
84 inline bool operator!=(const QListViewItem &other) const |
|
85 { return !(*this == other); } |
|
86 inline bool isValid() const |
|
87 { return rect().isValid() && (indexHint > -1); } |
|
88 inline void invalidate() |
|
89 { x = -1; y = -1; w = 0; h = 0; } |
|
90 inline void resize(const QSize &size) |
|
91 { w = qMin(size.width(), SHRT_MAX); h = qMin(size.height(), SHRT_MAX); } |
|
92 inline void move(const QPoint &position) |
|
93 { x = position.x(); y = position.y(); } |
|
94 inline int width() const { return w; } |
|
95 inline int height() const { return h; } |
|
96 private: |
|
97 inline QRect rect() const |
|
98 { return QRect(x, y, w, h); } |
|
99 int x, y; |
|
100 short w, h; |
|
101 mutable int indexHint; |
|
102 uint visited; |
|
103 }; |
|
104 |
|
105 struct QListViewLayoutInfo |
|
106 { |
|
107 QRect bounds; |
|
108 QSize grid; |
|
109 int spacing; |
|
110 int first; |
|
111 int last; |
|
112 bool wrap; |
|
113 QListView::Flow flow; |
|
114 int max; |
|
115 }; |
|
116 |
|
117 class QListView; |
|
118 class QListViewPrivate; |
|
119 |
|
120 class QCommonListViewBase |
|
121 { |
|
122 public: |
|
123 inline QCommonListViewBase(QListView *q, QListViewPrivate *d) : dd(d), qq(q), batchStartRow(0), batchSavedDeltaSeg(0) {} |
|
124 virtual ~QCommonListViewBase() {} |
|
125 |
|
126 //common interface |
|
127 virtual int itemIndex(const QListViewItem &item) const = 0; |
|
128 virtual QListViewItem indexToListViewItem(const QModelIndex &index) const = 0; |
|
129 virtual bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max) = 0; |
|
130 virtual void clear() = 0; |
|
131 virtual void setRowCount(int) = 0; |
|
132 virtual QVector<QModelIndex> intersectingSet(const QRect &area) const = 0; |
|
133 |
|
134 virtual int horizontalScrollToValue(int index, QListView::ScrollHint hint, |
|
135 bool leftOf, bool rightOf, const QRect &area, const QRect &rect) const; |
|
136 virtual int verticalScrollToValue(int index, QListView::ScrollHint hint, |
|
137 bool above, bool below, const QRect &area, const QRect &rect) const; |
|
138 virtual void scrollContentsBy(int dx, int dy, bool scrollElasticBand); |
|
139 virtual QRect mapToViewport(const QRect &rect) const {return rect;} |
|
140 virtual int horizontalOffset() const; |
|
141 virtual int verticalOffset() const { return verticalScrollBar()->value(); } |
|
142 virtual void updateHorizontalScrollBar(const QSize &step); |
|
143 virtual void updateVerticalScrollBar(const QSize &step); |
|
144 virtual void dataChanged(const QModelIndex &, const QModelIndex &) { } |
|
145 virtual void appendHiddenRow(int row); |
|
146 virtual void removeHiddenRow(int row); |
|
147 virtual void setPositionForIndex(const QPoint &, const QModelIndex &) { } |
|
148 |
|
149 #ifndef QT_NO_DRAGANDDROP |
|
150 virtual void paintDragDrop(QPainter *painter) = 0; |
|
151 virtual bool filterDragMoveEvent(QDragMoveEvent *) { return false; } |
|
152 virtual bool filterDragLeaveEvent(QDragLeaveEvent *) { return false; } |
|
153 virtual bool filterDropEvent(QDropEvent *) { return false; } |
|
154 virtual bool filterStartDrag(Qt::DropActions) { return false; } |
|
155 #endif |
|
156 |
|
157 |
|
158 //other inline members |
|
159 inline int spacing() const; |
|
160 inline bool isWrapping() const; |
|
161 inline QSize gridSize() const; |
|
162 inline QListView::Flow flow() const; |
|
163 inline QListView::Movement movement() const; |
|
164 |
|
165 inline QPoint offset() const; |
|
166 inline QPoint pressedPosition() const; |
|
167 inline bool uniformItemSizes() const; |
|
168 inline int column() const; |
|
169 |
|
170 inline QScrollBar *verticalScrollBar() const; |
|
171 inline QScrollBar *horizontalScrollBar() const; |
|
172 inline QListView::ScrollMode verticalScrollMode() const; |
|
173 inline QListView::ScrollMode horizontalScrollMode() const; |
|
174 |
|
175 inline QModelIndex modelIndex(int row) const; |
|
176 inline int rowCount() const; |
|
177 |
|
178 inline QStyleOptionViewItemV4 viewOptions() const; |
|
179 inline QWidget *viewport() const; |
|
180 inline QRect clipRect() const; |
|
181 |
|
182 inline QSize cachedItemSize() const; |
|
183 inline QRect viewItemRect(const QListViewItem &item) const; |
|
184 inline QSize itemSize(const QStyleOptionViewItemV2 &opt, const QModelIndex &idx) const; |
|
185 inline QAbstractItemDelegate *delegate(const QModelIndex &idx) const; |
|
186 |
|
187 inline bool isHidden(int row) const; |
|
188 inline int hiddenCount() const; |
|
189 |
|
190 inline bool isRightToLeft() const; |
|
191 |
|
192 QListViewPrivate *dd; |
|
193 QListView *qq; |
|
194 QSize contentsSize; |
|
195 int batchStartRow; |
|
196 int batchSavedDeltaSeg; |
|
197 }; |
|
198 |
|
199 class QListModeViewBase : public QCommonListViewBase |
|
200 { |
|
201 public: |
|
202 QListModeViewBase(QListView *q, QListViewPrivate *d) : QCommonListViewBase(q, d) {} |
|
203 |
|
204 QVector<int> flowPositions; |
|
205 QVector<int> segmentPositions; |
|
206 QVector<int> segmentStartRows; |
|
207 QVector<int> segmentExtents; |
|
208 QVector<int> scrollValueMap; |
|
209 |
|
210 // used when laying out in batches |
|
211 int batchSavedPosition; |
|
212 |
|
213 //reimplementations |
|
214 int itemIndex(const QListViewItem &item) const { return item.indexHint; } |
|
215 QListViewItem indexToListViewItem(const QModelIndex &index) const; |
|
216 bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max); |
|
217 void clear(); |
|
218 void setRowCount(int rowCount) { flowPositions.resize(rowCount); } |
|
219 QVector<QModelIndex> intersectingSet(const QRect &area) const; |
|
220 |
|
221 int horizontalScrollToValue(int index, QListView::ScrollHint hint, |
|
222 bool leftOf, bool rightOf,const QRect &area, const QRect &rect) const; |
|
223 int verticalScrollToValue(int index, QListView::ScrollHint hint, |
|
224 bool above, bool below, const QRect &area, const QRect &rect) const; |
|
225 void scrollContentsBy(int dx, int dy, bool scrollElasticBand); |
|
226 QRect mapToViewport(const QRect &rect) const; |
|
227 int horizontalOffset() const; |
|
228 int verticalOffset() const; |
|
229 void updateHorizontalScrollBar(const QSize &step); |
|
230 void updateVerticalScrollBar(const QSize &step); |
|
231 |
|
232 #ifndef QT_NO_DRAGANDDROP |
|
233 void paintDragDrop(QPainter *painter); |
|
234 #endif |
|
235 |
|
236 private: |
|
237 QPoint initStaticLayout(const QListViewLayoutInfo &info); |
|
238 void doStaticLayout(const QListViewLayoutInfo &info); |
|
239 int perItemScrollToValue(int index, int value, int height, |
|
240 QAbstractItemView::ScrollHint hint, |
|
241 Qt::Orientation orientation, bool wrap, int extent) const; |
|
242 int perItemScrollingPageSteps(int length, int bounds, bool wrap) const; |
|
243 }; |
|
244 |
|
245 class QIconModeViewBase : public QCommonListViewBase |
|
246 { |
|
247 public: |
|
248 QIconModeViewBase(QListView *q, QListViewPrivate *d) : QCommonListViewBase(q, d), interSectingVector(0) {} |
|
249 |
|
250 QBspTree tree; |
|
251 QVector<QListViewItem> items; |
|
252 QBitArray moved; |
|
253 |
|
254 QVector<QModelIndex> draggedItems; // indices to the tree.itemVector |
|
255 mutable QPoint draggedItemsPos; |
|
256 |
|
257 // used when laying out in batches |
|
258 QVector<QModelIndex> *interSectingVector; //used from within intersectingSet |
|
259 |
|
260 //reimplementations |
|
261 int itemIndex(const QListViewItem &item) const; |
|
262 QListViewItem indexToListViewItem(const QModelIndex &index) const; |
|
263 bool doBatchedItemLayout(const QListViewLayoutInfo &info, int max); |
|
264 void clear(); |
|
265 void setRowCount(int rowCount); |
|
266 QVector<QModelIndex> intersectingSet(const QRect &area) const; |
|
267 |
|
268 void scrollContentsBy(int dx, int dy, bool scrollElasticBand); |
|
269 void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
|
270 void appendHiddenRow(int row); |
|
271 void removeHiddenRow(int row); |
|
272 void setPositionForIndex(const QPoint &position, const QModelIndex &index); |
|
273 |
|
274 #ifndef QT_NO_DRAGANDDROP |
|
275 void paintDragDrop(QPainter *painter); |
|
276 bool filterDragMoveEvent(QDragMoveEvent *); |
|
277 bool filterDragLeaveEvent(QDragLeaveEvent *); |
|
278 bool filterDropEvent(QDropEvent *e); |
|
279 bool filterStartDrag(Qt::DropActions); |
|
280 #endif |
|
281 |
|
282 private: |
|
283 void initBspTree(const QSize &contents); |
|
284 QPoint initDynamicLayout(const QListViewLayoutInfo &info); |
|
285 void doDynamicLayout(const QListViewLayoutInfo &info); |
|
286 static void addLeaf(QVector<int> &leaf, const QRect &area, |
|
287 uint visited, QBspTree::Data data); |
|
288 QRect itemsRect(const QVector<QModelIndex> &indexes) const; |
|
289 QRect draggedItemsRect() const; |
|
290 QPoint snapToGrid(const QPoint &pos) const; |
|
291 void updateContentsSize(); |
|
292 QPoint draggedItemsDelta() const; |
|
293 void drawItems(QPainter *painter, const QVector<QModelIndex> &indexes) const; |
|
294 void moveItem(int index, const QPoint &dest); |
|
295 |
|
296 }; |
|
297 |
|
298 class QListViewPrivate: public QAbstractItemViewPrivate |
|
299 { |
|
300 Q_DECLARE_PUBLIC(QListView) |
|
301 public: |
|
302 QListViewPrivate(); |
|
303 ~QListViewPrivate(); |
|
304 |
|
305 void clear(); |
|
306 void prepareItemsLayout(); |
|
307 |
|
308 bool doItemsLayout(int num); |
|
309 |
|
310 inline QVector<QModelIndex> intersectingSet(const QRect &area, bool doLayout = true) const { |
|
311 if (doLayout) executePostedLayout(); |
|
312 QRect a = (q_func()->isRightToLeft() ? flipX(area.normalized()) : area.normalized()); |
|
313 return commonListView->intersectingSet(a); |
|
314 } |
|
315 |
|
316 inline void resetBatchStartRow() { commonListView->batchStartRow = 0; } |
|
317 inline int batchStartRow() const { return commonListView->batchStartRow; } |
|
318 inline QSize contentsSize() const { return commonListView->contentsSize; } |
|
319 inline void setContentsSize(int w, int h) { commonListView->contentsSize = QSize(w, h); } |
|
320 |
|
321 inline int flipX(int x) const |
|
322 { return qMax(viewport->width(), contentsSize().width()) - x; } |
|
323 inline QPoint flipX(const QPoint &p) const |
|
324 { return QPoint(flipX(p.x()), p.y()); } |
|
325 inline QRect flipX(const QRect &r) const |
|
326 { return QRect(flipX(r.x()) - r.width(), r.y(), r.width(), r.height()); } |
|
327 inline QRect viewItemRect(const QListViewItem &item) const |
|
328 { if (q_func()->isRightToLeft()) return flipX(item.rect()); return item.rect(); } |
|
329 |
|
330 QListViewItem indexToListViewItem(const QModelIndex &index) const; |
|
331 inline QModelIndex listViewItemToIndex(const QListViewItem &item) const |
|
332 { return model->index(commonListView->itemIndex(item), column, root); } |
|
333 |
|
334 QRect rectForIndex(const QModelIndex &index) const |
|
335 { |
|
336 if (!isIndexValid(index) || index.parent() != root || index.column() != column || isHidden(index.row())) |
|
337 return QRect(); |
|
338 executePostedLayout(); |
|
339 return viewItemRect(indexToListViewItem(index)); |
|
340 } |
|
341 |
|
342 void viewUpdateGeometries() { q_func()->updateGeometries(); } |
|
343 |
|
344 |
|
345 QRect mapToViewport(const QRect &rect, bool extend = true) const; |
|
346 |
|
347 QModelIndex closestIndex(const QRect &target, const QVector<QModelIndex> &candidates) const; |
|
348 QSize itemSize(const QStyleOptionViewItem &option, const QModelIndex &index) const; |
|
349 |
|
350 bool selectionAllowed(const QModelIndex &index) const |
|
351 { if (viewMode == QListView::ListMode && !showElasticBand) return index.isValid(); return true; } |
|
352 |
|
353 int horizontalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const; |
|
354 int verticalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const; |
|
355 |
|
356 QItemSelection selection(const QRect &rect) const; |
|
357 void selectAll(QItemSelectionModel::SelectionFlags command); |
|
358 |
|
359 inline void setGridSize(const QSize &size) { grid = size; } |
|
360 inline QSize gridSize() const { return grid; } |
|
361 inline void setWrapping(bool b) { wrap = b; } |
|
362 inline bool isWrapping() const { return wrap; } |
|
363 inline void setSpacing(int s) { space = s; } |
|
364 inline int spacing() const { return space; } |
|
365 inline void setSelectionRectVisible(bool visible) { showElasticBand = visible; } |
|
366 inline bool isSelectionRectVisible() const { return showElasticBand; } |
|
367 |
|
368 inline QModelIndex modelIndex(int row) const { return model->index(row, column, root); } |
|
369 inline bool isHidden(int row) const { return hiddenRows.contains(model->index(row, 0, root)); } |
|
370 inline bool isHiddenOrDisabled(int row) const { return isHidden(row) || !isIndexEnabled(modelIndex(row)); } |
|
371 |
|
372 inline void removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex ¤t) const { |
|
373 QVector<QModelIndex>::iterator it = indexes->begin(); |
|
374 while (it != indexes->end()) { |
|
375 if (!isIndexEnabled(*it) || (*it) == current) |
|
376 indexes->erase(it); |
|
377 else |
|
378 ++it; |
|
379 } |
|
380 } |
|
381 |
|
382 void scrollElasticBandBy(int dx, int dy); |
|
383 |
|
384 QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const; |
|
385 |
|
386 void emitIndexesMoved(const QModelIndexList &indexes) { emit q_func()->indexesMoved(indexes); } |
|
387 |
|
388 |
|
389 QCommonListViewBase *commonListView; |
|
390 |
|
391 // ### FIXME: see if we can move the members into the dynamic/static classes |
|
392 |
|
393 bool wrap; |
|
394 int space; |
|
395 QSize grid; |
|
396 |
|
397 QListView::Flow flow; |
|
398 QListView::Movement movement; |
|
399 QListView::ResizeMode resizeMode; |
|
400 QListView::LayoutMode layoutMode; |
|
401 QListView::ViewMode viewMode; |
|
402 |
|
403 // the properties controlling the |
|
404 // icon- or list-view modes |
|
405 enum ModeProperties { |
|
406 Wrap = 1, |
|
407 Spacing = 2, |
|
408 GridSize = 4, |
|
409 Flow = 8, |
|
410 Movement = 16, |
|
411 ResizeMode = 32, |
|
412 SelectionRectVisible = 64 |
|
413 }; |
|
414 |
|
415 uint modeProperties : 8; |
|
416 |
|
417 QRect layoutBounds; |
|
418 |
|
419 // timers |
|
420 QBasicTimer batchLayoutTimer; |
|
421 |
|
422 // used for hidden items |
|
423 QVector<QPersistentModelIndex> hiddenRows; |
|
424 |
|
425 int column; |
|
426 bool uniformItemSizes; |
|
427 mutable QSize cachedItemSize; |
|
428 int batchSize; |
|
429 |
|
430 QRect elasticBand; |
|
431 bool showElasticBand; |
|
432 }; |
|
433 |
|
434 // inline implementations |
|
435 |
|
436 inline int QCommonListViewBase::spacing() const { return dd->spacing(); } |
|
437 inline bool QCommonListViewBase::isWrapping() const { return dd->isWrapping(); } |
|
438 inline QSize QCommonListViewBase::gridSize() const { return dd->gridSize(); } |
|
439 inline QListView::Flow QCommonListViewBase::flow() const { return dd->flow; } |
|
440 inline QListView::Movement QCommonListViewBase::movement() const { return dd->movement; } |
|
441 |
|
442 inline QPoint QCommonListViewBase::offset() const { return dd->offset(); } |
|
443 inline QPoint QCommonListViewBase::pressedPosition() const { return dd->pressedPosition; } |
|
444 inline bool QCommonListViewBase::uniformItemSizes() const { return dd->uniformItemSizes; } |
|
445 inline int QCommonListViewBase::column() const { return dd->column; } |
|
446 |
|
447 inline QScrollBar *QCommonListViewBase::verticalScrollBar() const { return qq->verticalScrollBar(); } |
|
448 inline QScrollBar *QCommonListViewBase::horizontalScrollBar() const { return qq->horizontalScrollBar(); } |
|
449 inline QListView::ScrollMode QCommonListViewBase::verticalScrollMode() const { return qq->verticalScrollMode(); } |
|
450 inline QListView::ScrollMode QCommonListViewBase::horizontalScrollMode() const { return qq->horizontalScrollMode(); } |
|
451 |
|
452 inline QModelIndex QCommonListViewBase::modelIndex(int row) const |
|
453 { return dd->model->index(row, dd->column, dd->root); } |
|
454 inline int QCommonListViewBase::rowCount() const { return dd->model->rowCount(dd->root); } |
|
455 |
|
456 inline QStyleOptionViewItemV4 QCommonListViewBase::viewOptions() const { return dd->viewOptionsV4(); } |
|
457 inline QWidget *QCommonListViewBase::viewport() const { return dd->viewport; } |
|
458 inline QRect QCommonListViewBase::clipRect() const { return dd->clipRect(); } |
|
459 |
|
460 inline QSize QCommonListViewBase::cachedItemSize() const { return dd->cachedItemSize; } |
|
461 inline QRect QCommonListViewBase::viewItemRect(const QListViewItem &item) const { return dd->viewItemRect(item); } |
|
462 inline QSize QCommonListViewBase::itemSize(const QStyleOptionViewItemV2 &opt, const QModelIndex &idx) const |
|
463 { return dd->itemSize(opt, idx); } |
|
464 |
|
465 inline QAbstractItemDelegate *QCommonListViewBase::delegate(const QModelIndex &idx) const |
|
466 { return dd->delegateForIndex(idx); } |
|
467 |
|
468 inline bool QCommonListViewBase::isHidden(int row) const { return dd->isHidden(row); } |
|
469 inline int QCommonListViewBase::hiddenCount() const { return dd->hiddenRows.count(); } |
|
470 |
|
471 inline bool QCommonListViewBase::isRightToLeft() const { return qq->isRightToLeft(); } |
|
472 |
|
473 QT_END_NAMESPACE |
|
474 |
|
475 #endif // QT_NO_LISTVIEW |
|
476 |
|
477 #endif // QLISTVIEW_P_H |