From 96d5c28a112b5ccc1bfbb09c12e4fc76e8c6a82b Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 22 Aug 2012 18:01:32 +0200 Subject: Make QAbstractItemModel::sibling virtual. This would allow implementations to create an optimized way to create sibling indexes. A typical pattern of QAIM implementation is to use the same internalPointer for each row of a subtable of a model (such that the internalPointer is related to the common parent of each set of rows) and differentiate on the row value in the QModelIndex. Alternatively, it is also common to have the internalPointer correspond directly to the row value for the QModelIndex. In both cases it is possible for the implementation to optimally create a sibling QModelIndex in the same column as a known row. Provide a virtual method for them to do so. Change-Id: I3b076abcd5f6087a4cb108fbc6dceeef15529987 Reviewed-by: Marc Mutz Reviewed-by: Stephen Kelly --- src/corelib/itemmodels/qabstractitemmodel.cpp | 32 ++++++++++++++++----------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'src/corelib/itemmodels/qabstractitemmodel.cpp') diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 4a266e4891..5f6fcc6fd9 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -1394,19 +1394,6 @@ QAbstractItemModel::~QAbstractItemModel() d_func()->invalidatePersistentIndexes(); } -/*! - \fn QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const - - Returns the sibling at \a row and \a column for the item at \a index, or an - invalid QModelIndex if there is no sibling at that location. - - sibling() is just a convenience function that finds the item's parent, and - uses it to retrieve the index of the child item in the specified \a row and - \a column. - - \sa index(), QModelIndex::row(), QModelIndex::column() -*/ - /*! \fn int QAbstractItemModel::rowCount(const QModelIndex &parent) const @@ -1657,6 +1644,25 @@ bool QAbstractItemModel::hasChildren(const QModelIndex &parent) const return (rowCount(parent) > 0) && (columnCount(parent) > 0); } +/*! + \fn QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const + + Returns the sibling at \a row and \a column for the item at \a index, or an + invalid QModelIndex if there is no sibling at that location. + + sibling() is just a convenience function that finds the item's parent, and + uses it to retrieve the index of the child item in the specified \a row and + \a column. + + This method can optionally be overridden for implementation-specific optimization. + + \sa index(), QModelIndex::row(), QModelIndex::column() +*/ +QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &idx) const +{ + return (row == idx.row() && column == idx.column()) ? idx : index(row, column, parent(idx)); +} + /*! Returns a map with values for all predefined roles in the model for the -- cgit v1.2.3