Skip to content

Commit

Permalink
Initial work to fix potential crash on Windows
Browse files Browse the repository at this point in the history
Also make sure BABEL_DATADIR and BABEL_LIBDIR are set

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Nov 18, 2024
1 parent 857f011 commit a38783f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
3 changes: 1 addition & 2 deletions avogadro/qtplugins/forcefield/forcefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
43 changes: 34 additions & 9 deletions avogadro/qtplugins/forcefield/obenergy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a38783f

Please sign in to comment.