Skip to content

Commit

Permalink
Update the dipole moment only when the molecule changes
Browse files Browse the repository at this point in the history
Not a full solution yet - loading a molecule doesn't recompute

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Dec 6, 2024
1 parent 96abbe8 commit 2a1cc30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
57 changes: 41 additions & 16 deletions avogadro/qtplugins/dipole/dipole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ Dipole::~Dipole() {}
void Dipole::process(const QtGui::Molecule& molecule,
Rendering::GroupNode& node)
{
// check if the molecule is empty
// (single atoms don't have a dipole moment)
if (molecule.atomCount() < 0)
return;

// check if the molecule has the dipole set
if (!m_customDipole) {
if (molecule.hasData("dipoleMoment")) {
m_dipoleVector = molecule.data("dipoleMoment").toVector3();
} else {
// connect to molecule changes
connect(&molecule, &QtGui::Molecule::update, this, &Dipole::updateDipole);
connect(&molecule, SIGNAL(changed(unsigned int)), SLOT(updateDipole()));
}
} else {
// custom dipole moment set
m_dipoleVector = m_customDipoleVector;
}

// okay if we have all that, set up the arrow
auto* geometry = new GeometryNode;
node.addChild(geometry);

Expand All @@ -44,28 +64,33 @@ void Dipole::process(const QtGui::Molecule& molecule,
geometry->addDrawable(arrow);

Vector3f origin = Vector3f::Zero();

// check if the molecule has the dipole set
if (!m_customDipole) {
if (molecule.hasData("dipoleMoment")) {
m_dipoleVector = molecule.data("dipoleMoment").toVector3();
} else {
if (!molecule.isInteractive() && m_updateNeeded) {
m_updateNeeded = false;
// 500ms delay to allow the molecule to be updated
QTimer::singleShot(500, this, &Dipole::updateFinished);
m_dipoleVector =
Calc::ChargeManager::instance().dipoleMoment(m_type, molecule);
}
}
}

arrow->addSingleArrow(m_dipoleVector.cast<float>(), origin);
}

void Dipole::updateFinished()
{
m_updateNeeded = true;
emit drawablesChanged();
}

void Dipole::updateDipole()
{
QtGui::Molecule* molecule = qobject_cast<QtGui::Molecule*>(sender());
if (molecule == nullptr || molecule->isInteractive())
return;

// if the molecule has a dipole moment set, use it
if (molecule->hasData("dipoleMoment"))
return;

// otherwise, calculate it
if (m_updateNeeded) {
m_updateNeeded = false;
m_dipoleVector =
Calc::ChargeManager::instance().dipoleMoment(m_type, *molecule);
// single-shot
QTimer::singleShot(0, this, SLOT(updateFinished()));
}
}

QWidget* Dipole::setupWidget()
Expand Down
2 changes: 2 additions & 0 deletions avogadro/qtplugins/dipole/dipole.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class Dipole : public QtGui::ScenePlugin
bool hasSetupWidget() const override { return false; }

public slots:
void updateDipole();
void updateFinished();

private:
std::string m_name = "Dipole Moment";
std::string m_type = "MMFF94";
std::vector<std::string> m_types;
Vector3 m_dipoleVector;
Vector3 m_customDipoleVector;
bool m_customDipole = false; // Custom dipole moment set
bool m_updateNeeded = true;
};
Expand Down

1 comment on commit 2a1cc30

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERROR: clang-format-diff detected formatting issues. See the artifact for a patch or run clang-format on your branch.

Please sign in to comment.