diff options
| author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2022-05-31 21:45:01 +0200 |
|---|---|---|
| committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2022-06-03 08:46:10 +0200 |
| commit | 7dba66ab707b74458fe8c9f3422c42d2ff733040 (patch) | |
| tree | f721319e98dd2bb47fad8d03fc2f4bf23484b2b9 /src/quick/items/qquicktableview.cpp | |
| parent | 8ea96460198bea744a3583a714d2cc042d8403ab (diff) | |
QQuickTableView: add new property 'selectionBehavior'
This patch will add a 'selectionBehavior' property to
TableView. It will let the developer specify if
the user should be able to select individual cells,
rows, columns, or if selections should be disabled.
This is equal to the selectionBehavior enum in QAbstractItemView.
[ChangeLog][Quick][TableView] A new property 'selectionBehavior'
has been added that specifies if the user should be able to
select rows, columns, or cells.
Change-Id: Ia8855ae032bb02d278b284ed35049d9237523139
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quick/items/qquicktableview.cpp')
| -rw-r--r-- | src/quick/items/qquicktableview.cpp | 86 |
1 files changed, 72 insertions, 14 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 2e8d794385..9026cb13d2 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -191,10 +191,13 @@ \section1 Selecting items - You can add selection support to TableView by assigning an ItemSelectionModel to + You can add selection support to TableView by assigning an \l ItemSelectionModel to the \l selectionModel property. It will then use this model to control which delegate items should be shown as selected, and which item should be shown as - current. To find out whether a delegate is selected or current, declare the + current. You can set \l selectionBehavior to control if the user should + be allowed to select individual cells, rows, or columns. + + To find out whether a delegate is selected or current, declare the following properties: \code @@ -537,6 +540,22 @@ */ /*! + \qmlproperty enumeration QtQuick::TableView::selectionBehavior + \since 6.4 + + This property holds whether the user can select single cells, rows or columns. + + \list + \li TableView.SelectionDisabled - the user cannot perform selections. + \li TableView.SelectCells (default) - the user can select individual cells. + \li TableView.SelectRows - the user can only select rows. + \li TableView.SelectColumns - the user can only select columns. + \endlist + + \sa {Selecting items}, selectionModel, keyNavigationEnabled +*/ + +/*! \qmlmethod QtQuick::TableView::positionViewAtCell(point cell, PositionMode mode, point offset, rect subRect) Positions \l {Flickable::}{contentX} and \l {Flickable::}{contentY} such @@ -1041,14 +1060,26 @@ void QQuickTableViewPrivate::setSelectionStartPos(const QPointF &pos) return; const QRect prevSelection = selection(); - selectionStartCell = clampedCellAtPos(pos); - - if (!cellIsValid(selectionStartCell)) + const QPoint clampedCell = clampedCellAtPos(pos); + if (!cellIsValid(clampedCell)) return; - // Update selection rectangle - selectionStartCellRect = loadedTableItem(selectionStartCell)->geometry(); - setCurrentIndex(selectionStartCell); + setCurrentIndex(clampedCell); + selectionStartCellRect = loadedTableItem(clampedCell)->geometry(); + + switch (selectionBehavior) { + case QQuickTableView::SelectCells: + selectionStartCell = clampedCell; + break; + case QQuickTableView::SelectRows: + selectionStartCell = QPoint(0, clampedCell.y()); + break; + case QQuickTableView::SelectColumns: + selectionStartCell = QPoint(clampedCell.x(), 0); + break; + case QQuickTableView::SelectionDisabled: + return; + } if (!cellIsValid(selectionEndCell)) return; @@ -1072,14 +1103,26 @@ void QQuickTableViewPrivate::setSelectionEndPos(const QPointF &pos) return; const QRect prevSelection = selection(); - selectionEndCell = clampedCellAtPos(pos); - - if (!cellIsValid(selectionEndCell)) + const QPoint clampedCell = clampedCellAtPos(pos); + if (!cellIsValid(clampedCell)) return; - // Update selection rectangle - selectionEndCellRect = loadedTableItem(selectionEndCell)->geometry(); - setCurrentIndex(selectionEndCell); + setCurrentIndex(clampedCell); + selectionEndCellRect = loadedTableItem(clampedCell)->geometry();; + + switch (selectionBehavior) { + case QQuickTableView::SelectCells: + selectionEndCell = clampedCell; + break; + case QQuickTableView::SelectRows: + selectionEndCell = QPoint(tableSize.width() - 1, clampedCell.y()); + break; + case QQuickTableView::SelectColumns: + selectionEndCell = QPoint(clampedCell.x(), tableSize.height() - 1); + break; + case QQuickTableView::SelectionDisabled: + return; + } if (!cellIsValid(selectionStartCell)) return; @@ -5029,6 +5072,21 @@ void QQuickTableView::setAlternatingRows(bool alternatingRows) emit alternatingRowsChanged(); } +QQuickTableView::SelectionBehavior QQuickTableView::selectionBehavior() const +{ + return d_func()->selectionBehavior; +} + +void QQuickTableView::setSelectionBehavior(SelectionBehavior selectionBehavior) +{ + Q_D(QQuickTableView); + if (d->selectionBehavior == selectionBehavior) + return; + + d->selectionBehavior = selectionBehavior; + emit selectionBehaviorChanged(); +} + class QObjectPrivate; class QQuickTableSectionSizeProviderPrivate : public QObjectPrivate { public: |
