From d0f84fd74415e8759ef4d768faa4bd86e5ea97fe Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Wed, 29 Sep 2021 14:16:06 -0400 Subject: [PATCH 1/2] Always provide CJSON to interface scripts to enable selection, etc. Signed-off-by: Geoff Hutchison --- avogadro/qtgui/interfacescript.cpp | 63 ++++++++++++++++-------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/avogadro/qtgui/interfacescript.cpp b/avogadro/qtgui/interfacescript.cpp index 2a6cd59c13..44e017768d 100644 --- a/avogadro/qtgui/interfacescript.cpp +++ b/avogadro/qtgui/interfacescript.cpp @@ -163,7 +163,8 @@ bool InterfaceScript::runCommand(const QJsonObject& options_, if (!insertMolecule(allOptions, *mol)) return false; - connect(m_interpreter, &PythonScript::finished, this, &::Avogadro::QtGui::InterfaceScript::commandFinished); + connect(m_interpreter, &PythonScript::finished, this, + &::Avogadro::QtGui::InterfaceScript::commandFinished); m_interpreter->asyncExecute(QStringList() << QStringLiteral("--run-command"), QJsonDocument(allOptions).toJson()); return true; @@ -471,18 +472,20 @@ 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); 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") @@ -499,29 +502,31 @@ bool InterfaceScript::insertMolecule(QJsonObject& json, 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; - } + // And we *always* write the CJSON representation + // Embed CJSON as actual JSON, rather than a string, so we have to parse it again + 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; } @@ -604,7 +609,7 @@ bool InterfaceScript::parseHighlightStyles(const QJsonArray& json) const GenericHighlighter* highlighter( new GenericHighlighter(const_cast(this))); if (!parseRules(rulesArray, *highlighter)) { - qDebug() << "Error parsing style" << styleName << endl + qDebug() << "Error parsing style" << styleName << Qt::endl << QString(QJsonDocument(styleObj).toJson()); highlighter->deleteLater(); result = false; @@ -629,13 +634,13 @@ bool InterfaceScript::parseRules(const QJsonArray& json, QJsonObject ruleObj(ruleVal.toObject()); if (!ruleObj.contains(QStringLiteral("patterns"))) { - qDebug() << "Rule missing 'patterns' array:" << endl + qDebug() << "Rule missing 'patterns' array:" << Qt::endl << QString(QJsonDocument(ruleObj).toJson()); result = false; continue; } if (!ruleObj.value(QStringLiteral("patterns")).isArray()) { - qDebug() << "Rule 'patterns' member is not an array:" << endl + qDebug() << "Rule 'patterns' member is not an array:" << Qt::endl << QString(QJsonDocument(ruleObj).toJson()); result = false; continue; @@ -644,13 +649,13 @@ bool InterfaceScript::parseRules(const QJsonArray& json, ruleObj.value(QStringLiteral("patterns")).toArray()); if (!ruleObj.contains(QStringLiteral("format"))) { - qDebug() << "Rule missing 'format' object:" << endl + qDebug() << "Rule missing 'format' object:" << Qt::endl << QString(QJsonDocument(ruleObj).toJson()); result = false; continue; } if (!ruleObj.value(QStringLiteral("format")).isObject()) { - qDebug() << "Rule 'format' member is not an object:" << endl + qDebug() << "Rule 'format' member is not an object:" << Qt::endl << QString(QJsonDocument(ruleObj).toJson()); result = false; continue; @@ -662,7 +667,7 @@ bool InterfaceScript::parseRules(const QJsonArray& json, foreach (QJsonValue patternVal, patternsArray) { QRegExp pattern; if (!parsePattern(patternVal, pattern)) { - qDebug() << "Error while parsing pattern:" << endl + qDebug() << "Error while parsing pattern:" << Qt::endl << QString(QJsonDocument(patternVal.toObject()).toJson()); result = false; continue; @@ -672,7 +677,7 @@ bool InterfaceScript::parseRules(const QJsonArray& json, QTextCharFormat format; if (!parseFormat(formatObj, format)) { - qDebug() << "Error while parsing format:" << endl + qDebug() << "Error while parsing format:" << Qt::endl << QString(QJsonDocument(formatObj).toJson()); result = false; } From af234bf30d6e2714ce0908cb2a7a49db898a5d6b Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Fri, 24 Nov 2023 11:29:51 -0500 Subject: [PATCH 2/2] Add support for "message" return to pop up a dialog Signed-off-by: Geoff Hutchison --- avogadro/qtgui/interfacescript.cpp | 33 ++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/avogadro/qtgui/interfacescript.cpp b/avogadro/qtgui/interfacescript.cpp index 2e01d793ef..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; } @@ -505,9 +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")); + QScopedPointer cjsonFormat( + formats.newFormatFromFileExtension("cjson")); - // If we want something *other* than CJSON, check that we can supply that format + // 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") @@ -522,12 +545,14 @@ 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))); } - // And we *always* write the CJSON representation - // Embed CJSON as actual JSON, rather than a string, so we have to parse it again + // 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);