From ae69a991a359c1a1ebf9753b98c766e6febc65ba Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Thu, 29 Dec 2016 20:30:46 -0500 Subject: [PATCH] Update as "insert from SMILES" commands like Avo1.x Polish dialog box and put into the Build menu. Might be worth having as an "Import -> From Descriptor" command too? --- .../lineformatinput/lineformatinput.cpp | 83 ++++++++++--------- .../lineformatinput/lineformatinput.h | 4 +- .../lineformatinput/lineformatinputdialog.cpp | 7 ++ .../lineformatinput/lineformatinputdialog.h | 3 + .../lineformatinput/lineformatinputdialog.ui | 15 +++- 5 files changed, 68 insertions(+), 44 deletions(-) diff --git a/avogadro/qtplugins/lineformatinput/lineformatinput.cpp b/avogadro/qtplugins/lineformatinput/lineformatinput.cpp index e87e2af18..3fd2593e3 100644 --- a/avogadro/qtplugins/lineformatinput/lineformatinput.cpp +++ b/avogadro/qtplugins/lineformatinput/lineformatinput.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -37,9 +38,16 @@ namespace QtPlugins { LineFormatInput::LineFormatInput(QObject *parent_) : Avogadro::QtGui::ExtensionPlugin(parent_), - m_reader(NULL) + m_reader(NULL), + m_molecule(NULL) { - QAction *action = new QAction(tr("Paste Molecule Descriptor..."), this); + QAction *action = new QAction(tr("SMILES..."), this); + action->setData("SMILES"); + connect(action, SIGNAL(triggered()), SLOT(showDialog())); + m_actions.append(action); + + action = new QAction(tr("InChI..."), this); + action->setData("InChI"); connect(action, SIGNAL(triggered()), SLOT(showDialog())); m_actions.append(action); @@ -61,46 +69,21 @@ QList LineFormatInput::actions() const QStringList LineFormatInput::menuPath(QAction *) const { - return QStringList() << tr("&Edit"); + return QStringList() << tr("&Build") << tr("&Insert"); } -bool LineFormatInput::readMolecule(QtGui::Molecule &mol) +void LineFormatInput::setMolecule(QtGui::Molecule *mol) { - QWidget *parentAsWidget = qobject_cast(parent()); - if (!m_reader) { - QMessageBox::warning(parentAsWidget, tr("Paste Molecule Descriptor"), - tr("An internal error occurred."), QMessageBox::Ok); - return false; - } - - QProgressDialog dlg(parentAsWidget); - dlg.setModal(true); - dlg.setWindowTitle(tr("Paste Molecule Descriptor")); - dlg.setLabelText(tr("Generating 3D molecule...")); - dlg.setRange(0, 0); - dlg.setValue(0); - dlg.show(); - bool success = m_reader->readString(m_descriptor, mol); - dlg.hide(); - - if (!success && !dlg.wasCanceled()) { - QMessageBox::warning(parentAsWidget, tr("Paste Molecule Descriptor"), - tr("Error parsing descriptor:\n'%1'\nDescriptor: '%2'") - .arg(QString::fromStdString(m_reader->error())) - .arg(QString::fromStdString(m_descriptor)), - QMessageBox::Ok); - } - - m_descriptor.clear(); - delete m_reader; - m_reader = NULL; - - return success && !dlg.wasCanceled(); + m_molecule = mol; } void LineFormatInput::showDialog() { + if (!m_molecule) + return; + QWidget *parentAsWidget = qobject_cast(parent()); + QAction *theSender = qobject_cast(sender()); // Create a list of file formats that we can read: QStringList availableFormats; @@ -113,7 +96,7 @@ void LineFormatInput::showDialog() } if (availableFormats.empty()) { - QMessageBox::information(parentAsWidget, tr("No descriptors found!"), + QMessageBox::warning(parentAsWidget, tr("No descriptors found!"), tr("No line format readers found!"), QMessageBox::Ok); return; @@ -122,26 +105,48 @@ void LineFormatInput::showDialog() // Prompt user for input: LineFormatInputDialog dlg; dlg.setFormats(availableFormats); + if (theSender != NULL) + dlg.setCurrentFormat(theSender->data().toString()); dlg.exec(); + // check if the reply is empty + if (dlg.descriptor().isEmpty()) + return; // nothing to do + // Resolve any format conflicts: const std::string &ext = m_formats[dlg.format()]; const FileFormat *fmt = FileFormatDialog::findFileFormat( - parentAsWidget, tr("Paste Molecular Descriptor"), + parentAsWidget, tr("Insert Molecule..."), QString("file.%1").arg(QString::fromStdString(ext)), ops); if (fmt == NULL) { - QMessageBox::information(parentAsWidget, tr("No descriptors found!"), + QMessageBox::warning(parentAsWidget, tr("No descriptors found!"), tr("Unable to load requested format reader."), QMessageBox::Ok); return; } - // Let the application know that we're ready. m_reader = fmt->newInstance(); m_descriptor = dlg.descriptor().toStdString(); - emit moleculeReady(1); + + QProgressDialog progDlg(parentAsWidget); + progDlg.setModal(true); + progDlg.setWindowTitle(tr("Insert Molecule...")); + progDlg.setLabelText(tr("Generating 3D molecule...")); + progDlg.setRange(0, 0); + progDlg.setValue(0); + progDlg.show(); + + QtGui::Molecule newMol; + bool success = m_reader->readString(m_descriptor, newMol); + m_molecule->undoMolecule()->appendMolecule(newMol, "Insert Molecule"); + emit requestActiveTool("Manipulator"); + dlg.hide(); + + m_descriptor.clear(); + delete m_reader; + m_reader = NULL; } } // namespace QtPlugins diff --git a/avogadro/qtplugins/lineformatinput/lineformatinput.h b/avogadro/qtplugins/lineformatinput/lineformatinput.h index 9c082f063..46ef3e574 100644 --- a/avogadro/qtplugins/lineformatinput/lineformatinput.h +++ b/avogadro/qtplugins/lineformatinput/lineformatinput.h @@ -45,8 +45,7 @@ class LineFormatInput : public QtGui::ExtensionPlugin QStringList menuPath(QAction *) const; public slots: - bool readMolecule(QtGui::Molecule &mol); - void setMolecule(QtGui::Molecule *) {} + void setMolecule(QtGui::Molecule *); private slots: void showDialog(); @@ -56,6 +55,7 @@ private slots: /// Maps identifier to extension: QMap m_formats; + QtGui::Molecule *m_molecule; Io::FileFormat *m_reader; std::string m_descriptor; }; diff --git a/avogadro/qtplugins/lineformatinput/lineformatinputdialog.cpp b/avogadro/qtplugins/lineformatinput/lineformatinputdialog.cpp index 62ca28b18..78d218901 100644 --- a/avogadro/qtplugins/lineformatinput/lineformatinputdialog.cpp +++ b/avogadro/qtplugins/lineformatinput/lineformatinputdialog.cpp @@ -51,6 +51,13 @@ QString LineFormatInputDialog::format() const return m_ui->formats->currentText(); } +void LineFormatInputDialog::setCurrentFormat(const QString &format) +{ + int index = m_ui->formats->findText(format); + if (index >= 0) + m_ui->formats->setCurrentIndex(index); +} + QString LineFormatInputDialog::descriptor() const { return m_ui->descriptor->text(); diff --git a/avogadro/qtplugins/lineformatinput/lineformatinputdialog.h b/avogadro/qtplugins/lineformatinput/lineformatinputdialog.h index 84e4d761c..a904f498c 100644 --- a/avogadro/qtplugins/lineformatinput/lineformatinputdialog.h +++ b/avogadro/qtplugins/lineformatinput/lineformatinputdialog.h @@ -39,6 +39,9 @@ class LineFormatInputDialog : public QDialog void setFormats(const QStringList &indents); QString format() const; + + void setCurrentFormat(const QString &format); + QString descriptor() const; protected slots: diff --git a/avogadro/qtplugins/lineformatinput/lineformatinputdialog.ui b/avogadro/qtplugins/lineformatinput/lineformatinputdialog.ui index b938e4a42..12b61da0e 100644 --- a/avogadro/qtplugins/lineformatinput/lineformatinputdialog.ui +++ b/avogadro/qtplugins/lineformatinput/lineformatinputdialog.ui @@ -6,16 +6,25 @@ 0 0 - 556 - 115 + 269 + 123 + + + 0 + 0 + + - Paste Molecular Descriptor + Insert Molecule... + + QFormLayout::ExpandingFieldsGrow +