diff --git a/avogadro/qtplugins/forcefield/forcefield.cpp b/avogadro/qtplugins/forcefield/forcefield.cpp index f1545b95a8..e78095fbe2 100644 --- a/avogadro/qtplugins/forcefield/forcefield.cpp +++ b/avogadro/qtplugins/forcefield/forcefield.cpp @@ -195,11 +195,10 @@ void Forcefield::showDialog() void Forcefield::setMolecule(QtGui::Molecule* mol) { - if (m_molecule == mol) + if (mol == nullptr || m_molecule == mol) return; m_molecule = mol; - setupMethod(); } diff --git a/avogadro/qtplugins/forcefield/obenergy.cpp b/avogadro/qtplugins/forcefield/obenergy.cpp index d832637dea..03cf1d6dd4 100644 --- a/avogadro/qtplugins/forcefield/obenergy.cpp +++ b/avogadro/qtplugins/forcefield/obenergy.cpp @@ -50,15 +50,40 @@ OBEnergy::OBEnergy(const std::string& method) QByteArray dataDir = QString(QCoreApplication::applicationDirPath() + "/data").toLocal8Bit(); qputenv("BABEL_DATADIR", dataDir); -#elif defined(__APPLE__) - QByteArray dataDir = - QString(QCoreApplication::applicationDirPath() + "/../share/openbabel") - .toLocal8Bit(); - qputenv("BABEL_DATADIR", dataDir); - QByteArray libDir = - QString(QCoreApplication::applicationDirPath() + "/../lib/openbabel") - .toLocal8Bit(); - qputenv("BABEL_LIBDIR", libDir); +#else + // check if BABEL_DATADIR is set in the environment + QStringList filters; + filters << "3.*" + << "2.*"; + if (qgetenv("BABEL_DATADIR").isEmpty()) { + QDir dir(QCoreApplication::applicationDirPath() + "/../share/openbabel"); + QStringList dirs = dir.entryList(filters); + if (dirs.size() == 1) { + // versioned data directory + QString dataDir = QCoreApplication::applicationDirPath() + + "/../share/openbabel/" + dirs[0]; + qputenv("BABEL_DATADIR", dataDir.toLocal8Bit()); + } else { + qDebug() << "Error, Open Babel data directory not found."; + } + } + + // Check if BABEL_LIBDIR is set + if (qgetenv("BABEL_LIBDIR").isEmpty()) { + QDir dir(QCoreApplication::applicationDirPath() + "/../lib/openbabel"); + QStringList dirs = dir.entryList(filters); + if (dirs.size() == 0) { + QString libDir = + QCoreApplication::applicationDirPath() + "/../lib/openbabel/"; + qputenv("BABEL_LIBDIR", libDir.toLocal8Bit()); + } else if (dirs.size() == 1) { + QString libDir = + QCoreApplication::applicationDirPath() + "/../lib/openbabel/" + dirs[0]; + qputenv("BABEL_LIBDIR", libDir.toLocal8Bit()); + } else { + qDebug() << "Error, Open Babel plugins directory not found."; + } + } #endif // Ensure the plugins are loaded OBPlugin::LoadAllPlugins();