1// Copyright (C) 2018 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 QQUICKTABLEVIEW_P_H
5#define QQUICKTABLEVIEW_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 <private/qtquickglobal_p.h>
19QT_REQUIRE_CONFIG(quick_tableview);
20
21#include <QtCore/qpointer.h>
22#include <QtCore/qpoint.h>
23#include <QtCore/qrect.h>
24#include <QtQuick/private/qtquickglobal_p.h>
25#include <QtQuick/private/qquickflickable_p.h>
26#include <QtQml/qqmlcomponent.h>
27#include <QtQml/private/qqmlnullablevalue_p.h>
28#include <QtQml/private/qqmlfinalizer_p.h>
29#include <QtQml/private/qqmlguard_p.h>
30#include <QtQmlModels/private/qqmldelegatemodel_p.h>
31
32QT_BEGIN_NAMESPACE
33
34class QQuickTableViewAttached;
35class QQuickTableViewPrivate;
36class QItemSelectionModel;
37
38class Q_QUICK_EXPORT QQuickTableView : public QQuickFlickable, public QQmlFinalizerHook
39{
40 Q_OBJECT
41 Q_INTERFACES(QQmlFinalizerHook)
42
43 Q_PROPERTY(int rows READ rows NOTIFY rowsChanged)
44 Q_PROPERTY(int columns READ columns NOTIFY columnsChanged)
45 Q_PROPERTY(qreal rowSpacing READ rowSpacing WRITE setRowSpacing NOTIFY rowSpacingChanged)
46 Q_PROPERTY(qreal columnSpacing READ columnSpacing WRITE setColumnSpacing NOTIFY columnSpacingChanged)
47 Q_PROPERTY(QJSValue rowHeightProvider READ rowHeightProvider WRITE setRowHeightProvider NOTIFY rowHeightProviderChanged)
48 Q_PROPERTY(QJSValue columnWidthProvider READ columnWidthProvider WRITE setColumnWidthProvider NOTIFY columnWidthProviderChanged)
49 Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
50 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
51 Q_PROPERTY(bool reuseItems READ reuseItems WRITE setReuseItems NOTIFY reuseItemsChanged)
52 Q_PROPERTY(qreal contentWidth READ contentWidth WRITE setContentWidth NOTIFY contentWidthChanged)
53 Q_PROPERTY(qreal contentHeight READ contentHeight WRITE setContentHeight NOTIFY contentHeightChanged)
54 Q_PROPERTY(QQuickTableView *syncView READ syncView WRITE setSyncView NOTIFY syncViewChanged REVISION(2, 14))
55 Q_PROPERTY(Qt::Orientations syncDirection READ syncDirection WRITE setSyncDirection NOTIFY syncDirectionChanged REVISION(2, 14))
56 Q_PROPERTY(int leftColumn READ leftColumn NOTIFY leftColumnChanged REVISION(6, 0))
57 Q_PROPERTY(int rightColumn READ rightColumn NOTIFY rightColumnChanged REVISION(6, 0))
58 Q_PROPERTY(int topRow READ topRow NOTIFY topRowChanged REVISION(6, 0))
59 Q_PROPERTY(int bottomRow READ bottomRow NOTIFY bottomRowChanged REVISION(6, 0))
60 Q_PROPERTY(QItemSelectionModel *selectionModel READ selectionModel WRITE setSelectionModel NOTIFY selectionModelChanged REVISION(6, 2))
61 Q_PROPERTY(bool animate READ animate WRITE setAnimate NOTIFY animateChanged REVISION(6, 4))
62 Q_PROPERTY(bool keyNavigationEnabled READ keyNavigationEnabled WRITE setKeyNavigationEnabled NOTIFY keyNavigationEnabledChanged REVISION(6, 4))
63 Q_PROPERTY(bool pointerNavigationEnabled READ pointerNavigationEnabled WRITE setPointerNavigationEnabled NOTIFY pointerNavigationEnabledChanged REVISION(6, 4))
64 Q_PROPERTY(int currentRow READ currentRow NOTIFY currentRowChanged REVISION(6, 4) FINAL)
65 Q_PROPERTY(int currentColumn READ currentColumn NOTIFY currentColumnChanged REVISION(6, 4) FINAL)
66 Q_PROPERTY(bool alternatingRows READ alternatingRows WRITE setAlternatingRows NOTIFY alternatingRowsChanged REVISION(6, 4) FINAL)
67 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior NOTIFY selectionBehaviorChanged REVISION(6, 4) FINAL)
68 Q_PROPERTY(bool resizableColumns READ resizableColumns WRITE setResizableColumns NOTIFY resizableColumnsChanged REVISION(6, 5) FINAL)
69 Q_PROPERTY(bool resizableRows READ resizableRows WRITE setResizableRows NOTIFY resizableRowsChanged REVISION(6, 5) FINAL)
70 Q_PROPERTY(EditTriggers editTriggers READ editTriggers WRITE setEditTriggers NOTIFY editTriggersChanged REVISION(6, 5) FINAL)
71 Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode NOTIFY selectionModeChanged REVISION(6, 6) FINAL)
72 Q_PROPERTY(QQmlDelegateModel::DelegateModelAccess delegateModelAccess READ delegateModelAccess
73 WRITE setDelegateModelAccess NOTIFY delegateModelAccessChanged REVISION(6, 10) FINAL)
74
75 QML_NAMED_ELEMENT(TableView)
76 QML_ADDED_IN_VERSION(2, 12)
77 QML_ATTACHED(QQuickTableViewAttached)
78
79public:
80 enum PositionModeFlag {
81 AlignLeft = Qt::AlignLeft,
82 AlignRight = Qt::AlignRight,
83 AlignHCenter = Qt::AlignHCenter,
84 AlignTop = Qt::AlignTop,
85 AlignBottom = Qt::AlignBottom,
86 AlignVCenter = Qt::AlignVCenter,
87 AlignCenter = AlignVCenter | AlignHCenter,
88 Visible = 0x01000,
89 Contain = 0x02000
90 };
91 Q_DECLARE_FLAGS(PositionMode, PositionModeFlag)
92 Q_FLAG(PositionMode)
93
94 enum SelectionBehavior {
95 SelectionDisabled,
96 SelectCells,
97 SelectRows,
98 SelectColumns
99 };
100 Q_ENUM(SelectionBehavior)
101
102 enum SelectionMode {
103 SingleSelection,
104 ContiguousSelection,
105 ExtendedSelection
106 };
107 Q_ENUM(SelectionMode)
108
109 enum EditTrigger {
110 NoEditTriggers = 0x0,
111 SingleTapped = 0x1,
112 DoubleTapped = 0x2,
113 SelectedTapped = 0x4,
114 EditKeyPressed = 0x8,
115 AnyKeyPressed = 0x10,
116 };
117 Q_DECLARE_FLAGS(EditTriggers, EditTrigger)
118 Q_FLAG(EditTriggers)
119
120 QQuickTableView(QQuickItem *parent = nullptr);
121 ~QQuickTableView() override;
122 int rows() const;
123 int columns() const;
124
125 qreal rowSpacing() const;
126 void setRowSpacing(qreal spacing);
127
128 qreal columnSpacing() const;
129 void setColumnSpacing(qreal spacing);
130
131 QJSValue rowHeightProvider() const;
132 void setRowHeightProvider(const QJSValue &provider);
133
134 QJSValue columnWidthProvider() const;
135 void setColumnWidthProvider(const QJSValue &provider);
136
137 QVariant model() const;
138 void setModel(const QVariant &newModel);
139
140 QQmlComponent *delegate() const;
141 void setDelegate(QQmlComponent *);
142
143 bool reuseItems() const;
144 void setReuseItems(bool reuseItems);
145
146 void setContentWidth(qreal width);
147 void setContentHeight(qreal height);
148
149 QQuickTableView *syncView() const;
150 void setSyncView(QQuickTableView *view);
151
152 Qt::Orientations syncDirection() const;
153 void setSyncDirection(Qt::Orientations direction);
154
155 QItemSelectionModel *selectionModel() const;
156 void setSelectionModel(QItemSelectionModel *selectionModel);
157
158 bool animate() const;
159 void setAnimate(bool animate);
160
161 bool keyNavigationEnabled() const;
162 void setKeyNavigationEnabled(bool enabled);
163 bool pointerNavigationEnabled() const;
164 void setPointerNavigationEnabled(bool enabled);
165
166 int leftColumn() const;
167 int rightColumn() const;
168 int topRow() const;
169 int bottomRow() const;
170
171 int currentRow() const;
172 int currentColumn() const;
173
174 bool alternatingRows() const;
175 void setAlternatingRows(bool alternatingRows);
176
177 SelectionBehavior selectionBehavior() const;
178 void setSelectionBehavior(SelectionBehavior selectionBehavior);
179 SelectionMode selectionMode() const;
180 void setSelectionMode(SelectionMode selectionMode);
181
182 bool resizableColumns() const;
183 void setResizableColumns(bool enabled);
184
185 bool resizableRows() const;
186 void setResizableRows(bool enabled);
187
188 EditTriggers editTriggers() const;
189 void setEditTriggers(EditTriggers editTriggers);
190
191 QQmlDelegateModel::DelegateModelAccess delegateModelAccess() const;
192 void setDelegateModelAccess(QQmlDelegateModel::DelegateModelAccess delegateModelAccess);
193
194 Q_INVOKABLE void forceLayout();
195 Q_INVOKABLE void positionViewAtCell(const QPoint &cell, QQuickTableView::PositionMode mode, const QPointF &offset = QPointF(), const QRectF &subRect = QRectF());
196 Q_INVOKABLE void positionViewAtIndex(const QModelIndex &index, QQuickTableView::PositionMode mode, const QPointF &offset = QPointF(), const QRectF &subRect = QRectF());
197 Q_INVOKABLE void positionViewAtRow(int row, QQuickTableView::PositionMode mode, qreal offset = 0, const QRectF &subRect = QRectF());
198 Q_INVOKABLE void positionViewAtColumn(int column, QQuickTableView::PositionMode mode, qreal offset = 0, const QRectF &subRect = QRectF());
199 Q_INVOKABLE QQuickItem *itemAtCell(const QPoint &cell) const;
200
201 Q_REVISION(6, 4) Q_INVOKABLE QPoint cellAtPosition(const QPointF &position, bool includeSpacing = false) const;
202 Q_REVISION(6, 4) Q_INVOKABLE QPoint cellAtPosition(qreal x, qreal y, bool includeSpacing = false) const;
203#if QT_DEPRECATED_SINCE(6, 4)
204 QT_DEPRECATED_VERSION_X_6_4("Use index(row, column) instead")
205 Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(int row, int column) const;
206 QT_DEPRECATED_VERSION_X_6_4("Use cellAtPosition() instead")
207 Q_INVOKABLE QPoint cellAtPos(const QPointF &position, bool includeSpacing = false) const;
208 Q_INVOKABLE QPoint cellAtPos(qreal x, qreal y, bool includeSpacing = false) const;
209#endif
210
211 Q_REVISION(6, 2) Q_INVOKABLE bool isColumnLoaded(int column) const;
212 Q_REVISION(6, 2) Q_INVOKABLE bool isRowLoaded(int row) const;
213
214 Q_REVISION(6, 2) Q_INVOKABLE qreal columnWidth(int column) const;
215 Q_REVISION(6, 2) Q_INVOKABLE qreal rowHeight(int row) const;
216 Q_REVISION(6, 2) Q_INVOKABLE qreal implicitColumnWidth(int column) const;
217 Q_REVISION(6, 2) Q_INVOKABLE qreal implicitRowHeight(int row) const;
218
219 Q_REVISION(6, 4) Q_INVOKABLE QModelIndex index(int row, int column) const;
220 Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(const QPoint &cell) const;
221 Q_REVISION(6, 4) Q_INVOKABLE virtual QPoint cellAtIndex(const QModelIndex &index) const;
222 Q_REVISION(6, 4) Q_INVOKABLE int rowAtIndex(const QModelIndex &index) const;
223 Q_REVISION(6, 4) Q_INVOKABLE int columnAtIndex(const QModelIndex &index) const;
224
225 Q_REVISION(6, 5) Q_INVOKABLE void setColumnWidth(int column, qreal size);
226 Q_REVISION(6, 5) Q_INVOKABLE void clearColumnWidths();
227 Q_REVISION(6, 5) Q_INVOKABLE qreal explicitColumnWidth(int column) const;
228
229 Q_REVISION(6, 5) Q_INVOKABLE void setRowHeight(int row, qreal size);
230 Q_REVISION(6, 5) Q_INVOKABLE void clearRowHeights();
231 Q_REVISION(6, 5) Q_INVOKABLE qreal explicitRowHeight(int row) const;
232
233 Q_REVISION(6, 5) Q_INVOKABLE void edit(const QModelIndex &index);
234 Q_REVISION(6, 5) Q_INVOKABLE void closeEditor();
235
236 Q_REVISION(6, 5) Q_INVOKABLE QQuickItem *itemAtIndex(const QModelIndex &index) const;
237
238#if QT_DEPRECATED_SINCE(6, 5)
239 QT_DEPRECATED_VERSION_X_6_5("Use itemAtIndex(index(row, column)) instead")
240 Q_INVOKABLE QQuickItem *itemAtCell(int column, int row) const;
241 QT_DEPRECATED_VERSION_X_6_5("Use positionViewAtIndex(index(row, column)) instead")
242 Q_INVOKABLE void positionViewAtCell(int column, int row, QQuickTableView::PositionMode mode, const QPointF &offset = QPointF(), const QRectF &subRect = QRectF());
243#endif
244
245 Q_REVISION(6, 8) Q_INVOKABLE void moveColumn(int source, int destination);
246 Q_REVISION(6, 8) Q_INVOKABLE void moveRow(int source, int destination);
247 Q_REVISION(6, 8) Q_INVOKABLE void clearColumnReordering();
248 Q_REVISION(6, 8) Q_INVOKABLE void clearRowReordering();
249
250 static QQuickTableViewAttached *qmlAttachedProperties(QObject *);
251
252Q_SIGNALS:
253 void rowsChanged();
254 void columnsChanged();
255 void rowSpacingChanged();
256 void columnSpacingChanged();
257 void rowHeightProviderChanged();
258 void columnWidthProviderChanged();
259 void modelChanged();
260 void delegateChanged();
261 void reuseItemsChanged();
262 Q_REVISION(2, 14) void syncViewChanged();
263 Q_REVISION(2, 14) void syncDirectionChanged();
264 Q_REVISION(6, 0) void leftColumnChanged();
265 Q_REVISION(6, 0) void rightColumnChanged();
266 Q_REVISION(6, 0) void topRowChanged();
267 Q_REVISION(6, 0) void bottomRowChanged();
268 Q_REVISION(6, 2) void selectionModelChanged();
269 Q_REVISION(6, 4) void animateChanged();
270 Q_REVISION(6, 4) void keyNavigationEnabledChanged();
271 Q_REVISION(6, 4) void pointerNavigationEnabledChanged();
272 Q_REVISION(6, 4) void currentRowChanged();
273 Q_REVISION(6, 4) void currentColumnChanged();
274 Q_REVISION(6, 4) void alternatingRowsChanged();
275 Q_REVISION(6, 4) void selectionBehaviorChanged();
276 Q_REVISION(6, 5) void resizableColumnsChanged();
277 Q_REVISION(6, 5) void resizableRowsChanged();
278 Q_REVISION(6, 5) void editTriggersChanged();
279 Q_REVISION(6, 5) void layoutChanged();
280 Q_REVISION(6, 6) void selectionModeChanged();
281 Q_REVISION(6, 8) void rowMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex);
282 Q_REVISION(6, 8) void columnMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex);
283 Q_REVISION(6, 10) void delegateModelAccessChanged();
284
285protected:
286 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
287 void viewportMoved(Qt::Orientations orientation) override;
288 void keyPressEvent(QKeyEvent *e) override;
289 bool eventFilter(QObject *obj, QEvent *event) override;
290
291protected:
292 QQuickTableView(QQuickTableViewPrivate &dd, QQuickItem *parent);
293 // QQmlFinalizerHook interface
294 void componentFinalized() override;
295
296private:
297 Q_DISABLE_COPY(QQuickTableView)
298 Q_DECLARE_PRIVATE(QQuickTableView)
299
300 qreal minXExtent() const override;
301 qreal maxXExtent() const override;
302 qreal minYExtent() const override;
303 qreal maxYExtent() const override;
304};
305
306class Q_QUICK_EXPORT QQuickTableViewAttached : public QObject
307{
308 Q_OBJECT
309 Q_PROPERTY(QQuickTableView *view READ view NOTIFY viewChanged FINAL)
310 Q_PROPERTY(QQmlComponent *editDelegate READ editDelegate WRITE setEditDelegate NOTIFY editDelegateChanged FINAL)
311
312public:
313 QQuickTableViewAttached(QObject *parent);
314
315 QQuickTableView *view() const { return m_view; }
316 void setView(QQuickTableView *newTableView) {
317 if (newTableView == m_view)
318 return;
319 m_view = newTableView;
320 Q_EMIT viewChanged();
321 }
322
323 QQmlComponent *editDelegate() const { return m_editDelegate; }
324 void setEditDelegate(QQmlComponent *newEditDelegate)
325 {
326 if (m_editDelegate == newEditDelegate)
327 return;
328 m_editDelegate = newEditDelegate;
329 Q_EMIT editDelegateChanged();
330 }
331
332Q_SIGNALS:
333 void viewChanged();
334 void pooled();
335 void reused();
336 void editDelegateChanged();
337 void commit();
338
339private:
340 QPointer<QQuickTableView> m_view;
341 QQmlGuard<QQmlComponent> m_editDelegate;
342
343 friend class QQuickTableViewPrivate;
344};
345
346Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickTableView::PositionMode)
347Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickTableView::EditTriggers)
348
349QT_END_NAMESPACE
350
351#endif // QQUICKTABLEVIEW_P_H
352

source code of qtdeclarative/src/quick/items/qquicktableview_p.h