Qt 4.8
qabstractitemview_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QABSTRACTITEMVIEW_P_H
43 #define QABSTRACTITEMVIEW_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 purely as an
50 // implementation detail. 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/qabstractscrollarea_p.h"
57 #include "private/qabstractitemmodel_p.h"
58 #include "QtGui/qapplication.h"
59 #include "QtGui/qevent.h"
60 #include "QtGui/qmime.h"
61 #include "QtGui/qpainter.h"
62 #include "QtCore/qpair.h"
63 #include "QtGui/qregion.h"
64 #include "QtCore/qdebug.h"
65 #include "QtGui/qpainter.h"
66 #include "QtCore/qbasictimer.h"
67 #include "QtCore/qelapsedtimer.h"
68 
69 #ifndef QT_NO_ITEMVIEWS
70 
72 
73 struct QEditorInfo {
75  QEditorInfo(): isStatic(false) {}
76 
78  bool isStatic;
79 };
80 
81 // Fast associativity between Persistent editors and indices.
84 
87 
89 {
90 public:
91  explicit QEmptyModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
92  QModelIndex index(int, int, const QModelIndex &) const { return QModelIndex(); }
93  QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
94  int rowCount(const QModelIndex &) const { return 0; }
95  int columnCount(const QModelIndex &) const { return 0; }
96  bool hasChildren(const QModelIndex &) const { return false; }
97  QVariant data(const QModelIndex &, int) const { return QVariant(); }
98 };
99 
101 {
103 
104 public:
106  virtual ~QAbstractItemViewPrivate();
107 
108  void init();
109 
110  virtual void _q_rowsRemoved(const QModelIndex &parent, int start, int end);
111  virtual void _q_rowsInserted(const QModelIndex &parent, int start, int end);
112  virtual void _q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
113  virtual void _q_columnsRemoved(const QModelIndex &parent, int start, int end);
114  virtual void _q_columnsInserted(const QModelIndex &parent, int start, int end);
115  virtual void _q_modelDestroyed();
116  virtual void _q_layoutChanged();
117  void _q_headerDataChanged() { doDelayedItemsLayout(); }
118 
119  void fetchMore();
120 
121  bool shouldEdit(QAbstractItemView::EditTrigger trigger, const QModelIndex &index) const;
122  bool shouldForwardEvent(QAbstractItemView::EditTrigger trigger, const QEvent *event) const;
123  bool shouldAutoScroll(const QPoint &pos) const;
124  void doDelayedItemsLayout(int delay = 0);
125  void interruptDelayedItemsLayout() const;
126 
128  { // ### it would be nice to make this into a style hint one day
129  int scrollInterval = (verticalScrollMode == QAbstractItemView::ScrollPerItem) ? 150 : 50;
130  autoScrollTimer.start(scrollInterval, q_func());
131  autoScrollCount = 0;
132  }
133  void stopAutoScroll() { autoScrollTimer.stop(); autoScrollCount = 0;}
134 
135 #ifndef QT_NO_DRAGANDDROP
136  virtual bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index);
137 #endif
138  bool droppingOnItself(QDropEvent *event, const QModelIndex &index);
139 
140  QWidget *editor(const QModelIndex &index, const QStyleOptionViewItem &options);
141  bool sendDelegateEvent(const QModelIndex &index, QEvent *event) const;
142  bool openEditor(const QModelIndex &index, QEvent *event);
143  void updateEditorData(const QModelIndex &topLeft, const QModelIndex &bottomRight);
144 
145  QItemSelectionModel::SelectionFlags multiSelectionCommand(const QModelIndex &index,
146  const QEvent *event) const;
147  QItemSelectionModel::SelectionFlags extendedSelectionCommand(const QModelIndex &index,
148  const QEvent *event) const;
149  QItemSelectionModel::SelectionFlags contiguousSelectionCommand(const QModelIndex &index,
150  const QEvent *event) const;
151  virtual void selectAll(QItemSelectionModel::SelectionFlags command);
152 
153  void setHoverIndex(const QPersistentModelIndex &index);
154 
155  void checkMouseMove(const QPersistentModelIndex &index);
156  inline void checkMouseMove(const QPoint &pos) { checkMouseMove(q_func()->indexAt(pos)); }
157 
158  inline QItemSelectionModel::SelectionFlags selectionBehaviorFlags() const
159  {
160  switch (selectionBehavior) {
164  }
165  }
166 
167 #ifndef QT_NO_DRAGANDDROP
168  virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const;
169 
170  inline bool canDecode(QDropEvent *e) const {
171  QStringList modelTypes = model->mimeTypes();
172  const QMimeData *mime = e->mimeData();
173  for (int i = 0; i < modelTypes.count(); ++i)
174  if (mime->hasFormat(modelTypes.at(i))
175  && (e->dropAction() & model->supportedDropActions()))
176  return true;
177  return false;
178  }
179 
180  inline void paintDropIndicator(QPainter *painter)
181  {
182  if (showDropIndicator && state == QAbstractItemView::DraggingState
183 #ifndef QT_NO_CURSOR
184  && viewport->cursor().shape() != Qt::ForbiddenCursor
185 #endif
186  ) {
187  QStyleOption opt;
188  opt.init(q_func());
189  opt.rect = dropIndicatorRect;
190  q_func()->style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemDrop, &opt, painter, q_func());
191  }
192  }
193 
194 #endif
195  virtual QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const;
196  // reimplemented in subclasses
198 
199  inline void releaseEditor(QWidget *editor) const {
200  if (editor) {
201  QObject::disconnect(editor, SIGNAL(destroyed(QObject*)),
202  q_func(), SLOT(editorDestroyed(QObject*)));
203  editor->removeEventFilter(itemDelegate);
204  editor->hide();
205  editor->deleteLater();
206  }
207  }
208 
209  inline void executePostedLayout() const {
210  if (delayedPendingLayout && state != QAbstractItemView::CollapsingState) {
211  interruptDelayedItemsLayout();
212  const_cast<QAbstractItemView*>(q_func())->doItemsLayout();
213  }
214  }
215 
216  inline void setDirtyRegion(const QRegion &visualRegion) {
217  updateRegion += visualRegion;
218  if (!updateTimer.isActive())
219  updateTimer.start(0, q_func());
220  }
221 
222  inline void scrollDirtyRegion(int dx, int dy) {
223  scrollDelayOffset = QPoint(-dx, -dy);
224  updateDirtyRegion();
225  scrollDelayOffset = QPoint(0, 0);
226  }
227 
228  inline void scrollContentsBy(int dx, int dy) {
229  scrollDirtyRegion(dx, dy);
230  viewport->scroll(dx, dy);
231  }
232 
234  updateTimer.stop();
235  viewport->update(updateRegion);
236  updateRegion = QRegion();
237  }
238 
239  void clearOrRemove();
240  void checkPersistentEditorFocus();
241 
242  QPixmap renderToPixmap(const QModelIndexList &indexes, QRect *r) const;
243 
244  inline QPoint offset() const {
245  const Q_Q(QAbstractItemView);
246  return QPoint(q->isRightToLeft() ? -q->horizontalOffset()
247  : q->horizontalOffset(), q->verticalOffset());
248  }
249 
250  const QEditorInfo &editorForIndex(const QModelIndex &index) const;
251  inline bool hasEditor(const QModelIndex &index) const {
252  return indexEditorHash.find(index) != indexEditorHash.constEnd();
253  }
254 
255  QModelIndex indexForEditor(QWidget *editor) const;
256  void addEditor(const QModelIndex &index, QWidget *editor, bool isStatic);
257  void removeEditor(QWidget *editor);
258 
259  inline bool isAnimating() const {
260  return state == QAbstractItemView::AnimatingState;
261  }
262 
263  inline QAbstractItemDelegate *delegateForIndex(const QModelIndex &index) const {
265 
266  it = rowDelegates.find(index.row());
267  if (it != rowDelegates.end())
268  return it.value();
269 
270  it = columnDelegates.find(index.column());
271  if (it != columnDelegates.end())
272  return it.value();
273 
274  return itemDelegate;
275  }
276 
277  inline bool isIndexValid(const QModelIndex &index) const {
278  return (index.row() >= 0) && (index.column() >= 0) && (index.model() == model);
279  }
280  inline bool isIndexSelectable(const QModelIndex &index) const {
281  return (model->flags(index) & Qt::ItemIsSelectable);
282  }
283  inline bool isIndexEnabled(const QModelIndex &index) const {
284  return (model->flags(index) & Qt::ItemIsEnabled);
285  }
286  inline bool isIndexDropEnabled(const QModelIndex &index) const {
287  return (model->flags(index) & Qt::ItemIsDropEnabled);
288  }
289  inline bool isIndexDragEnabled(const QModelIndex &index) const {
290  return (model->flags(index) & Qt::ItemIsDragEnabled);
291  }
292 
293  virtual bool selectionAllowed(const QModelIndex &index) const {
294  // in some views we want to go ahead with selections, even if the index is invalid
295  return isIndexValid(index) && isIndexSelectable(index);
296  }
297 
298  // reimplemented from QAbstractScrollAreaPrivate
299  virtual QPoint contentsOffset() const {
300  Q_Q(const QAbstractItemView);
301  return QPoint(q->horizontalOffset(), q->verticalOffset());
302  }
303 
308  int delegateRefCount(const QAbstractItemDelegate *delegate) const
309  {
310  int ref = 0;
311  if (itemDelegate == delegate)
312  ++ref;
313 
314  for (int maps = 0; maps < 2; ++maps) {
315  const QMap<int, QPointer<QAbstractItemDelegate> > *delegates = maps ? &columnDelegates : &rowDelegates;
316  for (QMap<int, QPointer<QAbstractItemDelegate> >::const_iterator it = delegates->begin();
317  it != delegates->end(); ++it) {
318  if (it.value() == delegate) {
319  ++ref;
320  // optimization, we are only interested in the ref count values 0, 1 or >=2
321  if (ref >= 2) {
322  return ref;
323  }
324  }
325  }
326  }
327  return ref;
328  }
329 
333  inline bool isPersistent(const QModelIndex &index) const
334  {
335  return static_cast<QAbstractItemModelPrivate *>(model->d_ptr.data())->persistent.indexes.contains(index);
336  }
337 
338  QModelIndexList selectedDraggableIndexes() const;
339 
340  QStyleOptionViewItemV4 viewOptionsV4() const;
341 
343  {
344  //we delay the reset of the timer because some views (QTableView)
345  //with headers can't handle the fact that the model has been destroyed
346  //all _q_modelDestroyed slots must have been called
347  if (!delayedReset.isActive())
348  delayedReset.start(0, q_func());
349  }
350 
358 
361 
366 
369  Qt::KeyboardModifiers pressedModifiers;
372 
373  //forces the next mouseMoveEvent to send the viewportEntered signal
374  //if the mouse is over the viewport and not over an item
376 
379  QAbstractItemView::EditTriggers editTriggers;
381 
384 
386 
387 #ifndef QT_NO_DRAGANDDROP
392  bool overwrite;
395 #endif
396 
397 #ifdef QT_SOFTKEYS_ENABLED
398  QAction *doneSoftKey;
399 #endif
400 
403 
408  bool shouldScrollToCurrentOnShow; //used to know if we should scroll to current on show event
409  bool shouldClearStatusTip; //if there is a statustip currently shown that need to be cleared when leaving.
410 
412 
415 
416  QRegion updateRegion; // used for the internal update system
418 
421  QBasicTimer delayedAutoScroll; //used when an item is clicked
423 
426 
428 
430  mutable bool delayedPendingLayout;
432 
433 private:
436 };
437 
439 #include <qvector.h>
441 
442 template <typename T>
443 inline int qBinarySearch(const QVector<T> &vec, const T &item, int start, int end)
444 {
445  int i = (start + end + 1) >> 1;
446  while (end - start > 0) {
447  if (vec.at(i) > item)
448  end = i - 1;
449  else
450  start = i;
451  i = (start + end + 1) >> 1;
452  }
453  return i;
454 }
455 
457 
458 #endif // QT_NO_ITEMVIEWS
459 
460 #endif // QABSTRACTITEMVIEW_P_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
SelectionMode
This enum indicates how the view responds to user selections:
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
QAbstractItemView::DropIndicatorPosition dropIndicatorPosition
QPersistentModelIndex hover
The QAbstractItemDelegate class is used to display and edit data items from a model.
bool isIndexDragEnabled(const QModelIndex &index) const
bool hasEditor(const QModelIndex &index) const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
QAbstractItemView::EditTrigger lastTrigger
#define it(className, varName)
int qBinarySearch(const QVector< T > &vec, const T &item, int start, int end)
QWeakPointer< QWidget > widget
QAbstractItemView::State state
void setDirtyRegion(const QRegion &visualRegion)
bool isIndexDropEnabled(const QModelIndex &index) const
#define SLOT(a)
Definition: qobjectdefs.h:226
TextElideMode
Definition: qnamespace.h:263
bool hasChildren(const QModelIndex &) const
Returns true if parent has any children; otherwise returns false.
void removeEventFilter(QObject *)
Removes an event filter object obj from this object.
Definition: qobject.cpp:2099
QHash< QPersistentModelIndex, QEditorInfo > QIndexEditorHash
SelectionFlag
This enum describes the way the selection model will be updated.
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QStyleOptionViewItemV4 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:609
Qt::DropAction dropAction() const
Returns the action to be performed on the data by the target.
Definition: qevent.h:494
QAbstractItemView::SelectionBehavior selectionBehavior
const char * mime
QItemSelectionModel::SelectionFlags selectionBehaviorFlags() const
void init(const QWidget *w)
Use initFrom(widget) instead.
QAbstractItemView::SelectionMode selectionMode
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
QPointer< QItemSelectionModel > selectionModel
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QMap< int, QPointer< QAbstractItemDelegate > > columnDelegates
iterator find(const Key &key)
Returns an iterator pointing to the item with key key in the map.
Definition: qmap.h:618
DropIndicatorPosition
This enum indicates the position of the drop indicator in relation to the index at the current mouse ...
QAbstractItemView::ScrollMode horizontalScrollMode
void scrollContentsBy(int dx, int dy)
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QPair< QRect, QModelIndex > QItemViewPaintPair
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
QEmptyModel(QObject *parent=0)
virtual bool selectionAllowed(const QModelIndex &index) const
QPersistentModelIndex pressedIndex
QVariant data(const QModelIndex &, int) const
Returns the data stored under the given role for the item referred to by the index.
void checkMouseMove(const QPoint &pos)
#define Q_Q(Class)
Definition: qglobal.h:2483
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void releaseEditor(QWidget *editor) const
QAbstractItemView::ScrollMode verticalScrollMode
bool isIndexEnabled(const QModelIndex &index) const
The QStyleOption class stores the parameters used by QStyle functions.
Definition: qstyleoption.h:67
QEditorIndexHash editorIndexHash
QAbstractItemView::State stateBeforeAnimation
static bool init
int row() const
Returns the row this model index refers to.
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void scrollDirtyRegion(int dx, int dy)
bool canDecode(QDropEvent *e) const
QAbstractItemView::DragDropMode dragDropMode
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
#define QT_NO_CURSOR
State
Describes the different states the view can be in.
DropAction
Definition: qnamespace.h:1597
DragDropMode
Describes the various drag and drop events the view can act upon.
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
bool isIndexSelectable(const QModelIndex &index) const
void hide()
Hides the widget.
Definition: qwidget.h:501
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QItemSelectionModel::SelectionFlag ctrlDragSelectionFlag
The QAbstractItemModel class provides the abstract interface for item model classes.
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
QEditorInfo(QWidget *e, bool s)
QPersistentModelIndex root
Qt::TextElideMode textElideMode
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
QHash< QWidget *, QPersistentModelIndex > QEditorIndexHash
virtual bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
Definition: qmimedata.cpp:563
The QAbstractItemView class provides the basic functionality for item view classes.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QDropEvent class provides an event which is sent when a drag and drop action is completed...
Definition: qevent.h:476
The QPersistentModelIndex class is used to locate data in a data model.
QPointer< QAbstractItemDelegate > itemDelegate
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
virtual QPoint contentsOffset() const
QPersistentModelIndex enteredIndex
QAbstractItemView::EditTriggers editTriggers
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QModelIndex class is used to locate data in a data model.
QAbstractItemDelegate * delegateForIndex(const QModelIndex &index) const
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
int rowCount(const QModelIndex &) const
Returns the number of rows under the given parent.
bool isPersistent(const QModelIndex &index) const
QList< QItemViewPaintPair > QItemViewPaintPairs
quint16 index
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
EditTrigger
This enum describes actions which will initiate item editing.
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
The QBasicTimer class provides timer events for objects.
Definition: qbasictimer.h:55
const QMimeData * mimeData() const
Returns the data that was dropped on the widget and its associated MIME type information.
Definition: qevent.h:498
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
Qt::KeyboardModifiers pressedModifiers
virtual void adjustViewOptionsForIndex(QStyleOptionViewItemV4 *, const QModelIndex &) const
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
QModelIndex index(int, int, const QModelIndex &) const
Returns the index of the item in the model specified by the given row, column and parent index...
static const KeyPair *const end
QMap< int, QPointer< QAbstractItemDelegate > > rowDelegates
bool isIndexValid(const QModelIndex &index) const
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
QAbstractItemModel * model
int columnCount(const QModelIndex &) const
Returns the number of columns for the children of the given parent.
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
int delegateRefCount(const QAbstractItemDelegate *delegate) const
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
QIndexEditorHash indexEditorHash
The QWeakPointer class holds a weak reference to a shared pointer.
int column() const
Returns the column this model index refers to.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QModelIndex parent(const QModelIndex &) const
Returns the parent of the model item with the given index.
void paintDropIndicator(QPainter *painter)