Debian Codegraph @

avogadro/qtplugins/manipulator/manipulator.cpp

raw
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 "manipulator.h"
7 #include "ui_manipulatewidget.h"
8
9 #include <avogadro/core/vector.h>
10
11 #include <avogadro/qtgui/molecule.h>
12 #include <avogadro/qtgui/rwmolecule.h>
13
14 #include <avogadro/qtopengl/glwidget.h>
15
16 #include <avogadro/rendering/camera.h>
17 #include <avogadro/rendering/glrenderer.h>
18
19 #include <QAction>
20 #include <QtGui/QIcon>
21 #include <QtGui/QKeyEvent>
22 #include <QtGui/QMouseEvent>
23 #include <QtGui/QWheelEvent>
24
25 using Avogadro::QtGui::Molecule;
26
27 namespace Avogadro::QtPlugins {
28
29 using QtGui::Molecule;
30 using QtGui::RWAtom;
31
32 #define ROTATION_SPEED 0.5
33
34 class ManipulateWidget : public QWidget, public Ui::ManipulateWidget
35 {
36 public:
37 ManipulateWidget(QWidget* parent = nullptr) : QWidget(parent)
38 {
39 setupUi(this);
40 }
41 };
42
43 Manipulator::Manipulator(QObject* parent_)
44 : QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)),
45 m_molecule(nullptr), m_renderer(nullptr), m_pressedButtons(Qt::NoButton),
46 m_toolWidget(new ManipulateWidget(dynamic_cast<QWidget*>(parent_))),
47 m_currentAction(Nothing)
48 {
49 QString shortcut = tr("Ctrl+5", "control-key 5");
50 m_activateAction->setText(tr("Manipulate"));
51 m_activateAction->setToolTip(
52 tr("Manipulation Tool\t(%1)\n\n"
53 "Left Mouse:\tClick and drag to move atoms\n"
54 "Right Mouse:\tClick and drag to rotate atoms.")
55 .arg(shortcut));
56 setIcon();
57 connect(m_toolWidget->buttonBox, SIGNAL(clicked(QAbstractButton*)), this,
58 SLOT(buttonClicked(QAbstractButton*)));
59 }
60
61 Manipulator::~Manipulator() {}
62
63 void Manipulator::setIcon(bool darkTheme)
64 {
65 if (darkTheme)
66 m_activateAction->setIcon(QIcon(":/icons/manipulator_dark.svg"));
67 else
68 m_activateAction->setIcon(QIcon(":/icons/manipulator_light.svg"));
69 }
70
71 QWidget* Manipulator::toolWidget() const
72 {
73 return m_toolWidget;
74 }
75
76 void Manipulator::buttonClicked(QAbstractButton* button)
77 {
78 if (m_toolWidget == nullptr)
79 return;
80
81 // clear focus from the boxes (they eat up keystrokes)
82 m_toolWidget->xTranslateSpinBox->clearFocus();
83 m_toolWidget->yTranslateSpinBox->clearFocus();
84 m_toolWidget->zTranslateSpinBox->clearFocus();
85
86 m_toolWidget->xRotateSpinBox->clearFocus();
87 m_toolWidget->yRotateSpinBox->clearFocus();
88 m_toolWidget->zRotateSpinBox->clearFocus();
89
90 if (m_toolWidget->buttonBox->buttonRole(button) !=
91 QDialogButtonBox::ApplyRole) {
92 // reset values
93 m_toolWidget->xTranslateSpinBox->setValue(0.0);
94 m_toolWidget->yTranslateSpinBox->setValue(0.0);
95 m_toolWidget->zTranslateSpinBox->setValue(0.0);
96
97 m_toolWidget->xRotateSpinBox->setValue(0.0);
98 m_toolWidget->yRotateSpinBox->setValue(0.0);
99 m_toolWidget->zRotateSpinBox->setValue(0.0);
100
101 return;
102 }
103
104 bool moveSelected = (m_toolWidget->moveComboBox->currentIndex() == 0);
105
106 // apply values
107 Vector3 delta(m_toolWidget->xTranslateSpinBox->value(),
108 m_toolWidget->yTranslateSpinBox->value(),
109 m_toolWidget->zTranslateSpinBox->value());
110
111 translate(delta, moveSelected);
112
113 Vector3 rotation(m_toolWidget->xRotateSpinBox->value(),
114 m_toolWidget->yRotateSpinBox->value(),
115 m_toolWidget->zRotateSpinBox->value());
116 Vector3 center(0.0, 0.0, 0.0);
117
118 // Check if we're rotating around the origin, the molecule centroid
119 // or the center of selected atoms
120 // == 0 is the default = origin
121 if (m_toolWidget->rotateComboBox->currentIndex() == 1) {
122 // molecule centroid
123 center = m_molecule->molecule().centerOfGeometry();
124 } else if (m_toolWidget->rotateComboBox->currentIndex() == 2) {
125 // center of selected atoms
126 unsigned long selectedAtomCount = 0;
127 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
128 if (!m_molecule->atomSelected(i))
129 continue;
130
131 center += m_molecule->atomPosition3d(i);
132 selectedAtomCount++;
133 }
134 if (selectedAtomCount > 0)
135 center /= selectedAtomCount;
136 }
137
138 // Settings are in degrees
139 #ifndef DEG_TO_RAD
140 #define DEG_TO_RAD 0.0174532925
141 #endif
142 axisRotate(rotation * DEG_TO_RAD, center, moveSelected);
143
144 m_molecule->emitChanged(Molecule::Atoms | Molecule::Modified);
145 }
146
147 QUndoCommand* Manipulator::keyPressEvent(QKeyEvent* e)
148 {
149 switch (e->key()) {
150 case Qt::Key_Left:
151 case Qt::Key_H:
152 case Qt::Key_A:
153 translate(Vector3(-0.1, 0.0, 0.0));
154 e->accept();
155 break;
156 case Qt::Key_Right:
157 case Qt::Key_L:
158 case Qt::Key_D:
159 translate(Vector3(+0.1, 0.0, 0.0));
160 e->accept();
161 break;
162 case Qt::Key_Up:
163 case Qt::Key_K:
164 case Qt::Key_W:
165 translate(Vector3(0.0, +0.1, 0.0));
166 e->accept();
167 break;
168 case Qt::Key_Down:
169 case Qt::Key_J:
170 case Qt::Key_S:
171 translate(Vector3(0.0, -0.1, 0.0));
172 e->accept();
173 break;
174 default:
175 e->ignore();
176 }
177 m_molecule->emitChanged(Molecule::Atoms | Molecule::Modified);
178 return nullptr;
179 }
180
181 QUndoCommand* Manipulator::mousePressEvent(QMouseEvent* e)
182 {
183 if (!m_renderer)
184 return nullptr;
185
186 updatePressedButtons(e, false);
187 m_lastMousePosition = e->pos();
188 Vector2f windowPos(e->localPos().x(), e->localPos().y());
189 m_lastMouse3D = m_renderer->camera().unProject(windowPos);
190
191 if (m_molecule) {
192 m_molecule->setInteractive(true);
193 }
194
195 if (m_pressedButtons & Qt::LeftButton) {
196 m_object = m_renderer->hit(e->pos().x(), e->pos().y());
197
198 switch (m_object.type) {
199 case Rendering::AtomType:
200 e->accept();
201 return nullptr;
202 default:
203 break;
204 }
205 }
206
207 return nullptr;
208 }
209
210 QUndoCommand* Manipulator::mouseReleaseEvent(QMouseEvent* e)
211 {
212 if (!m_renderer)
213 return nullptr;
214
215 updatePressedButtons(e, true);
216
217 if (m_object.type == Rendering::InvalidType)
218 return nullptr;
219
220 if (m_molecule) {
221 m_molecule->setInteractive(false);
222 }
223
224 switch (e->button()) {
225 case Qt::LeftButton:
226 case Qt::RightButton:
227 resetObject();
228 e->accept();
229 break;
230 default:
231 break;
232 }
233
234 return nullptr;
235 }
236
237 QUndoCommand* Manipulator::mouseMoveEvent(QMouseEvent* e)
238 {
239 // if we're dragging through empty space, just return and ignore
240 // (e.g., fall back to the navigate tool)
241 const Core::Molecule* mol = &m_molecule->molecule();
242 if (mol->isSelectionEmpty() && m_object.type == Rendering::InvalidType) {
243 e->ignore();
244 return nullptr;
245 }
246
247 updatePressedButtons(e, false);
248 e->ignore();
249
250 Vector2f windowPos(e->localPos().x(), e->localPos().y());
251
252 if (mol->isSelectionEmpty() && m_object.type == Rendering::AtomType &&
253 m_object.molecule == &m_molecule->molecule()) {
254 // translate single atom position
255 RWAtom atom = m_molecule->atom(m_object.index);
256 Vector3f oldPos(atom.position3d().cast<float>());
257 Vector3f newPos = m_renderer->camera().unProject(windowPos, oldPos);
258 atom.setPosition3d(newPos.cast<double>());
259 } else if (!mol->isSelectionEmpty()) {
260 // update all selected atoms
261 Vector3f newPos = m_renderer->camera().unProject(windowPos);
262 Vector3 delta = (newPos - m_lastMouse3D).cast<double>();
263
264 if (m_currentAction == Translation) {
265 translate(delta);
266 } else {
267 // get the center of the selected atoms
268 Vector3 centroid(0.0, 0.0, 0.0);
269 unsigned long selectedAtomCount = 0;
270 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
271 if (!m_molecule->atomSelected(i))
272 continue;
273
274 centroid += m_molecule->atomPosition3d(i);
275 selectedAtomCount++;
276 }
277 if (selectedAtomCount > 0)
278 centroid /= selectedAtomCount;
279
280 if (m_currentAction == Rotation) {
281 rotate(delta, centroid);
282 } else if (m_currentAction == ZoomTilt) {
283 tilt(delta, centroid);
284 }
285 }
286
287 // now that we've moved things, save the position
288 m_lastMouse3D = newPos;
289 }
290
291 m_molecule->emitChanged(Molecule::Atoms | Molecule::Modified);
292 e->accept();
293 return nullptr;
294 }
295
296 void Manipulator::translate(Vector3 delta, bool moveSelected)
297 {
298 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
299 if (moveSelected && !m_molecule->atomSelected(i))
300 continue;
301 else if (!moveSelected && m_molecule->atomSelected(i))
302 continue;
303
304 Vector3 currentPos = m_molecule->atomPosition3d(i);
305 m_molecule->setAtomPosition3d(i, currentPos + delta.cast<double>());
306 }
307 }
308
309 void Manipulator::rotate(Vector3 delta, Vector3 centroid, bool moveSelected)
310 {
311 // Rotate the selected atoms about the center
312 // rotate only selected primitives
313 Rendering::Camera* camera = &m_renderer->camera();
314 Eigen::Vector3d backTransformX =
315 camera->modelView().cast<double>().linear().row(0).transpose().normalized();
316 Eigen::Vector3d backTransformY =
317 camera->modelView().cast<double>().linear().row(1).transpose().normalized();
318
319 Eigen::Projective3d fragmentRotation;
320 fragmentRotation.matrix().setIdentity();
321 fragmentRotation.translation() = centroid;
322 fragmentRotation.rotate(
323 Eigen::AngleAxisd(delta[1] * ROTATION_SPEED, backTransformX));
324 fragmentRotation.rotate(
325 Eigen::AngleAxisd(delta[0] * ROTATION_SPEED, backTransformY));
326 fragmentRotation.translate(-centroid);
327
328 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
329 if (moveSelected && !m_molecule->atomSelected(i))
330 continue;
331 else if (!moveSelected && m_molecule->atomSelected(i))
332 continue;
333
334 Vector3 currentPos = m_molecule->atomPosition3d(i);
335 m_molecule->setAtomPosition3d(
336 i, (fragmentRotation * currentPos.homogeneous()).head<3>());
337 }
338 }
339
340 void Manipulator::axisRotate(Vector3 delta, Vector3 centroid, bool moveSelected)
341 {
342 // rotate by the x, y, z axes by delta[0], delta[1], delta[2]
343 // (in radians)
344 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
345 if (moveSelected && !m_molecule->atomSelected(i))
346 continue;
347 else if (!moveSelected && m_molecule->atomSelected(i))
348 continue;
349
350 Vector3 currentPos = m_molecule->atomPosition3d(i);
351 Eigen::Projective3d fragmentRotation;
352 fragmentRotation.matrix().setIdentity();
353 fragmentRotation.translation() = centroid;
354
355 // Rotate around the x-axis
356 fragmentRotation.rotate(
357 Eigen::AngleAxisd(delta[0], Vector3(1.0, 0.0, 0.0)));
358 // Rotate around the y-axis
359 fragmentRotation.rotate(
360 Eigen::AngleAxisd(delta[1], Vector3(0.0, 1.0, 0.0)));
361 // Rotate around the z-axis
362 fragmentRotation.rotate(
363 Eigen::AngleAxisd(delta[2], Vector3(0.0, 0.0, 1.0)));
364
365 fragmentRotation.translate(-centroid);
366 m_molecule->setAtomPosition3d(
367 i, (fragmentRotation * currentPos.homogeneous()).head<3>());
368 }
369 }
370
371 void Manipulator::tilt(Vector3 delta, Vector3 centroid)
372 {
373 // Rotate the selected atoms about the center
374 // rotate only selected primitives
375 Rendering::Camera* camera = &m_renderer->camera();
376 Eigen::Vector3d backTransformZ =
377 camera->modelView().cast<double>().linear().row(2).transpose().normalized();
378
379 Eigen::Projective3d fragmentRotation;
380 fragmentRotation.matrix().setIdentity();
381 fragmentRotation.translation() = centroid;
382 fragmentRotation.rotate(
383 Eigen::AngleAxisd(delta[0] * ROTATION_SPEED, backTransformZ));
384 fragmentRotation.translate(-centroid);
385
386 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
387 if (!m_molecule->atomSelected(i))
388 continue;
389
390 Vector3 currentPos = m_molecule->atomPosition3d(i);
391 m_molecule->setAtomPosition3d(
392 i, (fragmentRotation * currentPos.homogeneous()).head<3>());
393 }
394 }
395
396 void Manipulator::updatePressedButtons(QMouseEvent* e, bool release)
397 {
398 if (release)
399 m_pressedButtons &= e->buttons();
400 else
401 m_pressedButtons |= e->buttons();
402
403 // check for modifier keys (e.g., Mac)
404 if (e->buttons() & Qt::LeftButton && e->modifiers() == Qt::NoModifier) {
405 m_currentAction = Translation;
406 } else if (e->buttons() & Qt::MiddleButton ||
407 (e->buttons() & Qt::LeftButton &&
408 e->modifiers() == Qt::ShiftModifier)) {
409 m_currentAction = ZoomTilt;
410 } else if (e->buttons() & Qt::RightButton ||
411 (e->buttons() & Qt::LeftButton &&
412 (e->modifiers() == Qt::ControlModifier ||
413 e->modifiers() == Qt::MetaModifier))) {
414 m_currentAction = Rotation;
415 }
416 }
417
418 } // namespace Avogadro::QtPlugins