Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always supply cjson to scripts #1465

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading