qheaderview.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 QHEADERVIEW_H
  40. #define QHEADERVIEW_H
  41. #include <QtWidgets/qabstractitemview.h>
  42. QT_BEGIN_NAMESPACE
  43. #ifndef QT_NO_ITEMVIEWS
  44. class QHeaderViewPrivate;
  45. class QStyleOptionHeader;
  46. class Q_WIDGETS_EXPORT QHeaderView : public QAbstractItemView
  47. {
  48. Q_OBJECT
  49. Q_PROPERTY(bool showSortIndicator READ isSortIndicatorShown WRITE setSortIndicatorShown)
  50. Q_PROPERTY(bool highlightSections READ highlightSections WRITE setHighlightSections)
  51. Q_PROPERTY(bool stretchLastSection READ stretchLastSection WRITE setStretchLastSection)
  52. Q_PROPERTY(bool cascadingSectionResizes READ cascadingSectionResizes WRITE setCascadingSectionResizes)
  53. Q_PROPERTY(int defaultSectionSize READ defaultSectionSize WRITE setDefaultSectionSize RESET resetDefaultSectionSize)
  54. Q_PROPERTY(int minimumSectionSize READ minimumSectionSize WRITE setMinimumSectionSize)
  55. Q_PROPERTY(int maximumSectionSize READ maximumSectionSize WRITE setMaximumSectionSize)
  56. Q_PROPERTY(Qt::Alignment defaultAlignment READ defaultAlignment WRITE setDefaultAlignment)
  57. public:
  58. enum ResizeMode
  59. {
  60. Interactive,
  61. Stretch,
  62. Fixed,
  63. ResizeToContents,
  64. Custom = Fixed
  65. };
  66. Q_ENUM(ResizeMode)
  67. explicit QHeaderView(Qt::Orientation orientation, QWidget *parent = Q_NULLPTR);
  68. virtual ~QHeaderView();
  69. void setModel(QAbstractItemModel *model) Q_DECL_OVERRIDE;
  70. Qt::Orientation orientation() const;
  71. int offset() const;
  72. int length() const;
  73. QSize sizeHint() const Q_DECL_OVERRIDE;
  74. void setVisible(bool v) Q_DECL_OVERRIDE;
  75. int sectionSizeHint(int logicalIndex) const;
  76. int visualIndexAt(int position) const;
  77. int logicalIndexAt(int position) const;
  78. inline int logicalIndexAt(int x, int y) const;
  79. inline int logicalIndexAt(const QPoint &pos) const;
  80. int sectionSize(int logicalIndex) const;
  81. int sectionPosition(int logicalIndex) const;
  82. int sectionViewportPosition(int logicalIndex) const;
  83. void moveSection(int from, int to);
  84. void swapSections(int first, int second);
  85. void resizeSection(int logicalIndex, int size);
  86. void resizeSections(QHeaderView::ResizeMode mode);
  87. bool isSectionHidden(int logicalIndex) const;
  88. void setSectionHidden(int logicalIndex, bool hide);
  89. int hiddenSectionCount() const;
  90. inline void hideSection(int logicalIndex);
  91. inline void showSection(int logicalIndex);
  92. int count() const;
  93. int visualIndex(int logicalIndex) const;
  94. int logicalIndex(int visualIndex) const;
  95. void setSectionsMovable(bool movable);
  96. bool sectionsMovable() const;
  97. #if QT_DEPRECATED_SINCE(5, 0)
  98. inline QT_DEPRECATED void setMovable(bool movable) { setSectionsMovable(movable); }
  99. inline QT_DEPRECATED bool isMovable() const { return sectionsMovable(); }
  100. #endif
  101. void setSectionsClickable(bool clickable);
  102. bool sectionsClickable() const;
  103. #if QT_DEPRECATED_SINCE(5, 0)
  104. inline QT_DEPRECATED void setClickable(bool clickable) { setSectionsClickable(clickable); }
  105. inline QT_DEPRECATED bool isClickable() const { return sectionsClickable(); }
  106. #endif
  107. void setHighlightSections(bool highlight);
  108. bool highlightSections() const;
  109. ResizeMode sectionResizeMode(int logicalIndex) const;
  110. void setSectionResizeMode(ResizeMode mode);
  111. void setSectionResizeMode(int logicalIndex, ResizeMode mode);
  112. void setResizeContentsPrecision(int precision);
  113. int resizeContentsPrecision() const;
  114. #if QT_DEPRECATED_SINCE(5, 0)
  115. inline QT_DEPRECATED void setResizeMode(ResizeMode mode)
  116. { setSectionResizeMode(mode); }
  117. inline QT_DEPRECATED void setResizeMode(int logicalindex, ResizeMode mode)
  118. { setSectionResizeMode(logicalindex, mode); }
  119. inline QT_DEPRECATED ResizeMode resizeMode(int logicalindex) const
  120. { return sectionResizeMode(logicalindex); }
  121. #endif
  122. int stretchSectionCount() const;
  123. void setSortIndicatorShown(bool show);
  124. bool isSortIndicatorShown() const;
  125. void setSortIndicator(int logicalIndex, Qt::SortOrder order);
  126. int sortIndicatorSection() const;
  127. Qt::SortOrder sortIndicatorOrder() const;
  128. bool stretchLastSection() const;
  129. void setStretchLastSection(bool stretch);
  130. bool cascadingSectionResizes() const;
  131. void setCascadingSectionResizes(bool enable);
  132. int defaultSectionSize() const;
  133. void setDefaultSectionSize(int size);
  134. void resetDefaultSectionSize();
  135. int minimumSectionSize() const;
  136. void setMinimumSectionSize(int size);
  137. int maximumSectionSize() const;
  138. void setMaximumSectionSize(int size);
  139. Qt::Alignment defaultAlignment() const;
  140. void setDefaultAlignment(Qt::Alignment alignment);
  141. void doItemsLayout() Q_DECL_OVERRIDE;
  142. bool sectionsMoved() const;
  143. bool sectionsHidden() const;
  144. #ifndef QT_NO_DATASTREAM
  145. QByteArray saveState() const;
  146. bool restoreState(const QByteArray &state);
  147. #endif
  148. void reset() Q_DECL_OVERRIDE;
  149. public Q_SLOTS:
  150. void setOffset(int offset);
  151. void setOffsetToSectionPosition(int visualIndex);
  152. void setOffsetToLastSection();
  153. void headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast);
  154. Q_SIGNALS:
  155. void sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex);
  156. void sectionResized(int logicalIndex, int oldSize, int newSize);
  157. void sectionPressed(int logicalIndex);
  158. void sectionClicked(int logicalIndex);
  159. void sectionEntered(int logicalIndex);
  160. void sectionDoubleClicked(int logicalIndex);
  161. void sectionCountChanged(int oldCount, int newCount);
  162. void sectionHandleDoubleClicked(int logicalIndex);
  163. void geometriesChanged();
  164. void sortIndicatorChanged(int logicalIndex, Qt::SortOrder order);
  165. protected Q_SLOTS:
  166. void updateSection(int logicalIndex);
  167. void resizeSections();
  168. void sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast);
  169. void sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast);
  170. protected:
  171. QHeaderView(QHeaderViewPrivate &dd, Qt::Orientation orientation, QWidget *parent = Q_NULLPTR);
  172. void initialize();
  173. void initializeSections();
  174. void initializeSections(int start, int end);
  175. void currentChanged(const QModelIndex &current, const QModelIndex &old) Q_DECL_OVERRIDE;
  176. bool event(QEvent *e) Q_DECL_OVERRIDE;
  177. void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
  178. void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
  179. void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
  180. void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
  181. void mouseDoubleClickEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
  182. bool viewportEvent(QEvent *e) Q_DECL_OVERRIDE;
  183. virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
  184. virtual QSize sectionSizeFromContents(int logicalIndex) const;
  185. int horizontalOffset() const Q_DECL_OVERRIDE;
  186. int verticalOffset() const Q_DECL_OVERRIDE;
  187. void updateGeometries() Q_DECL_OVERRIDE;
  188. void scrollContentsBy(int dx, int dy) Q_DECL_OVERRIDE;
  189. void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) Q_DECL_OVERRIDE;
  190. void rowsInserted(const QModelIndex &parent, int start, int end) Q_DECL_OVERRIDE;
  191. QRect visualRect(const QModelIndex &index) const Q_DECL_OVERRIDE;
  192. void scrollTo(const QModelIndex &index, ScrollHint hint) Q_DECL_OVERRIDE;
  193. QModelIndex indexAt(const QPoint &p) const Q_DECL_OVERRIDE;
  194. bool isIndexHidden(const QModelIndex &index) const Q_DECL_OVERRIDE;
  195. QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers) Q_DECL_OVERRIDE;
  196. void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags) Q_DECL_OVERRIDE;
  197. QRegion visualRegionForSelection(const QItemSelection &selection) const Q_DECL_OVERRIDE;
  198. void initStyleOption(QStyleOptionHeader *option) const;
  199. friend class QTableView;
  200. friend class QTreeView;
  201. private:
  202. Q_PRIVATE_SLOT(d_func(), void _q_sectionsRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast))
  203. Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged())
  204. Q_DECLARE_PRIVATE(QHeaderView)
  205. Q_DISABLE_COPY(QHeaderView)
  206. };
  207. inline int QHeaderView::logicalIndexAt(int ax, int ay) const
  208. { return orientation() == Qt::Horizontal ? logicalIndexAt(ax) : logicalIndexAt(ay); }
  209. inline int QHeaderView::logicalIndexAt(const QPoint &apos) const
  210. { return logicalIndexAt(apos.x(), apos.y()); }
  211. inline void QHeaderView::hideSection(int alogicalIndex)
  212. { setSectionHidden(alogicalIndex, true); }
  213. inline void QHeaderView::showSection(int alogicalIndex)
  214. { setSectionHidden(alogicalIndex, false); }
  215. #endif // QT_NO_ITEMVIEWS
  216. QT_END_NAMESPACE
  217. #endif // QHEADERVIEW_H