Skip to content

Commit

Permalink
Add support for settings in template tool (remember previous options)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Nov 19, 2023
1 parent 8652889 commit 06679fc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 33 deletions.
109 changes: 76 additions & 33 deletions avogadro/qtplugins/templatetool/templatetoolwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <avogadro/core/elements.h>

#include <avogadro/qtgui/insertfragmentdialog.h>
#include <avogadro/qtgui/periodictableview.h>

#include <algorithm>
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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"));
}

Expand All @@ -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();
Expand All @@ -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
Expand All @@ -228,7 +268,7 @@ void TemplateToolWidget::typeChanged(int index)
<< "ester"
<< "nitro"
<< "phenyl"
<< "sulfonate";
<< "sulfonate" << tr("Other…");
m_ligands = ligandNames;
m_denticity = 1;
break;
Expand Down Expand Up @@ -261,6 +301,9 @@ void TemplateToolWidget::elementChanged(int index)
m_currentElement = static_cast<unsigned char>(itemData.toInt());
}
}

QSettings settings;
settings.setValue("templatetool/element", m_currentElement);
}

void TemplateToolWidget::updateElementCombo()
Expand All @@ -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<unsigned char>(
Expand Down
3 changes: 3 additions & 0 deletions avogadro/qtplugins/templatetool/templatetoolwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Avogadro {
namespace QtGui {
class PeriodicTableView;
class InsertFragmentDialog;
}

namespace QtPlugins {
Expand Down Expand Up @@ -60,12 +61,14 @@ private slots:
void saveElements();

Ui::TemplateToolWidget *m_ui;
QtGui::InsertFragmentDialog *m_fragmentDialog;
QtGui::PeriodicTableView *m_elementSelector;
QList<unsigned char> m_defaultElements;
QList<unsigned char> m_userElements;
unsigned char m_currentElement;
QStringList m_centers;
QStringList m_ligands;
QString m_ligandPath;

int m_denticity;
std::vector<size_t> m_selectedUIDs;
Expand Down

0 comments on commit 06679fc

Please sign in to comment.