Debian Codegraph @

avogadro/qtplugins/gamessinput/gamessinput.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 "gamessinput.h"
7
8 #include "gamessinputdialog.h"
9
10 #include <avogadro/io/fileformat.h>
11 #include <avogadro/qtgui/fileformatdialog.h>
12 #include <avogadro/qtgui/molecule.h>
13
14 #include <avogadro/molequeue/client/jobobject.h>
15
16 #include <QtCore/QDebug>
17
18 #include <QAction>
19 #include <QtWidgets/QMessageBox>
20
21 namespace Avogadro {
22 namespace Core {
23 class Molecule;
24 }
25 namespace QtPlugins {
26
27 using MoleQueue::JobObject;
28
29 GamessInput::GamessInput(QObject* parent_)
30 : ExtensionPlugin(parent_), m_action(new QAction(this)), m_molecule(nullptr),
31 m_dialog(nullptr), m_outputFormat(nullptr)
32 {
33 m_action->setEnabled(true);
34 m_action->setText(tr("&GAMESS…"));
35 connect(m_action, SIGNAL(triggered()), SLOT(menuActivated()));
36 }
37
38 GamessInput::~GamessInput() {}
39
40 QList<QAction*> GamessInput::actions() const
41 {
42 QList<QAction*> actions_;
43 actions_.append(m_action);
44 return actions_;
45 }
46
47 QStringList GamessInput::menuPath(QAction*) const
48 {
49 QStringList path;
50 path << tr("&Input");
51 return path;
52 }
53
54 void GamessInput::setMolecule(QtGui::Molecule* mol)
55 {
56 if (m_dialog)
57 m_dialog->setMolecule(mol);
58 m_molecule = mol;
59
60 bool isPeriodic = (m_molecule && m_molecule->unitCell() != nullptr);
61
62 // GAMESS only handles molecules
63 m_action->setEnabled(!isPeriodic);
64 }
65
66 void GamessInput::openJobOutput(const JobObject& job)
67 {
68 m_outputFormat = nullptr;
69 m_outputFileName.clear();
70
71 QString outputPath(job.value("outputDirectory").toString());
72
73 using QtGui::FileFormatDialog;
74 FileFormatDialog::FormatFilePair result = FileFormatDialog::fileToRead(
75 qobject_cast<QWidget*>(parent()), tr("Open Output File"), outputPath);
76
77 if (result.first == nullptr) // User canceled
78 return;
79
80 m_outputFormat = result.first;
81 m_outputFileName = result.second;
82
83 emit moleculeReady(1);
84 }
85
86 bool GamessInput::readMolecule(QtGui::Molecule& mol)
87 {
88 Io::FileFormat* reader = m_outputFormat->newInstance();
89 bool success = reader->readFile(m_outputFileName.toStdString(), mol);
90 if (!success) {
91 QMessageBox::information(qobject_cast<QWidget*>(parent()), tr("Error"),
92 tr("Error reading output file '%1':\n%2")
93 .arg(m_outputFileName)
94 .arg(QString::fromStdString(reader->error())));
95 }
96
97 m_outputFormat = nullptr;
98 m_outputFileName.clear();
99
100 return success;
101 }
102
103 void GamessInput::menuActivated()
104 {
105 if (!m_dialog) {
106 m_dialog = new GamessInputDialog(qobject_cast<QWidget*>(parent()));
107 connect(m_dialog, SIGNAL(openJobOutput(Avogadro::MoleQueue::JobObject)),
108 this, SLOT(openJobOutput(Avogadro::MoleQueue::JobObject)));
109 }
110 m_dialog->setMolecule(m_molecule);
111 m_dialog->show();
112 m_dialog->raise();
113 m_dialog->activateWindow();
114 }
115 } // namespace QtPlugins
116 } // namespace Avogadro