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 
46 QT_REQUIRE_CONFIG(itemviews);
47 
48 QT_BEGIN_NAMESPACE
49 
50 class QHeaderViewPrivate;
51 class QStyleOptionHeader;
52 
53 class 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 
66 public:
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 #if QT_DEPRECATED_SINCE(5, 0)
setMovable(bool movable)119     inline QT_DEPRECATED void setMovable(bool movable) { setSectionsMovable(movable); }
isMovable()120     inline QT_DEPRECATED bool isMovable() const { return sectionsMovable(); }
121 #endif
122     void setFirstSectionMovable(bool movable);
123     bool isFirstSectionMovable() const;
124 
125     void setSectionsClickable(bool clickable);
126     bool sectionsClickable() const;
127 #if QT_DEPRECATED_SINCE(5, 0)
setClickable(bool clickable)128     inline QT_DEPRECATED void setClickable(bool clickable) { setSectionsClickable(clickable); }
isClickable()129     inline QT_DEPRECATED bool isClickable() const { return sectionsClickable(); }
130 #endif
131 
132     void setHighlightSections(bool highlight);
133     bool highlightSections() const;
134 
135     ResizeMode sectionResizeMode(int logicalIndex) const;
136     void setSectionResizeMode(ResizeMode mode);
137     void setSectionResizeMode(int logicalIndex, ResizeMode mode);
138 
139     void setResizeContentsPrecision(int precision);
140     int  resizeContentsPrecision() const;
141 
142 #if QT_DEPRECATED_SINCE(5, 0)
setResizeMode(ResizeMode mode)143     inline QT_DEPRECATED void setResizeMode(ResizeMode mode)
144         { setSectionResizeMode(mode); }
setResizeMode(int logicalindex,ResizeMode mode)145     inline QT_DEPRECATED void setResizeMode(int logicalindex, ResizeMode mode)
146         { setSectionResizeMode(logicalindex, mode); }
resizeMode(int logicalindex)147     inline QT_DEPRECATED ResizeMode resizeMode(int logicalindex) const
148         { return sectionResizeMode(logicalindex); }
149 #endif
150 
151     int stretchSectionCount() const;
152 
153     void setSortIndicatorShown(bool show);
154     bool isSortIndicatorShown() const;
155 
156     void setSortIndicator(int logicalIndex, Qt::SortOrder order);
157     int sortIndicatorSection() const;
158     Qt::SortOrder sortIndicatorOrder() const;
159 
160     bool stretchLastSection() const;
161     void setStretchLastSection(bool stretch);
162 
163     bool cascadingSectionResizes() const;
164     void setCascadingSectionResizes(bool enable);
165 
166     int defaultSectionSize() const;
167     void setDefaultSectionSize(int size);
168     void resetDefaultSectionSize();
169 
170     int minimumSectionSize() const;
171     void setMinimumSectionSize(int size);
172     int maximumSectionSize() const;
173     void setMaximumSectionSize(int size);
174 
175     Qt::Alignment defaultAlignment() const;
176     void setDefaultAlignment(Qt::Alignment alignment);
177 
178     void doItemsLayout() override;
179     bool sectionsMoved() const;
180     bool sectionsHidden() const;
181 
182 #ifndef QT_NO_DATASTREAM
183     QByteArray saveState() const;
184     bool restoreState(const QByteArray &state);
185 #endif
186 
187     void reset() override;
188 
189 public Q_SLOTS:
190     void setOffset(int offset);
191     void setOffsetToSectionPosition(int visualIndex);
192     void setOffsetToLastSection();
193     void headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast);
194 
195 Q_SIGNALS:
196     void sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex);
197     void sectionResized(int logicalIndex, int oldSize, int newSize);
198     void sectionPressed(int logicalIndex);
199     void sectionClicked(int logicalIndex);
200     void sectionEntered(int logicalIndex);
201     void sectionDoubleClicked(int logicalIndex);
202     void sectionCountChanged(int oldCount, int newCount);
203     void sectionHandleDoubleClicked(int logicalIndex);
204     void geometriesChanged();
205     void sortIndicatorChanged(int logicalIndex, Qt::SortOrder order);
206 
207 protected Q_SLOTS:
208     void updateSection(int logicalIndex);
209     void resizeSections();
210     void sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast);
211     void sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast);
212 
213 protected:
214     QHeaderView(QHeaderViewPrivate &dd, Qt::Orientation orientation, QWidget *parent = nullptr);
215     void initialize();
216 
217     void initializeSections();
218     void initializeSections(int start, int end);
219     void currentChanged(const QModelIndex &current, const QModelIndex &old) override;
220 
221     bool event(QEvent *e) override;
222     void paintEvent(QPaintEvent *e) override;
223     void mousePressEvent(QMouseEvent *e) override;
224     void mouseMoveEvent(QMouseEvent *e) override;
225     void mouseReleaseEvent(QMouseEvent *e) override;
226     void mouseDoubleClickEvent(QMouseEvent *e) override;
227     bool viewportEvent(QEvent *e) override;
228 
229     virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
230     virtual QSize sectionSizeFromContents(int logicalIndex) const;
231 
232     int horizontalOffset() const override;
233     int verticalOffset() const override;
234     void updateGeometries() override;
235     void scrollContentsBy(int dx, int dy) override;
236 
237     void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override;
238     void rowsInserted(const QModelIndex &parent, int start, int end) override;
239 
240     QRect visualRect(const QModelIndex &index) const override;
241     void scrollTo(const QModelIndex &index, ScrollHint hint) override;
242 
243     QModelIndex indexAt(const QPoint &p) const override;
244     bool isIndexHidden(const QModelIndex &index) const override;
245 
246     QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers) override;
247     void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags) override;
248     QRegion visualRegionForSelection(const QItemSelection &selection) const override;
249     void initStyleOption(QStyleOptionHeader *option) const;
250 
251     friend class QTableView;
252     friend class QTreeView;
253 
254 private:
255     // ### Qt6: make them protected slots in QHeaderViewPrivate
256     Q_PRIVATE_SLOT(d_func(), void _q_sectionsRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast))
257     Q_PRIVATE_SLOT(d_func(), void _q_sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination))
258     Q_PRIVATE_SLOT(d_func(), void _q_sectionsMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination))
259     Q_PRIVATE_SLOT(d_func(), void _q_sectionsAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
260                                                               QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint))
261     Q_PRIVATE_SLOT(d_func(), void _q_sectionsChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(),
262                                                      QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint))
263     Q_DECLARE_PRIVATE(QHeaderView)
264     Q_DISABLE_COPY(QHeaderView)
265 };
266 
logicalIndexAt(int ax,int ay)267 inline int QHeaderView::logicalIndexAt(int ax, int ay) const
268 { return orientation() == Qt::Horizontal ? logicalIndexAt(ax) : logicalIndexAt(ay); }
logicalIndexAt(const QPoint & apos)269 inline int QHeaderView::logicalIndexAt(const QPoint &apos) const
270 { return logicalIndexAt(apos.x(), apos.y()); }
hideSection(int alogicalIndex)271 inline void QHeaderView::hideSection(int alogicalIndex)
272 { setSectionHidden(alogicalIndex, true); }
showSection(int alogicalIndex)273 inline void QHeaderView::showSection(int alogicalIndex)
274 { setSectionHidden(alogicalIndex, false); }
275 
276 QT_END_NAMESPACE
277 
278 #endif // QHEADERVIEW_H
279