| 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 "measuretool.h" |
| 7 |
|
| 8 |
#include <avogadro/qtopengl/glwidget.h> |
| 9 |
|
| 10 |
#include <avogadro/rendering/geometrynode.h> |
| 11 |
#include <avogadro/rendering/glrenderer.h> |
| 12 |
#include <avogadro/rendering/groupnode.h> |
| 13 |
#include <avogadro/rendering/scene.h> |
| 14 |
#include <avogadro/rendering/textlabel2d.h> |
| 15 |
#include <avogadro/rendering/textlabel3d.h> |
| 16 |
#include <avogadro/rendering/textproperties.h> |
| 17 |
|
| 18 |
#include <avogadro/core/atom.h> |
| 19 |
#include <avogadro/core/contrastcolor.h> |
| 20 |
#include <avogadro/core/elements.h> |
| 21 |
#include <avogadro/core/vector.h> |
| 22 |
#include <avogadro/qtgui/molecule.h> |
| 23 |
#include <avogadro/qtgui/rwmolecule.h> |
| 24 |
|
| 25 |
#include <avogadro/core/angletools.h> |
| 26 |
|
| 27 |
#include <QAction> |
| 28 |
#include <QtGui/QGuiApplication> |
| 29 |
#include <QtGui/QIcon> |
| 30 |
#include <QtGui/QMouseEvent> |
| 31 |
|
| 32 |
#include <QDebug> |
| 33 |
|
| 34 |
#include <cmath> |
| 35 |
|
| 36 |
using Avogadro::Core::contrastColor; |
| 37 |
using Avogadro::Core::Elements; |
| 38 |
using Avogadro::Rendering::GeometryNode; |
| 39 |
using Avogadro::Rendering::GroupNode; |
| 40 |
using Avogadro::Rendering::Identifier; |
| 41 |
using Avogadro::Rendering::TextLabel2D; |
| 42 |
using Avogadro::Rendering::TextLabel3D; |
| 43 |
using Avogadro::Rendering::TextProperties; |
| 44 |
|
| 45 |
namespace Avogadro::QtPlugins { |
| 46 |
|
| 47 |
MeasureTool::MeasureTool(QObject* parent_) |
| 48 |
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), |
| 49 |
m_molecule(nullptr), m_rwMolecule(nullptr), m_renderer(nullptr) |
| 50 |
{ |
| 51 |
QString shortcut = tr("Ctrl+8", "control-key 8"); |
| 52 |
m_activateAction->setText(tr("Measure")); |
| 53 |
m_activateAction->setToolTip( |
| 54 |
tr("Measure Tool\t(%1)\n\n" |
| 55 |
"Left Mouse:\tSelect up to four Atoms.\n" |
| 56 |
"\tDistances are measured between 1-2 and 2-3\n" |
| 57 |
"\tAngle is measured between 1-3 using 2 as the common point\n" |
| 58 |
"\tDihedral is measured between 1-2-3-4\n" |
| 59 |
"Right Mouse:\tReset the measurements.") |
| 60 |
.arg(shortcut)); |
| 61 |
setIcon(); |
| 62 |
} |
| 63 |
|
| 64 |
MeasureTool::~MeasureTool() {} |
| 65 |
|
| 66 |
void MeasureTool::setIcon(bool darkTheme) |
| 67 |
{ |
| 68 |
if (darkTheme) |
| 69 |
m_activateAction->setIcon(QIcon(":/icons/measure_dark.svg")); |
| 70 |
else |
| 71 |
m_activateAction->setIcon(QIcon(":/icons/measure_light.svg")); |
| 72 |
} |
| 73 |
|
| 74 |
QWidget* MeasureTool::toolWidget() const |
| 75 |
{ |
| 76 |
return nullptr; |
| 77 |
} |
| 78 |
|
| 79 |
QUndoCommand* MeasureTool::mousePressEvent(QMouseEvent* e) |
| 80 |
{ |
| 81 |
if (e->button() != Qt::LeftButton || !m_renderer) |
| 82 |
return nullptr; |
| 83 |
|
| 84 |
Identifier hit = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 85 |
|
| 86 |
// If an atom is clicked, accept the event, but don't add it to the atom list |
| 87 |
// until the button is released (this way the user can cancel the click by |
| 88 |
// moving off the atom, and the click won't get passed to the default tool). |
| 89 |
if (hit.type == Rendering::AtomType) |
| 90 |
e->accept(); |
| 91 |
|
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
|
| 95 |
QUndoCommand* MeasureTool::mouseReleaseEvent(QMouseEvent* e) |
| 96 |
{ |
| 97 |
// If the click is released on an atom, add it to the list |
| 98 |
if (e->button() != Qt::LeftButton || !m_renderer) |
| 99 |
return nullptr; |
| 100 |
|
| 101 |
Identifier hit = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 102 |
|
| 103 |
// Now add the atom on release. |
| 104 |
if (hit.type == Rendering::AtomType) { |
| 105 |
if (toggleAtom(hit)) |
| 106 |
emit drawablesChanged(); |
| 107 |
e->accept(); |
| 108 |
} |
| 109 |
|
| 110 |
return nullptr; |
| 111 |
} |
| 112 |
|
| 113 |
QUndoCommand* MeasureTool::mouseDoubleClickEvent(QMouseEvent* e) |
| 114 |
{ |
| 115 |
// Reset the atom list |
| 116 |
if (e->button() == Qt::LeftButton && !m_atoms.isEmpty()) { |
| 117 |
m_atoms.clear(); |
| 118 |
emit drawablesChanged(); |
| 119 |
e->accept(); |
| 120 |
} |
| 121 |
return nullptr; |
| 122 |
} |
| 123 |
|
| 124 |
template <typename T> |
| 125 |
void MeasureTool::createLabels(T* mol, GeometryNode* geo, |
| 126 |
QVector<Vector3>& positions) |
| 127 |
{ |
| 128 |
TextProperties atomLabelProp; |
| 129 |
atomLabelProp.setFontFamily(TextProperties::SansSerif); |
| 130 |
atomLabelProp.setAlign(TextProperties::HCenter, TextProperties::VCenter); |
| 131 |
|
| 132 |
for (int i = 0; i < m_atoms.size(); ++i) { |
| 133 |
Identifier& ident = m_atoms[i]; |
| 134 |
Q_ASSERT(ident.type == Rendering::AtomType); |
| 135 |
Q_ASSERT(ident.molecule != nullptr); |
| 136 |
|
| 137 |
typename T::AtomType atom = mol->atom(ident.index); |
| 138 |
Q_ASSERT(atom.isValid()); |
| 139 |
unsigned char atomicNumber(atom.atomicNumber()); |
| 140 |
positions[i] = atom.position3d(); |
| 141 |
|
| 142 |
const unsigned char* color = Elements::color(atomicNumber); |
| 143 |
atomLabelProp.setColorRgb(contrastColor(Vector3ub(color)).data()); |
| 144 |
|
| 145 |
auto* label = new TextLabel3D; |
| 146 |
label->setText(QString("#%1").arg(i + 1).toStdString()); |
| 147 |
label->setTextProperties(atomLabelProp); |
| 148 |
label->setAnchor(positions[i].cast<float>()); |
| 149 |
label->setRadius( |
| 150 |
static_cast<float>(Elements::radiusCovalent(atomicNumber)) + 0.1f); |
| 151 |
geo->addDrawable(label); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void MeasureTool::draw(Rendering::GroupNode& node) |
| 156 |
{ |
| 157 |
if (m_atoms.size() == 0) |
| 158 |
return; |
| 159 |
|
| 160 |
auto* geo = new GeometryNode; |
| 161 |
node.addChild(geo); |
| 162 |
|
| 163 |
// Add labels, extract positions |
| 164 |
QVector<Vector3> positions(m_atoms.size(), Vector3()); |
| 165 |
if (m_molecule) |
| 166 |
createLabels(m_molecule, geo, positions); |
| 167 |
else if (m_rwMolecule) |
| 168 |
createLabels(m_rwMolecule, geo, positions); |
| 169 |
|
| 170 |
// Calculate angles and distances |
| 171 |
Vector3 v1; |
| 172 |
Vector3 v2; |
| 173 |
Vector3 v3; |
| 174 |
Real v1Norm = -1.f; |
| 175 |
Real v2Norm = -1.f; |
| 176 |
Real v3Norm = -1.f; |
| 177 |
|
| 178 |
switch (m_atoms.size()) { |
| 179 |
case 4: |
| 180 |
v3 = positions[3] - positions[2]; |
| 181 |
v3Norm = v3.norm(); |
| 182 |
[[fallthrough]]; |
| 183 |
case 3: |
| 184 |
v2 = positions[2] - positions[1]; |
| 185 |
v2Norm = v2.norm(); |
| 186 |
[[fallthrough]]; |
| 187 |
case 2: |
| 188 |
v1 = positions[1] - positions[0]; |
| 189 |
v1Norm = v1.norm(); |
| 190 |
[[fallthrough]]; |
| 191 |
default: |
| 192 |
break; |
| 193 |
} |
| 194 |
|
| 195 |
QString overlayText; |
| 196 |
float angle23 = 361.f; |
| 197 |
float angle12 = 361.f; |
| 198 |
QString dihedralLabel = tr("Dihedral:"); |
| 199 |
QString angleLabel = tr("Angle:"); |
| 200 |
QString distanceLabel = tr("Distance:"); |
| 201 |
// Use the longest label size to determine the field width. Negate it to |
| 202 |
// indicate left-alignment. |
| 203 |
int labelWidth = -std::max( |
| 204 |
{ dihedralLabel.size(), angleLabel.size(), distanceLabel.size() }); |
| 205 |
switch (m_atoms.size()) { |
| 206 |
case 4: |
| 207 |
overlayText += QString("%1 %L2°\n") |
| 208 |
.arg(tr("Dihedral:"), labelWidth) |
| 209 |
.arg(dihedralAngle(v1, v2, v3), 9, 'f', 3); |
| 210 |
angle23 = bondAngle(v2, v3); |
| 211 |
[[fallthrough]]; |
| 212 |
case 3: |
| 213 |
angle12 = bondAngle(v1, v2); |
| 214 |
overlayText += QString("%1 %L2°") |
| 215 |
.arg(tr("Angle:"), labelWidth) |
| 216 |
.arg(angle12, 9, 'f', 3); |
| 217 |
if (angle23 < 360.f) |
| 218 |
overlayText += QString(" %L1°").arg(angle23, 9, 'f', 3); |
| 219 |
overlayText += '\n'; |
| 220 |
[[fallthrough]]; |
| 221 |
case 2: |
| 222 |
overlayText += QString("%1 %L2 Å") |
| 223 |
.arg(tr("Distance:"), labelWidth) |
| 224 |
.arg(v1Norm, 9, 'f', 3); |
| 225 |
if (v2Norm >= 0.f) |
| 226 |
overlayText += QString(" %L1 Å").arg(v2Norm, 9, 'f', 3); |
| 227 |
if (v3Norm >= 0.f) |
| 228 |
overlayText += QString(" %L1 Å").arg(v3Norm, 9, 'f', 3); |
| 229 |
[[fallthrough]]; |
| 230 |
default: |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
if (overlayText.isEmpty()) |
| 235 |
return; |
| 236 |
|
| 237 |
TextProperties overlayTProp; |
| 238 |
overlayTProp.setFontFamily(TextProperties::Mono); |
| 239 |
|
| 240 |
Vector3ub color(64, 255, 220); |
| 241 |
if (m_renderer) { |
| 242 |
auto backgroundColor = m_renderer->scene().backgroundColor(); |
| 243 |
color = contrastColor( |
| 244 |
Vector3ub(backgroundColor[0], backgroundColor[1], backgroundColor[2])); |
| 245 |
} |
| 246 |
|
| 247 |
overlayTProp.setColorRgb(color[0], color[1], color[2]); |
| 248 |
overlayTProp.setAlign(TextProperties::HLeft, TextProperties::VBottom); |
| 249 |
|
| 250 |
auto* label = new TextLabel2D; |
| 251 |
label->setText(overlayText.toStdString()); |
| 252 |
label->setTextProperties(overlayTProp); |
| 253 |
label->setRenderPass(Rendering::Overlay2DPass); |
| 254 |
label->setAnchor(Vector2i(10, 10)); |
| 255 |
|
| 256 |
geo->addDrawable(label); |
| 257 |
} |
| 258 |
|
| 259 |
bool MeasureTool::toggleAtom(const Rendering::Identifier& atom) |
| 260 |
{ |
| 261 |
int ind = m_atoms.indexOf(atom); |
| 262 |
if (ind >= 0) { |
| 263 |
m_atoms.remove(ind); |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
if (m_atoms.size() >= 4) |
| 268 |
return false; |
| 269 |
|
| 270 |
m_atoms.push_back(atom); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
} // namespace Avogadro::QtPlugins |