From 6339912ff6a9e66b9b506809c661838a5ec1ef8f Mon Sep 17 00:00:00 2001 From: KartikShrivastava Date: Sun, 17 Apr 2022 09:31:29 -0400 Subject: [PATCH 1/5] Fix for naming index bug #223 --- MainWindow.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index 7520482b5..d4754ff77 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -564,7 +564,13 @@ 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()); + + // add new resource with created name, helps in creating another unique name + auto messageModelRef = MessageModel(ProtoModel::NonProtoParent{this}, &child); + resourceMap->AddResource(typeCase, resourceName, &messageModelRef); + // 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); From 37640fc543e231e44a336df085b63182e759d19e Mon Sep 17 00:00:00 2001 From: KartikShrivastava Date: Sun, 17 Apr 2022 13:31:01 -0400 Subject: [PATCH 2/5] Handle index correctly on delete --- Models/ResourceModelMap.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Models/ResourceModelMap.cpp b/Models/ResourceModelMap.cpp index 8f5a857ae..4bea20253 100644 --- a/Models/ResourceModelMap.cpp +++ b/Models/ResourceModelMap.cpp @@ -63,6 +63,8 @@ void ResourceModelMap::ResourceRemoved(TypeCase type, const QString& name, if (type == TypeCase::kFolder || !_resources.contains(type)) return; if (!_resources[type].contains(name)) return; + std::cout<<"Resource re moved called"< Date: Sun, 17 Apr 2022 14:01:08 -0400 Subject: [PATCH 3/5] Remove unnecessary logging/comments --- Models/ResourceModelMap.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Models/ResourceModelMap.cpp b/Models/ResourceModelMap.cpp index 4bea20253..e1c9b738b 100644 --- a/Models/ResourceModelMap.cpp +++ b/Models/ResourceModelMap.cpp @@ -63,8 +63,6 @@ void ResourceModelMap::ResourceRemoved(TypeCase type, const QString& name, if (type == TypeCase::kFolder || !_resources.contains(type)) return; if (!_resources[type].contains(name)) return; - std::cout<<"Resource re moved called"< Date: Fri, 22 Apr 2022 04:07:15 -0400 Subject: [PATCH 4/5] Update new resource creation logic --- MainWindow.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index d4754ff77..54126022e 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -567,13 +567,16 @@ void MainWindow::CreateResource(TypeCase typeCase) { QString resourceName = resourceMap->CreateResourceName(&child); child.set_name(resourceName.toStdString()); - // add new resource with created name, helps in creating another unique name - auto messageModelRef = MessageModel(ProtoModel::NonProtoParent{this}, &child); - resourceMap->AddResource(typeCase, resourceName, &messageModelRef); - // 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); + + // add new resource with created name, helps in creating another unique name + MessageModel* messageModel = treeModel->IndexToNode(index)->BackingModel()->TryCastAsMessageModel(); + if(messageModel) + resourceMap->AddResource(typeCase, resourceName, messageModel); + else + qDebug() << "Attempt to add resource with NULL messageModel..."; } void MainWindow::ResourceModelDeleted(MessageModel *m) { From 2de760caa4dda1f0d16acf513676d557815c99a9 Mon Sep 17 00:00:00 2001 From: KartikShrivastava Date: Sat, 18 Jun 2022 13:41:31 +0530 Subject: [PATCH 5/5] Add nullptr checks --- MainWindow.cpp | 19 ++++++++++++++++++- Models/ResourceModelMap.cpp | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index 54126022e..984971981 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -554,6 +554,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(); @@ -571,8 +576,20 @@ void MainWindow::CreateResource(TypeCase typeCase) { 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 - MessageModel* messageModel = treeModel->IndexToNode(index)->BackingModel()->TryCastAsMessageModel(); if(messageModel) resourceMap->AddResource(typeCase, resourceName, messageModel); else diff --git a/Models/ResourceModelMap.cpp b/Models/ResourceModelMap.cpp index e1c9b738b..02c774efa 100644 --- a/Models/ResourceModelMap.cpp +++ b/Models/ResourceModelMap.cpp @@ -129,6 +129,12 @@ 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, ""); + // Remove references to this resource _resources[type].remove(name); emit DataChanged();