From 059a8fd32ac1a4b3342dab523273353ce6de69bf Mon Sep 17 00:00:00 2001
From: Andrei Kortunov <andrei.kortunov@yandex.ru>
Date: Thu, 15 Nov 2018 17:50:23 +0400
Subject: [PATCH] Refactor dynamic casts in the editor

---
 apps/opencs/model/world/commands.cpp       | 14 ++----------
 apps/opencs/view/render/pathgrid.cpp       | 25 ++++------------------
 apps/opencs/view/world/cellcreator.cpp     |  6 +-----
 apps/opencs/view/world/dialoguesubview.cpp |  7 +-----
 4 files changed, 8 insertions(+), 44 deletions(-)

diff --git a/apps/opencs/model/world/commands.cpp b/apps/opencs/model/world/commands.cpp
index 01ee2f7382..e9682d7c9a 100644
--- a/apps/opencs/model/world/commands.cpp
+++ b/apps/opencs/model/world/commands.cpp
@@ -198,12 +198,7 @@ CSMWorld::ModifyCommand::ModifyCommand (QAbstractItemModel& model, const QModelI
 
     if (mIndex.parent().isValid())
     {
-        CSMWorld::IdTree* tree = dynamic_cast<CSMWorld::IdTree*>(mModel);
-        if (tree == nullptr)
-        {
-            throw std::logic_error("CSMWorld::ModifyCommand: Attempt to add nested values to the non-nested model");
-        }
-
+        CSMWorld::IdTree* tree = &dynamic_cast<CSMWorld::IdTree&>(*mModel);
         setText ("Modify " + tree->nestedHeaderData (
                     mIndex.parent().column(), mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString());
     }
@@ -249,12 +244,7 @@ void CSMWorld::CreateCommand::applyModifications()
 {
     if (!mNestedValues.empty())
     {
-        CSMWorld::IdTree *tree = dynamic_cast<CSMWorld::IdTree *>(&mModel);
-        if (tree == nullptr)
-        {
-            throw std::logic_error("CSMWorld::CreateCommand: Attempt to add nested values to the non-nested model");
-        }
-
+        CSMWorld::IdTree* tree = &dynamic_cast<CSMWorld::IdTree&>(mModel);
         std::map<int, std::pair<int, QVariant> >::const_iterator current = mNestedValues.begin();
         std::map<int, std::pair<int, QVariant> >::const_iterator end = mNestedValues.end();
         for (; current != end; ++current)
diff --git a/apps/opencs/view/render/pathgrid.cpp b/apps/opencs/view/render/pathgrid.cpp
index d184b4938e..d8acfe2e13 100644
--- a/apps/opencs/view/render/pathgrid.cpp
+++ b/apps/opencs/view/render/pathgrid.cpp
@@ -224,11 +224,7 @@ namespace CSVRender
 
     void Pathgrid::applyPoint(CSMWorld::CommandMacro& commands, const osg::Vec3d& worldPos)
     {
-        CSMWorld::IdTree* model = dynamic_cast<CSMWorld::IdTree*>(mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
-        if (model == nullptr)
-        {
-            throw std::logic_error("CSVRender::Pathgrid: Attempt to add nested values to the non-nested model");
-        }
+        CSMWorld::IdTree* model = &dynamic_cast<CSMWorld::IdTree&>(*mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
 
         const CSMWorld::Pathgrid* source = getPathgridSource();
         if (source)
@@ -360,11 +356,7 @@ namespace CSVRender
         const CSMWorld::Pathgrid* source = getPathgridSource();
         if (source)
         {
-            CSMWorld::IdTree* model = dynamic_cast<CSMWorld::IdTree*>(mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
-            if (model == nullptr)
-            {
-                throw std::logic_error("CSVRender::Pathgrid: Attempt to add nested values to the non-nested model");
-            }
+            CSMWorld::IdTree* model = &dynamic_cast<CSMWorld::IdTree&>(*mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
 
             // Want to remove nodes from end of list first
             std::sort(mSelected.begin(), mSelected.end(), std::greater<int>());
@@ -464,12 +456,7 @@ namespace CSVRender
                 }
             }
 
-            CSMWorld::IdTree* model = dynamic_cast<CSMWorld::IdTree*>(mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
-            if (model == nullptr)
-            {
-                throw std::logic_error("CSVRender::Pathgrid: Attempt to add nested values to the non-nested model");
-            }
-
+            CSMWorld::IdTree* model = &dynamic_cast<CSMWorld::IdTree&>(*mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
             int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridEdges);
 
             std::set<int, std::greater<int> >::iterator row;
@@ -642,11 +629,7 @@ namespace CSVRender
     void Pathgrid::addEdge(CSMWorld::CommandMacro& commands, const CSMWorld::Pathgrid& source, unsigned short node1,
         unsigned short node2)
     {
-        CSMWorld::IdTree* model = dynamic_cast<CSMWorld::IdTree*>(mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
-        if (model == nullptr)
-        {
-            throw std::logic_error("CSVRender::Pathgrid: Attempt to add nested values to the non-nested model");
-        }
+        CSMWorld::IdTree* model = &dynamic_cast<CSMWorld::IdTree&>(*mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids));
 
         int recordIndex = mPathgridCollection.getIndex(mId);
         int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridEdges);
diff --git a/apps/opencs/view/world/cellcreator.cpp b/apps/opencs/view/world/cellcreator.cpp
index 54e618cd9a..5b428a4b37 100644
--- a/apps/opencs/view/world/cellcreator.cpp
+++ b/apps/opencs/view/world/cellcreator.cpp
@@ -24,11 +24,7 @@ std::string CSVWorld::CellCreator::getId() const
 
 void CSVWorld::CellCreator::configureCreateCommand(CSMWorld::CreateCommand& command) const
 {
-    CSMWorld::IdTree *model = dynamic_cast<CSMWorld::IdTree *>(getData().getTableModel(getCollectionId()));
-    if (model == nullptr)
-    {
-        throw std::logic_error("CSVWorld::CellCreator: Attempt to add nested values to the non-nested model");
-    }
+    CSMWorld::IdTree* model = &dynamic_cast<CSMWorld::IdTree&>(*getData().getTableModel(getCollectionId()));
 
     int parentIndex = model->findColumnIndex(CSMWorld::Columns::ColumnId_Cell);
     int index = model->findNestedColumnIndex(parentIndex, CSMWorld::Columns::ColumnId_Interior);
diff --git a/apps/opencs/view/world/dialoguesubview.cpp b/apps/opencs/view/world/dialoguesubview.cpp
index 9f73430f02..eeba29d95a 100644
--- a/apps/opencs/view/world/dialoguesubview.cpp
+++ b/apps/opencs/view/world/dialoguesubview.cpp
@@ -555,12 +555,7 @@ void CSVWorld::EditWidget::remake(int row)
             if (mTable->hasChildren(mTable->index(row, i)) &&
                     !(flags & CSMWorld::ColumnBase::Flag_Dialogue_List))
             {
-                CSMWorld::IdTree *innerTable = dynamic_cast<CSMWorld::IdTree*>(mTable);
-                if (innerTable == nullptr)
-                {
-                    throw std::logic_error("CSVWorld::EditWidget: Attempt to add nested values to the non-nested model");
-                }
-
+                CSMWorld::IdTree* innerTable = &dynamic_cast<CSMWorld::IdTree&>(*mTable);
                 mNestedModels.push_back(new CSMWorld::NestedTableProxyModel (mTable->index(row, i), display, innerTable));
 
                 int idColumn = mTable->findColumnIndex (CSMWorld::Columns::ColumnId_Id);