From f1d05a93bf38a283f5ff9f9883d4aec1fb96fd3b Mon Sep 17 00:00:00 2001 From: unelsson Date: Thu, 12 Aug 2021 22:28:16 +0300 Subject: [PATCH 1/3] Get index and model from proxy --- apps/opencs/model/world/commanddispatcher.cpp | 15 ++++++++++++++- apps/opencs/model/world/commanddispatcher.hpp | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/apps/opencs/model/world/commanddispatcher.cpp b/apps/opencs/model/world/commanddispatcher.cpp index b517fed0fb..f753ec2e31 100644 --- a/apps/opencs/model/world/commanddispatcher.cpp +++ b/apps/opencs/model/world/commanddispatcher.cpp @@ -3,6 +3,9 @@ #include #include +#include +#include + #include #include @@ -134,7 +137,7 @@ std::vector CSMWorld::CommandDispatcher::getExtendedTypes return tables; } -void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *model, const QModelIndex& index, const QVariant& new_) +void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *sourceModel, const QModelIndex& sourceIndex, const QVariant& new_) { if (mLocked) return; @@ -147,6 +150,16 @@ void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *model, cons std::unique_ptr modifyData; std::unique_ptr modifyCell; + QAbstractItemModel *model; + QModelIndex index; + + if (QAbstractProxyModel *proxy = dynamic_cast (sourceModel)) + { + // Replace proxy with actual model + index = proxy->mapToSource (sourceIndex); + model = proxy->sourceModel(); + } + int columnId = model->data (index, ColumnBase::Role_ColumnId).toInt(); int stateColumn = dynamic_cast(*model).findColumnIndex(Columns::ColumnId_Modification); diff --git a/apps/opencs/model/world/commanddispatcher.hpp b/apps/opencs/model/world/commanddispatcher.hpp index 538fd7f188..13724f5dbd 100644 --- a/apps/opencs/model/world/commanddispatcher.hpp +++ b/apps/opencs/model/world/commanddispatcher.hpp @@ -60,7 +60,7 @@ namespace CSMWorld /// /// \attention model must either be a model for the table operated on by this /// dispatcher or a proxy of it. - void executeModify (QAbstractItemModel *model, const QModelIndex& index, const QVariant& new_); + void executeModify (QAbstractItemModel *sourceModel, const QModelIndex& sourceIndex, const QVariant& new_); public slots: From 298db2ef76e6ea8a8f9b251b100e66c602db45fa Mon Sep 17 00:00:00 2001 From: unelsson Date: Thu, 12 Aug 2021 22:31:37 +0300 Subject: [PATCH 2/3] Initialize and check pointer. --- apps/opencs/model/world/commanddispatcher.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/opencs/model/world/commanddispatcher.cpp b/apps/opencs/model/world/commanddispatcher.cpp index f753ec2e31..13e66fd5cd 100644 --- a/apps/opencs/model/world/commanddispatcher.cpp +++ b/apps/opencs/model/world/commanddispatcher.cpp @@ -150,7 +150,7 @@ void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *sourceModel std::unique_ptr modifyData; std::unique_ptr modifyCell; - QAbstractItemModel *model; + QAbstractItemModel *model(nullptr); QModelIndex index; if (QAbstractProxyModel *proxy = dynamic_cast (sourceModel)) @@ -160,6 +160,8 @@ void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *sourceModel model = proxy->sourceModel(); } + if (!model) return; + int columnId = model->data (index, ColumnBase::Role_ColumnId).toInt(); int stateColumn = dynamic_cast(*model).findColumnIndex(Columns::ColumnId_Modification); From 7801f420059ad4540dbda554acaaa343ff0f1919 Mon Sep 17 00:00:00 2001 From: unelsson Date: Fri, 20 Aug 2021 22:32:41 +0300 Subject: [PATCH 3/3] Don't do mapToSource at executeModify --- apps/opencs/model/world/commanddispatcher.cpp | 25 ++++++------------- apps/opencs/model/world/commanddispatcher.hpp | 2 +- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/apps/opencs/model/world/commanddispatcher.cpp b/apps/opencs/model/world/commanddispatcher.cpp index 13e66fd5cd..3da3e3d22c 100644 --- a/apps/opencs/model/world/commanddispatcher.cpp +++ b/apps/opencs/model/world/commanddispatcher.cpp @@ -137,7 +137,7 @@ std::vector CSMWorld::CommandDispatcher::getExtendedTypes return tables; } -void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *sourceModel, const QModelIndex& sourceIndex, const QVariant& new_) +void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *model, const QModelIndex& index, const QVariant& new_) { if (mLocked) return; @@ -150,26 +150,17 @@ void CSMWorld::CommandDispatcher::executeModify (QAbstractItemModel *sourceModel std::unique_ptr modifyData; std::unique_ptr modifyCell; - QAbstractItemModel *model(nullptr); - QModelIndex index; + QAbstractItemModel* sourceModel = model; + if (IdTableProxyModel* proxy = dynamic_cast (model)) + sourceModel = proxy->sourceModel(); - if (QAbstractProxyModel *proxy = dynamic_cast (sourceModel)) - { - // Replace proxy with actual model - index = proxy->mapToSource (sourceIndex); - model = proxy->sourceModel(); - } - - if (!model) return; + CSMWorld::IdTable& table = dynamic_cast(*sourceModel); // for getId() + int stateColumn = table.findColumnIndex(Columns::ColumnId_Modification); + QModelIndex stateIndex = table.getModelIndex(table.getId(index.row()), stateColumn); + RecordBase::State state = static_cast (sourceModel->data(stateIndex).toInt()); int columnId = model->data (index, ColumnBase::Role_ColumnId).toInt(); - int stateColumn = dynamic_cast(*model).findColumnIndex(Columns::ColumnId_Modification); - - CSMWorld::IdTable& table = dynamic_cast(*model); // for getId() - QModelIndex stateIndex = table.getModelIndex(table.getId(index.row()), stateColumn); - RecordBase::State state = static_cast (model->data(stateIndex).toInt()); - // This is not guaranteed to be the same as \a model, since a proxy could be used. IdTable& model2 = dynamic_cast (*mDocument.getData().getTableModel(mId)); diff --git a/apps/opencs/model/world/commanddispatcher.hpp b/apps/opencs/model/world/commanddispatcher.hpp index 13724f5dbd..538fd7f188 100644 --- a/apps/opencs/model/world/commanddispatcher.hpp +++ b/apps/opencs/model/world/commanddispatcher.hpp @@ -60,7 +60,7 @@ namespace CSMWorld /// /// \attention model must either be a model for the table operated on by this /// dispatcher or a proxy of it. - void executeModify (QAbstractItemModel *sourceModel, const QModelIndex& sourceIndex, const QVariant& new_); + void executeModify (QAbstractItemModel *model, const QModelIndex& index, const QVariant& new_); public slots: