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

Commit

Permalink
Merge topic 'FIX_insert-crystal-crash' into master
Browse files Browse the repository at this point in the history
893df09 Fixed indentation, removed unused variable added
0949d41 Prevent a quick double-insert.
0efeaed Fix crash reported when crystal files not present.
  • Loading branch information
cryos authored and Code Review committed Aug 27, 2012
2 parents 9e53411 + 893df09 commit 75c5e7f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
17 changes: 14 additions & 3 deletions libavogadro/src/extensions/insertfragmentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
38 changes: 33 additions & 5 deletions libavogadro/src/extensions/insertfragmentextension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <QInputDialog>
#include <QDebug>
#include <QTimer>

using namespace std;
using namespace OpenBabel;
Expand All @@ -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..."));
Expand Down Expand Up @@ -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());
}
}
}

Expand All @@ -251,13 +256,22 @@ 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;

*m_molecule = fragment;
m_molecule->update();
emit moleculeChanged(m_molecule, Extension::NewWindow);
m_justFinished = true;
}

// only called by the fragment dialog (not SMILES)
Expand All @@ -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)
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions libavogadro/src/extensions/insertfragmentextension.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<QAction *> m_actions;
Expand All @@ -65,6 +68,8 @@ namespace Avogadro {
InsertFragmentDialog *m_crystalDialog;
QString m_smilesString;
Molecule *m_molecule;

bool m_justFinished;
};

class InsertFragmentExtensionFactory : public QObject, public PluginFactory
Expand Down

0 comments on commit 75c5e7f

Please sign in to comment.