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