| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "dipole.h" |
| 7 |
|
| 8 |
#include <avogadro/calc/chargemanager.h> |
| 9 |
#include <avogadro/core/elements.h> |
| 10 |
#include <avogadro/qtgui/molecule.h> |
| 11 |
#include <avogadro/qtgui/rwmolecule.h> |
| 12 |
#include <avogadro/rendering/arrowgeometry.h> |
| 13 |
#include <avogadro/rendering/geometrynode.h> |
| 14 |
#include <avogadro/rendering/groupnode.h> |
| 15 |
|
| 16 |
#include <QDebug> |
| 17 |
#include <QTimer> |
| 18 |
#include <QtWidgets/QWidget> |
| 19 |
|
| 20 |
namespace Avogadro::QtPlugins { |
| 21 |
|
| 22 |
using QtGui::Molecule; |
| 23 |
using Rendering::ArrowGeometry; |
| 24 |
using Rendering::GeometryNode; |
| 25 |
using Rendering::GroupNode; |
| 26 |
|
| 27 |
Dipole::Dipole(QObject* p) : ScenePlugin(p) |
| 28 |
{ |
| 29 |
m_layerManager = QtGui::PluginLayerManager(m_name); |
| 30 |
} |
| 31 |
|
| 32 |
Dipole::~Dipole() {} |
| 33 |
|
| 34 |
void Dipole::process(const QtGui::Molecule& molecule, |
| 35 |
Rendering::GroupNode& node) |
| 36 |
{ |
| 37 |
// check if the molecule is empty |
| 38 |
// (single atoms don't have a dipole moment) |
| 39 |
if (molecule.atomCount() < 2) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
// check if the molecule has the dipole set |
| 44 |
if (!m_customDipole) { |
| 45 |
if (molecule.hasData("dipoleMoment")) { |
| 46 |
m_dipoleVector = molecule.data("dipoleMoment").toVector3(); |
| 47 |
} else { |
| 48 |
// connect to molecule changes |
| 49 |
connect(&molecule, &QtGui::Molecule::update, this, &Dipole::updateDipole); |
| 50 |
connect(&molecule, SIGNAL(changed(unsigned int)), SLOT(updateDipole())); |
| 51 |
} |
| 52 |
} else { |
| 53 |
// custom dipole moment set |
| 54 |
m_dipoleVector = m_customDipoleVector; |
| 55 |
} |
| 56 |
|
| 57 |
// okay if we have all that, set up the arrow |
| 58 |
auto* geometry = new GeometryNode; |
| 59 |
node.addChild(geometry); |
| 60 |
|
| 61 |
auto* arrow = new ArrowGeometry; |
| 62 |
arrow->identifier().molecule = &molecule; |
| 63 |
|
| 64 |
arrow->setColor(Vector3ub(255, 0, 0)); |
| 65 |
geometry->addDrawable(arrow); |
| 66 |
|
| 67 |
Vector3f origin = Vector3f::Zero(); |
| 68 |
arrow->addSingleArrow(m_dipoleVector.cast<float>(), origin); |
| 69 |
} |
| 70 |
|
| 71 |
void Dipole::updateFinished() |
| 72 |
{ |
| 73 |
m_updateNeeded = true; |
| 74 |
emit drawablesChanged(); |
| 75 |
} |
| 76 |
|
| 77 |
void Dipole::updateDipole() |
| 78 |
{ |
| 79 |
QtGui::Molecule* molecule = qobject_cast<QtGui::Molecule*>(sender()); |
| 80 |
if (molecule == nullptr || molecule->isInteractive()) |
| 81 |
return; |
| 82 |
|
| 83 |
// if the molecule has a dipole moment set, use it |
| 84 |
if (molecule->hasData("dipoleMoment")) |
| 85 |
return; |
| 86 |
|
| 87 |
// otherwise, calculate it |
| 88 |
if (m_updateNeeded) { |
| 89 |
m_updateNeeded = false; |
| 90 |
m_dipoleVector = |
| 91 |
Calc::ChargeManager::instance().dipoleMoment(m_type, *molecule); |
| 92 |
// single-shot |
| 93 |
QTimer::singleShot(0, this, SLOT(updateFinished())); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
QWidget* Dipole::setupWidget() |
| 98 |
{ |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
} // namespace Avogadro::QtPlugins |