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 QTABLEVIEW_P_H
5#define QTABLEVIEW_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include "qtableview.h"
20#include "qheaderview.h"
21
22#include <QtCore/QBasicTimer>
23#include <QtCore/QList>
24#include <QtCore/QMap>
25#include <QtCore/QSet>
26#include "private/qabstractitemview_p.h"
27
28#include <array>
29#include <list>
30#include <vector>
31
32QT_REQUIRE_CONFIG(tableview);
33
34QT_BEGIN_NAMESPACE
35
36/** \internal
37*
38* This is a list of span with a binary index to look up quickly a span at a certain index.
39*
40* The index is a map of map.
41* spans are mentaly divided into sub spans so that the start of any subspans doesn't overlap
42* with any other subspans. There is no real representation of the subspans.
43* The key of the first map is the row where the subspan starts, the value of the first map is
44* a list (map) of all subspans that starts at the same row. It is indexed with its row
45*/
46class Q_AUTOTEST_EXPORT QSpanCollection
47{
48public:
49 struct Span
50 {
51 int m_top;
52 int m_left;
53 int m_bottom;
54 int m_right;
55 bool will_be_deleted;
56 Span()
57 : m_top(-1), m_left(-1), m_bottom(-1), m_right(-1), will_be_deleted(false) { }
58 Span(int row, int column, int rowCount, int columnCount)
59 : m_top(row), m_left(column), m_bottom(row+rowCount-1), m_right(column+columnCount-1), will_be_deleted(false) { }
60 inline int top() const { return m_top; }
61 inline int left() const { return m_left; }
62 inline int bottom() const { return m_bottom; }
63 inline int right() const { return m_right; }
64 inline int height() const { return m_bottom - m_top + 1; }
65 inline int width() const { return m_right - m_left + 1; }
66 };
67
68 ~QSpanCollection()
69 {
70 qDeleteAll(c: spans);
71 }
72
73 void addSpan(Span *span);
74 void updateSpan(Span *span, int old_height);
75 Span *spanAt(int x, int y) const;
76 void clear();
77 QSet<Span *> spansInRect(int x, int y, int w, int h) const;
78
79 void updateInsertedRows(int start, int end);
80 void updateInsertedColumns(int start, int end);
81 void updateRemovedRows(int start, int end);
82 void updateRemovedColumns(int start, int end);
83
84#ifdef QT_BUILD_INTERNAL
85 bool checkConsistency() const;
86#endif
87
88 typedef std::list<Span *> SpanList;
89 SpanList spans; //lists of all spans
90private:
91 //the indexes are negative so the QMap::lowerBound do what i need.
92 typedef QMap<int, Span *> SubIndex;
93 typedef QMap<int, SubIndex> Index;
94 Index index;
95
96 bool cleanSpanSubIndex(SubIndex &subindex, int end, bool update = false);
97};
98
99Q_DECLARE_TYPEINFO ( QSpanCollection::Span, Q_RELOCATABLE_TYPE);
100
101#if QT_CONFIG(abstractbutton)
102class QTableCornerButton;
103#endif
104class Q_AUTOTEST_EXPORT QTableViewPrivate : public QAbstractItemViewPrivate
105{
106 Q_DECLARE_PUBLIC(QTableView)
107public:
108 QTableViewPrivate()
109 : showGrid(true), gridStyle(Qt::SolidLine),
110 horizontalHeader(nullptr), verticalHeader(nullptr),
111 sortingEnabled(false), geometryRecursionBlock(false),
112 visualCursor(QPoint())
113 {
114 wrapItemText = true;
115#if QT_CONFIG(draganddrop)
116 overwrite = true;
117#endif
118 }
119 void init();
120 void clearConnections();
121 void trimHiddenSelections(QItemSelectionRange *range) const;
122 QRect intersectedRect(const QRect rect, const QModelIndex &topLeft, const QModelIndex &bottomRight) const override;
123
124 inline bool isHidden(int row, int col) const {
125 return verticalHeader->isSectionHidden(logicalIndex: row)
126 || horizontalHeader->isSectionHidden(logicalIndex: col);
127 }
128 inline int visualRow(int logicalRow) const {
129 return verticalHeader->visualIndex(logicalIndex: logicalRow);
130 }
131 inline int visualColumn(int logicalCol) const {
132 return horizontalHeader->visualIndex(logicalIndex: logicalCol);
133 }
134 inline int logicalRow(int visualRow) const {
135 return verticalHeader->logicalIndex(visualIndex: visualRow);
136 }
137 inline int logicalColumn(int visualCol) const {
138 return horizontalHeader->logicalIndex(visualIndex: visualCol);
139 }
140
141 QStyleOptionViewItem::ViewItemPosition viewItemPosition(const QModelIndex &index) const;
142
143 inline int accessibleTable2Index(const QModelIndex &index) const {
144 const int vHeader = verticalHeader ? 1 : 0;
145 return (index.row() + (horizontalHeader ? 1 : 0)) * (index.model()->columnCount() + vHeader)
146 + index.column() + vHeader;
147 }
148
149 int sectionSpanEndLogical(const QHeaderView *header, int logical, int span) const;
150 int sectionSpanSize(const QHeaderView *header, int logical, int span) const;
151 bool spanContainsSection(const QHeaderView *header, int logical, int spanLogical, int span) const;
152 void drawAndClipSpans(const QRegion &area, QPainter *painter,
153 const QStyleOptionViewItem &option, QBitArray *drawn,
154 int firstVisualRow, int lastVisualRow, int firstVisualColumn, int lastVisualColumn);
155 void drawCell(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index);
156 int widthHintForIndex(const QModelIndex &index, int hint, const QStyleOptionViewItem &option) const;
157 int heightHintForIndex(const QModelIndex &index, int hint, QStyleOptionViewItem &option) const;
158
159 bool showGrid;
160 Qt::PenStyle gridStyle;
161 QBasicTimer columnResizeTimer;
162 QBasicTimer rowResizeTimer;
163 QList<int> columnsToUpdate;
164 QList<int> rowsToUpdate;
165 QHeaderView *horizontalHeader;
166 QHeaderView *verticalHeader;
167#if QT_CONFIG(abstractbutton)
168 QTableCornerButton *cornerWidget;
169 QMetaObject::Connection cornerWidgetConnection;
170#endif
171 QMetaObject::Connection selectionmodelConnection;
172 std::array<QMetaObject::Connection, 4> modelConnections;
173 std::array<QMetaObject::Connection, 7> verHeaderConnections;
174 std::array<QMetaObject::Connection, 5> horHeaderConnections;
175 std::vector<QMetaObject::Connection> dynHorHeaderConnections;
176
177 bool sortingEnabled;
178 bool geometryRecursionBlock;
179 QPoint visualCursor; // (Row,column) cell coordinates to track through span navigation.
180
181 QSpanCollection spans;
182
183 void setSpan(int row, int column, int rowSpan, int columnSpan);
184 QSpanCollection::Span span(int row, int column) const;
185 inline int rowSpan(int row, int column) const {
186 return span(row, column).height();
187 }
188 inline int columnSpan(int row, int column) const {
189 return span(row, column).width();
190 }
191 inline bool hasSpans() const {
192 return !spans.spans.empty();
193 }
194 inline int rowSpanHeight(int row, int span) const {
195 return sectionSpanSize(header: verticalHeader, logical: row, span);
196 }
197 inline int columnSpanWidth(int column, int span) const {
198 return sectionSpanSize(header: horizontalHeader, logical: column, span);
199 }
200 inline int rowSpanEndLogical(int row, int span) const {
201 return sectionSpanEndLogical(header: verticalHeader, logical: row, span);
202 }
203 inline int columnSpanEndLogical(int column, int span) const {
204 return sectionSpanEndLogical(header: horizontalHeader, logical: column, span);
205 }
206
207 inline bool isRowHidden(int row) const {
208 return verticalHeader->isSectionHidden(logicalIndex: row);
209 }
210 inline bool isColumnHidden(int column) const {
211 return horizontalHeader->isSectionHidden(logicalIndex: column);
212 }
213 inline bool isCellEnabled(int row, int column) const {
214 return isIndexEnabled(index: model->index(row, column, parent: root));
215 }
216
217 enum class SearchDirection
218 {
219 Increasing,
220 Decreasing
221 };
222 int nextActiveVisualRow(int rowToStart, int column, int limit,
223 SearchDirection searchDirection) const;
224 int nextActiveVisualColumn(int row, int columnToStart, int limit,
225 SearchDirection searchDirection) const;
226
227 QRect visualSpanRect(const QSpanCollection::Span &span) const;
228
229 void selectRow(int row, bool anchor);
230 void selectColumn(int column, bool anchor);
231
232 void updateSpanInsertedRows(const QModelIndex &parent, int start, int end);
233 void updateSpanInsertedColumns(const QModelIndex &parent, int start, int end);
234 void updateSpanRemovedRows(const QModelIndex &parent, int start, int end);
235 void updateSpanRemovedColumns(const QModelIndex &parent, int start, int end);
236 void sortIndicatorChanged(int column, Qt::SortOrder order);
237};
238
239QT_END_NAMESPACE
240
241#endif // QTABLEVIEW_P_H
242

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