qabstractitemview.h 14 KB

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