From 0949d413a645f65024d43d51ac2a8d06fa2c8cee Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 12 Jun 2012 22:36:15 -0400 Subject: [PATCH] Prevent a quick double-insert. With the "grow by fragment" feature, accidentally double-clicking "insert" could give you much more than you intended. Adds a 2-second delay to ensure you actually want to grow the selected insert. Change-Id: Ib9fb77b067a6470dd7d2672d8ba9e3b7e3738ca4 --- .../extensions/insertfragmentextension.cpp | 38 ++++++++++++++++--- .../src/extensions/insertfragmentextension.h | 5 +++ 2 files changed, 38 insertions(+), 5 deletions(-) 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