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_H
5#define QABSTRACTITEMVIEW_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtWidgets/qabstractscrollarea.h>
9#include <QtCore/qabstractitemmodel.h>
10#include <QtCore/qitemselectionmodel.h>
11#include <QtWidgets/qabstractitemdelegate.h>
12
13class tst_QAbstractItemView;
14class tst_QTreeView;
15
16QT_REQUIRE_CONFIG(itemviews);
17
18QT_BEGIN_NAMESPACE
19
20class QMenu;
21class QDrag;
22class QEvent;
23class QAbstractItemViewPrivate;
24
25class Q_WIDGETS_EXPORT QAbstractItemView : public QAbstractScrollArea
26{
27 Q_OBJECT
28 Q_PROPERTY(bool autoScroll READ hasAutoScroll WRITE setAutoScroll)
29 Q_PROPERTY(int autoScrollMargin READ autoScrollMargin WRITE setAutoScrollMargin)
30 Q_PROPERTY(EditTriggers editTriggers READ editTriggers WRITE setEditTriggers)
31 Q_PROPERTY(bool tabKeyNavigation READ tabKeyNavigation WRITE setTabKeyNavigation)
32#if QT_CONFIG(draganddrop)
33 Q_PROPERTY(bool showDropIndicator READ showDropIndicator WRITE setDropIndicatorShown)
34 Q_PROPERTY(bool dragEnabled READ dragEnabled WRITE setDragEnabled)
35 Q_PROPERTY(bool dragDropOverwriteMode READ dragDropOverwriteMode WRITE setDragDropOverwriteMode)
36 Q_PROPERTY(DragDropMode dragDropMode READ dragDropMode WRITE setDragDropMode)
37 Q_PROPERTY(Qt::DropAction defaultDropAction READ defaultDropAction WRITE setDefaultDropAction)
38#endif
39 Q_PROPERTY(bool alternatingRowColors READ alternatingRowColors WRITE setAlternatingRowColors)
40 Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
41 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior
42 WRITE setSelectionBehavior)
43 Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
44 Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode)
45 Q_PROPERTY(ScrollMode verticalScrollMode READ verticalScrollMode WRITE setVerticalScrollMode
46 RESET resetVerticalScrollMode)
47 Q_PROPERTY(ScrollMode horizontalScrollMode READ horizontalScrollMode
48 WRITE setHorizontalScrollMode RESET resetHorizontalScrollMode)
49
50public:
51 enum SelectionMode {
52 NoSelection,
53 SingleSelection,
54 MultiSelection,
55 ExtendedSelection,
56 ContiguousSelection
57 };
58 Q_ENUM(SelectionMode)
59
60 enum SelectionBehavior {
61 SelectItems,
62 SelectRows,
63 SelectColumns
64 };
65 Q_ENUM(SelectionBehavior)
66
67 enum ScrollHint {
68 EnsureVisible,
69 PositionAtTop,
70 PositionAtBottom,
71 PositionAtCenter
72 };
73 Q_ENUM(ScrollHint)
74
75 enum EditTrigger {
76 NoEditTriggers = 0,
77 CurrentChanged = 1,
78 DoubleClicked = 2,
79 SelectedClicked = 4,
80 EditKeyPressed = 8,
81 AnyKeyPressed = 16,
82 AllEditTriggers = 31
83 };
84
85 Q_DECLARE_FLAGS(EditTriggers, EditTrigger)
86 Q_FLAG(EditTriggers)
87
88 enum ScrollMode {
89 ScrollPerItem,
90 ScrollPerPixel
91 };
92 Q_ENUM(ScrollMode)
93
94 explicit QAbstractItemView(QWidget *parent = nullptr);
95 ~QAbstractItemView();
96
97 virtual void setModel(QAbstractItemModel *model);
98 QAbstractItemModel *model() const;
99
100 virtual void setSelectionModel(QItemSelectionModel *selectionModel);
101 QItemSelectionModel *selectionModel() const;
102
103 void setItemDelegate(QAbstractItemDelegate *delegate);
104 QAbstractItemDelegate *itemDelegate() const;
105
106 void setSelectionMode(QAbstractItemView::SelectionMode mode);
107 QAbstractItemView::SelectionMode selectionMode() const;
108
109 void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior);
110 QAbstractItemView::SelectionBehavior selectionBehavior() const;
111
112 QModelIndex currentIndex() const;
113 QModelIndex rootIndex() const;
114
115 void setEditTriggers(EditTriggers triggers);
116 EditTriggers editTriggers() const;
117
118 void setVerticalScrollMode(ScrollMode mode);
119 ScrollMode verticalScrollMode() const;
120 void resetVerticalScrollMode();
121
122 void setHorizontalScrollMode(ScrollMode mode);
123 ScrollMode horizontalScrollMode() const;
124 void resetHorizontalScrollMode();
125
126 void setAutoScroll(bool enable);
127 bool hasAutoScroll() const;
128
129 void setAutoScrollMargin(int margin);
130 int autoScrollMargin() const;
131
132 void setTabKeyNavigation(bool enable);
133 bool tabKeyNavigation() const;
134
135#if QT_CONFIG(draganddrop)
136 void setDropIndicatorShown(bool enable);
137 bool showDropIndicator() const;
138
139 void setDragEnabled(bool enable);
140 bool dragEnabled() const;
141
142 void setDragDropOverwriteMode(bool overwrite);
143 bool dragDropOverwriteMode() const;
144
145 enum DragDropMode {
146 NoDragDrop,
147 DragOnly,
148 DropOnly,
149 DragDrop,
150 InternalMove
151 };
152 Q_ENUM(DragDropMode)
153
154 void setDragDropMode(DragDropMode behavior);
155 DragDropMode dragDropMode() const;
156
157 void setDefaultDropAction(Qt::DropAction dropAction);
158 Qt::DropAction defaultDropAction() const;
159#endif
160
161 void setAlternatingRowColors(bool enable);
162 bool alternatingRowColors() const;
163
164 void setIconSize(const QSize &size);
165 QSize iconSize() const;
166
167 void setTextElideMode(Qt::TextElideMode mode);
168 Qt::TextElideMode textElideMode() const;
169
170 virtual void keyboardSearch(const QString &search);
171
172 virtual QRect visualRect(const QModelIndex &index) const = 0;
173 virtual void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) = 0;
174 virtual QModelIndex indexAt(const QPoint &point) const = 0;
175
176 QSize sizeHintForIndex(const QModelIndex &index) const;
177 virtual int sizeHintForRow(int row) const;
178 virtual int sizeHintForColumn(int column) const;
179
180 void openPersistentEditor(const QModelIndex &index);
181 void closePersistentEditor(const QModelIndex &index);
182 bool isPersistentEditorOpen(const QModelIndex &index) const;
183
184 void setIndexWidget(const QModelIndex &index, QWidget *widget);
185 QWidget *indexWidget(const QModelIndex &index) const;
186
187 void setItemDelegateForRow(int row, QAbstractItemDelegate *delegate);
188 QAbstractItemDelegate *itemDelegateForRow(int row) const;
189
190 void setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate);
191 QAbstractItemDelegate *itemDelegateForColumn(int column) const;
192
193#if QT_DEPRECATED_SINCE(6, 0)
194 QT_DEPRECATED_VERSION_X_6_0("Use itemDelegateForIndex instead")
195 QAbstractItemDelegate *itemDelegate(const QModelIndex &index) const
196 { return itemDelegateForIndex(index); }
197#endif
198 virtual QAbstractItemDelegate *itemDelegateForIndex(const QModelIndex &index) const;
199
200 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
201
202 using QAbstractScrollArea::update;
203
204public Q_SLOTS:
205 virtual void reset();
206 virtual void setRootIndex(const QModelIndex &index);
207 virtual void doItemsLayout();
208 virtual void selectAll();
209 void edit(const QModelIndex &index);
210 void clearSelection();
211 void setCurrentIndex(const QModelIndex &index);
212 void scrollToTop();
213 void scrollToBottom();
214 void update(const QModelIndex &index);
215
216protected Q_SLOTS:
217 virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
218 const QList<int> &roles = QList<int>());
219 virtual void rowsInserted(const QModelIndex &parent, int start, int end);
220 virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
221 virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
222 virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous);
223 virtual void updateEditorData();
224 virtual void updateEditorGeometries();
225 virtual void updateGeometries();
226 virtual void verticalScrollbarAction(int action);
227 virtual void horizontalScrollbarAction(int action);
228 virtual void verticalScrollbarValueChanged(int value);
229 virtual void horizontalScrollbarValueChanged(int value);
230 virtual void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);
231 virtual void commitData(QWidget *editor);
232 virtual void editorDestroyed(QObject *editor);
233
234Q_SIGNALS:
235 void pressed(const QModelIndex &index);
236 void clicked(const QModelIndex &index);
237 void doubleClicked(const QModelIndex &index);
238
239 void activated(const QModelIndex &index);
240 void entered(const QModelIndex &index);
241 void viewportEntered();
242
243 void iconSizeChanged(const QSize &size);
244
245protected:
246 QAbstractItemView(QAbstractItemViewPrivate &, QWidget *parent = nullptr);
247
248 enum CursorAction { MoveUp, MoveDown, MoveLeft, MoveRight,
249 MoveHome, MoveEnd, MovePageUp, MovePageDown,
250 MoveNext, MovePrevious };
251 virtual QModelIndex moveCursor(CursorAction cursorAction,
252 Qt::KeyboardModifiers modifiers) = 0;
253
254 virtual int horizontalOffset() const = 0;
255 virtual int verticalOffset() const = 0;
256
257 virtual bool isIndexHidden(const QModelIndex &index) const = 0;
258
259 virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) = 0;
260 virtual QRegion visualRegionForSelection(const QItemSelection &selection) const = 0;
261 virtual QModelIndexList selectedIndexes() const;
262
263 virtual bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event);
264
265 virtual QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex &index,
266 const QEvent *event = nullptr) const;
267
268#if QT_CONFIG(draganddrop)
269 virtual void startDrag(Qt::DropActions supportedActions);
270#endif
271
272 virtual void initViewItemOption(QStyleOptionViewItem *option) const;
273
274 enum State {
275 NoState,
276 DraggingState,
277 DragSelectingState,
278 EditingState,
279 ExpandingState,
280 CollapsingState,
281 AnimatingState
282 };
283
284 State state() const;
285 void setState(State state);
286
287 void scheduleDelayedItemsLayout();
288 void executeDelayedItemsLayout();
289
290 void setDirtyRegion(const QRegion &region);
291 void scrollDirtyRegion(int dx, int dy);
292 QPoint dirtyRegionOffset() const;
293
294 void startAutoScroll();
295 void stopAutoScroll();
296 void doAutoScroll();
297
298 bool focusNextPrevChild(bool next) override;
299 bool event(QEvent *event) override;
300 bool viewportEvent(QEvent *event) override;
301 void mousePressEvent(QMouseEvent *event) override;
302 void mouseMoveEvent(QMouseEvent *event) override;
303 void mouseReleaseEvent(QMouseEvent *event) override;
304 void mouseDoubleClickEvent(QMouseEvent *event) override;
305#if QT_CONFIG(draganddrop)
306 void dragEnterEvent(QDragEnterEvent *event) override;
307 void dragMoveEvent(QDragMoveEvent *event) override;
308 void dragLeaveEvent(QDragLeaveEvent *event) override;
309 void dropEvent(QDropEvent *event) override;
310#endif
311 void focusInEvent(QFocusEvent *event) override;
312 void focusOutEvent(QFocusEvent *event) override;
313 void keyPressEvent(QKeyEvent *event) override;
314 void resizeEvent(QResizeEvent *event) override;
315 void timerEvent(QTimerEvent *event) override;
316 void inputMethodEvent(QInputMethodEvent *event) override;
317 bool eventFilter(QObject *object, QEvent *event) override;
318
319#if QT_CONFIG(draganddrop)
320 enum DropIndicatorPosition { OnItem, AboveItem, BelowItem, OnViewport };
321 DropIndicatorPosition dropIndicatorPosition() const;
322#endif
323
324 QSize viewportSizeHint() const override;
325
326private:
327 Q_DECLARE_PRIVATE(QAbstractItemView)
328 Q_DISABLE_COPY(QAbstractItemView)
329 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
330 Q_PRIVATE_SLOT(d_func(), void _q_columnsRemoved(const QModelIndex&, int, int))
331 Q_PRIVATE_SLOT(d_func(), void _q_columnsInserted(const QModelIndex&, int, int))
332 Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex&, int, int))
333 Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex&, int, int))
334 Q_PRIVATE_SLOT(d_func(), void _q_columnsMoved(const QModelIndex&, int, int, const QModelIndex&, int))
335 Q_PRIVATE_SLOT(d_func(), void _q_rowsMoved(const QModelIndex&, int, int, const QModelIndex&, int))
336 Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
337 Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged())
338 Q_PRIVATE_SLOT(d_func(), void _q_headerDataChanged())
339#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
340 Q_PRIVATE_SLOT(d_func(), void _q_scrollerStateChanged())
341#endif
342 Q_PRIVATE_SLOT(d_func(), void _q_delegateSizeHintChanged(const QModelIndex&))
343
344 friend class ::tst_QAbstractItemView;
345 friend class ::tst_QTreeView;
346 friend class QTreeViewPrivate; // needed to compile with MSVC
347 friend class QListModeViewBase;
348 friend class QListViewPrivate;
349 friend class QAbstractSlider;
350};
351
352Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemView::EditTriggers)
353
354QT_END_NAMESPACE
355
356#endif // QABSTRACTITEMVIEW_H
357

source code of qtbase/src/widgets/itemviews/qabstractitemview.h