Skip to content

Commit

Permalink
Merge pull request #1465 from ghutchis/always-supply-cjson-scripts
Browse files Browse the repository at this point in the history
Always supply cjson to scripts
  • Loading branch information
ghutchis authored Nov 24, 2023
2 parents 02e2d5a + af234bf commit 2e59b64
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions avogadro/qtgui/interfacescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QtCore/QJsonArray>
#include <QtCore/QJsonDocument>

#include <QtWidgets/QMessageBox>

namespace Avogadro::QtGui {

using QtGui::GenericHighlighter;
Expand Down Expand Up @@ -271,6 +273,25 @@ bool InterfaceScript::processCommand(Core::Molecule* mol)
}
guiMol->emitChanged(Molecule::Atoms);
}

// check if there are messages for the user
if (obj.contains("message")) {
QString message;

if (obj["message"].isString())
message = obj["message"].toString();
else if (obj["message"].isArray()) {
QJsonArray messageList = obj["message"].toArray();
for (int i = 0; i < messageList.size(); ++i) {
if (messageList[i].isString())
message += messageList[i].toString() + "\n";
}
}
if (!message.isEmpty()) {
QMessageBox::information(qobject_cast<QWidget*>(parent()),
tr("%1 Message").arg(m_displayName), message);
}
}
}
return result;
}
Expand Down Expand Up @@ -488,13 +509,13 @@ bool InterfaceScript::insertMolecule(QJsonObject& json,
if (m_moleculeExtension == QLatin1String("None"))
return true;

// insert the selected atoms
// Always insert the selected atoms
QJsonArray selectedList;
for (Index i = 0; i < mol.atomCount(); ++i) {
if (mol.atomSelected(i))
selectedList.append(static_cast<qint64>(i));
}
json.insert("selectedatoms", selectedList);
json.insert("selectedAtoms", selectedList);

// insert the total charge
json.insert("charge", mol.totalCharge());
Expand All @@ -505,7 +526,11 @@ bool InterfaceScript::insertMolecule(QJsonObject& json,
Io::FileFormatManager& formats = Io::FileFormatManager::instance();
QScopedPointer<Io::FileFormat> format(
formats.newFormatFromFileExtension(m_moleculeExtension.toStdString()));
QScopedPointer<Io::FileFormat> cjsonFormat(
formats.newFormatFromFileExtension("cjson"));

// If we want something *other* than CJSON, check that we can supply that
// format
if (format.isNull()) {
m_errors << tr("Error writing molecule representation to string: "
"Unrecognized file format: %1")
Expand All @@ -520,31 +545,35 @@ bool InterfaceScript::insertMolecule(QJsonObject& json,
return false;
}

// if we need a different format, insert it
if (m_moleculeExtension != QLatin1String("cjson")) {
json.insert(m_moleculeExtension, QJsonValue(QString::fromStdString(str)));
} else {
// If cjson was requested, embed the actual JSON, rather than the string.
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(str.c_str(), &error);
if (error.error != QJsonParseError::NoError) {
m_errors << tr("Error generating cjson object: Parse error at offset %1: "
"%2\nRaw JSON:\n\n%3")
.arg(error.offset)
.arg(error.errorString())
.arg(QString::fromStdString(str));
return false;
}
}

if (!doc.isObject()) {
m_errors << tr("Error generator cjson object: Parsed JSON is not an "
"object:\n%1")
.arg(QString::fromStdString(str));
return false;
}
// We will *always* write the CJSON representation
// Embed CJSON as actual JSON, rather than a string,
// .. so we'll have to re-parse it
cjsonFormat->writeString(str, mol);
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(str.c_str(), &error);
if (error.error != QJsonParseError::NoError) {
m_errors << tr("Error generating cjson object: Parse error at offset %1: "
"%2\nRaw JSON:\n\n%3")
.arg(error.offset)
.arg(error.errorString())
.arg(QString::fromStdString(str));
return false;
}

json.insert(m_moleculeExtension, doc.object());
if (!doc.isObject()) {
m_errors << tr("Error generator cjson object: Parsed JSON is not an "
"object:\n%1")
.arg(QString::fromStdString(str));
return false;
}

json.insert("cjson", doc.object());

return true;
}

Expand Down

0 comments on commit 2e59b64

Please sign in to comment.