| 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 "networkdatabases.h" |
| 7 |
|
| 8 |
#include <avogadro/io/fileformatmanager.h> |
| 9 |
#include <avogadro/qtgui/molecule.h> |
| 10 |
|
| 11 |
#include <QtNetwork/QNetworkAccessManager> |
| 12 |
#include <QtNetwork/QNetworkReply> |
| 13 |
#include <QAction> |
| 14 |
#include <QtWidgets/QInputDialog> |
| 15 |
#include <QtWidgets/QMessageBox> |
| 16 |
#include <QtWidgets/QProgressDialog> |
| 17 |
|
| 18 |
namespace Avogadro::QtPlugins { |
| 19 |
|
| 20 |
NetworkDatabases::NetworkDatabases(QObject* parent_) |
| 21 |
: ExtensionPlugin(parent_), m_action(new QAction(this)), m_molecule(nullptr), |
| 22 |
m_network(nullptr), m_progressDialog(nullptr) |
| 23 |
{ |
| 24 |
m_action->setEnabled(true); |
| 25 |
m_action->setText("Download by &Nameā¦"); |
| 26 |
m_action->setProperty("menu priority", 190); |
| 27 |
connect(m_action, SIGNAL(triggered()), SLOT(showDialog())); |
| 28 |
} |
| 29 |
|
| 30 |
NetworkDatabases::~NetworkDatabases() {} |
| 31 |
|
| 32 |
QList<QAction*> NetworkDatabases::actions() const |
| 33 |
{ |
| 34 |
return QList<QAction*>() << m_action; |
| 35 |
} |
| 36 |
|
| 37 |
QStringList NetworkDatabases::menuPath(QAction*) const |
| 38 |
{ |
| 39 |
return QStringList() << tr("&File") << tr("&Import"); |
| 40 |
} |
| 41 |
|
| 42 |
void NetworkDatabases::setMolecule(QtGui::Molecule* mol) |
| 43 |
{ |
| 44 |
m_molecule = mol; |
| 45 |
} |
| 46 |
|
| 47 |
bool NetworkDatabases::readMolecule(QtGui::Molecule& mol) |
| 48 |
{ |
| 49 |
if (m_moleculeData.isEmpty() || m_moleculeName.isEmpty()) |
| 50 |
return false; |
| 51 |
|
| 52 |
bool readOK = Io::FileFormatManager::instance().readString( |
| 53 |
mol, m_moleculeData.data(), "sdf"); |
| 54 |
if (readOK) // worked, so set the filename |
| 55 |
mol.setData("name", m_moleculeName.toStdString()); |
| 56 |
|
| 57 |
return readOK; |
| 58 |
} |
| 59 |
|
| 60 |
void NetworkDatabases::showDialog() |
| 61 |
{ |
| 62 |
if (!m_network) { |
| 63 |
m_network = new QNetworkAccessManager(this); |
| 64 |
connect(m_network, SIGNAL(finished(QNetworkReply*)), this, |
| 65 |
SLOT(replyFinished(QNetworkReply*))); |
| 66 |
} |
| 67 |
// Prompt for a chemical structure name |
| 68 |
bool ok; |
| 69 |
QString structureName = QInputDialog::getText( |
| 70 |
qobject_cast<QWidget*>(parent()), tr("Chemical Name"), |
| 71 |
tr("Chemical structure to download."), QLineEdit::Normal, "", &ok); |
| 72 |
|
| 73 |
if (!ok || structureName.isEmpty()) |
| 74 |
return; |
| 75 |
|
| 76 |
// Hard coding the NIH resolver download URL - this could be used for other |
| 77 |
// services |
| 78 |
m_network->get( |
| 79 |
QNetworkRequest(QUrl("https://cactus.nci.nih.gov/chemical/structure/" + |
| 80 |
structureName + "/file?format=sdf&get3d=true"))); |
| 81 |
|
| 82 |
m_moleculeName = structureName; |
| 83 |
if (!m_progressDialog) { |
| 84 |
m_progressDialog = new QProgressDialog(qobject_cast<QWidget*>(parent())); |
| 85 |
} |
| 86 |
m_progressDialog->setLabelText(tr("Querying for %1").arg(structureName)); |
| 87 |
m_progressDialog->setRange(0, 0); |
| 88 |
m_progressDialog->show(); |
| 89 |
} |
| 90 |
|
| 91 |
void NetworkDatabases::replyFinished(QNetworkReply* reply) |
| 92 |
{ |
| 93 |
m_progressDialog->hide(); |
| 94 |
// Read in all the data |
| 95 |
if (!reply->isReadable()) { |
| 96 |
QMessageBox::warning(qobject_cast<QWidget*>(parent()), |
| 97 |
tr("Network Download Failed"), |
| 98 |
tr("Network timeout or other error.")); |
| 99 |
reply->deleteLater(); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
m_moleculeData = reply->readAll(); |
| 104 |
// Check if the file was successfully downloaded |
| 105 |
if (m_moleculeData.contains("Error report") || |
| 106 |
m_moleculeData.contains("Page not found (404)")) { |
| 107 |
QMessageBox::warning( |
| 108 |
qobject_cast<QWidget*>(parent()), tr("Network Download Failed"), |
| 109 |
tr("Specified molecule could not be found: %1").arg(m_moleculeName)); |
| 110 |
reply->deleteLater(); |
| 111 |
return; |
| 112 |
} |
| 113 |
emit moleculeReady(1); |
| 114 |
reply->deleteLater(); |
| 115 |
} |
| 116 |
} // namespace Avogadro::QtPlugins |