34
34
35
35
#include < QInputDialog>
36
36
#include < QDebug>
37
+ #include < QTimer>
37
38
38
39
using namespace std ;
39
40
using namespace OpenBabel ;
@@ -51,7 +52,8 @@ namespace Avogadro {
51
52
Extension (parent),
52
53
m_fragmentDialog (0 ),
53
54
m_crystalDialog (0 ),
54
- m_molecule (0 )
55
+ m_molecule (0 ),
56
+ m_justFinished (false )
55
57
{
56
58
QAction *action = new QAction (this );
57
59
action->setText (tr (" Crystal..." ));
@@ -230,15 +232,18 @@ namespace Avogadro {
230
232
}
231
233
}
232
234
}
233
- if (noSelectedHatoms) // add the heavy atom
235
+ if (noSelectedHatoms && !selectedIds. contains (atom-> id ())) { // add the heavy atom
234
236
selectedIds.append (atom->id ());
237
+ }
235
238
236
- } else {
239
+ } else { // this is a hydrogen
237
240
const Atom *hydrogen = atom;
238
- if (!hydrogen->neighbors ().empty ()) {
241
+ if (!hydrogen->neighbors ().empty ()) { // it's bonded to something
239
242
atom = m_molecule->atomById (hydrogen->neighbors ()[0 ]); // the first bonded atom to this "H"
240
243
}
241
- selectedIds.append (atom->id ());
244
+ if (!selectedIds.contains (atom->id ())) {
245
+ selectedIds.append (atom->id ());
246
+ }
242
247
}
243
248
}
244
249
@@ -251,13 +256,22 @@ namespace Avogadro {
251
256
if (!dialog)
252
257
return ;
253
258
259
+ // Prevent "double insert"
260
+ if (m_justFinished)
261
+ return ;
262
+
263
+ // Don't allow two inserts unless spaced by 2 seconds
264
+ // This avoids a "double insert"
265
+ QTimer::singleShot (2 *1000 , this , SLOT (resetTimer ()));
266
+
254
267
const Molecule fragment = dialog->fragment ();
255
268
if (fragment.numAtoms () == 0 )
256
269
return ;
257
270
258
271
*m_molecule = fragment;
259
272
m_molecule->update ();
260
273
emit moleculeChanged (m_molecule, Extension::NewWindow);
274
+ m_justFinished = true ;
261
275
}
262
276
263
277
// only called by the fragment dialog (not SMILES)
@@ -267,6 +281,14 @@ namespace Avogadro {
267
281
if (!dialog)
268
282
return ;
269
283
284
+ // Prevent "double insert"
285
+ if (m_justFinished)
286
+ return ;
287
+
288
+ // Don't allow two inserts unless spaced by 2 seconds
289
+ // This avoids a "double insert"
290
+ QTimer::singleShot (2 *1000 , this , SLOT (resetTimer ()));
291
+
270
292
// Get the fragment and make sure it exists (e.g., we didn't try to insert a directory
271
293
const Molecule fragment = dialog->fragment ();
272
294
if (fragment.numAtoms () == 0 )
@@ -287,6 +309,12 @@ namespace Avogadro {
287
309
foreach (int id, selectedIds) {
288
310
emit performCommand (new InsertFragmentCommand (m_molecule, fragment, m_widget, tr (" Insert Fragment" ), id));
289
311
}
312
+ m_justFinished = true ;
313
+ }
314
+
315
+ void InsertFragmentExtension::resetTimer ()
316
+ {
317
+ m_justFinished = false ; // done with the delay
290
318
}
291
319
292
320
} // end namespace Avogadro
0 commit comments