chore(ConcatModel): Forward setData to proxy models

This is a nice to have feature to allow ContactModel.sourceModel instances to implement setData
This commit is contained in:
Alex Jbanca 2024-06-03 11:51:49 +03:00 committed by Alex Jbanca
parent ccc2dfc547
commit f241928cc9
2 changed files with 33 additions and 0 deletions

View File

@ -67,6 +67,7 @@ public:
// QAbstractItemModel interface
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
QHash<int, QByteArray> roleNames() const override;
// QQmlParserStatus interface

View File

@ -305,6 +305,38 @@ QVariant ConcatModel::data(const QModelIndex &index, int role) const
return {};
}
bool ConcatModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
return false;
auto row = index.row();
auto source = sourceForIndex(row);
if (source.first == nullptr)
return false;
auto model = source.first->model();
if (model == nullptr)
return false;
auto sourcePosition = m_sources.indexOf(source.first);
if (sourcePosition == -1)
return false;
auto& mapping = m_rolesMappingToSource[sourcePosition];
auto it = mapping.find(role);
if (it == mapping.end())
return false;
auto sourceIndex = model->index(source.second, 0);
if(!sourceIndex.isValid())
return false;
return model->setData(sourceIndex, value, it->second);
}
QHash<int, QByteArray> ConcatModel::roleNames() const
{
return m_roleNames;