From 43efe50bc70318e61207214954e471904d801fd7 Mon Sep 17 00:00:00 2001 From: "Frank T. Bergmann" Date: Tue, 21 Jan 2025 10:05:07 +0100 Subject: [PATCH] - issue 3276: resolve items before deleting as they may already be gone --- copasi/UI/CQCompartmentDM.cpp | 27 ++++++++++++++------------- copasi/UI/CQGlobalQuantityDM.cpp | 19 +++++++++++-------- copasi/UI/CQSpecieDM.cpp | 26 +++++++++++++------------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/copasi/UI/CQCompartmentDM.cpp b/copasi/UI/CQCompartmentDM.cpp index f911d536e5..da15bfe95a 100644 --- a/copasi/UI/CQCompartmentDM.cpp +++ b/copasi/UI/CQCompartmentDM.cpp @@ -373,23 +373,24 @@ bool CQCompartmentDM::removeRows(QModelIndexList rows, const QModelIndex& index) if (rows.isEmpty()) return false; - // Build the list of pointers to items to be deleted + // Build the list of items to be deleted // before actually deleting any item. - QList Compartments; - QModelIndexList::const_iterator i; - - for (i = rows.begin(); i != rows.end(); ++i) - if (i->isValid() - && !isDefaultRow(*i)) + QList names; + + for (const auto& index : rows) + if (index.isValid() + && !isDefaultRow(index)) { - Compartments.append(&mpCompartments->operator[](i->row())); + names.append(mpCompartments->operator[](index.row()).getObjectName()); } - QList< CCompartment * >::const_iterator j; - - for (j = Compartments.begin(); j != Compartments.end(); ++j) + for (const auto& name : names) { - CCompartment * pCompartment = *j; + auto index = mpCompartments->getIndex(name); + if (index == C_INVALID_INDEX) + continue; + + CCompartment * pCompartment = &mpCompartments->operator[](name); QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(ListViews::ancestor(this), "compartment", @@ -398,7 +399,7 @@ bool CQCompartmentDM::removeRows(QModelIndexList rows, const QModelIndex& index) if (choice == QMessageBox::Ok) { - removeRows(mpCompartments->getIndex(pCompartment->getObjectName()), 1); + removeRows(index, 1); } } diff --git a/copasi/UI/CQGlobalQuantityDM.cpp b/copasi/UI/CQGlobalQuantityDM.cpp index aae36109e5..4d217b5d1a 100644 --- a/copasi/UI/CQGlobalQuantityDM.cpp +++ b/copasi/UI/CQGlobalQuantityDM.cpp @@ -371,23 +371,26 @@ bool CQGlobalQuantityDM::removeRows(QModelIndexList rows, const QModelIndex& ind if (rows.isEmpty()) return false; - // Build the list of pointers to items to be deleted + // Build the list of items to be deleted // before actually deleting any item. - QList< CModelValue * > ModelValues; + QList< std::string > names; QModelIndexList::const_iterator i; for (i = rows.begin(); i != rows.end(); ++i) if (i->isValid() && !isDefaultRow(*i)) { - ModelValues.append(&mpGlobalQuantities->operator[](i->row())); + names.append(mpGlobalQuantities->operator[](i->row()).getObjectName()); } - QList< CModelValue * >::const_iterator j; - - for (j = ModelValues.begin(); j != ModelValues.end(); ++j) + + for (auto& objName : names) { - CModelValue * pModelValue = *j; + auto index = mpGlobalQuantities->getIndex(objName); + if (index == C_INVALID_INDEX) + continue; + + CModelValue * pModelValue = &mpGlobalQuantities->operator[](objName); QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(ListViews::ancestor(this), "quantity", @@ -396,7 +399,7 @@ bool CQGlobalQuantityDM::removeRows(QModelIndexList rows, const QModelIndex& ind if (choice == QMessageBox::Ok) { - removeRows(mpGlobalQuantities->getIndex(pModelValue->getObjectName()), 1); + removeRows(index, 1); } } diff --git a/copasi/UI/CQSpecieDM.cpp b/copasi/UI/CQSpecieDM.cpp index b902580f5d..a6347f7c4d 100644 --- a/copasi/UI/CQSpecieDM.cpp +++ b/copasi/UI/CQSpecieDM.cpp @@ -583,23 +583,23 @@ bool CQSpecieDM::removeRows(QModelIndexList rows, const QModelIndex & parent) if (rows.isEmpty()) return false; - //Build the list of pointers to items to be deleted + //Build the list of items to be deleted //before actually deleting any item. - QList Species; - QModelIndexList::const_iterator i; + QList names; + + size_t numSpecies = mpMetabolites->size(); - for (i = rows.begin(); i != rows.end(); ++i) + for (const auto& index : rows) { - if (i->isValid() - && !isDefaultRow(*i)) - Species.append(&mpMetabolites->operator[](i->row())); + if (index.isValid() && !isDefaultRow(index)) + names.append(mpMetabolites->operator[](index.row()).getStringCN()); } - - QList< CMetab * >::const_iterator j; - - for (j = Species.begin(); j != Species.end(); ++j) + + for (auto& objectCn : names) { - CMetab * pSpecies = *j; + CMetab* pSpecies = const_cast< CMetab* >( dynamic_cast (mpMetabolites->getObjectDataModel()->getObject(CRegisteredCommonName(objectCn)))); + if (!pSpecies) + continue; QMessageBox::StandardButton choice = CQMessageBox::confirmDelete(ListViews::ancestor(this), "species", @@ -611,6 +611,6 @@ bool CQSpecieDM::removeRows(QModelIndexList rows, const QModelIndex & parent) removeRows(mpMetabolites->getIndex(pSpecies), 1); } } - + return true; }