| 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 "lineformatinput.h" |
| 7 |
|
| 8 |
#include "lineformatinputdialog.h" |
| 9 |
|
| 10 |
#include <avogadro/qtgui/fileformatdialog.h> |
| 11 |
#include <avogadro/qtgui/molecule.h> |
| 12 |
#include <avogadro/qtgui/rwmolecule.h> |
| 13 |
|
| 14 |
#include <avogadro/io/fileformat.h> |
| 15 |
#include <avogadro/io/fileformatmanager.h> |
| 16 |
|
| 17 |
#include <QAction> |
| 18 |
#include <QMessageBox> |
| 19 |
#include <QProgressDialog> |
| 20 |
|
| 21 |
using Avogadro::Io::FileFormat; |
| 22 |
using Avogadro::Io::FileFormatManager; |
| 23 |
using Avogadro::QtGui::FileFormatDialog; |
| 24 |
using namespace std::string_literals; |
| 25 |
|
| 26 |
namespace Avogadro::QtPlugins { |
| 27 |
|
| 28 |
LineFormatInput::LineFormatInput(QObject* parent_) |
| 29 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr), |
| 30 |
m_reader(nullptr) |
| 31 |
{ |
| 32 |
auto* action = new QAction(tr("SMILES…"), this); |
| 33 |
action->setProperty("menu priority", 710); |
| 34 |
action->setData("SMILES"); |
| 35 |
connect(action, SIGNAL(triggered()), SLOT(showDialog())); |
| 36 |
m_actions.append(action); |
| 37 |
|
| 38 |
action = new QAction(tr("InChI…"), this); |
| 39 |
action->setProperty("menu priority", 700); |
| 40 |
action->setData("InChI"); |
| 41 |
connect(action, SIGNAL(triggered()), SLOT(showDialog())); |
| 42 |
m_actions.append(action); |
| 43 |
|
| 44 |
// These are the line formats that we can load -- key is a user-friendly name, |
| 45 |
// value is the file extension used to identify the file format. |
| 46 |
m_formats.insert(tr("InChI"), "inchi"s); |
| 47 |
m_formats.insert(tr("SMILES"), "smi"s); |
| 48 |
} |
| 49 |
|
| 50 |
LineFormatInput::~LineFormatInput() |
| 51 |
{ |
| 52 |
delete m_reader; |
| 53 |
} |
| 54 |
|
| 55 |
QList<QAction*> LineFormatInput::actions() const |
| 56 |
{ |
| 57 |
return m_actions; |
| 58 |
} |
| 59 |
|
| 60 |
QStringList LineFormatInput::menuPath(QAction*) const |
| 61 |
{ |
| 62 |
return QStringList() << tr("&Build") << tr("&Insert"); |
| 63 |
} |
| 64 |
|
| 65 |
void LineFormatInput::setMolecule(QtGui::Molecule* mol) |
| 66 |
{ |
| 67 |
m_molecule = mol; |
| 68 |
} |
| 69 |
|
| 70 |
void LineFormatInput::showDialog() |
| 71 |
{ |
| 72 |
if (!m_molecule) |
| 73 |
return; |
| 74 |
|
| 75 |
QWidget* parentAsWidget = qobject_cast<QWidget*>(parent()); |
| 76 |
auto* theSender = qobject_cast<QAction*>(sender()); |
| 77 |
|
| 78 |
// Create a list of file formats that we can read: |
| 79 |
QStringList availableFormats; |
| 80 |
FileFormatManager& ffm = FileFormatManager::instance(); |
| 81 |
const FileFormat::Operations ops = FileFormat::Read | FileFormat::String; |
| 82 |
foreach (const QString& ident, m_formats.keys()) { |
| 83 |
const std::string& ext = m_formats[ident]; |
| 84 |
if (!ffm.fileFormatsFromFileExtension(ext, ops).empty()) |
| 85 |
availableFormats.push_back(ident); |
| 86 |
} |
| 87 |
|
| 88 |
if (availableFormats.empty()) { |
| 89 |
QMessageBox::warning(parentAsWidget, tr("No descriptors found!"), |
| 90 |
tr("No line format readers found!"), QMessageBox::Ok); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Prompt user for input: |
| 95 |
LineFormatInputDialog dlg; |
| 96 |
dlg.setFormats(availableFormats); |
| 97 |
if (theSender != nullptr) |
| 98 |
dlg.setCurrentFormat(theSender->data().toString()); |
| 99 |
dlg.exec(); |
| 100 |
|
| 101 |
// check if the reply is empty |
| 102 |
if (dlg.result() != LineFormatInputDialog::Accepted || |
| 103 |
dlg.descriptor().isEmpty()) |
| 104 |
return; // nothing to do |
| 105 |
|
| 106 |
// Resolve any format conflicts: |
| 107 |
const std::string& ext = m_formats[dlg.format()]; |
| 108 |
|
| 109 |
const FileFormat* fmt = FileFormatDialog::findFileFormat( |
| 110 |
parentAsWidget, tr("Insert Molecule…"), |
| 111 |
QString("file.%1").arg(QString::fromStdString(ext)), ops); |
| 112 |
|
| 113 |
if (fmt == nullptr) { |
| 114 |
QMessageBox::warning(parentAsWidget, tr("No descriptors found!"), |
| 115 |
tr("Unable to load requested format reader."), |
| 116 |
QMessageBox::Ok); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
m_reader = fmt->newInstance(); |
| 121 |
m_descriptor = dlg.descriptor().toStdString(); |
| 122 |
|
| 123 |
QProgressDialog progDlg(parentAsWidget); |
| 124 |
progDlg.setModal(true); |
| 125 |
progDlg.setWindowTitle(tr("Insert Molecule…")); |
| 126 |
progDlg.setLabelText(tr("Generating 3D molecule…")); |
| 127 |
progDlg.setRange(0, 0); |
| 128 |
progDlg.setValue(0); |
| 129 |
progDlg.show(); |
| 130 |
|
| 131 |
QtGui::Molecule newMol; |
| 132 |
m_reader->readString(m_descriptor, newMol); |
| 133 |
|
| 134 |
// check to make sure the coordinates of newMol are non-zero |
| 135 |
bool allZero = true; |
| 136 |
for (Index i = 0; i < newMol.atomCount(); i++) { |
| 137 |
auto vector = newMol.atomPosition3d(i); |
| 138 |
if (vector.x() != 0.0 || vector.y() != 0.0 || vector.z() != 0.0) { |
| 139 |
allZero = false; |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (allZero) { |
| 145 |
QMessageBox::warning(parentAsWidget, tr("No coordinates found!"), |
| 146 |
tr("Unable to generate 3D coordinates."), |
| 147 |
QMessageBox::Ok); |
| 148 |
delete m_reader; |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
m_molecule->undoMolecule()->appendMolecule(newMol, "Insert Molecule"); |
| 153 |
emit requestActiveTool("Manipulator"); |
| 154 |
dlg.hide(); |
| 155 |
|
| 156 |
m_descriptor.clear(); |
| 157 |
delete m_reader; |
| 158 |
m_reader = nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
} // namespace Avogadro::QtPlugins |