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

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