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