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 
49 class tst_QAbstractItemView;
50 class tst_QTreeView;
51 
52 QT_REQUIRE_CONFIG(itemviews);
53 
54 QT_BEGIN_NAMESPACE
55 
56 class QMenu;
57 class QDrag;
58 class QEvent;
59 class QAbstractItemViewPrivate;
60 
61 class 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 
83 public:
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     QAbstractItemDelegate *itemDelegate(const QModelIndex &index) const;
227 
228     virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
229 
230     using QAbstractScrollArea::update;
231 
232 public Q_SLOTS:
233     virtual void reset();
234     virtual void setRootIndex(const QModelIndex &index);
235     virtual void doItemsLayout();
236     virtual void selectAll();
237     void edit(const QModelIndex &index);
238     void clearSelection();
239     void setCurrentIndex(const QModelIndex &index);
240     void scrollToTop();
241     void scrollToBottom();
242     void update(const QModelIndex &index);
243 
244 protected Q_SLOTS:
245     virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
246     virtual void rowsInserted(const QModelIndex &parent, int start, int end);
247     virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
248     virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
249     virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous);
250     virtual void updateEditorData();
251     virtual void updateEditorGeometries();
252     virtual void updateGeometries();
253     virtual void verticalScrollbarAction(int action);
254     virtual void horizontalScrollbarAction(int action);
255     virtual void verticalScrollbarValueChanged(int value);
256     virtual void horizontalScrollbarValueChanged(int value);
257     virtual void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);
258     virtual void commitData(QWidget *editor);
259     virtual void editorDestroyed(QObject *editor);
260 
261 Q_SIGNALS:
262     void pressed(const QModelIndex &index);
263     void clicked(const QModelIndex &index);
264     void doubleClicked(const QModelIndex &index);
265 
266     void activated(const QModelIndex &index);
267     void entered(const QModelIndex &index);
268     void viewportEntered();
269 
270     void iconSizeChanged(const QSize &size);
271 
272 protected:
273     QAbstractItemView(QAbstractItemViewPrivate &, QWidget *parent = nullptr);
274 
275 #if QT_DEPRECATED_SINCE(5, 13)
276     QT_DEPRECATED void setHorizontalStepsPerItem(int steps);
277     QT_DEPRECATED int horizontalStepsPerItem() const;
278     QT_DEPRECATED void setVerticalStepsPerItem(int steps);
279     QT_DEPRECATED int verticalStepsPerItem() const;
280 #endif
281 
282     enum CursorAction { MoveUp, MoveDown, MoveLeft, MoveRight,
283                         MoveHome, MoveEnd, MovePageUp, MovePageDown,
284                         MoveNext, MovePrevious };
285     virtual QModelIndex moveCursor(CursorAction cursorAction,
286                                    Qt::KeyboardModifiers modifiers) = 0;
287 
288     virtual int horizontalOffset() const = 0;
289     virtual int verticalOffset() const = 0;
290 
291     virtual bool isIndexHidden(const QModelIndex &index) const = 0;
292 
293     virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) = 0;
294     virtual QRegion visualRegionForSelection(const QItemSelection &selection) const = 0;
295     virtual QModelIndexList selectedIndexes() const;
296 
297     virtual bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event);
298 
299     virtual QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex &index,
300                                                                  const QEvent *event = nullptr) const;
301 
302 #if QT_CONFIG(draganddrop)
303     virtual void startDrag(Qt::DropActions supportedActions);
304 #endif
305 
306     virtual QStyleOptionViewItem viewOptions() const;
307 
308     enum State {
309         NoState,
310         DraggingState,
311         DragSelectingState,
312         EditingState,
313         ExpandingState,
314         CollapsingState,
315         AnimatingState
316     };
317 
318     State state() const;
319     void setState(State state);
320 
321     void scheduleDelayedItemsLayout();
322     void executeDelayedItemsLayout();
323 
324     void setDirtyRegion(const QRegion &region);
325     void scrollDirtyRegion(int dx, int dy);
326     QPoint dirtyRegionOffset() const;
327 
328     void startAutoScroll();
329     void stopAutoScroll();
330     void doAutoScroll();
331 
332     bool focusNextPrevChild(bool next) override;
333     bool event(QEvent *event) override;
334     bool viewportEvent(QEvent *event) override;
335     void mousePressEvent(QMouseEvent *event) override;
336     void mouseMoveEvent(QMouseEvent *event) override;
337     void mouseReleaseEvent(QMouseEvent *event) override;
338     void mouseDoubleClickEvent(QMouseEvent *event) override;
339 #if QT_CONFIG(draganddrop)
340     void dragEnterEvent(QDragEnterEvent *event) override;
341     void dragMoveEvent(QDragMoveEvent *event) override;
342     void dragLeaveEvent(QDragLeaveEvent *event) override;
343     void dropEvent(QDropEvent *event) override;
344 #endif
345     void focusInEvent(QFocusEvent *event) override;
346     void focusOutEvent(QFocusEvent *event) override;
347     void keyPressEvent(QKeyEvent *event) override;
348     void resizeEvent(QResizeEvent *event) override;
349     void timerEvent(QTimerEvent *event) override;
350     void inputMethodEvent(QInputMethodEvent *event) override;
351     bool eventFilter(QObject *object, QEvent *event) override;
352 
353 #if QT_CONFIG(draganddrop)
354     enum DropIndicatorPosition { OnItem, AboveItem, BelowItem, OnViewport };
355     DropIndicatorPosition dropIndicatorPosition() const;
356 #endif
357 
358     QSize viewportSizeHint() const override;
359 
360 private:
361     Q_DECLARE_PRIVATE(QAbstractItemView)
362     Q_DISABLE_COPY(QAbstractItemView)
363     Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
364     Q_PRIVATE_SLOT(d_func(), void _q_columnsRemoved(const QModelIndex&, int, int))
365     Q_PRIVATE_SLOT(d_func(), void _q_columnsInserted(const QModelIndex&, int, int))
366     Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex&, int, int))
367     Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex&, int, int))
368     Q_PRIVATE_SLOT(d_func(), void _q_columnsMoved(const QModelIndex&, int, int, const QModelIndex&, int))
369     Q_PRIVATE_SLOT(d_func(), void _q_rowsMoved(const QModelIndex&, int, int, const QModelIndex&, int))
370     Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
371     Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged())
372     Q_PRIVATE_SLOT(d_func(), void _q_headerDataChanged())
373 #if QT_CONFIG(gestures) && QT_CONFIG(scroller)
374     Q_PRIVATE_SLOT(d_func(), void _q_scrollerStateChanged())
375 #endif
376 
377     friend class ::tst_QAbstractItemView;
378     friend class ::tst_QTreeView;
379     friend class QTreeViewPrivate; // needed to compile with MSVC
380     friend class QListModeViewBase;
381     friend class QListViewPrivate;
382     friend class QAbstractSlider;
383 };
384 
385 Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemView::EditTriggers)
386 
387 QT_END_NAMESPACE
388 
389 #endif // QABSTRACTITEMVIEW_H
390