| 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 "importpqr.h" |
| 7 |
|
| 8 |
#include "pqrwidget.h" |
| 9 |
|
| 10 |
#include <avogadro/io/fileformat.h> |
| 11 |
#include <avogadro/qtgui/fileformatdialog.h> |
| 12 |
#include <avogadro/qtgui/molecule.h> |
| 13 |
|
| 14 |
#include <QAction> |
| 15 |
#include <QMessageBox> |
| 16 |
|
| 17 |
namespace Avogadro::QtPlugins { |
| 18 |
|
| 19 |
ImportPQR::ImportPQR(QObject* parent_) |
| 20 |
: ExtensionPlugin(parent_), m_action(new QAction(this)), m_molecule(nullptr), |
| 21 |
m_dialog(nullptr), m_outputFormat(nullptr), |
| 22 |
m_manager(new QNetworkAccessManager(this)) |
| 23 |
{ |
| 24 |
m_action->setEnabled(false); |
| 25 |
m_action->setText(tr("&Search PQR…")); |
| 26 |
connect(m_action, SIGNAL(triggered()), SLOT(menuActivated())); |
| 27 |
|
| 28 |
// check if PQR is up |
| 29 |
connect(m_manager, SIGNAL(finished(QNetworkReply*)), |
| 30 |
SLOT(checkAccess(QNetworkReply*))); |
| 31 |
m_manager->get(QNetworkRequest(QUrl("https://pqr.pitt.edu"))); |
| 32 |
} |
| 33 |
|
| 34 |
ImportPQR::~ImportPQR() |
| 35 |
{ |
| 36 |
delete m_outputFormat; |
| 37 |
m_manager->deleteLater(); |
| 38 |
} |
| 39 |
|
| 40 |
void ImportPQR::checkAccess(QNetworkReply* reply) |
| 41 |
{ |
| 42 |
// only enable if we can access the site |
| 43 |
if (reply->error() == QNetworkReply::NoError) { |
| 44 |
m_action->setEnabled(true); |
| 45 |
} |
| 46 |
reply->deleteLater(); |
| 47 |
} |
| 48 |
|
| 49 |
QList<QAction*> ImportPQR::actions() const |
| 50 |
{ |
| 51 |
QList<QAction*> actions_; |
| 52 |
actions_.append(m_action); |
| 53 |
return actions_; |
| 54 |
} |
| 55 |
|
| 56 |
QStringList ImportPQR::menuPath(QAction*) const |
| 57 |
{ |
| 58 |
QStringList path; |
| 59 |
path << tr("&File") << tr("&Import"); |
| 60 |
return path; |
| 61 |
} |
| 62 |
|
| 63 |
void ImportPQR::setMolecule(QtGui::Molecule* mol) |
| 64 |
{ |
| 65 |
m_molecule = mol; |
| 66 |
} |
| 67 |
|
| 68 |
bool ImportPQR::readMolecule(QtGui::Molecule& mol) |
| 69 |
{ |
| 70 |
bool readOK = Io::FileFormatManager::instance().readString( |
| 71 |
mol, m_moleculeData.data(), "mol2"); |
| 72 |
|
| 73 |
if (readOK) // worked, so set the filename |
| 74 |
mol.setData("name", m_moleculeName.toStdString()); |
| 75 |
|
| 76 |
return readOK; |
| 77 |
} |
| 78 |
|
| 79 |
void ImportPQR::menuActivated() |
| 80 |
{ |
| 81 |
if (!m_dialog) |
| 82 |
m_dialog = new PQRWidget(qobject_cast<QWidget*>(this), this); |
| 83 |
|
| 84 |
m_dialog->show(); |
| 85 |
m_dialog->raise(); |
| 86 |
m_dialog->activateWindow(); |
| 87 |
} |
| 88 |
|
| 89 |
// called by widget |
| 90 |
void ImportPQR::setMoleculeData(QByteArray& molData, QString name) |
| 91 |
{ |
| 92 |
m_moleculeName = name; |
| 93 |
m_moleculeData = molData; |
| 94 |
|
| 95 |
m_dialog->hide(); |
| 96 |
emit moleculeReady(1); |
| 97 |
} |
| 98 |
} // namespace Avogadro::QtPlugins |