Skip to content

Commit

Permalink
- issue 3276: ensure that index.row is valid, avoid duplicated defau…
Browse files Browse the repository at this point in the history
…lt rows
  • Loading branch information
fbergmann committed Feb 3, 2025
1 parent b465ead commit 997c47c
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 29 deletions.
6 changes: 3 additions & 3 deletions copasi/UI/CQCompartmentDM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -376,10 +376,10 @@ bool CQCompartmentDM::removeRows(QModelIndexList rows, const QModelIndex& index)
// Build the list of items to be deleted
// before actually deleting any item.
QList <std::string> names;

for (const auto& index : rows)
if (index.isValid()
&& !isDefaultRow(index))
&& !isDefaultRow(index) && index.row() < (int) mpCompartments->size())
{
names.append(mpCompartments->operator[](index.row()).getObjectName());
}
Expand Down
3 changes: 2 additions & 1 deletion copasi/UI/CQCompartmentsWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -119,6 +119,7 @@ void CQCompartmentsWidget::deleteSelectedCompartments()
{return;}

mpCompartmentDM->removeRows(mappedSelRows);
enterProtected();
}

void CQCompartmentsWidget::slotBtnClearClicked()
Expand Down
4 changes: 2 additions & 2 deletions copasi/UI/CQEventDM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -312,7 +312,7 @@ bool CQEventDM::removeRows(QModelIndexList rows, const QModelIndex& index)

for (i = rows.begin(); i != rows.end(); ++i)
if (i->isValid()
&& !isDefaultRow(*i))
&& !isDefaultRow(*i) && i->row() < (int) mpEvents->size())
{
Events.append(&mpEvents->operator[](i->row()));
}
Expand Down
4 changes: 2 additions & 2 deletions copasi/UI/CQEventsWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -118,6 +118,7 @@ void CQEventsWidget::deleteSelectedEvents()
{return;}

mpEventDM->removeRows(mappedSelRows);
enterProtected();
}

void CQEventsWidget::slotBtnClearClicked()
Expand Down Expand Up @@ -188,7 +189,6 @@ void CQEventsWidget::dataChanged(const QModelIndex &C_UNUSED(topLeft),
{
mpTblEvents->resizeColumnsToContents();
}

}

void CQEventsWidget::slotDoubleClicked(const QModelIndex proxyIndex)
Expand Down
4 changes: 2 additions & 2 deletions copasi/UI/CQGlobalQuantitiesWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -118,6 +118,7 @@ void CQGlobalQuantitiesWidget::deleteSelectedGlobalQuantities()
{return;}

mpGlobalQuantityDM->removeRows(mappedSelRows);
enterProtected();
}

void CQGlobalQuantitiesWidget::slotBtnClearClicked()
Expand All @@ -129,7 +130,6 @@ void CQGlobalQuantitiesWidget::slotBtnClearClicked()
{
mpGlobalQuantityDM->clear();
}

}

bool CQGlobalQuantitiesWidget::updateProtected(ListViews::ObjectType objectType, ListViews::Action action, const CRegisteredCommonName & cn)
Expand Down
5 changes: 2 additions & 3 deletions copasi/UI/CQGlobalQuantityDM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -378,12 +378,11 @@ bool CQGlobalQuantityDM::removeRows(QModelIndexList rows, const QModelIndex& ind

for (i = rows.begin(); i != rows.end(); ++i)
if (i->isValid()
&& !isDefaultRow(*i))
&& !isDefaultRow(*i) && i->row() < (int) mpGlobalQuantities->size())
{
names.append(mpGlobalQuantities->operator[](i->row()).getObjectName());
}


for (auto& objName : names)
{
auto index = mpGlobalQuantities->getIndex(objName);
Expand Down
4 changes: 2 additions & 2 deletions copasi/UI/CQReactionDM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -323,7 +323,7 @@ bool CQReactionDM::removeRows(QModelIndexList rows, const QModelIndex& index)
for (i = rows.begin(); i != rows.end(); ++i)
{
if (i->isValid()
&& !isDefaultRow(*i))
&& !isDefaultRow(*i) && i->row() < (int) mpReactions->size())
Reactions.append(&mpReactions->operator[](i->row()));
}

Expand Down
6 changes: 2 additions & 4 deletions copasi/UI/CQReactionsWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -99,7 +99,6 @@ void CQReactionsWidget::slotBtnDeleteClicked(bool needFocus)
{
if (!needFocus || mpTblReactions->hasFocus())
{deleteSelectedReactions();}

}

void CQReactionsWidget::slotBtnClearClicked()
Expand All @@ -111,7 +110,6 @@ void CQReactionsWidget::slotBtnClearClicked()
{
mpReactionDM->clear();
}

}

bool CQReactionsWidget::updateProtected(ListViews::ObjectType objectType, ListViews::Action action, const CRegisteredCommonName & cn)
Expand Down Expand Up @@ -162,7 +160,6 @@ bool CQReactionsWidget::enterProtected()
return true;
}


void CQReactionsWidget::dataChanged(const QModelIndex &C_UNUSED(topLeft),
const QModelIndex &C_UNUSED(bottomRight))
{
Expand Down Expand Up @@ -282,4 +279,5 @@ void CQReactionsWidget::deleteSelectedReactions()
{return;}

mpReactionDM->removeRows(mappedSelRows);
enterProtected();
}
12 changes: 6 additions & 6 deletions copasi/UI/CQSpecieDM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -586,18 +586,18 @@ bool CQSpecieDM::removeRows(QModelIndexList rows, const QModelIndex & parent)
//Build the list of items to be deleted
//before actually deleting any item.
QList <std::string> names;

size_t numSpecies = mpMetabolites->size();

for (const auto& index : rows)
{
if (index.isValid() && !isDefaultRow(index))
if (index.isValid() && !isDefaultRow(index) && index.row() < (int) mpMetabolites->size())
names.append(mpMetabolites->operator[](index.row()).getStringCN());
}

for (auto& objectCn : names)
{
CMetab* pSpecies = const_cast< CMetab* >( dynamic_cast<const CMetab*> (mpMetabolites->getObjectDataModel()->getObject(CRegisteredCommonName(objectCn))));
CMetab* pSpecies = const_cast< CMetab* >(dynamic_cast<const CMetab*> (mpMetabolites->getObjectDataModel()->getObject(CRegisteredCommonName(objectCn))));
if (!pSpecies)
continue;

Expand All @@ -611,6 +611,6 @@ bool CQSpecieDM::removeRows(QModelIndexList rows, const QModelIndex & parent)
removeRows(mpMetabolites->getIndex(pSpecies), 1);
}
}

return true;
}
6 changes: 2 additions & 4 deletions copasi/UI/CQSpeciesWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
// Copyright (C) 2019 - 2025 by Pedro Mendes, Rector and Visitors of the
// University of Virginia, University of Heidelberg, and University
// of Connecticut School of Medicine.
// All rights reserved.
Expand Down Expand Up @@ -106,7 +106,6 @@ void CQSpeciesWidget::slotBtnDeleteClicked(bool needFocus)
{
if (!needFocus || mpTblSpecies->hasFocus())
{deleteSelectedSpecies();}

}

void CQSpeciesWidget::deleteSelectedSpecies()
Expand All @@ -127,6 +126,7 @@ void CQSpeciesWidget::deleteSelectedSpecies()
{return;}

mpSpecieDM->removeRows(mappedSelRows);
enterProtected();
}

void CQSpeciesWidget::slotBtnClearClicked()
Expand All @@ -138,7 +138,6 @@ void CQSpeciesWidget::slotBtnClearClicked()
{
mpSpecieDM->clear();
}

}

bool CQSpeciesWidget::updateProtected(ListViews::ObjectType objectType, ListViews::Action action, const CRegisteredCommonName & cn)
Expand Down Expand Up @@ -189,7 +188,6 @@ bool CQSpeciesWidget::enterProtected()
return true;
}


void CQSpeciesWidget::dataChanged(const QModelIndex &C_UNUSED(topLeft),
const QModelIndex &C_UNUSED(bottomRight))
{
Expand Down

0 comments on commit 997c47c

Please sign in to comment.