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

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