From 5d0be364499017bfcadcc99e2d33d01a412b4396 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Fri, 16 Feb 2024 20:45:12 -0500 Subject: [PATCH] Fix crash from centroids with empty molecule https://discuss.avogadro.cc/t/the-issue-of-empty-molecule-still-exists-in-avogadro-1-99v/5285 Signed-off-by: Geoff Hutchison --- avogadro/qtplugins/centroid/centroid.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/avogadro/qtplugins/centroid/centroid.cpp b/avogadro/qtplugins/centroid/centroid.cpp index e59ecc9ad3..f87920a826 100644 --- a/avogadro/qtplugins/centroid/centroid.cpp +++ b/avogadro/qtplugins/centroid/centroid.cpp @@ -19,9 +19,10 @@ Centroid::Centroid(QObject* parent_) : Avogadro::QtGui::ExtensionPlugin(parent_), m_centroidAction(new QAction(tr("Add Centroid"), this)), m_comAction(new QAction(tr("Add Center of Mass"), this)), - m_normalAction(new QAction( - tr("Add Perpendicular", "add a point normal to the plane of the molecule"), - this)) + m_normalAction( + new QAction(tr("Add Perpendicular", + "add a point normal to the plane of the molecule"), + this)) { m_centroidAction->setProperty("menu priority", 190); m_comAction->setProperty("menu priority", 180); @@ -52,6 +53,9 @@ void Centroid::setMolecule(QtGui::Molecule* mol) void Centroid::addCentroid() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + if (m_molecule->isSelectionEmpty()) { m_molecule->addAtom(0, m_molecule->centerOfGeometry()); } else { @@ -74,6 +78,9 @@ void Centroid::addCentroid() void Centroid::addCenterOfMass() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + if (m_molecule->isSelectionEmpty()) { m_molecule->addAtom(0, m_molecule->centerOfMass()); } else { @@ -102,6 +109,9 @@ void Centroid::addCenterOfMass() void Centroid::normal() { + if (m_molecule == nullptr || m_molecule->atomCount() == 0) + return; + if (m_molecule->isSelectionEmpty()) { auto pair = m_molecule->bestFitPlane(); m_molecule->addAtom(0.0, pair.second * 2.0);