diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-03-11 11:47:02 +0100 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-03-23 15:37:37 +0100 |
commit | 2d99c751cc8f2609c16d0feb260213eda88266f1 (patch) | |
tree | 4e000dadab60032ff39b6f0bd60e497124ea967b /src/quick/items/qquicktableview.cpp | |
parent | 8a7e8190b7c2e3c8a7fa4c2139f34136422a5bbc (diff) |
QQuickTableView: add private support for transposing the view
This is needed by HorizontalHeaderView when assigning it one
dimensional models.
Change-Id: I183f0d35b8f3a97853fc7496dc68b0e13e9be990
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/quick/items/qquicktableview.cpp')
-rw-r--r-- | src/quick/items/qquicktableview.cpp | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 4926d81cfe..b236216ac7 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -493,20 +493,34 @@ QQuickTableViewAttached *QQuickTableViewPrivate::getAttachedObject(const QObject int QQuickTableViewPrivate::modelIndexAtCell(const QPoint &cell) const { - int availableRows = tableSize.height(); - int modelIndex = cell.y() + (cell.x() * availableRows); - Q_TABLEVIEW_ASSERT(modelIndex < model->count(), - "modelIndex:" << modelIndex << "cell:" << cell << "count:" << model->count()); - return modelIndex; + // QQmlTableInstanceModel expects index to be in column-major + // order. This means that if the view is transposed (with a flipped + // width and height), we need to calculate it in row-major instead. + if (isTransposed) { + int availableColumns = tableSize.width(); + return (cell.y() * availableColumns) + cell.x(); + } else { + int availableRows = tableSize.height(); + return (cell.x() * availableRows) + cell.y(); + } } QPoint QQuickTableViewPrivate::cellAtModelIndex(int modelIndex) const { - int availableRows = tableSize.height(); - Q_TABLEVIEW_ASSERT(availableRows > 0, availableRows); - int column = int(modelIndex / availableRows); - int row = modelIndex % availableRows; - return QPoint(column, row); + // QQmlTableInstanceModel expects index to be in column-major + // order. This means that if the view is transposed (with a flipped + // width and height), we need to calculate it in row-major instead. + if (isTransposed) { + int availableColumns = tableSize.width(); + int row = int(modelIndex / availableColumns); + int column = modelIndex % availableColumns; + return QPoint(column, row); + } else { + int availableRows = tableSize.height(); + int column = int(modelIndex / availableRows); + int row = modelIndex % availableRows; + return QPoint(column, row); + } } int QQuickTableViewPrivate::edgeToArrayIndex(Qt::Edge edge) @@ -1234,12 +1248,13 @@ void QQuickTableViewPrivate::updateTableSize() QSize QQuickTableViewPrivate::calculateTableSize() { + QSize size(0, 0); if (tableModel) - return QSize(tableModel->columns(), tableModel->rows()); + size = QSize(tableModel->columns(), tableModel->rows()); else if (model) - return QSize(1, model->count()); + size = QSize(1, model->count()); - return QSize(0, 0); + return isTransposed ? size.transposed() : size; } qreal QQuickTableViewPrivate::getColumnLayoutWidth(int column) |