Debian Codegraph @

avogadro/qtplugins/apbs/apbs.cpp

raw
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 "apbs.h"
7
8 #include "apbsdialog.h"
9 #include "opendxreader.h"
10
11 #include <avogadro/core/cube.h>
12 #include <avogadro/core/mesh.h>
13 #include <avogadro/io/fileformatmanager.h>
14 #include <avogadro/qtgui/meshgenerator.h>
15 #include <avogadro/qtgui/molecule.h>
16
17 #include <QAction>
18 #include <QApplication>
19 #include <QFileDialog>
20 #include <QMessageBox>
21 #include <QProgressDialog>
22
23 namespace Avogadro::QtPlugins {
24
25 using Core::Mesh;
26
27 Apbs::Apbs(QObject* parent_)
28 : QtGui::ExtensionPlugin(parent_), m_molecule(nullptr),
29 m_progressDialog(nullptr), m_dialog(nullptr)
30 {
31 auto* action = new QAction(this);
32 action->setText(tr("Run APBS…"));
33 connect(action, SIGNAL(triggered()), this, SLOT(onRunApbs()));
34 m_actions.append(action);
35
36 action = new QAction(this);
37 action->setText(tr("Open Output File…"));
38 connect(action, SIGNAL(triggered()), this, SLOT(onOpenOutputFile()));
39 m_actions.append(action);
40 }
41
42 Apbs::~Apbs()
43 {
44 delete m_dialog;
45 delete m_progressDialog;
46 }
47
48 QStringList Apbs::menuPath(QAction*) const
49 {
50 return QStringList() << tr("&Input") << tr("&APBS");
51 }
52
53 void Apbs::setMolecule(QtGui::Molecule* mol)
54 {
55 if (mol != m_molecule)
56 m_molecule = mol;
57 }
58
59 void Apbs::onOpenOutputFile()
60 {
61 QString fileName = QFileDialog::getOpenFileName(
62 qobject_cast<QWidget*>(parent()), tr("Open Output File"), QString(),
63 tr("OpenDX File (*.dx)"));
64 if (fileName.isEmpty())
65 return;
66
67 if (!m_molecule)
68 return;
69
70 loadOpenDxFile(fileName, *m_molecule);
71 }
72
73 void Apbs::meshGeneratorFinished()
74 {
75 auto* generator = qobject_cast<QtGui::MeshGenerator*>(sender());
76 if (!generator) {
77 return;
78 }
79
80 // delete the generator
81 generator->deleteLater();
82
83 m_progressDialog->setValue(m_progressDialog->maximum());
84 m_progressDialog->hide();
85 }
86
87 void Apbs::onMeshGeneratorProgress(int value)
88 {
89 m_progressDialog->setValue(value);
90 qApp->processEvents();
91 }
92
93 void Apbs::onRunApbs()
94 {
95 if (!m_dialog)
96 m_dialog = new ApbsDialog(qobject_cast<QWidget*>(parent()));
97
98 m_dialog->setMolecule(m_molecule);
99 int code = m_dialog->exec();
100 m_dialog->hide();
101 if (code == QDialog::Accepted) {
102 m_pqrFileName = m_dialog->pqrFileName();
103 m_cubeFileName = m_dialog->cubeFileName();
104
105 emit moleculeReady(1);
106 }
107 }
108
109 bool Apbs::readMolecule(QtGui::Molecule& molecule)
110 {
111 bool ok = Io::FileFormatManager::instance().readFile(
112 molecule, m_pqrFileName.toStdString());
113 if (!ok) {
114 QMessageBox::critical(
115 qobject_cast<QWidget*>(parent()), tr("IO Error"),
116 tr("Error reading structure file (%1).").arg(m_pqrFileName));
117 return false;
118 }
119
120 if (!m_cubeFileName.isEmpty()) {
121 // load the cube file and generate meshes
122 ok = loadOpenDxFile(m_cubeFileName, molecule);
123 if (!ok)
124 return false;
125 }
126
127 return true;
128 }
129
130 bool Apbs::loadOpenDxFile(const QString& fileName, QtGui::Molecule& molecule)
131 {
132 OpenDxReader reader;
133 bool ok = reader.readFile(fileName);
134 if (!ok) {
135 QMessageBox::critical(
136 qobject_cast<QWidget*>(parent()), tr("OpenDX Error"),
137 tr("Error reading OpenDX file: %1").arg(reader.errorString()));
138 } else {
139 const Core::Cube* cube = reader.cube();
140
141 if (!cube) {
142 QMessageBox::critical(qobject_cast<QWidget*>(parent()),
143 tr("OpenDX Error"),
144 tr("Error reading OpenDX file: No cube found"));
145 } else {
146 if (!m_progressDialog)
147 m_progressDialog =
148 new QProgressDialog(qobject_cast<QWidget*>(parent()));
149
150 // generate positive mesh
151 m_progressDialog->setLabelText("Generating Positive Potential Mesh");
152 m_progressDialog->setRange(0, 100);
153 m_progressDialog->setValue(1);
154 qApp->processEvents();
155
156 Mesh* mesh = molecule.addMesh();
157 auto* meshGenerator = new QtGui::MeshGenerator(cube, mesh, 0.1f);
158 connect(meshGenerator, SIGNAL(finished()), this,
159 SLOT(meshGeneratorFinished()));
160 connect(meshGenerator, SIGNAL(progressValueChanged(int)), this,
161 SLOT(onMeshGeneratorProgress(int)));
162 meshGenerator->run();
163
164 // generate negative mesh
165 m_progressDialog->setLabelText("Generating Negative Potential Mesh");
166 m_progressDialog->setRange(0, 100);
167 m_progressDialog->setValue(1);
168 qApp->processEvents();
169
170 mesh = molecule.addMesh();
171 meshGenerator = new QtGui::MeshGenerator(cube, mesh, -0.1f);
172 connect(meshGenerator, SIGNAL(finished()), this,
173 SLOT(meshGeneratorFinished()));
174 connect(meshGenerator, SIGNAL(progressValueChanged(int)), this,
175 SLOT(onMeshGeneratorProgress(int)));
176 meshGenerator->run();
177
178 m_progressDialog->setValue(100);
179 m_progressDialog->hide();
180 }
181 }
182
183 return true;
184 }
185 } // namespace Avogadro::QtPlugins