From 0efeaed51f99ce4d888900a597b938457bcf5a8e Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 12 Jun 2012 22:33:13 -0400 Subject: [PATCH 1/3] Fix crash reported when crystal files not present. Simply disables the entire interface. Uses qWarning() to spit out a warning to console. Fixes bug report PR#3530795. Change-Id: Ifbbf1b6a3c1b99fdfedb9158862d889eebd856af --- .../src/extensions/insertfragmentdialog.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libavogadro/src/extensions/insertfragmentdialog.cpp b/libavogadro/src/extensions/insertfragmentdialog.cpp index a0ef8e560..803aadd0f 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(); @@ -166,6 +177,7 @@ namespace Avogadro { if (fileInfo.isDir()) return d->fragment; // return an empty fragment and do nothing + OBMol obfragment; Molecule *mol; if (d->crystalFiles) { // No bonding, at first From 0949d413a645f65024d43d51ac2a8d06fa2c8cee Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 12 Jun 2012 22:36:15 -0400 Subject: [PATCH 2/3] 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 From 893df09e96b820805f1f57e95df7d5f191471833 Mon Sep 17 00:00:00 2001 From: "Marcus D. Hanwell" Date: Mon, 27 Aug 2012 13:48:56 -0400 Subject: [PATCH 3/3] Fixed indentation, removed unused variable added Change-Id: Idec3f5312c5dbb0910f9a00daffcf4d74f6e8fa1 --- libavogadro/src/extensions/insertfragmentdialog.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libavogadro/src/extensions/insertfragmentdialog.cpp b/libavogadro/src/extensions/insertfragmentdialog.cpp index 803aadd0f..31058b4fb 100644 --- a/libavogadro/src/extensions/insertfragmentdialog.cpp +++ b/libavogadro/src/extensions/insertfragmentdialog.cpp @@ -99,13 +99,13 @@ namespace Avogadro { 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); + // Can't really do anything! + ui.directoryTreeView->setEnabled(false); + ui.insertFragmentButton->setEnabled(false); + ui.filterLineEdit->setEnabled(false); + ui.clearToolButton->setEnabled(false); - return; + return; } d->model = new QFileSystemModel(this); @@ -177,7 +177,6 @@ namespace Avogadro { if (fileInfo.isDir()) return d->fragment; // return an empty fragment and do nothing - OBMol obfragment; Molecule *mol; if (d->crystalFiles) { // No bonding, at first