QGIM: access m_itemModel through getter

- improves internal encapsulation
- allows moving a bunch of private stuff to the impl header.

Change-Id: I04d73559568e43c15ff6b80c8a0a89d8748c74c8
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Artem Dyomin 2025-05-03 11:45:57 +02:00
parent 55e86d53c8
commit 4eb5c735aa
2 changed files with 19 additions and 10 deletions

View File

@ -183,6 +183,14 @@ void QGenericItemModelImplBase::endMoveRows()
{ {
m_itemModel->endMoveRows(); m_itemModel->endMoveRows();
} }
QAbstractItemModel &QGenericItemModelImplBase::itemModel()
{
return *m_itemModel;
}
const QAbstractItemModel &QGenericItemModelImplBase::itemModel() const
{
return *m_itemModel;
}
template <typename Structure, typename Range, template <typename Structure, typename Range,
typename Protocol = QGenericItemModelDetails::table_protocol_t<Range>> typename Protocol = QGenericItemModelDetails::table_protocol_t<Range>>
@ -363,7 +371,7 @@ public:
if (row == index.row() && column == index.column()) if (row == index.row() && column == index.column())
return index; return index;
if (column < 0 || column >= m_itemModel->columnCount()) if (column < 0 || column >= itemModel().columnCount())
return {}; return {};
if (row == index.row()) if (row == index.row())
@ -420,7 +428,7 @@ public:
QVariant result; QVariant result;
if (role != Qt::DisplayRole || orientation != Qt::Horizontal if (role != Qt::DisplayRole || orientation != Qt::Horizontal
|| section < 0 || section >= that().columnCount({})) { || section < 0 || section >= that().columnCount({})) {
return m_itemModel->QAbstractItemModel::headerData(section, orientation, role); return itemModel().QAbstractItemModel::headerData(section, orientation, role);
} }
if constexpr (has_metaobject<wrapped_row_type>) { if constexpr (has_metaobject<wrapped_row_type>) {
@ -438,7 +446,7 @@ public:
result = QString::fromUtf8(metaType.name()); result = QString::fromUtf8(metaType.name());
} }
if (!result.isValid()) if (!result.isValid())
result = m_itemModel->QAbstractItemModel::headerData(section, orientation, role); result = itemModel().QAbstractItemModel::headerData(section, orientation, role);
return result; return result;
} }
@ -533,7 +541,7 @@ public:
readAt(index, readItemData); readAt(index, readItemData);
if (!tried) // no multi-role item found if (!tried) // no multi-role item found
result = m_itemModel->QAbstractItemModel::itemData(index); result = itemModel().QAbstractItemModel::itemData(index);
} }
return result; return result;
} }
@ -697,7 +705,7 @@ public:
// setItemData will emit the dataChanged signal // setItemData will emit the dataChanged signal
Q_ASSERT(!success); Q_ASSERT(!success);
emitDataChanged.dismiss(); emitDataChanged.dismiss();
success = m_itemModel->QAbstractItemModel::setItemData(index, data); success = itemModel().QAbstractItemModel::setItemData(index, data);
} }
} }
return success; return success;
@ -907,8 +915,8 @@ public:
} }
if (sourceRow == destRow || sourceRow == destRow - 1 || count <= 0 if (sourceRow == destRow || sourceRow == destRow - 1 || count <= 0
|| sourceRow < 0 || sourceRow + count - 1 >= m_itemModel->rowCount(sourceParent) || sourceRow < 0 || sourceRow + count - 1 >= itemModel().rowCount(sourceParent)
|| destRow < 0 || destRow > m_itemModel->rowCount(destParent)) { || destRow < 0 || destRow > itemModel().rowCount(destParent)) {
return false; return false;
} }
@ -1614,7 +1622,7 @@ protected:
// dynamically sized rows all have to have the same column count // dynamically sized rows all have to have the same column count
if constexpr (Base::dynamicColumns() && row_features::has_resize) { if constexpr (Base::dynamicColumns() && row_features::has_resize) {
if (QGenericItemModelDetails::isValid(empty_row)) if (QGenericItemModelDetails::isValid(empty_row))
QGenericItemModelDetails::refTo(empty_row).resize(Base::m_itemModel->columnCount()); QGenericItemModelDetails::refTo(empty_row).resize(this->itemModel().columnCount());
} }
return empty_row; return empty_row;

View File

@ -605,6 +605,7 @@ private:
CallConstFN *callConst_fn; CallConstFN *callConst_fn;
CallTupleFN *call_fn; CallTupleFN *call_fn;
QGenericItemModel *m_itemModel;
protected: protected:
template <typename Impl> // type deduction template <typename Impl> // type deduction
@ -613,8 +614,6 @@ protected:
{} {}
~QGenericItemModelImplBase() = default; ~QGenericItemModelImplBase() = default;
QGenericItemModel *m_itemModel;
inline QModelIndex createIndex(int row, int column, const void *ptr = nullptr) const; inline QModelIndex createIndex(int row, int column, const void *ptr = nullptr) const;
inline void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to); inline void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
inline QHash<int, QByteArray> roleNames() const; inline QHash<int, QByteArray> roleNames() const;
@ -634,6 +633,8 @@ protected:
inline bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, inline bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
const QModelIndex &destParent, int destRow); const QModelIndex &destParent, int destRow);
inline void endMoveRows(); inline void endMoveRows();
inline QAbstractItemModel &itemModel();
inline const QAbstractItemModel &itemModel() const;
public: public:
template <typename Ret, typename ...Args> template <typename Ret, typename ...Args>