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