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