From 06679fc4de601a0df23e80e3059d5e73ca37bfea Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Sat, 18 Nov 2023 22:53:04 -0500 Subject: [PATCH] Add support for settings in template tool (remember previous options) Signed-off-by: Geoff Hutchison --- .../templatetool/templatetoolwidget.cpp | 109 ++++++++++++------ .../templatetool/templatetoolwidget.h | 3 + 2 files changed, 79 insertions(+), 33 deletions(-) diff --git a/avogadro/qtplugins/templatetool/templatetoolwidget.cpp b/avogadro/qtplugins/templatetool/templatetoolwidget.cpp index a9b310fc8f..fc4d6a1c3b 100644 --- a/avogadro/qtplugins/templatetool/templatetoolwidget.cpp +++ b/avogadro/qtplugins/templatetool/templatetoolwidget.cpp @@ -8,6 +8,7 @@ #include +#include #include #include @@ -39,29 +40,18 @@ enum LigandType }; TemplateToolWidget::TemplateToolWidget(QWidget* parent_) - : QWidget(parent_), m_ui(new Ui::TemplateToolWidget), m_elementSelector(NULL), - m_currentElement(26) + : QWidget(parent_), m_ui(new Ui::TemplateToolWidget), + m_elementSelector(nullptr), m_fragmentDialog(nullptr), m_currentElement(26) { m_ui->setupUi(this); buildElements(); - connect(m_ui->elementComboBox, SIGNAL(currentIndexChanged(int)), this, - SLOT(elementChanged(int))); - - connect(m_ui->coordinationComboBox, SIGNAL(currentIndexChanged(int)), this, - SLOT(coordinationChanged(int))); - - connect(m_ui->typeComboBox, SIGNAL(currentIndexChanged(int)), this, - SLOT(typeChanged(int))); - - connect(m_ui->ligandComboBox, SIGNAL(currentIndexChanged(int)), this, - SLOT(ligandChanged(int))); - - // Show iron at startup. - selectElement(26); - // default coordination = octahedral - m_ui->centerPreview->setIcon(QIcon(":/icons/centers/6-oct.png")); + // Get default options + QSettings settings; + settings.beginGroup("templatetool"); + unsigned int currentElement = settings.value("element", 26).toUInt(); + selectElement(currentElement); // In the same order of the coordinationComboBox // append ".png" for the icon and ".cjson" for the template @@ -77,7 +67,33 @@ TemplateToolWidget::TemplateToolWidget(QWidget* parent_) << "7-pbp" << "8-sqa"; - typeChanged(LigandType::Monodentate); + connect(m_ui->elementComboBox, SIGNAL(currentIndexChanged(int)), this, + SLOT(elementChanged(int))); + + connect(m_ui->coordinationComboBox, SIGNAL(currentIndexChanged(int)), this, + SLOT(coordinationChanged(int))); + + connect(m_ui->typeComboBox, SIGNAL(currentIndexChanged(int)), this, + SLOT(typeChanged(int))); + + connect(m_ui->ligandComboBox, SIGNAL(currentIndexChanged(int)), this, + SLOT(ligandChanged(int))); + + // default coordination = octahedral + QString currentCoord = settings.value("coordination", "6-oct").toString(); + int index = m_centers.indexOf(currentCoord); + if (index < 0) + index = 7; // octahedral + m_ui->coordinationComboBox->setCurrentIndex(index); + + // update the preview icon + QString iconPath = QString(":/icons/centers/%1.png").arg(currentCoord); + m_ui->centerPreview->setIcon(QIcon(iconPath)); + + unsigned int ligandType = settings.value("ligandType", 0).toUInt(); + m_ui->typeComboBox->setCurrentIndex(ligandType); + // update the ligand combo box + typeChanged(ligandType); } TemplateToolWidget::~TemplateToolWidget() @@ -150,6 +166,9 @@ void TemplateToolWidget::coordinationChanged(int index) // get the icon name QString iconName = m_centers[index]; + QSettings settings; + settings.setValue("templatetool/coordination", iconName); + m_ui->centerPreview->setIcon(QIcon(":/icons/centers/" + iconName + ".png")); } @@ -160,11 +179,26 @@ void TemplateToolWidget::ligandChanged(int index) // get the icon name QString iconName = m_ligands[index]; + + // check if it's "other" + if (iconName.endsWith("-other")) { + if (!m_fragmentDialog) { + m_fragmentDialog = new QtGui::InsertFragmentDialog(this); + connect(m_fragmentDialog, SIGNAL(accepted()), this, SLOT(accepted())); + } + m_fragmentDialog->show(); + return; + } + m_ui->ligandPreview->setIcon(QIcon(":/icons/ligands/" + iconName + ".png")); } void TemplateToolWidget::typeChanged(int index) { + QSettings settings; + settings.beginGroup("templatetool"); + settings.setValue("ligandType", index); + m_selectedUIDs.clear(); m_ui->ligandComboBox->clear(); m_ligands = QStringList(); @@ -176,50 +210,56 @@ void TemplateToolWidget::typeChanged(int index) << "carbonyl" << "cyano" << "phosphine" - << "thiol"; + << "thiol" << tr("Other…"); m_ligands << "1-ammine" << "1-aqua" << "1-carbonyl" << "1-cyano" << "1-phosphine" - << "1-thiol"; + << "1-thiol" + << "1-other"; m_denticity = 1; break; case LigandType::Bidentate: // Bidentate ligandNames << "acetylacetonate" << "bipyridine" - << "ethylenediamine"; + << "ethylenediamine" << tr("Other…"); m_ligands << "2-acetylacetonate" << "2-bipyridine" - << "2-ethylenediamine"; + << "2-ethylenediamine" + << "2-other"; m_denticity = 2; break; case LigandType::Tridentate: // Tridentate - ligandNames << "terpyridine"; - m_ligands << "3-terpyridine"; + ligandNames << "terpyridine" << tr("Other…"); + m_ligands << "3-terpyridine" + << "3-other"; m_denticity = 3; break; case LigandType::Tetradentate: // Tetradentate ligandNames << "phthalocyanine" << "porphin" - << "salen"; + << "salen" << tr("Other…"); m_ligands << "4-phthalocyanine" << "4-porphin" - << "4-salen"; + << "4-salen" + << "4-other"; m_denticity = 4; break; case LigandType::Hexadentate: // Hexadentate - ligandNames << "edta"; - m_ligands << "6-edta"; + ligandNames << "edta" << tr("Other…"); + m_ligands << "6-edta" + << "6-other"; m_denticity = 6; break; case LigandType::Haptic: // Haptic ligandNames << "η2-ethylene" << "η5-cyclopentyl" - << "η6-benzene"; + << "η6-benzene" << tr("Other…"); m_ligands << "eta2-ethylene" << "eta5-cyclopentyl" - << "eta6-benzene"; + << "eta6-benzene" + << "eta-other"; m_denticity = 1; break; case LigandType::FunctionalGroup: // Functional Groups @@ -228,7 +268,7 @@ void TemplateToolWidget::typeChanged(int index) << "ester" << "nitro" << "phenyl" - << "sulfonate"; + << "sulfonate" << tr("Other…"); m_ligands = ligandNames; m_denticity = 1; break; @@ -261,6 +301,9 @@ void TemplateToolWidget::elementChanged(int index) m_currentElement = static_cast(itemData.toInt()); } } + + QSettings settings; + settings.setValue("templatetool/element", m_currentElement); } void TemplateToolWidget::updateElementCombo() @@ -285,7 +328,7 @@ void TemplateToolWidget::updateElementCombo() atomicNum); } m_ui->elementComboBox->insertSeparator(m_ui->elementComboBox->count()); - m_ui->elementComboBox->addItem(tr("Other..."), ELEMENT_SELECTOR_TAG); + m_ui->elementComboBox->addItem(tr("Other…"), ELEMENT_SELECTOR_TAG); // Reset the element if it still exists selectElement(static_cast( diff --git a/avogadro/qtplugins/templatetool/templatetoolwidget.h b/avogadro/qtplugins/templatetool/templatetoolwidget.h index 274c27d05e..8fe7451d55 100644 --- a/avogadro/qtplugins/templatetool/templatetoolwidget.h +++ b/avogadro/qtplugins/templatetool/templatetoolwidget.h @@ -11,6 +11,7 @@ namespace Avogadro { namespace QtGui { class PeriodicTableView; +class InsertFragmentDialog; } namespace QtPlugins { @@ -60,12 +61,14 @@ private slots: void saveElements(); Ui::TemplateToolWidget *m_ui; + QtGui::InsertFragmentDialog *m_fragmentDialog; QtGui::PeriodicTableView *m_elementSelector; QList m_defaultElements; QList m_userElements; unsigned char m_currentElement; QStringList m_centers; QStringList m_ligands; + QString m_ligandPath; int m_denticity; std::vector m_selectedUIDs;