Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit 75c5e7f

Browse files
cryosCode Review
authored and
Code Review
committed
Merge topic 'FIX_insert-crystal-crash' into master
893df09 Fixed indentation, removed unused variable added 0949d41 Prevent a quick double-insert. 0efeaed Fix crash reported when crystal files not present.
2 parents 9e53411 + 893df09 commit 75c5e7f

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

libavogadro/src/extensions/insertfragmentdialog.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,25 @@ namespace Avogadro {
8989
// Mac and Windows use relative path from application location
9090
m_directory = QCoreApplication::applicationDirPath() + "/../share/avogadro/";
9191
#endif
92-
m_directory += directory; // fragments or crystals
92+
m_directory += directory; // fragments or crystals or whatever
9393
if ( directory.contains(QLatin1String("crystals")) )
9494
d->crystalFiles = true;
9595
else
9696
d->crystalFiles = false;
9797

98+
QDir dir(m_directory);
99+
if (!dir.exists() || !dir.isReadable() ) {
100+
qWarning() << "Cannot find the directory: " << m_directory;
101+
102+
// Can't really do anything!
103+
ui.directoryTreeView->setEnabled(false);
104+
ui.insertFragmentButton->setEnabled(false);
105+
ui.filterLineEdit->setEnabled(false);
106+
ui.clearToolButton->setEnabled(false);
107+
108+
return;
109+
}
110+
98111
d->model = new QFileSystemModel(this);
99112
d->model->setReadOnly(true);
100113
QModelIndex rootIndex = d->model->setRootPath(m_directory);
@@ -141,8 +154,6 @@ namespace Avogadro {
141154

142155
const Molecule &InsertFragmentDialog::fragment()
143156
{
144-
OBMol obfragment;
145-
146157
// The selected model index is in the proxy
147158
QModelIndexList selected = ui.directoryTreeView->selectionModel()->selectedIndexes();
148159

libavogadro/src/extensions/insertfragmentextension.cpp

+33-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include <QInputDialog>
3636
#include <QDebug>
37+
#include <QTimer>
3738

3839
using namespace std;
3940
using namespace OpenBabel;
@@ -51,7 +52,8 @@ namespace Avogadro {
5152
Extension(parent),
5253
m_fragmentDialog(0),
5354
m_crystalDialog(0),
54-
m_molecule(0)
55+
m_molecule(0),
56+
m_justFinished(false)
5557
{
5658
QAction *action = new QAction(this);
5759
action->setText(tr("Crystal..."));
@@ -230,15 +232,18 @@ namespace Avogadro {
230232
}
231233
}
232234
}
233-
if (noSelectedHatoms) // add the heavy atom
235+
if (noSelectedHatoms && !selectedIds.contains(atom->id())) { // add the heavy atom
234236
selectedIds.append(atom->id());
237+
}
235238

236-
} else {
239+
} else { // this is a hydrogen
237240
const Atom *hydrogen = atom;
238-
if (!hydrogen->neighbors().empty()) {
241+
if (!hydrogen->neighbors().empty()) { // it's bonded to something
239242
atom = m_molecule->atomById(hydrogen->neighbors()[0]); // the first bonded atom to this "H"
240243
}
241-
selectedIds.append(atom->id());
244+
if (!selectedIds.contains(atom->id())) {
245+
selectedIds.append(atom->id());
246+
}
242247
}
243248
}
244249

@@ -251,13 +256,22 @@ namespace Avogadro {
251256
if (!dialog)
252257
return;
253258

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+
254267
const Molecule fragment = dialog->fragment();
255268
if (fragment.numAtoms() == 0)
256269
return;
257270

258271
*m_molecule = fragment;
259272
m_molecule->update();
260273
emit moleculeChanged(m_molecule, Extension::NewWindow);
274+
m_justFinished = true;
261275
}
262276

263277
// only called by the fragment dialog (not SMILES)
@@ -267,6 +281,14 @@ namespace Avogadro {
267281
if (!dialog)
268282
return;
269283

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+
270292
// Get the fragment and make sure it exists (e.g., we didn't try to insert a directory
271293
const Molecule fragment = dialog->fragment();
272294
if (fragment.numAtoms() == 0)
@@ -287,6 +309,12 @@ namespace Avogadro {
287309
foreach(int id, selectedIds) {
288310
emit performCommand(new InsertFragmentCommand(m_molecule, fragment, m_widget, tr("Insert Fragment"), id));
289311
}
312+
m_justFinished = true;
313+
}
314+
315+
void InsertFragmentExtension::resetTimer()
316+
{
317+
m_justFinished = false; // done with the delay
290318
}
291319

292320
} // end namespace Avogadro

libavogadro/src/extensions/insertfragmentextension.h

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ namespace Avogadro {
5757
void insertCrystal();
5858
void insertFragment();
5959

60+
// With the "grow selected atoms," feature, we need a delay timer
61+
void resetTimer();
62+
6063
private:
6164

6265
QList<QAction *> m_actions;
@@ -65,6 +68,8 @@ namespace Avogadro {
6568
InsertFragmentDialog *m_crystalDialog;
6669
QString m_smilesString;
6770
Molecule *m_molecule;
71+
72+
bool m_justFinished;
6873
};
6974

7075
class InsertFragmentExtensionFactory : public QObject, public PluginFactory

0 commit comments

Comments
 (0)