| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "molecularproperties.h" |
| 7 |
#include "molecularmodel.h" |
| 8 |
#include "molecularview.h" |
| 9 |
|
| 10 |
#include <avogadro/io/fileformatmanager.h> |
| 11 |
#include <avogadro/qtgui/molecule.h> |
| 12 |
#include <avogadro/qtgui/richtextdelegate.h> |
| 13 |
|
| 14 |
#include <QAction> |
| 15 |
#include <QStringList> |
| 16 |
#include <QtCore/QRegularExpression> |
| 17 |
#include <QtCore/QJsonArray> |
| 18 |
#include <QtCore/QJsonDocument> |
| 19 |
#include <QtCore/QJsonObject> |
| 20 |
#include <QtCore/QJsonValue> |
| 21 |
#include <QtNetwork/QNetworkAccessManager> |
| 22 |
#include <QtNetwork/QNetworkReply> |
| 23 |
#include <QtWidgets/QDialog> |
| 24 |
#include <QtWidgets/QHeaderView> |
| 25 |
#include <QtWidgets/QScrollBar> |
| 26 |
#include <QtWidgets/QVBoxLayout> |
| 27 |
|
| 28 |
using Avogadro::QtGui::RichTextDelegate; |
| 29 |
|
| 30 |
namespace Avogadro::QtPlugins { |
| 31 |
|
| 32 |
MolecularProperties::MolecularProperties(QObject* parent_) |
| 33 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_action(new QAction(this)), |
| 34 |
m_molecule(nullptr), m_dialog(nullptr), m_model(nullptr), |
| 35 |
m_network(new QNetworkAccessManager(this)) |
| 36 |
{ |
| 37 |
m_action->setEnabled(true); |
| 38 |
m_action->setText(tr("&Molecular…")); |
| 39 |
m_action->setProperty("menu priority", 990); |
| 40 |
|
| 41 |
connect(m_action, SIGNAL(triggered()), SLOT(showDialog())); |
| 42 |
connect(m_network, SIGNAL(finished(QNetworkReply*)), this, |
| 43 |
SLOT(updateNameReady(QNetworkReply*))); |
| 44 |
} |
| 45 |
|
| 46 |
MolecularProperties::~MolecularProperties() {} |
| 47 |
|
| 48 |
QString MolecularProperties::description() const |
| 49 |
{ |
| 50 |
return tr("View general properties of a molecule."); |
| 51 |
} |
| 52 |
|
| 53 |
QList<QAction*> MolecularProperties::actions() const |
| 54 |
{ |
| 55 |
return QList<QAction*>() << m_action; |
| 56 |
} |
| 57 |
|
| 58 |
QStringList MolecularProperties::menuPath(QAction*) const |
| 59 |
{ |
| 60 |
return QStringList() << tr("&Analyze") << tr("&Properties"); |
| 61 |
} |
| 62 |
|
| 63 |
void MolecularProperties::setMolecule(QtGui::Molecule* mol) |
| 64 |
{ |
| 65 |
if (mol == m_molecule) |
| 66 |
return; |
| 67 |
|
| 68 |
// Cancel any pending network request to avoid applying the wrong name |
| 69 |
if (m_nameRequestPending && m_pendingReply != nullptr) { |
| 70 |
m_pendingReply->abort(); |
| 71 |
m_pendingReply = nullptr; |
| 72 |
m_nameRequestPending = false; |
| 73 |
} |
| 74 |
|
| 75 |
// Disconnect from the old molecule if set |
| 76 |
if (m_molecule) { |
| 77 |
disconnect(m_molecule, &QtGui::Molecule::changed, this, |
| 78 |
&MolecularProperties::updateName); |
| 79 |
} |
| 80 |
|
| 81 |
m_molecule = mol; |
| 82 |
|
| 83 |
if (m_molecule) { |
| 84 |
if (m_model) |
| 85 |
m_model->setMolecule(m_molecule); |
| 86 |
|
| 87 |
connect(m_molecule, &QtGui::Molecule::changed, this, |
| 88 |
&MolecularProperties::updateName); |
| 89 |
updateName(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void MolecularProperties::updateName(bool checkDialog) |
| 94 |
{ |
| 95 |
if (!m_molecule || m_molecule->atomCount() == 0) |
| 96 |
return; |
| 97 |
|
| 98 |
// only update the name if the dialog is open |
| 99 |
if (checkDialog && (m_dialog == nullptr || !m_dialog->isVisible())) |
| 100 |
return; |
| 101 |
|
| 102 |
// don't send multiple requests |
| 103 |
if (m_nameRequestPending) |
| 104 |
return; |
| 105 |
|
| 106 |
m_nameRequestPending = true; |
| 107 |
|
| 108 |
std::string smiles; |
| 109 |
Io::FileFormatManager::instance().writeString(*m_molecule, smiles, "smi"); |
| 110 |
QString smilesString = QString::fromStdString(smiles); |
| 111 |
smilesString.remove(QRegularExpression("\\s+.*")); |
| 112 |
QString requestURL = |
| 113 |
QString("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/" + |
| 114 |
QUrl::toPercentEncoding(smilesString) + "/json"); |
| 115 |
m_pendingReply = m_network->get(QNetworkRequest(QUrl(requestURL))); |
| 116 |
} |
| 117 |
|
| 118 |
void MolecularProperties::updateNameReady(QNetworkReply* reply) |
| 119 |
{ |
| 120 |
m_nameRequestPending = false; |
| 121 |
m_pendingReply = nullptr; |
| 122 |
|
| 123 |
// Check if request was aborted or had network error |
| 124 |
if (reply->error() != QNetworkReply::NoError || !reply->isReadable()) { |
| 125 |
reply->deleteLater(); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Read in all the data |
| 130 |
// and check if the data came through |
| 131 |
QByteArray data = reply->readAll(); |
| 132 |
if (data.contains("Error report") || data.contains("<h1>")) { |
| 133 |
reply->deleteLater(); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// parse the JSON |
| 138 |
// https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/…/json |
| 139 |
// PC_Compounds[0].props |
| 140 |
// iterate // get "urn" / "name" == "Markup" and "Preferred" |
| 141 |
// .. get "value" / "sval" |
| 142 |
|
| 143 |
QJsonDocument doc = QJsonDocument::fromJson(data); |
| 144 |
QJsonObject obj = doc.object(); |
| 145 |
QJsonArray array = obj["PC_Compounds"].toArray(); |
| 146 |
if (array.isEmpty()) { |
| 147 |
reply->deleteLater(); |
| 148 |
return; |
| 149 |
} |
| 150 |
obj = array.first().toObject(); |
| 151 |
array = obj["props"].toArray(); // props is an array of objects |
| 152 |
for (const QJsonValue& value : array) { |
| 153 |
obj = value.toObject(); |
| 154 |
QJsonObject urn = obj["urn"].toObject(); |
| 155 |
|
| 156 |
if (urn["name"].toString() == "Preferred") { |
| 157 |
// save this text version for files and copy/paste |
| 158 |
QJsonObject nameValue = obj["value"].toObject(); |
| 159 |
if (m_molecule != nullptr) |
| 160 |
m_molecule->setData("name", nameValue["sval"].toString().toStdString()); |
| 161 |
} |
| 162 |
if (urn["name"].toString() == "Markup") { |
| 163 |
// HTML version for dialog |
| 164 |
QJsonObject nameValue = obj["value"].toObject(); |
| 165 |
if (m_molecule != nullptr) |
| 166 |
m_molecule->setData("markup_name", |
| 167 |
nameValue["sval"].toString().toStdString()); |
| 168 |
} |
| 169 |
} |
| 170 |
// tell the model to update |
| 171 |
if (m_model != nullptr) |
| 172 |
m_model->dataChanged(m_model->index(0, 0), m_model->index(0, 0)); |
| 173 |
|
| 174 |
reply->deleteLater(); |
| 175 |
} |
| 176 |
|
| 177 |
void MolecularProperties::showDialog() |
| 178 |
{ |
| 179 |
if (m_dialog == nullptr) { |
| 180 |
// create it |
| 181 |
// copied from the propeties dialog |
| 182 |
m_dialog = new QDialog(qobject_cast<QWidget*>(parent())); |
| 183 |
auto* layout = new QVBoxLayout(m_dialog); |
| 184 |
m_dialog->setLayout(layout); |
| 185 |
// Don't show whitespace around the table view |
| 186 |
layout->setSpacing(0); |
| 187 |
layout->setContentsMargins(0, 0, 0, 0); |
| 188 |
|
| 189 |
m_model = new MolecularModel(m_dialog); |
| 190 |
m_model->setMolecule(m_molecule); |
| 191 |
// view will delete itself & model using deleteLater() |
| 192 |
auto* view = new MolecularView(m_dialog); |
| 193 |
view->setMolecule(m_molecule); |
| 194 |
view->setSourceModel(m_model); |
| 195 |
view->setModel(m_model); |
| 196 |
|
| 197 |
// set the headers to true |
| 198 |
QFont font = view->horizontalHeader()->font(); |
| 199 |
font.setBold(true); |
| 200 |
view->horizontalHeader()->setFont(font); |
| 201 |
view->verticalHeader()->setFont(font); |
| 202 |
|
| 203 |
view->setItemDelegateForColumn(0, new RichTextDelegate(view)); |
| 204 |
|
| 205 |
view->horizontalHeader()->setStretchLastSection(true); |
| 206 |
view->resizeColumnsToContents(); |
| 207 |
|
| 208 |
layout->addWidget(view); |
| 209 |
|
| 210 |
m_dialog->setWindowTitle(view->windowTitle()); |
| 211 |
m_dialog->setWindowFlags(Qt::Window); |
| 212 |
} |
| 213 |
|
| 214 |
// update the name before we show the dialog |
| 215 |
if (m_molecule != nullptr) |
| 216 |
updateName(false); // we don't want to check the dialog |
| 217 |
|
| 218 |
m_dialog->show(); |
| 219 |
m_dialog->raise(); |
| 220 |
m_dialog->activateWindow(); |
| 221 |
} |
| 222 |
|
| 223 |
} // namespace Avogadro::QtPlugins |