Skip to content

Commit

Permalink
Fix for naming index bug #223 (#226)
Browse files Browse the repository at this point in the history
* Fix for naming index bug #223

* Handle index correctly on delete

* Remove unnecessary logging/comments

* Update new resource creation logic

* Add nullptr checks
  • Loading branch information
KartikShrivastava authored Jun 30, 2022
1 parent 3e69593 commit 576d6ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 27 additions & 1 deletion MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion Models/ResourceModelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 576d6ee

Please sign in to comment.