diff --git a/avogadro/qtgui/interfacescript.cpp b/avogadro/qtgui/interfacescript.cpp index e0cdecf2d7..d5737cbe70 100644 --- a/avogadro/qtgui/interfacescript.cpp +++ b/avogadro/qtgui/interfacescript.cpp @@ -21,6 +21,8 @@ #include #include +#include + namespace Avogadro::QtGui { using QtGui::GenericHighlighter; @@ -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(parent()), + tr("%1 Message").arg(m_displayName), message); + } + } } return result; } @@ -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(i)); } - json.insert("selectedatoms", selectedList); + json.insert("selectedAtoms", selectedList); // insert the total charge json.insert("charge", mol.totalCharge()); @@ -505,7 +526,11 @@ bool InterfaceScript::insertMolecule(QJsonObject& json, Io::FileFormatManager& formats = Io::FileFormatManager::instance(); QScopedPointer format( formats.newFormatFromFileExtension(m_moleculeExtension.toStdString())); + QScopedPointer 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") @@ -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; }