Skip to content

Commit

Permalink
Update as "insert from SMILES" commands like Avo1.x
Browse files Browse the repository at this point in the history
Polish dialog box and put into the Build menu.
Might be worth having as an "Import -> From Descriptor" command too?
  • Loading branch information
ghutchis committed Dec 30, 2016
1 parent a9c4411 commit ae69a99
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 44 deletions.
83 changes: 44 additions & 39 deletions avogadro/qtplugins/lineformatinput/lineformatinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <avogadro/qtgui/fileformatdialog.h>
#include <avogadro/qtgui/molecule.h>
#include <avogadro/qtgui/rwmolecule.h>

#include <avogadro/io/fileformat.h>
#include <avogadro/io/fileformatmanager.h>
Expand All @@ -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);

Expand All @@ -61,46 +69,21 @@ QList<QAction *> 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<QWidget*>(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<QWidget*>(parent());
QAction *theSender = qobject_cast<QAction*>(sender());

// Create a list of file formats that we can read:
QStringList availableFormats;
Expand All @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions avogadro/qtplugins/lineformatinput/lineformatinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -56,6 +55,7 @@ private slots:
/// Maps identifier to extension:
QMap<QString, std::string> m_formats;

QtGui::Molecule *m_molecule;
Io::FileFormat *m_reader;
std::string m_descriptor;
};
Expand Down
7 changes: 7 additions & 0 deletions avogadro/qtplugins/lineformatinput/lineformatinputdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions avogadro/qtplugins/lineformatinput/lineformatinputdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 12 additions & 3 deletions avogadro/qtplugins/lineformatinput/lineformatinputdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
<rect>
<x>0</x>
<y>0</y>
<width>556</width>
<height>115</height>
<width>269</width>
<height>123</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Paste Molecular Descriptor</string>
<string>Insert Molecule...</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
Expand Down

0 comments on commit ae69a99

Please sign in to comment.