diff --git a/libavogadro/src/extensions/insertfragmentdialog.cpp b/libavogadro/src/extensions/insertfragmentdialog.cpp index a0ef8e560..31058b4fb 100644 --- a/libavogadro/src/extensions/insertfragmentdialog.cpp +++ b/libavogadro/src/extensions/insertfragmentdialog.cpp @@ -89,12 +89,25 @@ namespace Avogadro { // Mac and Windows use relative path from application location m_directory = QCoreApplication::applicationDirPath() + "/../share/avogadro/"; #endif - m_directory += directory; // fragments or crystals + m_directory += directory; // fragments or crystals or whatever if ( directory.contains(QLatin1String("crystals")) ) d->crystalFiles = true; else d->crystalFiles = false; + QDir dir(m_directory); + if (!dir.exists() || !dir.isReadable() ) { + qWarning() << "Cannot find the directory: " << m_directory; + + // Can't really do anything! + ui.directoryTreeView->setEnabled(false); + ui.insertFragmentButton->setEnabled(false); + ui.filterLineEdit->setEnabled(false); + ui.clearToolButton->setEnabled(false); + + return; + } + d->model = new QFileSystemModel(this); d->model->setReadOnly(true); QModelIndex rootIndex = d->model->setRootPath(m_directory); @@ -141,8 +154,6 @@ namespace Avogadro { const Molecule &InsertFragmentDialog::fragment() { - OBMol obfragment; - // The selected model index is in the proxy QModelIndexList selected = ui.directoryTreeView->selectionModel()->selectedIndexes(); diff --git a/libavogadro/src/extensions/insertfragmentextension.cpp b/libavogadro/src/extensions/insertfragmentextension.cpp index 48b5075fe..fe9f59923 100644 --- a/libavogadro/src/extensions/insertfragmentextension.cpp +++ b/libavogadro/src/extensions/insertfragmentextension.cpp @@ -34,6 +34,7 @@ #include #include +#include using namespace std; using namespace OpenBabel; @@ -51,7 +52,8 @@ namespace Avogadro { Extension(parent), m_fragmentDialog(0), m_crystalDialog(0), - m_molecule(0) + m_molecule(0), + m_justFinished(false) { QAction *action = new QAction(this); action->setText(tr("Crystal...")); @@ -230,15 +232,18 @@ namespace Avogadro { } } } - if (noSelectedHatoms) // add the heavy atom + if (noSelectedHatoms && !selectedIds.contains(atom->id())) { // add the heavy atom selectedIds.append(atom->id()); + } - } else { + } else { // this is a hydrogen const Atom *hydrogen = atom; - if (!hydrogen->neighbors().empty()) { + if (!hydrogen->neighbors().empty()) { // it's bonded to something atom = m_molecule->atomById(hydrogen->neighbors()[0]); // the first bonded atom to this "H" } - selectedIds.append(atom->id()); + if (!selectedIds.contains(atom->id())) { + selectedIds.append(atom->id()); + } } } @@ -251,6 +256,14 @@ namespace Avogadro { if (!dialog) return; + // Prevent "double insert" + if (m_justFinished) + return; + + // Don't allow two inserts unless spaced by 2 seconds + // This avoids a "double insert" + QTimer::singleShot(2*1000, this, SLOT(resetTimer())); + const Molecule fragment = dialog->fragment(); if (fragment.numAtoms() == 0) return; @@ -258,6 +271,7 @@ namespace Avogadro { *m_molecule = fragment; m_molecule->update(); emit moleculeChanged(m_molecule, Extension::NewWindow); + m_justFinished = true; } // only called by the fragment dialog (not SMILES) @@ -267,6 +281,14 @@ namespace Avogadro { if (!dialog) return; + // Prevent "double insert" + if (m_justFinished) + return; + + // Don't allow two inserts unless spaced by 2 seconds + // This avoids a "double insert" + QTimer::singleShot(2*1000, this, SLOT(resetTimer())); + // Get the fragment and make sure it exists (e.g., we didn't try to insert a directory const Molecule fragment = dialog->fragment(); if (fragment.numAtoms() == 0) @@ -287,6 +309,12 @@ namespace Avogadro { foreach(int id, selectedIds) { emit performCommand(new InsertFragmentCommand(m_molecule, fragment, m_widget, tr("Insert Fragment"), id)); } + m_justFinished = true; + } + + void InsertFragmentExtension::resetTimer() + { + m_justFinished = false; // done with the delay } } // end namespace Avogadro diff --git a/libavogadro/src/extensions/insertfragmentextension.h b/libavogadro/src/extensions/insertfragmentextension.h index 1d56fe575..40b30ca34 100644 --- a/libavogadro/src/extensions/insertfragmentextension.h +++ b/libavogadro/src/extensions/insertfragmentextension.h @@ -57,6 +57,9 @@ namespace Avogadro { void insertCrystal(); void insertFragment(); + // With the "grow selected atoms," feature, we need a delay timer + void resetTimer(); + private: QList m_actions; @@ -65,6 +68,8 @@ namespace Avogadro { InsertFragmentDialog *m_crystalDialog; QString m_smilesString; Molecule *m_molecule; + + bool m_justFinished; }; class InsertFragmentExtensionFactory : public QObject, public PluginFactory