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