From 576d6ee874642dccb4c4c2c8002845ad88957fcb Mon Sep 17 00:00:00 2001
From: ka_sh <shrivastavakartik19@gmail.com>
Date: Thu, 30 Jun 2022 20:17:48 +0530
Subject: [PATCH] Fix for naming index bug #223 (#226)

* Fix for naming index bug #223

* Handle index correctly on delete

* Remove unnecessary logging/comments

* Update new resource creation logic

* Add nullptr checks
---
 MainWindow.cpp              | 28 +++++++++++++++++++++++++++-
 Models/ResourceModelMap.cpp |  9 ++++++++-
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/MainWindow.cpp b/MainWindow.cpp
index a7435d231..47a91a349 100644
--- a/MainWindow.cpp
+++ b/MainWindow.cpp
@@ -578,6 +578,11 @@ void MainWindow::on_treeView_doubleClicked(const QModelIndex &index) {
 void MainWindow::on_actionClearRecentMenu_triggered() { _recentFiles->clear(); }
 
 void MainWindow::CreateResource(TypeCase typeCase) {
+  if(this->treeModel == NULL){
+    qDebug() << "Attempt to add resource with NULL treeModel...";
+    return;
+  }
+
   TreeNode child;
   auto fieldNum = ResTypeFields[typeCase];
   const Descriptor *desc = child.GetDescriptor();
@@ -588,10 +593,31 @@ void MainWindow::CreateResource(TypeCase typeCase) {
   refl->MutableMessage(&child, field);
 
   // find a unique name for the new resource
-  child.set_name(resourceMap->CreateResourceName(&child).toStdString());
+  QString resourceName = resourceMap->CreateResourceName(&child);
+  child.set_name(resourceName.toStdString());
+
   // release ownership of the new child to its parent and the tree
   auto index = this->treeModel->addNode(child, _ui->treeView->currentIndex());
   treeModel->triggerNodeEdit(index, _ui->treeView);
+
+  TreeModel::Node *node = this->treeModel->IndexToNode(index);
+  if(node == NULL){
+    qDebug() << "Attempt to add resource with NULL treeModel node...";
+    return;
+  }
+
+  ProtoModel *protoModel = node->BackingModel();
+  if(protoModel == NULL){
+    qDebug() << "Attempt to add resource with NULL protoModel...";
+    return;
+  }
+
+  MessageModel *messageModel = protoModel->TryCastAsMessageModel();
+  // add new resource with created name, helps in creating another unique name
+  if(messageModel)
+    resourceMap->AddResource(typeCase, resourceName, messageModel);
+  else
+    qDebug() << "Attempt to add resource with NULL messageModel...";
 }
 
 void MainWindow::ResourceModelDeleted(MessageModel *m) {
diff --git a/Models/ResourceModelMap.cpp b/Models/ResourceModelMap.cpp
index 8f5a857ae..02c774efa 100644
--- a/Models/ResourceModelMap.cpp
+++ b/Models/ResourceModelMap.cpp
@@ -129,8 +129,15 @@ void ResourceModelMap::ResourceRemoved(TypeCase type, const QString& name,
     }
   }
 
+  // NOTE: There is an unhandled BUG related to order in which references are deleted,
+  // and following hack doesnt solve it
+
   // Remove an references to this resource
-  emit ResourceRenamed(ResTypeAsString(type), name, "");
+  //emit ResourceRenamed(ResTypeAsString(type), name, "");
+
+  // Remove references to this resource
+  _resources[type].remove(name);
+  emit DataChanged();
 }
 
 QString ResourceModelMap::CreateResourceName(TreeNode* node) {