Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qabstractitemview_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QABSTRACTITEMVIEW_P_H
5#define QABSTRACTITEMVIEW_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include "qabstractitemview.h"
20#include "private/qabstractscrollarea_p.h"
21#include "private/qabstractitemmodel_p.h"
22#include "QtWidgets/qapplication.h"
23#include "QtGui/qevent.h"
24#include "QtCore/qmimedata.h"
25#include "QtGui/qpainter.h"
26#include "QtGui/qregion.h"
27
28#include "QtCore/qdebug.h"
29#include "QtCore/qbasictimer.h"
30#include "QtCore/qelapsedtimer.h"
31#include <QtCore/qpointer.h>
32
33
34#include <array>
35
37
39
43
44 QPointer<QWidget> widget;
46};
47
48// Fast associativity between Persistent editors and indices.
49typedef QHash<QWidget *, QPersistentModelIndex> QEditorIndexHash;
50typedef QHash<QPersistentModelIndex, QEditorInfo> QIndexEditorHash;
51
56template <>
57class QTypeInfo<QItemViewPaintPair> : public QTypeInfoMerger<QItemViewPaintPair, QRect, QModelIndex> {};
58
59typedef QList<QItemViewPaintPair> QItemViewPaintPairs;
60
61class Q_AUTOTEST_EXPORT QAbstractItemViewPrivate : public QAbstractScrollAreaPrivate
62{
63 Q_DECLARE_PUBLIC(QAbstractItemView)
64
65public:
68
69 void init();
70
71 virtual void rowsRemoved(const QModelIndex &parent, int start, int end);
72 virtual void rowsInserted(const QModelIndex &parent, int start, int end);
73 virtual void columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
74 virtual void columnsRemoved(const QModelIndex &parent, int start, int end);
75 virtual void columnsInserted(const QModelIndex &parent, int start, int end);
76 virtual void modelDestroyed();
77 virtual void layoutChanged();
78 virtual void rowsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
79 virtual void columnsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
80 virtual QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const;
81
82 void headerDataChanged() { doDelayedItemsLayout(); }
84 void delegateSizeHintChanged(const QModelIndex &index);
85
86 void fetchMore();
87
88 bool shouldEdit(QAbstractItemView::EditTrigger trigger, const QModelIndex &index) const;
89 bool shouldForwardEvent(QAbstractItemView::EditTrigger trigger, const QEvent *event) const;
90 bool shouldAutoScroll(const QPoint &pos) const;
91 void doDelayedItemsLayout(int delay = 0);
92 void interruptDelayedItemsLayout() const;
93
94 void updateGeometry();
95
97 { // ### it would be nice to make this into a style hint one day
98 int scrollInterval = (verticalScrollMode == QAbstractItemView::ScrollPerItem) ? 150 : 50;
99 autoScrollTimer.start(scrollInterval, q_func());
100 autoScrollCount = 0;
101 }
102 void stopAutoScroll() { autoScrollTimer.stop(); autoScrollCount = 0;}
103
104#if QT_CONFIG(draganddrop)
105 virtual bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index);
106#endif
107 bool droppingOnItself(QDropEvent *event, const QModelIndex &index);
108
109 QWidget *editor(const QModelIndex &index, const QStyleOptionViewItem &options);
110 bool sendDelegateEvent(const QModelIndex &index, QEvent *event) const;
111 bool openEditor(const QModelIndex &index, QEvent *event);
112 void updateEditorData(const QModelIndex &topLeft, const QModelIndex &bottomRight);
113 void selectAllInEditor(QWidget *w);
114
115 QItemSelectionModel::SelectionFlags multiSelectionCommand(const QModelIndex &index,
116 const QEvent *event) const;
117 QItemSelectionModel::SelectionFlags extendedSelectionCommand(const QModelIndex &index,
118 const QEvent *event) const;
119 QItemSelectionModel::SelectionFlags contiguousSelectionCommand(const QModelIndex &index,
120 const QEvent *event) const;
121 virtual void selectAll(QItemSelectionModel::SelectionFlags command);
122
123 void setHoverIndex(const QPersistentModelIndex &index);
124
125 void checkMouseMove(const QPersistentModelIndex &index);
126 inline void checkMouseMove(const QPoint &pos) { checkMouseMove(q_func()->indexAt(pos)); }
127
128 inline QItemSelectionModel::SelectionFlags selectionBehaviorFlags() const
129 {
130 switch (selectionBehavior) {
134 }
135 }
136
137#if QT_CONFIG(draganddrop)
138 virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const;
139
140 inline bool canDrop(QDropEvent *event) {
141 const QMimeData *mime = event->mimeData();
142
143 // Drag enter event shall always be accepted, if mime type and action match.
144 // Whether the data can actually be dropped will be checked in drag move.
145 if (event->type() == QEvent::DragEnter && (event->dropAction() & model->supportedDropActions())) {
146 const QStringList modelTypes = model->mimeTypes();
147 for (const auto &modelType : modelTypes) {
148 if (mime->hasFormat(modelType))
149 return true;
150 }
151 }
152
154 int col = -1;
155 int row = -1;
156 if (dropOn(event, &row, &col, &index)) {
157 return model->canDropMimeData(mime,
158 dragDropMode == QAbstractItemView::InternalMove ? Qt::MoveAction : event->dropAction(),
159 row, col, index);
160 }
161 return false;
162 }
163
164 inline void paintDropIndicator(QPainter *painter)
165 {
166 if (showDropIndicator && state == QAbstractItemView::DraggingState
167#ifndef QT_NO_CURSOR
168 && viewport->cursor().shape() != Qt::ForbiddenCursor
169#endif
170 ) {
172 opt.initFrom(q_func());
173 opt.rect = dropIndicatorRect;
174 q_func()->style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemDrop, &opt, painter, q_func());
175 }
176 }
177
178#endif
179 virtual QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const;
180 // reimplemented in subclasses
181 virtual void adjustViewOptionsForIndex(QStyleOptionViewItem*, const QModelIndex&) const {}
182
183 inline void releaseEditor(QWidget *editor, const QModelIndex &index = QModelIndex()) const {
184 if (editor) {
185 Q_Q(const QAbstractItemView);
188 editor->removeEventFilter(itemDelegate);
189 editor->hide();
190 QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
191
192 if (delegate)
193 delegate->destroyEditor(editor, index);
194 else
195 editor->deleteLater();
196 }
197 }
198
199 inline void executePostedLayout() const {
200 if (delayedPendingLayout && state != QAbstractItemView::CollapsingState) {
201 interruptDelayedItemsLayout();
202 const_cast<QAbstractItemView*>(q_func())->doItemsLayout();
203 }
204 }
205
206 inline void setDirtyRegion(const QRegion &visualRegion) {
207 updateRegion += visualRegion;
208 if (!updateTimer.isActive())
209 updateTimer.start(0, q_func());
210 }
211
212 inline void scrollDirtyRegion(int dx, int dy) {
213 scrollDelayOffset = QPoint(-dx, -dy);
214 updateDirtyRegion();
215 scrollDelayOffset = QPoint(0, 0);
216 }
217
218 inline void scrollContentsBy(int dx, int dy) {
219 scrollDirtyRegion(dx, dy);
220 viewport->scroll(dx, dy);
221 }
222
224 updateTimer.stop();
225 viewport->update(updateRegion);
226 updateRegion = QRegion();
227 }
228
229 void clearOrRemove();
230 void checkPersistentEditorFocus();
231
232 QPixmap renderToPixmap(const QModelIndexList &indexes, QRect *r) const;
233
234 inline QPoint offset() const {
235 const Q_Q(QAbstractItemView);
236 return QPoint(q->isRightToLeft() ? -q->horizontalOffset()
237 : q->horizontalOffset(), q->verticalOffset());
238 }
239
240 const QEditorInfo &editorForIndex(const QModelIndex &index) const;
241 bool hasEditor(const QModelIndex &index) const;
242
243 QModelIndex indexForEditor(QWidget *editor) const;
244 void addEditor(const QModelIndex &index, QWidget *editor, bool isStatic);
245 void removeEditor(QWidget *editor);
246
247 inline bool isAnimating() const {
249 }
250
251 inline bool isIndexValid(const QModelIndex &index) const {
252 return (index.row() >= 0) && (index.column() >= 0) && (index.model() == model);
253 }
254 inline bool isIndexSelectable(const QModelIndex &index) const {
256 }
257 inline bool isIndexEnabled(const QModelIndex &index) const {
258 return (model->flags(index) & Qt::ItemIsEnabled);
259 }
260#if QT_CONFIG(draganddrop)
261 inline bool isIndexDropEnabled(const QModelIndex &index) const {
263 }
264 inline bool isIndexDragEnabled(const QModelIndex &index) const {
266 }
267#endif
268
269 virtual bool selectionAllowed(const QModelIndex &index) const {
270 // in some views we want to go ahead with selections, even if the index is invalid
271 return isIndexValid(index) && isIndexSelectable(index);
272 }
273
274 // reimplemented from QAbstractScrollAreaPrivate
275 QPoint contentsOffset() const override {
276 Q_Q(const QAbstractItemView);
277 return QPoint(q->horizontalOffset(), q->verticalOffset());
278 }
279
284 int delegateRefCount(const QAbstractItemDelegate *delegate) const
285 {
286 int ref = 0;
287 if (itemDelegate == delegate)
288 ++ref;
289
290 for (int maps = 0; maps < 2; ++maps) {
291 const QMap<int, QPointer<QAbstractItemDelegate> > *delegates = maps ? &columnDelegates : &rowDelegates;
292 for (QMap<int, QPointer<QAbstractItemDelegate> >::const_iterator it = delegates->begin();
293 it != delegates->end(); ++it) {
294 if (it.value() == delegate) {
295 ++ref;
296 // optimization, we are only interested in the ref count values 0, 1 or >=2
297 if (ref >= 2) {
298 return ref;
299 }
300 }
301 }
302 }
303 return ref;
304 }
305
309 inline bool isPersistent(const QModelIndex &index) const
310 {
311 return static_cast<QAbstractItemModelPrivate *>(model->d_ptr.data())->persistent.indexes.contains(index);
312 }
313
314#if QT_CONFIG(draganddrop)
315 QModelIndexList selectedDraggableIndexes() const;
316 void maybeStartDrag(QPoint eventPoint);
317#endif
318
320 {
321 //we delay the reset of the timer because some views (QTableView)
322 //with headers can't handle the fact that the model has been destroyed
323 //all modelDestroyed() slots must have been called
324 if (!delayedReset.isActive())
325 delayedReset.start(0, q_func());
326 }
327
329 QPointer<QAbstractItemDelegate> itemDelegate;
330 QMap<int, QPointer<QAbstractItemDelegate> > rowDelegates;
331 QMap<int, QPointer<QAbstractItemDelegate> > columnDelegates;
332 QPointer<QItemSelectionModel> selectionModel;
335
338
341 QSet<QWidget*> persistent;
347
351 Qt::KeyboardModifiers pressedModifiers;
356
357 //forces the next mouseMoveEvent to send the viewportEntered signal
358 //if the mouse is over the viewport and not over an item
360
363 QAbstractItemView::EditTriggers editTriggers;
365
368
370
371#if QT_CONFIG(draganddrop)
372 bool showDropIndicator;
373 QRect dropIndicatorRect;
374 bool dragEnabled;
375 QAbstractItemView::DragDropMode dragDropMode;
376 bool overwrite;
377 bool dropEventMoved;
378 QAbstractItemView::DropIndicatorPosition dropIndicatorPosition;
379 Qt::DropAction defaultDropAction;
380#endif
381
384
389 bool shouldScrollToCurrentOnShow; //used to know if we should scroll to current on show event
390 bool shouldClearStatusTip; //if there is a statustip currently shown that need to be cleared when leaving.
391
393
396
397 QRegion updateRegion; // used for the internal update system
399
402 QBasicTimer delayedAutoScroll; //used when an item is clicked
404
407
408#ifndef QT_NO_GESTURES
409 // the selection before the last mouse down. In case we have to restore it for scrolling
412#endif
413
415
419
420 // Whether scroll mode has been explicitly set or its value come from SH_ItemView_ScrollMode
423
424 virtual QRect visualRect(const QModelIndex &index) const { return q_func()->visualRect(index); }
425
426 std::array<QMetaObject::Connection, 14> modelConnections;
427 std::array<QMetaObject::Connection, 4> scrollbarConnections;
428#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
429 QMetaObject::Connection scollerConnection;
430#endif
431
432private:
433 void connectDelegate(QAbstractItemDelegate *delegate);
434 void disconnectDelegate(QAbstractItemDelegate *delegate);
435 void disconnectAll();
436 inline QAbstractItemDelegate *delegateForIndex(const QModelIndex &index) const {
437 QMap<int, QPointer<QAbstractItemDelegate> >::ConstIterator it;
438
439 it = rowDelegates.find(index.row());
440 if (it != rowDelegates.end())
441 return it.value();
442
443 it = columnDelegates.find(index.column());
444 if (it != columnDelegates.end())
445 return it.value();
446
447 return itemDelegate;
448 }
449
450 mutable QBasicTimer delayedLayout;
451 mutable QBasicTimer fetchMoreTimer;
452};
453
455#include <qlist.h>
457
458template<typename T>
459inline int qBinarySearch(const QList<T> &vec, const T &item, int start, int end)
460{
461 int i = (start + end + 1) >> 1;
462 while (end - start > 0) {
463 if (vec.at(i) > item)
464 end = i - 1;
465 else
466 start = i;
467 i = (start + end + 1) >> 1;
468 }
469 return i;
470}
471
473
474#endif // QABSTRACTITEMVIEW_P_H
The QAbstractItemDelegate class is used to display and edit data items from a model.
virtual void destroyEditor(QWidget *editor, const QModelIndex &index) const
Called when the editor is no longer needed for editing the data item with the given index and should ...
virtual Qt::DropActions supportedDropActions() const
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
Returns {true} if a model can accept a drop of the data.
virtual QStringList mimeTypes() const
Returns the list of allowed MIME types.
virtual void adjustViewOptionsForIndex(QStyleOptionViewItem *, const QModelIndex &) const
QPointer< QAbstractItemDelegate > itemDelegate
void checkMouseMove(const QPoint &pos)
QMap< int, QPointer< QAbstractItemDelegate > > columnDelegates
void scrollContentsBy(int dx, int dy)
QAbstractItemView::EditTriggers editTriggers
QAbstractItemView::EditTrigger lastTrigger
QPersistentModelIndex currentSelectionStartIndex
QAbstractItemView::ScrollMode horizontalScrollMode
QMap< int, QPointer< QAbstractItemDelegate > > rowDelegates
QAbstractItemView::State state
QPointer< QItemSelectionModel > selectionModel
bool isPersistent(const QModelIndex &index) const
QItemSelectionModel::SelectionFlag ctrlDragSelectionFlag
QItemSelectionModel::SelectionFlags selectionBehaviorFlags() const
QAbstractItemView::State stateBeforeAnimation
Qt::KeyboardModifiers pressedModifiers
QPersistentModelIndex root
bool droppingOnItself(QDropEvent *event, const QModelIndex &index)
QPoint contentsOffset() const override
std::array< QMetaObject::Connection, 4 > scrollbarConnections
virtual QRect visualRect(const QModelIndex &index) const
virtual bool selectionAllowed(const QModelIndex &index) const
QPersistentModelIndex pressedIndex
bool isIndexSelectable(const QModelIndex &index) const
QAbstractItemView::SelectionMode selectionMode
QPersistentModelIndex lastEditedIndex
QPersistentModelIndex enteredIndex
bool isIndexValid(const QModelIndex &index) const
void releaseEditor(QWidget *editor, const QModelIndex &index=QModelIndex()) const
int delegateRefCount(const QAbstractItemDelegate *delegate) const
QAbstractItemView::SelectionBehavior selectionBehavior
void setDirtyRegion(const QRegion &visualRegion)
QAbstractItemView::ScrollMode verticalScrollMode
void scrollDirtyRegion(int dx, int dy)
QPersistentModelIndex hover
bool isIndexEnabled(const QModelIndex &index) const
std::array< QMetaObject::Connection, 14 > modelConnections
The QAbstractItemView class provides the basic functionality for item view classes.
SelectionMode
This enum indicates how the view responds to user selections:
SelectionBehavior
\value SelectItems Selecting single items.
State
Describes the different states the view can be in.
EditTrigger
This enum describes actions which will initiate item editing.
virtual void editorDestroyed(QObject *editor)
This function is called when the given editor has been destroyed.
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
\inmodule QtCore
Definition qbasictimer.h:18
\inmodule QtCore
\inmodule QtCore
Definition qcoreevent.h:45
@ DragEnter
Definition qcoreevent.h:101
SelectionFlag
This enum describes the way the selection model will be updated.
\inmodule QtCore
Definition qmap.h:187
\inmodule QtCore Represents a handle to a signal-slot (or signal-functor) connection.
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
QScopedPointer< QObjectData > d_ptr
Definition qobject.h:373
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
T * data() const noexcept
Returns the value of the pointer referenced by this object.
iterator begin()
Definition qset.h:136
iterator end()
Definition qset.h:140
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QStyleOption class stores the parameters used by QStyle functions.
void initFrom(const QWidget *w)
@ PE_IndicatorItemViewItemDrop
Definition qstyle.h:151
\inmodule QtCore
Definition qtypeinfo.h:100
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QSet< QString >::iterator it
rect
[4]
QStyleOptionButton opt
else opt state
[0]
Combined button and popup list for selecting options.
@ ForbiddenCursor
DropAction
@ MoveAction
TextElideMode
Definition qnamespace.h:188
@ ItemIsDragEnabled
@ ItemIsSelectable
@ ItemIsEnabled
@ ItemIsDropEnabled
QT_BEGIN_INCLUDE_NAMESPACE QT_END_INCLUDE_NAMESPACE int qBinarySearch(const QList< T > &vec, const T &item, int start, int end)
QHash< QPersistentModelIndex, QEditorInfo > QIndexEditorHash
QHash< QWidget *, QPersistentModelIndex > QEditorIndexHash
QList< QItemViewPaintPair > QItemViewPaintPairs
static jboolean selectAll(JNIEnv *, jobject)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char * destination
GLfloat GLfloat GLfloat w
[0]
GLuint index
[2]
GLboolean r
[2]
GLuint GLuint end
GLuint start
GLint ref
GLsizei GLsizei GLchar * source
struct _cl_event * event
GLdouble s
[6]
Definition qopenglext.h:235
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define QT_BEGIN_INCLUDE_NAMESPACE
#define Q_AUTOTEST_EXPORT
#define QT_END_INCLUDE_NAMESPACE
#define QT_REQUIRE_CONFIG(feature)
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
QSqlQueryModel * model
[16]
application x qt windows mime
[2]
QGraphicsItem * item
view viewport() -> scroll(dx, dy, deviceRect)
QPainter painter(this)
[7]
QEditorInfo(QWidget *e, bool s)
QPointer< QWidget > widget