Skip to content

Commit

Permalink
Merge pull request #1810 from ghutchis/charge-spin-properties
Browse files Browse the repository at this point in the history
Edit molecule name, charge, and spin in properties
  • Loading branch information
ghutchis authored Nov 22, 2024
2 parents 328621a + e2fc0b8 commit 16a8ef4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 12 deletions.
8 changes: 7 additions & 1 deletion avogadro/core/variant-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ inline const Vector3& Variant::value() const
if (m_type == Vector)
return *m_value.vector;

return Vector3::Zero();
static Vector3 nullVector(0, 0, 0);
return nullVector;
}

inline void Variant::clear()
Expand All @@ -422,6 +423,9 @@ inline void Variant::clear()
} else if (m_type == Matrix) {
delete m_value.matrix;
m_value.matrix = 0;
} else if (m_type == Vector) {
delete m_value.vector;
m_value.vector = 0;
}

m_type = Null;
Expand Down Expand Up @@ -527,6 +531,8 @@ inline Variant& Variant::operator=(const Variant& variant)
m_value.string = new std::string(variant.toString());
else if (m_type == Matrix)
m_value.matrix = new MatrixX(*variant.m_value.matrix);
else if (m_type == Vector)
m_value.vector = new Vector3(*variant.m_value.vector);
else if (m_type != Null)
m_value = variant.m_value;
}
Expand Down
13 changes: 11 additions & 2 deletions avogadro/molequeue/inputgeneratorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ void InputGeneratorWidget::setMolecule(QtGui::Molecule* mol)
m_molecule->disconnect(this);

m_molecule = mol;

if (mol) {
// make sure to call the base class method
QtGui::JsonWidget::setMolecule(mol);

connect(mol, SIGNAL(changed(unsigned int)), SLOT(updatePreviewText()));
connect(mol, SIGNAL(changed(unsigned int)), SLOT(updateTitlePlaceholder()));
}
Expand Down Expand Up @@ -120,6 +122,13 @@ void InputGeneratorWidget::showEvent(QShowEvent* e)
{
QWidget::showEvent(e);

if (m_molecule != nullptr) {
int charge = static_cast<int>(m_molecule->totalCharge());
int multiplicity = static_cast<int>(m_molecule->totalSpinMultiplicity());
setOption("Charge", charge);
setOption("Multiplicity", multiplicity);
}

// Update the preview text if an update was requested while hidden. Use a
// single shot to allow the dialog to show before popping up any warnings.
if (m_updatePending)
Expand Down Expand Up @@ -649,4 +658,4 @@ void InputGeneratorWidget::updateOptions()
setOptionDefaults();
}

} // namespace Avogadro
} // namespace Avogadro::MoleQueue
14 changes: 13 additions & 1 deletion avogadro/qtgui/jsonwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ JsonWidget::~JsonWidget() {}

void JsonWidget::setMolecule(QtGui::Molecule* mol)
{
if (m_molecule != nullptr) {
// update charge and multiplicity if needed
int charge = static_cast<int>(m_molecule->totalCharge());
int multiplicity = static_cast<int>(m_molecule->totalSpinMultiplicity());

setOption("Charge", charge);
setOption("Multiplicity", multiplicity);
}

if (mol == m_molecule)
return;

Expand Down Expand Up @@ -816,8 +825,11 @@ QJsonObject JsonWidget::collectOptions() const

void JsonWidget::applyOptions(const QJsonObject& opts)
{
foreach (const QString& label, opts.keys())
foreach (const QString& label, opts.keys()) {
setOption(label, opts[label]);

qDebug() << "Setting option" << label << "to" << opts[label];
}
}

QString JsonWidget::generateJobTitle() const
Expand Down
56 changes: 48 additions & 8 deletions avogadro/qtplugins/molecularproperties/molecularmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ QVariant MolecularModel::data(const QModelIndex& index, int role) const
else if (key == " 7residues")
return QVariant::fromValue(m_molecule->residueCount());
else if (key == " 9totalCharge")
return QVariant::fromValue(m_molecule->totalCharge());
return QVariant::fromValue(static_cast<int>(m_molecule->totalCharge()));
else if (key == " 9totalSpinMultiplicity")
return QVariant::fromValue(m_molecule->totalSpinMultiplicity());
return QVariant::fromValue(
static_cast<int>(m_molecule->totalSpinMultiplicity()));

return QString::fromStdString(it->second.toString());
}
Expand Down Expand Up @@ -336,6 +337,18 @@ Qt::ItemFlags MolecularModel::flags(const QModelIndex& index) const
// for the types and columns that can be edited
auto editable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;

int row = index.row();

if (row == 0) // name
return editable;

const auto map = m_propertiesCache;
auto it = map.begin();
std::advance(it, row);
auto key = it->first;
if (key == " 9totalCharge" || key == " 9totalSpinMultiplicity")
return editable;

return QAbstractItemModel::flags(index);
}

Expand All @@ -348,7 +361,34 @@ bool MolecularModel::setData(const QModelIndex& index, const QVariant& value,
if (role != Qt::EditRole)
return false;

// TODO allow editing name, total charge, total spin multiplicity
int row = index.row();

if (row == 0) { // name should always be the first row
m_name = value.toString();
m_autoName = false;
m_molecule->setData("name", m_name.toStdString());
emit dataChanged(index, index);
return true;
}

const auto map = m_propertiesCache;
auto it = map.begin();
std::advance(it, row);
auto key = it->first;
if (key == " 9totalCharge") {
m_molecule->setData("totalCharge", value.toInt());
emit dataChanged(index, index);
return true;
} else if (key == " 9totalSpinMultiplicity") {
int spin = value.toInt();
if (spin < 1)
return false;

m_molecule->setData("totalSpinMultiplicity", value.toInt());
emit dataChanged(index, index);
return true;
}

return false;
}

Expand Down Expand Up @@ -391,11 +431,11 @@ void MolecularModel::updateTable(unsigned int flags)
m_propertiesCache.setValue(" 8chains", chainCount);
}

if (m_molecule->totalCharge() != 0)
m_propertiesCache.setValue(" 9totalCharge", m_molecule->totalCharge());
if (m_molecule->totalSpinMultiplicity() != 1)
m_propertiesCache.setValue(" 9totalSpinMultiplicity",
m_molecule->totalSpinMultiplicity());
m_propertiesCache.setValue(" 9totalCharge",
static_cast<int>(m_molecule->totalCharge()));
m_propertiesCache.setValue(
" 9totalSpinMultiplicity",
static_cast<int>(m_molecule->totalSpinMultiplicity()));
if (m_molecule->hasData("dipoleMoment")) {
auto dipole = m_molecule->data("dipoleMoment").toVector3();
QString moment = QString::number(dipole.norm(), 'f', 3);
Expand Down
1 change: 1 addition & 0 deletions avogadro/qtplugins/molecularproperties/molecularview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ void MolecularView::openExportDialogBox()

// Write the data rows
for (int row = 0; row < model()->rowCount(); ++row) {
stream << model()->headerData(row, Qt::Vertical).toString() << ",";
for (int col = 0; col < model()->columnCount(); ++col) {
stream << model()->index(row, col).data().toString();
if (col < model()->columnCount() - 1) {
Expand Down

0 comments on commit 16a8ef4

Please sign in to comment.