| 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 |
#ifndef AVOGADRO_QTPLUGINS_BONDCENTRICTOOL_H |
| 7 |
#define AVOGADRO_QTPLUGINS_BONDCENTRICTOOL_H |
| 8 |
|
| 9 |
#include <avogadro/qtgui/toolplugin.h> |
| 10 |
|
| 11 |
#include <avogadro/rendering/primitive.h> |
| 12 |
|
| 13 |
#include <avogadro/qtgui/persistentatom.h> |
| 14 |
#include <avogadro/qtgui/persistentbond.h> |
| 15 |
#include <avogadro/qtgui/rwmolecule.h> |
| 16 |
|
| 17 |
#include <avogadro/core/avogadrocore.h> |
| 18 |
|
| 19 |
#include <QtCore/QPoint> |
| 20 |
|
| 21 |
#include <Eigen/Geometry> |
| 22 |
|
| 23 |
#include <set> |
| 24 |
|
| 25 |
namespace Avogadro { |
| 26 |
namespace Rendering { |
| 27 |
class GeometryNode; |
| 28 |
} |
| 29 |
|
| 30 |
namespace QtPlugins { |
| 31 |
|
| 32 |
/** |
| 33 |
* @brief BondCentricTool manipulates molecular geometry by adjusting bond |
| 34 |
* angles/lengths. |
| 35 |
* |
| 36 |
* @note This class is adapted from the class of the same name in Avogadro 1.x, |
| 37 |
* written by Shahzad Ali, Ross Braithwaite, James Bunt, Marcus D. Hanwell, and |
| 38 |
* Benoit Jacob. |
| 39 |
*/ |
| 40 |
class BondCentricTool : public QtGui::ToolPlugin |
| 41 |
{ |
| 42 |
Q_OBJECT |
| 43 |
public: |
| 44 |
explicit BondCentricTool(QObject* parent_ = nullptr); |
| 45 |
~BondCentricTool() override; |
| 46 |
|
| 47 |
QString name() const override; |
| 48 |
QString description() const override; |
| 49 |
unsigned char priority() const override { return 40; } |
| 50 |
QAction* activateAction() const override { return m_activateAction; } |
| 51 |
QWidget* toolWidget() const override; |
| 52 |
void setIcon(bool darkTheme = false) override; |
| 53 |
|
| 54 |
void setMolecule(QtGui::Molecule*) override; |
| 55 |
void setEditMolecule(QtGui::RWMolecule*) override; |
| 56 |
void setGLWidget(QtOpenGL::GLWidget* widget) override; |
| 57 |
void setGLRenderer(Rendering::GLRenderer* ren) override; |
| 58 |
|
| 59 |
QUndoCommand* mousePressEvent(QMouseEvent* e) override; |
| 60 |
QUndoCommand* mouseDoubleClickEvent(QMouseEvent* e) override; |
| 61 |
QUndoCommand* mouseMoveEvent(QMouseEvent* e) override; |
| 62 |
QUndoCommand* mouseReleaseEvent(QMouseEvent* e) override; |
| 63 |
|
| 64 |
void draw(Rendering::GroupNode& node) override; |
| 65 |
|
| 66 |
private: |
| 67 |
enum MoveState |
| 68 |
{ |
| 69 |
IgnoreMove = 0, |
| 70 |
RotatePlane, |
| 71 |
RotateBondedAtom, |
| 72 |
AdjustBondLength, |
| 73 |
RotateNeighborAtom |
| 74 |
}; |
| 75 |
|
| 76 |
enum ResetBondBehavior |
| 77 |
{ |
| 78 |
KeepBond = 0, |
| 79 |
ResetBond |
| 80 |
}; |
| 81 |
void reset(ResetBondBehavior bond = ResetBond); |
| 82 |
|
| 83 |
void initializeBondVectors(); |
| 84 |
void updateBondVector(); |
| 85 |
|
| 86 |
// Mouse press event handlers: |
| 87 |
QUndoCommand* initRotatePlane(QMouseEvent* e, |
| 88 |
const Rendering::Identifier& ident); |
| 89 |
QUndoCommand* initRotateBondedAtom(QMouseEvent* e, |
| 90 |
const QtGui::RWAtom& clickedAtom); |
| 91 |
QUndoCommand* initAdjustBondLength(QMouseEvent* e, |
| 92 |
const QtGui::RWAtom& clickedAtom); |
| 93 |
QUndoCommand* initRotateNeighborAtom(QMouseEvent* e, |
| 94 |
const QtGui::RWAtom& clickedAtom, |
| 95 |
const QtGui::RWAtom& anchorAtom); |
| 96 |
|
| 97 |
// Mouse move event handlers: |
| 98 |
QUndoCommand* rotatePlane(QMouseEvent* e); |
| 99 |
QUndoCommand* rotateBondedAtom(QMouseEvent* e); |
| 100 |
QUndoCommand* adjustBondLength(QMouseEvent* e); |
| 101 |
QUndoCommand* rotateNeighborAtom(QMouseEvent* e); |
| 102 |
|
| 103 |
// Drawing helpers: |
| 104 |
void drawBondQuad(Rendering::GeometryNode& node, |
| 105 |
const QtGui::RWBond& bond) const; |
| 106 |
void drawBondAngle(Rendering::GeometryNode& node, |
| 107 |
const QtGui::RWBond& selectedBond, |
| 108 |
const QtGui::RWBond& movingBond) const; |
| 109 |
void drawBondLengthLabel(Rendering::GeometryNode& node, |
| 110 |
const QtGui::RWBond& bond); |
| 111 |
void drawAtomBondAngles(Rendering::GeometryNode& node, |
| 112 |
const QtGui::RWAtom& atom, |
| 113 |
const QtGui::RWBond& anchorBond); |
| 114 |
void drawAtomBondAngle(Rendering::GeometryNode& node, |
| 115 |
const QtGui::RWAtom& atom, |
| 116 |
const QtGui::RWBond& anchorBond, |
| 117 |
const QtGui::RWBond& otherBond, |
| 118 |
const Vector3ub& color); |
| 119 |
|
| 120 |
// Bond utilities |
| 121 |
bool bondContainsAtom(const QtGui::RWBond& bond, |
| 122 |
const QtGui::RWAtom& atom) const; |
| 123 |
|
| 124 |
// The 'fragment' is the SkeletonTree of the 1.x implementation. It is a list |
| 125 |
// of atoms created by buildFragment(bond, startAtom), which walks the bonds |
| 126 |
// connected to startAtom (not including the passed-in bond), adding each |
| 127 |
// atom it encounters to the list, and then walking that atom's bonds. If a |
| 128 |
// cycle is detected, only startAtom is added to m_fragment. |
| 129 |
void resetFragment() { m_fragment.clear(); } |
| 130 |
bool fragmentHasAtom(int uid) const; |
| 131 |
void buildFragment(const QtGui::RWBond& bond, const QtGui::RWAtom& startAtom); |
| 132 |
bool buildFragmentRecurse(const QtGui::RWBond& bond, |
| 133 |
const QtGui::RWAtom& startAtom, |
| 134 |
const QtGui::RWAtom& currentAtom); |
| 135 |
// Use transformFragment to transform the position of each atom in the |
| 136 |
// fragment by m_transform. |
| 137 |
void transformFragment() const; |
| 138 |
|
| 139 |
QAction* m_activateAction; |
| 140 |
QtGui::RWMolecule* m_molecule; |
| 141 |
Rendering::GLRenderer* m_renderer; |
| 142 |
MoveState m_moveState; |
| 143 |
QPoint m_clickedPoint; |
| 144 |
QPoint m_lastDragPoint; |
| 145 |
Vector3f m_bondVector; |
| 146 |
Vector3f m_planeNormalMouse; |
| 147 |
Vector3f m_planeNormal; |
| 148 |
|
| 149 |
// unique ids of atoms that will need to be moved: |
| 150 |
std::vector<int> m_fragment; |
| 151 |
Eigen::Affine3f m_transform; |
| 152 |
|
| 153 |
// Snap angles for RotatePlane. Angles are relative to m_planeSnapRef and |
| 154 |
// follow a right-hand rule around m_bondVector. Range is [-180, 180). |
| 155 |
std::set<float> m_planeSnapAngles; |
| 156 |
float m_planeSnapIncr; |
| 157 |
Vector3f m_planeSnapRef; |
| 158 |
bool m_snapPlaneToBonds; |
| 159 |
void updatePlaneSnapAngles(); |
| 160 |
void updateSnappedPlaneNormal(); |
| 161 |
|
| 162 |
QtGui::RWMolecule::PersistentBondType m_selectedBond; |
| 163 |
QtGui::RWMolecule::PersistentAtomType m_anchorAtom; |
| 164 |
QtGui::RWMolecule::PersistentAtomType m_clickedAtom; |
| 165 |
}; |
| 166 |
|
| 167 |
inline QString BondCentricTool::name() const |
| 168 |
{ |
| 169 |
return tr("Bond centric manipulation tool."); |
| 170 |
} |
| 171 |
|
| 172 |
inline QString BondCentricTool::description() const |
| 173 |
{ |
| 174 |
return tr("Tool used to edit molecular geometry by changing bond lengths and " |
| 175 |
"angles."); |
| 176 |
} |
| 177 |
|
| 178 |
} // namespace QtPlugins |
| 179 |
} // namespace Avogadro |
| 180 |
|
| 181 |
#endif // AVOGADRO_QTPLUGINS_BONDCENTRICTOOL_H |