Qt 6.x
The Qt SDK
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 "QtCore/qpair.h"
27#include "QtGui/qregion.h"
28#include "QtCore/qdebug.h"
29#include "QtCore/qbasictimer.h"
30#include "QtCore/qelapsedtimer.h"
31
33
35
39
42};
43
44// Fast associativity between Persistent editors and indices.
47
51};
52template <>
53class QTypeInfo<QItemViewPaintPair> : public QTypeInfoMerger<QItemViewPaintPair, QRect, QModelIndex> {};
54
56
57class Q_AUTOTEST_EXPORT QAbstractItemViewPrivate : public QAbstractScrollAreaPrivate
58{
59 Q_DECLARE_PUBLIC(QAbstractItemView)
60
61public:
64
65 void init();
66
67 virtual void _q_rowsRemoved(const QModelIndex &parent, int start, int end);
68 virtual void _q_rowsInserted(const QModelIndex &parent, int start, int end);
69 virtual void _q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
70 virtual void _q_columnsRemoved(const QModelIndex &parent, int start, int end);
71 virtual void _q_columnsInserted(const QModelIndex &parent, int start, int end);
72 virtual void _q_modelDestroyed();
73 virtual void _q_layoutChanged();
74 virtual void _q_rowsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
75 virtual void _q_columnsMoved(const QModelIndex &source, int sourceStart, int sourceEnd, const QModelIndex &destination, int destinationStart);
76 virtual QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const;
77
78 void _q_headerDataChanged() { doDelayedItemsLayout(); }
80 void _q_delegateSizeHintChanged(const QModelIndex &index);
81
82 void fetchMore();
83
84 bool shouldEdit(QAbstractItemView::EditTrigger trigger, const QModelIndex &index) const;
85 bool shouldForwardEvent(QAbstractItemView::EditTrigger trigger, const QEvent *event) const;
86 bool shouldAutoScroll(const QPoint &pos) const;
87 void doDelayedItemsLayout(int delay = 0);
88 void interruptDelayedItemsLayout() const;
89
90 void updateGeometry();
91
93 { // ### it would be nice to make this into a style hint one day
94 int scrollInterval = (verticalScrollMode == QAbstractItemView::ScrollPerItem) ? 150 : 50;
95 autoScrollTimer.start(scrollInterval, q_func());
96 autoScrollCount = 0;
97 }
98 void stopAutoScroll() { autoScrollTimer.stop(); autoScrollCount = 0;}
99
100#if QT_CONFIG(draganddrop)
101 virtual bool dropOn(QDropEvent *event, int *row, int *col, QModelIndex *index);
102#endif
103 bool droppingOnItself(QDropEvent *event, const QModelIndex &index);
104
105 QWidget *editor(const QModelIndex &index, const QStyleOptionViewItem &options);
106 bool sendDelegateEvent(const QModelIndex &index, QEvent *event) const;
107 bool openEditor(const QModelIndex &index, QEvent *event);
108 void updateEditorData(const QModelIndex &topLeft, const QModelIndex &bottomRight);
109 void selectAllInEditor(QWidget *w);
110
111 QItemSelectionModel::SelectionFlags multiSelectionCommand(const QModelIndex &index,
112 const QEvent *event) const;
113 QItemSelectionModel::SelectionFlags extendedSelectionCommand(const QModelIndex &index,
114 const QEvent *event) const;
115 QItemSelectionModel::SelectionFlags contiguousSelectionCommand(const QModelIndex &index,
116 const QEvent *event) const;
117 virtual void selectAll(QItemSelectionModel::SelectionFlags command);
118
119 void setHoverIndex(const QPersistentModelIndex &index);
120
121 void checkMouseMove(const QPersistentModelIndex &index);
122 inline void checkMouseMove(const QPoint &pos) { checkMouseMove(q_func()->indexAt(pos)); }
123
124 inline QItemSelectionModel::SelectionFlags selectionBehaviorFlags() const
125 {
126 switch (selectionBehavior) {
130 }
131 }
132
133#if QT_CONFIG(draganddrop)
134 virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const;
135
136 inline bool canDrop(QDropEvent *event) {
137 const QMimeData *mime = event->mimeData();
138
139 // Drag enter event shall always be accepted, if mime type and action match.
140 // Whether the data can actually be dropped will be checked in drag move.
141 if (event->type() == QEvent::DragEnter && (event->dropAction() & model->supportedDropActions())) {
142 const QStringList modelTypes = model->mimeTypes();
143 for (const auto &modelType : modelTypes) {
144 if (mime->hasFormat(modelType))
145 return true;
146 }
147 }
148
150 int col = -1;
151 int row = -1;
152 if (dropOn(event, &row, &col, &index)) {
153 return model->canDropMimeData(mime,
154 dragDropMode == QAbstractItemView::InternalMove ? Qt::MoveAction : event->dropAction(),
155 row, col, index);
156 }
157 return false;
158 }
159
160 inline void paintDropIndicator(QPainter *painter)
161 {
162 if (showDropIndicator && state == QAbstractItemView::DraggingState
163#ifndef QT_NO_CURSOR
164 && viewport->cursor().shape() != Qt::ForbiddenCursor
165#endif
166 ) {
168 opt.initFrom(q_func());
169 opt.rect = dropIndicatorRect;
170 q_func()->style()->drawPrimitive(QStyle::PE_IndicatorItemViewItemDrop, &opt, painter, q_func());
171 }
172 }
173
174#endif
175 virtual QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const;
176 // reimplemented in subclasses
177 virtual void adjustViewOptionsForIndex(QStyleOptionViewItem*, const QModelIndex&) const {}
178
179 inline void releaseEditor(QWidget *editor, const QModelIndex &index = QModelIndex()) const {
180 if (editor) {
181 Q_Q(const QAbstractItemView);
182 QObject::disconnect(editor, SIGNAL(destroyed(QObject*)),
183 q_func(), SLOT(editorDestroyed(QObject*)));
184 editor->removeEventFilter(itemDelegate);
185 editor->hide();
186 QAbstractItemDelegate *delegate = q->itemDelegateForIndex(index);
187
188 if (delegate)
189 delegate->destroyEditor(editor, index);
190 else
191 editor->deleteLater();
192 }
193 }
194
195 inline void executePostedLayout() const {
196 if (delayedPendingLayout && state != QAbstractItemView::CollapsingState) {
197 interruptDelayedItemsLayout();
198 const_cast<QAbstractItemView*>(q_func())->doItemsLayout();
199 }
200 }
201
202 inline void setDirtyRegion(const QRegion &visualRegion) {
203 updateRegion += visualRegion;
204 if (!updateTimer.isActive())
205 updateTimer.start(0, q_func());
206 }
207
208 inline void scrollDirtyRegion(int dx, int dy) {
209 scrollDelayOffset = QPoint(-dx, -dy);
210 updateDirtyRegion();
211 scrollDelayOffset = QPoint(0, 0);
212 }
213
214 inline void scrollContentsBy(int dx, int dy) {
215 scrollDirtyRegion(dx, dy);
216 viewport->scroll(dx, dy);
217 }
218
220 updateTimer.stop();
221 viewport->update(updateRegion);
222 updateRegion = QRegion();
223 }
224
225 void clearOrRemove();
226 void checkPersistentEditorFocus();
227
228 QPixmap renderToPixmap(const QModelIndexList &indexes, QRect *r) const;
229
230 inline QPoint offset() const {
231 const Q_Q(QAbstractItemView);
232 return QPoint(q->isRightToLeft() ? -q->horizontalOffset()
233 : q->horizontalOffset(), q->verticalOffset());
234 }
235
236 const QEditorInfo &editorForIndex(const QModelIndex &index) const;
237 bool hasEditor(const QModelIndex &index) const;
238
239 QModelIndex indexForEditor(QWidget *editor) const;
240 void addEditor(const QModelIndex &index, QWidget *editor, bool isStatic);
241 void removeEditor(QWidget *editor);
242
243 inline bool isAnimating() const {
245 }
246
247 inline bool isIndexValid(const QModelIndex &index) const {
248 return (index.row() >= 0) && (index.column() >= 0) && (index.model() == model);
249 }
250 inline bool isIndexSelectable(const QModelIndex &index) const {
252 }
253 inline bool isIndexEnabled(const QModelIndex &index) const {
254 return (model->flags(index) & Qt::ItemIsEnabled);
255 }
256#if QT_CONFIG(draganddrop)
257 inline bool isIndexDropEnabled(const QModelIndex &index) const {
259 }
260 inline bool isIndexDragEnabled(const QModelIndex &index) const {
262 }
263#endif
264
265 virtual bool selectionAllowed(const QModelIndex &index) const {
266 // in some views we want to go ahead with selections, even if the index is invalid
267 return isIndexValid(index) && isIndexSelectable(index);
268 }
269
270 // reimplemented from QAbstractScrollAreaPrivate
271 QPoint contentsOffset() const override {
272 Q_Q(const QAbstractItemView);
273 return QPoint(q->horizontalOffset(), q->verticalOffset());
274 }
275
280 int delegateRefCount(const QAbstractItemDelegate *delegate) const
281 {
282 int ref = 0;
283 if (itemDelegate == delegate)
284 ++ref;
285
286 for (int maps = 0; maps < 2; ++maps) {
287 const QMap<int, QPointer<QAbstractItemDelegate> > *delegates = maps ? &columnDelegates : &rowDelegates;
288 for (QMap<int, QPointer<QAbstractItemDelegate> >::const_iterator it = delegates->begin();
289 it != delegates->end(); ++it) {
290 if (it.value() == delegate) {
291 ++ref;
292 // optimization, we are only interested in the ref count values 0, 1 or >=2
293 if (ref >= 2) {
294 return ref;
295 }
296 }
297 }
298 }
299 return ref;
300 }
301
305 inline bool isPersistent(const QModelIndex &index) const
306 {
307 return static_cast<QAbstractItemModelPrivate *>(model->d_ptr.data())->persistent.indexes.contains(index);
308 }
309
310#if QT_CONFIG(draganddrop)
311 QModelIndexList selectedDraggableIndexes() const;
312 void maybeStartDrag(QPoint eventPoint);
313#endif
314
316 {
317 //we delay the reset of the timer because some views (QTableView)
318 //with headers can't handle the fact that the model has been destroyed
319 //all _q_modelDestroyed slots must have been called
320 if (!delayedReset.isActive())
321 delayedReset.start(0, q_func());
322 }
323
331
334
343
347 Qt::KeyboardModifiers pressedModifiers;
352
353 //forces the next mouseMoveEvent to send the viewportEntered signal
354 //if the mouse is over the viewport and not over an item
356
359 QAbstractItemView::EditTriggers editTriggers;
361
364
366
367#if QT_CONFIG(draganddrop)
368 bool showDropIndicator;
369 QRect dropIndicatorRect;
370 bool dragEnabled;
371 QAbstractItemView::DragDropMode dragDropMode;
372 bool overwrite;
373 bool dropEventMoved;
374 QAbstractItemView::DropIndicatorPosition dropIndicatorPosition;
375 Qt::DropAction defaultDropAction;
376#endif
377
380
385 bool shouldScrollToCurrentOnShow; //used to know if we should scroll to current on show event
386 bool shouldClearStatusTip; //if there is a statustip currently shown that need to be cleared when leaving.
387
389
392
393 QRegion updateRegion; // used for the internal update system
395
398 QBasicTimer delayedAutoScroll; //used when an item is clicked
400
403
404#ifndef QT_NO_GESTURES
405 // the selection before the last mouse down. In case we have to restore it for scrolling
408#endif
409
411
415
416 // Whether scroll mode has been explicitly set or its value come from SH_ItemView_ScrollMode
419
420private:
421 inline QAbstractItemDelegate *delegateForIndex(const QModelIndex &index) const {
423
424 it = rowDelegates.find(index.row());
425 if (it != rowDelegates.end())
426 return it.value();
427
428 it = columnDelegates.find(index.column());
429 if (it != columnDelegates.end())
430 return it.value();
431
432 return itemDelegate;
433 }
434
435 mutable QBasicTimer delayedLayout;
436 mutable QBasicTimer fetchMoreTimer;
437};
438
440#include <qlist.h>
442
443template<typename T>
444inline int qBinarySearch(const QList<T> &vec, const T &item, int start, int end)
445{
446 int i = (start + end + 1) >> 1;
447 while (end - start > 0) {
448 if (vec.at(i) > item)
449 end = i - 1;
450 else
451 start = i;
452 i = (start + end + 1) >> 1;
453 }
454 return i;
455}
456
458
459#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
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
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.
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
\inmodule QtCore
Definition qhash.h:818
SelectionFlag
This enum describes the way the selection model will be updated.
\inmodule QtCore
Definition qlist.h:74
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
Definition qmap.h:186
iterator find(const Key &key)
Definition qmap.h:640
iterator begin()
Definition qmap.h:597
iterator end()
Definition qmap.h:601
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:90
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3099
QScopedPointer< QObjectData > d_ptr
Definition qobject.h:338
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:23
\inmodule QtCore
Definition qpointer.h:18
\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.
Definition qset.h:18
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
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:92
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
double e
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:187
@ 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
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
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 GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
GLdouble s
[6]
Definition qopenglext.h:235
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
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent