/
editor.cpp
702 lines (594 loc) · 21.2 KB
/
editor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/
#include "editor.h"
#include "editortoolwidget.h"
#include <avogadro/core/atom.h>
#include <avogadro/core/bond.h>
#include <avogadro/core/elements.h>
#include <avogadro/core/vector.h>
#include <avogadro/qtgui/hydrogentools.h>
#include <avogadro/qtgui/molecule.h>
#include <avogadro/qtgui/rwmolecule.h>
#include <avogadro/qtopengl/glwidget.h>
#include <avogadro/rendering/camera.h>
#include <avogadro/rendering/geometrynode.h>
#include <avogadro/rendering/glrenderer.h>
#include <avogadro/rendering/groupnode.h>
#include <avogadro/rendering/textlabel2d.h>
#include <avogadro/rendering/textlabel3d.h>
#include <avogadro/rendering/textproperties.h>
#include <QAction>
#include <QtGui/QGuiApplication>
#include <QtGui/QIcon>
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QWidget>
#include <QtCore/QTimer>
#include <QDebug>
#include <limits>
namespace {
const unsigned char INVALID_ATOMIC_NUMBER =
std::numeric_limits<unsigned char>::max();
}
namespace Avogadro::QtPlugins {
using QtGui::Molecule;
using QtGui::RWAtom;
using QtGui::RWBond;
using Avogadro::Core::Elements;
using Avogadro::Rendering::GeometryNode;
using Avogadro::Rendering::GroupNode;
using Avogadro::Rendering::Identifier;
using Avogadro::Rendering::TextLabel2D;
using Avogadro::Rendering::TextProperties;
Editor::Editor(QObject* parent_)
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)),
m_molecule(nullptr), m_glWidget(nullptr), m_renderer(nullptr),
m_toolWidget(new EditorToolWidget(qobject_cast<QWidget*>(parent_))),
m_pressedButtons(Qt::NoButton),
m_clickedAtomicNumber(INVALID_ATOMIC_NUMBER), m_bondAdded(false),
m_fixValenceLater(false), m_layerManager("Editor")
{
m_activateAction->setText(tr("Draw"));
m_activateAction->setToolTip(
tr("Draw Tool\n\n"
"Left Mouse: \tClick and Drag to create Atoms and Bond\n"
"Right Mouse: \tDelete Atom"));
setIcon();
reset();
}
Editor::~Editor() {}
void Editor::setIcon(bool darkTheme)
{
if (darkTheme)
m_activateAction->setIcon(QIcon(":/icons/editor_dark.svg"));
else
m_activateAction->setIcon(QIcon(":/icons/editor_light.svg"));
}
QWidget* Editor::toolWidget() const
{
return m_toolWidget;
}
QUndoCommand* Editor::mousePressEvent(QMouseEvent* e)
{
clearKeyPressBuffer();
if (!m_renderer || !m_molecule)
return nullptr;
updatePressedButtons(e, false);
m_clickPosition = e->pos();
if (m_pressedButtons & Qt::LeftButton) {
m_clickedObject = m_renderer->hit(e->pos().x(), e->pos().y());
if (m_layerManager.activeLayerLocked()) {
e->accept();
return nullptr;
}
switch (m_clickedObject.type) {
case Rendering::InvalidType:
m_molecule->beginMergeMode(tr("Draw Atom"));
emptyLeftClick(e);
return nullptr;
case Rendering::AtomType:
// We don't know yet if we are drawing a bond/atom or replacing an atom
// unfortunately...
m_molecule->beginMergeMode(tr("Draw"));
atomLeftClick(e);
return nullptr;
case Rendering::BondType:
m_molecule->beginMergeMode(tr("Change Bond Type"));
bondLeftClick(e);
return nullptr;
}
} else if (m_pressedButtons & Qt::RightButton) {
m_clickedObject = m_renderer->hit(e->pos().x(), e->pos().y());
switch (m_clickedObject.type) {
case Rendering::AtomType:
m_molecule->beginMergeMode(tr("Remove Atom"));
atomRightClick(e);
return nullptr;
case Rendering::BondType:
m_molecule->beginMergeMode(tr("Remove Bond"));
bondRightClick(e);
return nullptr;
default:
break;
}
}
return nullptr;
}
QUndoCommand* Editor::mouseReleaseEvent(QMouseEvent* e)
{
if (!m_renderer || !m_molecule)
return nullptr;
if (m_layerManager.activeLayerLocked()) {
e->accept();
return nullptr;
}
updatePressedButtons(e, true);
if (m_clickedObject.type == Rendering::InvalidType)
return nullptr;
switch (e->button()) {
case Qt::LeftButton:
case Qt::RightButton:
reset();
e->accept();
m_molecule->endMergeMode();
// Let's cover all possible changes - the undo stack won't update
// without this
m_molecule->emitChanged(Molecule::Atoms | Molecule::Bonds |
Molecule::Added | Molecule::Removed |
Molecule::Modified);
break;
default:
break;
}
return nullptr;
}
QUndoCommand* Editor::mouseMoveEvent(QMouseEvent* e)
{
if (!m_renderer)
return nullptr;
if (m_pressedButtons & Qt::LeftButton)
if (m_clickedObject.type == Rendering::AtomType) {
if (m_layerManager.activeLayerLocked()) {
e->accept();
return nullptr;
}
atomLeftDrag(e);
}
return nullptr;
}
QUndoCommand* Editor::keyPressEvent(QKeyEvent* e)
{
if (e->text().isEmpty())
return nullptr;
e->accept();
if (m_layerManager.activeLayerLocked()) {
return nullptr;
}
// Set a timer to clear the buffer on first keypress:
if (m_keyPressBuffer.isEmpty())
QTimer::singleShot(2000, this, SLOT(clearKeyPressBuffer()));
m_keyPressBuffer.append(m_keyPressBuffer.isEmpty() ? e->text().toUpper()
: e->text().toLower());
if (m_keyPressBuffer.size() >= 3) {
clearKeyPressBuffer();
return nullptr;
}
bool ok = false;
int atomicNum;
int bondOrder = m_keyPressBuffer.toInt(&ok);
if (ok && bondOrder > 0 && bondOrder <= 4) {
m_toolWidget->setBondOrder(static_cast<unsigned char>(bondOrder));
} else {
atomicNum =
Core::Elements::atomicNumberFromSymbol(m_keyPressBuffer.toStdString());
if (atomicNum != Avogadro::InvalidElement)
m_toolWidget->setAtomicNumber(static_cast<unsigned char>(atomicNum));
}
return nullptr;
}
inline Vector3ub contrastingColor(const Vector3ub& rgb)
{
// If we're far 'enough' (+/-32) away from 128, just invert the component.
// If we're close to 128, inverting the color will end up too close to the
// input -- adjust the component before inverting.
const unsigned char minVal = 32;
const unsigned char maxVal = 223;
Vector3ub result;
for (size_t i = 0; i < 3; ++i) {
unsigned char input = rgb[i];
if (input > 160 || input < 96)
result[i] = static_cast<unsigned char>(255 - input);
else
result[i] = static_cast<unsigned char>(255 - (input / 4));
// Clamp to 32-->223 to prevent pure black/white
result[i] = std::min(maxVal, std::max(minVal, result[i]));
}
return result;
}
void Editor::draw(Rendering::GroupNode& node)
{
if (fabs(m_bondDistance) < 0.3)
return;
auto* geo = new GeometryNode;
node.addChild(geo);
// Determine the field width. Negate it to indicate left-alignment.
QString distanceLabel = tr("Distance:");
int labelWidth = -1 * distanceLabel.size();
QString overlayText = tr("%1 %L2")
.arg(distanceLabel, labelWidth)
.arg(tr("%L1 Å").arg(m_bondDistance, 9, 'f', 3), 9);
Vector3ub color(64, 255, 220);
if (m_renderer) {
auto backgroundColor = m_renderer->scene().backgroundColor();
color = contrastingColor(
Vector3ub(backgroundColor[0], backgroundColor[1], backgroundColor[2]));
}
TextProperties overlayTProp;
overlayTProp.setFontFamily(TextProperties::Mono);
overlayTProp.setColorRgb(color[0], color[1], color[2]);
overlayTProp.setAlign(TextProperties::HLeft, TextProperties::VBottom);
auto* label = new TextLabel2D;
label->setText(overlayText.toStdString());
label->setTextProperties(overlayTProp);
label->setRenderPass(Rendering::Overlay2DPass);
label->setAnchor(Vector2i(10, 10));
geo->addDrawable(label);
}
void Editor::updatePressedButtons(QMouseEvent* e, bool release)
{
/// @todo Use modifier keys on mac
if (release)
m_pressedButtons &= e->buttons();
else
m_pressedButtons |= e->buttons();
}
void Editor::reset()
{
if (m_fixValenceLater) {
Index a1 = m_newObject.index;
Index a2 = m_bondedAtom.index;
Index a3 = m_clickedObject.index;
// don't order them
// this caused bug:
// https://github.com/OpenChemistry/avogadrolibs/issues/678
/*
if (a1 > a2)
std::swap(a1, a2);
if (a1 > a3)
std::swap(a1, a3);
if (a2 > a3)
std::swap(a2, a3);
*/
// This preserves the order so they are adjusted in order.
// This is important for the undo stack to work correctly.
Core::Array<Index> atomIds;
atomIds.push_back(a3);
atomIds.push_back(a2);
atomIds.push_back(a1);
// This function checks to make sure the ids are valid, so no need
// to check out here.
m_molecule->adjustHydrogens(atomIds);
Molecule::MoleculeChanges changes = Molecule::Atoms | Molecule::Added;
changes |= Molecule::Bonds | Molecule::Added | Molecule::Removed;
m_molecule->emitChanged(changes);
m_fixValenceLater = false;
}
m_clickedObject = Identifier();
m_newObject = Identifier();
m_bondedAtom = Identifier();
m_clickPosition = QPoint();
m_pressedButtons = Qt::NoButton;
m_clickedAtomicNumber = INVALID_ATOMIC_NUMBER;
m_bondAdded = false;
m_bondDistance = 0.0f;
emit drawablesChanged();
}
void Editor::emptyLeftClick(QMouseEvent* e)
{
// Add an atom at the clicked position
Vector2f windowPos(e->localPos().x(), e->localPos().y());
Vector3f atomPos = m_renderer->camera().unProject(windowPos);
RWAtom newAtom =
m_molecule->addAtom(m_toolWidget->atomicNumber(), atomPos.cast<double>());
Molecule::MoleculeChanges changes = Molecule::Atoms | Molecule::Modified;
if (m_toolWidget->adjustHydrogens()) {
m_fixValenceLater = true;
}
// Update the clicked object
m_clickedObject.type = Rendering::AtomType;
m_clickedObject.molecule = m_molecule;
m_clickedObject.index = newAtom.index();
// Emit changed signal
m_molecule->emitChanged(changes);
e->accept();
}
void Editor::atomLeftClick(QMouseEvent* e)
{
RWAtom atom = m_molecule->atom(m_clickedObject.index);
if (atom.isValid()) {
// Store the original atomic number of the clicked atom before updating it.
unsigned char atomicNumber = m_toolWidget->atomicNumber();
if (atom.atomicNumber() != atomicNumber) {
m_clickedAtomicNumber = atom.atomicNumber();
atom.setAtomicNumber(atomicNumber);
Molecule::MoleculeChanges changes = Molecule::Atoms | Molecule::Modified;
if (m_toolWidget->adjustHydrogens())
m_fixValenceLater = true;
m_molecule->emitChanged(changes);
}
e->accept();
}
}
void Editor::bondLeftClick(QMouseEvent* e)
{
RWBond bond = m_molecule->bond(m_clickedObject.index);
bond.setOrder(static_cast<unsigned char>((bond.order() % 3) + 1));
Molecule::MoleculeChanges changes = Molecule::Bonds | Molecule::Modified;
if (m_toolWidget->adjustHydrogens()) {
// change for the new bond order
RWAtom atom1 = bond.atom1();
RWAtom atom2 = bond.atom2();
QtGui::HydrogenTools::adjustHydrogens(atom1);
QtGui::HydrogenTools::adjustHydrogens(atom2);
changes |= Molecule::Atoms | Molecule::Added | Molecule::Removed;
}
m_molecule->emitChanged(changes);
e->accept();
}
void Editor::atomRightClick(QMouseEvent* e)
{
e->accept();
// check to see if we need to adjust hydrogens
Core::Array<Index> bondedAtoms;
if (m_toolWidget->adjustHydrogens()) {
// before we remove the atom, we need to delete any H atoms
// that are bonded to it
RWAtom atom = m_molecule->atom(m_clickedObject.index);
if (atom.isValid()) {
// get the list of bonded atoms
Core::Array<RWBond> atomBonds = m_molecule->bonds(atom);
for (const RWBond& bond : atomBonds) {
RWAtom bondedAtom = bond.getOtherAtom(atom);
if (bondedAtom.atomicNumber() == Core::Hydrogen) {
// remove the H atom
m_molecule->removeAtom(bondedAtom.index());
} else {
// save the atom to adjust after we remove the target
bondedAtoms.push_back(m_molecule->atomUniqueId(bondedAtom));
}
}
}
}
m_molecule->removeAtom(m_clickedObject.index);
// okay, now adjust the valence on the bonded atoms
// (e.g., add back some hydrogens)
for (Index atomIndex : bondedAtoms) {
RWAtom atom = m_molecule->atomByUniqueId(atomIndex);
QtGui::HydrogenTools::adjustHydrogens(atom);
}
m_molecule->emitChanged(Molecule::Atoms | Molecule::Removed);
}
void Editor::bondRightClick(QMouseEvent* e)
{
e->accept();
m_molecule->removeBond(m_clickedObject.index);
m_molecule->emitChanged(Molecule::Bonds | Molecule::Removed);
}
int expectedBondOrder(RWAtom atom1, RWAtom atom2)
{
Vector3 bondVector = atom1.position3d() - atom2.position3d();
double bondDistance = bondVector.norm();
double radiiSum;
radiiSum = Elements::radiusCovalent(atom1.atomicNumber()) +
Elements::radiusCovalent(atom2.atomicNumber());
double ratio = bondDistance / radiiSum;
int bondOrder;
if (ratio > 1.0)
bondOrder = 1;
else if (ratio > 0.91 && ratio < 1.0)
bondOrder = 2;
else
bondOrder = 3;
return bondOrder;
}
void Editor::atomLeftDrag(QMouseEvent* e)
{
// Always accept move events when atoms are clicked:
e->accept();
// Build up a MoleculeChanges bitfield
Molecule::MoleculeChanges changes = Molecule::NoChange;
// Get the list of hits at the current mouse position:
const std::multimap<float, Identifier> hits =
m_renderer->hits(e->pos().x(), e->pos().y());
// Check if the previously clicked atom is still under the mouse.
float depth = -1.0f;
for (const auto& hit : hits) {
if (hit.second == m_clickedObject) {
depth = hit.first;
break;
}
}
// If the clicked atom is under the mouse...
if (depth >= 0.f) {
// ...and we've created a new atom, remove the new atom and reset the
// clicked atom's atomic number
if (m_newObject.type == Rendering::AtomType &&
m_molecule == m_newObject.molecule) {
m_molecule->removeAtom(m_newObject.index);
changes |= Molecule::Atoms | Molecule::Bonds | Molecule::Removed;
m_newObject = Identifier();
RWAtom atom = m_molecule->atom(m_clickedObject.index);
if (atom.atomicNumber() != m_toolWidget->atomicNumber()) {
m_clickedAtomicNumber = atom.atomicNumber();
atom.setAtomicNumber(m_toolWidget->atomicNumber());
changes |= Molecule::Atoms | Molecule::Modified;
}
m_molecule->emitChanged(changes);
return;
}
// If there is no new atom, do nothing.
return;
}
// If we get here, the clicked atom is no longer under the cursor.
// If the clicked atom's identity has been changed from the initial click,
// reset its atomic number
if (m_clickedAtomicNumber != INVALID_ATOMIC_NUMBER) {
RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index);
clickedAtom.setAtomicNumber(m_clickedAtomicNumber);
m_clickedAtomicNumber = INVALID_ATOMIC_NUMBER;
changes |= Molecule::Atoms | Molecule::Modified;
}
// Does a bonded atom already exist?
if (m_bondedAtom.isValid()) {
// Is it still under the mouse?
depth = -1.0f;
for (const auto& hit : hits) {
if (hit.second == m_bondedAtom) {
depth = hit.first;
break;
}
}
// If the bonded atom is no longer under the mouse, remove the bond.
if (depth < 0.f) {
RWAtom bondedAtom = m_molecule->atom(m_bondedAtom.index);
RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index);
if (m_bondAdded)
m_molecule->removeBond(clickedAtom, bondedAtom);
changes |= Molecule::Bonds | Molecule::Removed;
m_bondedAtom = Identifier();
m_bondAdded = false;
}
}
// Is there another atom under the cursor, besides newAtom? If so, we'll draw
// a bond to it.
Identifier atomToBond;
for (const auto& hit : hits) {
const Identifier& ident = hit.second;
// Are we on an atom
if (ident.type == Rendering::AtomType)
// besides the one that was clicked or a new atom
if (ident != m_newObject && ident != m_clickedObject) {
// then we have an atom that we should be drawing a bond to.
atomToBond = ident;
break;
}
}
if (atomToBond.isValid() && (atomToBond.index != m_newObject.index)) {
// If we have a newAtom, destroy it
if (m_newObject.isValid() && m_newObject.type == Rendering::AtomType) {
m_molecule->removeAtom(m_newObject.index);
changes |= Molecule::Atoms | Molecule::Bonds | Molecule::Removed;
m_newObject = Identifier();
}
// Skip the rest of this block if atomToBond is already bonded
if (m_bondedAtom != atomToBond) {
// If the currently bonded atom exists, break the bond
if (m_bondedAtom.isValid() && m_clickedObject.isValid() &&
m_bondedAtom.index < m_molecule->atomCount() &&
m_clickedObject.index < m_molecule->atomCount() &&
m_molecule->bond(m_bondedAtom.index, m_clickedObject.index)
.isValid()) {
if (m_molecule->removeBond(m_molecule->atom(m_bondedAtom.index),
m_molecule->atom(m_clickedObject.index))) {
changes |= Molecule::Bonds | Molecule::Removed;
}
m_bondedAtom = Identifier();
}
// Create a new bond between clicked atom and atomToBond.
RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index);
RWAtom bondedAtom = m_molecule->atom(atomToBond.index);
if (!m_molecule->bond(clickedAtom, bondedAtom).isValid()) {
int bondOrder = m_toolWidget->bondOrder();
if (bondOrder == 0) {
// automatic - guess the size
bondOrder = expectedBondOrder(clickedAtom, bondedAtom);
}
m_molecule->addBond(clickedAtom, bondedAtom, bondOrder);
m_bondAdded = true;
} // we have a bond, but it might be the wrong order
else {
RWBond bond = m_molecule->bond(clickedAtom, bondedAtom);
int bondOrder = m_toolWidget->bondOrder();
if (bondOrder == 0) {
// automatic - guess the size
bondOrder = expectedBondOrder(clickedAtom, bondedAtom);
}
if (bond.order() != bondOrder)
bond.setOrder(bondOrder);
}
m_bondedAtom = atomToBond;
changes |= Molecule::Bonds | Molecule::Added;
}
m_molecule->emitChanged(changes);
return;
}
// If we make it here, the cursor is not over any existing atom, with the
// possible exception of a new atom we've added that's bonded to clicked atom.
// We just need to create the new atom (if we haven't already), then update
// its position.
RWAtom newAtom;
if (!m_newObject.isValid()) {
// Add a new atom bonded to the clicked atom
RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index);
newAtom = m_molecule->addAtom(m_toolWidget->atomicNumber(),
clickedAtom.position3d());
// Handle the automatic bond order
int bondOrder = m_toolWidget->bondOrder();
if (bondOrder == 0) {
// automatic - guess the size
bondOrder = expectedBondOrder(clickedAtom, newAtom);
}
m_molecule->addBond(clickedAtom, newAtom, bondOrder);
// now if we need to adjust hydrogens, do it
if (m_toolWidget->adjustHydrogens())
m_fixValenceLater = true;
changes |= Molecule::Atoms | Molecule::Bonds | Molecule::Added;
m_newObject.type = Rendering::AtomType;
m_newObject.index = newAtom.index();
const Core::Molecule* mol = &m_molecule->molecule();
m_newObject.molecule = mol;
} else if (m_newObject.type == Rendering::AtomType) {
// Grab the previously created atom
newAtom = m_molecule->atom(m_newObject.index);
} else {
// Shouldn't happen
qWarning() << "Editor::atomLeftDrag: m_newObject already set and not an "
"atom? This is a bug.";
}
if (newAtom.isValid()) {
Vector2f windowPos(e->localPos().x(), e->localPos().y());
Vector3f oldPos(newAtom.position3d().cast<float>());
Vector3f newPos = m_renderer->camera().unProject(windowPos, oldPos);
newAtom.setPosition3d(newPos.cast<double>());
changes |= Molecule::Atoms | Molecule::Modified;
RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index);
if (clickedAtom.isValid()) {
Vector3f bondVector = clickedAtom.position3d().cast<float>() - newPos;
m_bondDistance = bondVector.norm();
// need to check if bond order needs to change
if (m_toolWidget->bondOrder() == 0) { // automatic
RWBond bond = m_molecule->bond(newAtom, clickedAtom);
if (bond.isValid()) {
int bondOrder = expectedBondOrder(newAtom, clickedAtom);
if (bondOrder != bond.order())
bond.setOrder(bondOrder);
changes |= Molecule::Bonds | Molecule::Modified;
}
} // otherwise see if the bond order is different than what's there
else {
int bondOrder = m_toolWidget->bondOrder();
RWBond bond = m_molecule->bond(newAtom, clickedAtom);
if (bond.isValid() && bondOrder != bond.order())
bond.setOrder(bondOrder);
changes |= Molecule::Bonds | Molecule::Modified;
}
}
}
m_molecule->emitChanged(changes);
return;
}
} // namespace Avogadro::QtPlugins