aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-02-22 10:51:14 +0100
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2022-03-05 13:33:24 +0100
commitb18fb72e7f0a886120f0208bd505354993990cfe (patch)
tree3a1abee2789e26580721832f44f43bbd90909703
parentc67e7922f2b21453b37e7e2397863c449a5e415b (diff)
QQuickTableView: refactor mapping functions from QQuickTreeView
It turns out we need the modelIndex() and cellAtIndex() mapping functions in QQuickTableView as well, and not only in QQuickTreeView. The reason is that functionality for moving the current index around need to use them in order for the logic to end up correctly for both QQuickTreeView and QQuickTableView. [ChangeLog][QtQuick][TableView] Added a set of functions to get the model index for a cell, and vice versa. Task-number: QTBUG-100696 Change-Id: Ifd2cd3e42bf8141a51c48635e57fb3a22358f1f6 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/quick/items/qquicktableview.cpp105
-rw-r--r--src/quick/items/qquicktableview_p.h6
-rw-r--r--src/quick/items/qquicktreeview.cpp95
-rw-r--r--src/quick/items/qquicktreeview_p.h8
4 files changed, 115 insertions, 99 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp
index acbfa28da3..9f7f49ffeb 100644
--- a/src/quick/items/qquicktableview.cpp
+++ b/src/quick/items/qquicktableview.cpp
@@ -643,6 +643,66 @@
*/
/*!
+ \qmlmethod QModelIndex QtQuick::TableView::modelIndex(int row, int column)
+ \since 6.4
+
+ Returns the \l QModelIndex that maps to \a row and \a column in the view.
+
+ \a row and \a column should be the row and column in the view (table row and
+ table column), and not a row and column in the model.
+
+ \sa rowAtIndex(), columnAtIndex()
+*/
+
+/*!
+ \qmlmethod QModelIndex QtQuick::TableView::modelIndex(point cell)
+ \since 6.4
+
+ Convenience function for doing:
+ \code
+ modelIndex(cell.y, cell.x)
+ \endcode
+
+ A cell is simply a \l point that combines row and column into
+ a single type. Note that \c point.x will map to the column, and
+ \c point.y will map to the row.
+*/
+
+/*!
+ \qmlmethod int QtQuick::TableView::rowAtIndex(QModelIndex modelIndex)
+ \since 6.4
+
+ Returns the row in the view that maps to \a modelIndex in the model.
+
+ \sa columnAtIndex(), modelIndex()
+*/
+
+/*!
+ \qmlmethod int QtQuick::TableView::columnAtIndex(QModelIndex modelIndex)
+ \since 6.4
+
+ Returns the column in the view that maps to \a modelIndex in the model.
+
+ \sa rowAtIndex(), modelIndex()
+*/
+
+/*!
+ \qmlmethod point QtQuick::TableView::cellAtIndex(QModelIndex modelIndex)
+ \since 6.4
+
+ Returns the cell in the view that maps to \a modelIndex in the model.
+ Convenience function for doing:
+
+ \code
+ Qt.point(columnAtIndex(modelIndex), rowAtIndex(modelIndex))
+ \endcode
+
+ A cell is simply a \l point that combines row and column into
+ a single type. Note that \c point.x will map to the column, and
+ \c point.y will map to the row.
+*/
+
+/*!
\qmlattachedproperty TableView QtQuick::TableView::view
This attached property holds the view that manages the delegate instance.
@@ -1116,11 +1176,10 @@ int QQuickTableViewPrivate::modelIndexToCellIndex(const QModelIndex &modelIndex)
{
// Convert QModelIndex to cell index. A cell index is just an
// integer representation of a cell instead of using a QPoint.
- if (modelIndex.parent().isValid()) {
- // TableView only uses the root items of the model
+ const QPoint cell = q_func()->cellAtIndex(modelIndex);
+ if (!cellIsValid(cell))
return -1;
- }
- return modelIndexAtCell(QPoint(modelIndex.column(), modelIndex.row()));
+ return modelIndexAtCell(cell);
}
int QQuickTableViewPrivate::edgeToArrayIndex(Qt::Edge edge) const
@@ -3085,8 +3144,7 @@ bool QQuickTableViewPrivate::selectedInSelectionModel(const QPoint &cell) const
if (!model)
return false;
- const QModelIndex modelIndex = model->index(cell.y(), cell.x());
- return selectionModel->isSelected(modelIndex);
+ return selectionModel->isSelected(q_func()->modelIndex(cell));
}
void QQuickTableViewPrivate::selectionChangedInSelectionModel(const QItemSelection &selected, const QItemSelection &deselected)
@@ -4092,6 +4150,41 @@ qreal QQuickTableView::implicitRowHeight(int row) const
return d->sizeHintForRow(row);
}
+QModelIndex QQuickTableView::modelIndex(const QPoint &cell) const
+{
+ Q_D(const QQuickTableView);
+ if (cell.x() < 0 || cell.x() >= columns() || cell.y() < 0 || cell.y() >= rows())
+ return {};
+
+ auto const qaim = d->model->abstractItemModel();
+ if (!qaim)
+ return {};
+
+ return qaim->index(cell.y(), cell.x());
+}
+
+QPoint QQuickTableView::cellAtIndex(const QModelIndex &index) const
+{
+ if (!index.isValid() || index.parent().isValid())
+ return {-1, -1};
+ return {index.column(), index.row()};
+}
+
+QModelIndex QQuickTableView::modelIndex(int row, int column) const
+{
+ return modelIndex({column, row});
+}
+
+int QQuickTableView::rowAtIndex(const QModelIndex &index) const
+{
+ return cellAtIndex(index).y();
+}
+
+int QQuickTableView::columnAtIndex(const QModelIndex &index) const
+{
+ return cellAtIndex(index).x();
+}
+
void QQuickTableView::forceLayout()
{
d_func()->forceLayout();
diff --git a/src/quick/items/qquicktableview_p.h b/src/quick/items/qquicktableview_p.h
index 40a0fae7c1..830bef6c68 100644
--- a/src/quick/items/qquicktableview_p.h
+++ b/src/quick/items/qquicktableview_p.h
@@ -156,6 +156,12 @@ public:
Q_REVISION(6, 2) Q_INVOKABLE qreal implicitColumnWidth(int column) const;
Q_REVISION(6, 2) Q_INVOKABLE qreal implicitRowHeight(int row) const;
+ Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(const QPoint &cell) const;
+ Q_REVISION(6, 4) Q_INVOKABLE virtual QPoint cellAtIndex(const QModelIndex &index) const;
+ Q_REVISION(6, 4) Q_INVOKABLE virtual QModelIndex modelIndex(int row, int column) const;
+ Q_REVISION(6, 4) Q_INVOKABLE int rowAtIndex(const QModelIndex &index) const;
+ Q_REVISION(6, 4) Q_INVOKABLE int columnAtIndex(const QModelIndex &index) const;
+
static QQuickTableViewAttached *qmlAttachedProperties(QObject *);
Q_SIGNALS:
diff --git a/src/quick/items/qquicktreeview.cpp b/src/quick/items/qquicktreeview.cpp
index a996f26ae1..e6cf4a8305 100644
--- a/src/quick/items/qquicktreeview.cpp
+++ b/src/quick/items/qquicktreeview.cpp
@@ -243,77 +243,6 @@
*/
/*!
- \qmlmethod QModelIndex QtQuick::TreeView::modelIndex(row, column)
-
- Returns the \l QModelIndex that maps to \a row and \a column in the view.
-
- \a row and \a column should be the row and column in the view (table row and
- table column), and not a row and column in the model.
-
- The assigned model, which is a tree model, is converted to a flat table
- model internally so that it can be shown in a TableView (which TreeView
- inherits). This function can be used whenever you need to know which
- index in the tree model maps to the given row and column in the view.
-
- \sa rowAtIndex(), columnAtIndex()
-*/
-
-/*!
- \qmlmethod QModelIndex QtQuick::TreeView::modelIndex(point cell)
-
- Convenience function for doing:
- \code
- modelIndex(cell.y, cell.x)
- \endcode
-
- A cell is simply a \l point that combines row and column into
- a single type. Note that \c point.x will map to the column, and
- \c point.y will map to the row.
-*/
-
-/*!
- \qmlmethod int QtQuick::TreeView::rowAtIndex(modelIndex)
-
- Returns the row in the view that maps to \a modelIndex in the model.
-
- The assigned model, which is a tree model, is converted to a flat table
- model internally so that it can be shown in a TableView (which TreeView
- inherits). This function can be used whenever you need to know which
- row in the view the given model index maps to.
-
- \note \a modelIndex must be a \l QModelIndex.
-
- \sa columnAtIndex(), modelIndex()
-*/
-
-/*!
- \qmlmethod int QtQuick::TreeView::columnAtIndex(modelIndex)
-
- Returns the column in the view that maps to \a modelIndex in the model.
-
- The assigned model, which is a tree model, is converted to a flat table
- model internally so that it can be shown in a TableView (which TreeView
- inherits). This function can be used whenever you need to know which
- column in the view the given model index maps to.
-
- \note \a modelIndex must be a \l QModelIndex.
-
- \sa rowAtIndex(), modelIndex()
-*/
-
-/*!
- \qmlmethod point QtQuick::TreeView::cellAtIndex(modelIndex)
-
- Convenience function for doing:
-
- \c {Qt.point(columnAtIndex(}\a {modelIndex}\c{), rowAtIndex(}\a {modelIndex}\c{))}
-
- A cell is simply a \l point that combines row and column into
- a single type. Note that \c point.x will map to the column, and
- \c point.y will map to the row.
-*/
-
-/*!
\qmlsignal QtQuick::TreeView::expanded(row, depth)
This signal is emitted when a \a row is expanded in the view.
@@ -624,32 +553,22 @@ void QQuickTreeView::toggleExpanded(int row)
expand(row);
}
-QModelIndex QQuickTreeView::modelIndex(int row, int column) const
+QModelIndex QQuickTreeView::modelIndex(const QPoint &cell) const
{
Q_D(const QQuickTreeView);
- const QModelIndex tableIndex = d->m_treeModelToTableModel.index(row, column);
+ const QModelIndex tableIndex = d->m_treeModelToTableModel.index(cell.y(), cell.x());
return d->m_treeModelToTableModel.mapToModel(tableIndex);
}
-QModelIndex QQuickTreeView::modelIndex(const QPoint &cell) const
-{
- return modelIndex(cell.y(), cell.x());
-}
-
-int QQuickTreeView::rowAtIndex(const QModelIndex &index) const
-{
- return d_func()->m_treeModelToTableModel.mapFromModel(index).row();
-}
-
-int QQuickTreeView::columnAtIndex(const QModelIndex &index) const
-{
- return d_func()->m_treeModelToTableModel.mapFromModel(index).column();
-}
-
QPoint QQuickTreeView::cellAtIndex(const QModelIndex &index) const
{
const QModelIndex tableIndex = d_func()->m_treeModelToTableModel.mapFromModel(index);
return QPoint(tableIndex.column(), tableIndex.row());
}
+QModelIndex QQuickTreeView::modelIndex(int row, int column) const
+{
+ return modelIndex({column, row});
+}
+
QT_END_NAMESPACE
diff --git a/src/quick/items/qquicktreeview_p.h b/src/quick/items/qquicktreeview_p.h
index 0546dbccf4..870b29d329 100644
--- a/src/quick/items/qquicktreeview_p.h
+++ b/src/quick/items/qquicktreeview_p.h
@@ -79,11 +79,9 @@ public:
Q_REVISION(6, 4) Q_INVOKABLE void collapseRecursively(int row = -1);
Q_REVISION(6, 4) Q_INVOKABLE void expandToIndex(const QModelIndex &index);
- Q_INVOKABLE QModelIndex modelIndex(int row, int column) const;
- Q_INVOKABLE QModelIndex modelIndex(const QPoint &cell) const;
- Q_INVOKABLE int rowAtIndex(const QModelIndex &index) const;
- Q_INVOKABLE int columnAtIndex(const QModelIndex &index) const;
- Q_INVOKABLE QPoint cellAtIndex(const QModelIndex &index) const;
+ Q_INVOKABLE QModelIndex modelIndex(const QPoint &cell) const override;
+ Q_INVOKABLE QPoint cellAtIndex(const QModelIndex &index) const override;
+ Q_INVOKABLE QModelIndex modelIndex(int row, int column) const override;
signals:
void expanded(int row, int depth);