Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit molecule name, charge, and spin in properties #1810

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 50 additions & 8 deletions avogadro/qtplugins/molecularproperties/molecularmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@
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,19 @@
// for the types and columns that can be edited
auto editable = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;

int row = index.row();
int col = index.column();
Fixed Show fixed Hide fixed

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 +362,35 @@
if (role != Qt::EditRole)
return false;

// TODO allow editing name, total charge, total spin multiplicity
int row = index.row();
int col = index.column();
Fixed Show fixed Hide fixed

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 < 0)
return false;

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

return false;
}

Expand Down Expand Up @@ -391,11 +433,11 @@
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
Loading