| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * GLMoleculeObject_atom.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Aug 17, 2011 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "GLMoleculeObject_atom.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include <Qt3D/qglbuilder.h> | 
|---|
| 23 | #include <Qt3D/qglscenenode.h> | 
|---|
| 24 | #include <Qt3D/qglsphere.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 27 |  | 
|---|
| 28 | #include "CodePatterns/Assert.hpp" | 
|---|
| 29 | #include "CodePatterns/Log.hpp" | 
|---|
| 30 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 31 |  | 
|---|
| 32 | #include "Atom/atom.hpp" | 
|---|
| 33 | #include "Bond/bond.hpp" | 
|---|
| 34 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 35 | #include "Element/element.hpp" | 
|---|
| 36 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 37 | #include "GLMoleculeObject_bond.hpp" | 
|---|
| 38 | #include "World.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | static QGLSceneNode * createAtom(QObject *parent) | 
|---|
| 41 | { | 
|---|
| 42 | QGLBuilder builder; | 
|---|
| 43 | builder << QGLSphere(2.0); | 
|---|
| 44 | QGLSceneNode *n = builder.finalizedSceneNode(); | 
|---|
| 45 | n->setParent(parent); | 
|---|
| 46 | return n; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | GLMoleculeObject_atom::GLMoleculeObject_atom(QObject *parent, const atom *atomref) : | 
|---|
| 50 | GLMoleculeObject(createAtom(parent), parent), | 
|---|
| 51 | Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())), | 
|---|
| 52 | _atom(atomref) | 
|---|
| 53 | { | 
|---|
| 54 | // sign on as observer (obtain non-const instance before) | 
|---|
| 55 | atomref->signOn(this, AtomObservable::IndexChanged); | 
|---|
| 56 | atomref->signOn(this, AtomObservable::PositionChanged); | 
|---|
| 57 | atomref->signOn(this, AtomObservable::ElementChanged); | 
|---|
| 58 | atomref->signOn(this, AtomObservable::BondsAdded); | 
|---|
| 59 | World::getInstance().signOn(this, World::SelectionChanged); | 
|---|
| 60 |  | 
|---|
| 61 | // set the object's id | 
|---|
| 62 | resetProperties(); | 
|---|
| 63 |  | 
|---|
| 64 | LOG(2, "INFO: Created sphere for atom " << _atom->getId() << "."); | 
|---|
| 65 |  | 
|---|
| 66 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked())); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | GLMoleculeObject_atom::~GLMoleculeObject_atom() | 
|---|
| 70 | { | 
|---|
| 71 | _atom->signOff(this, AtomObservable::IndexChanged); | 
|---|
| 72 | _atom->signOff(this, AtomObservable::PositionChanged); | 
|---|
| 73 | _atom->signOff(this, AtomObservable::ElementChanged); | 
|---|
| 74 | _atom->signOff(this, AtomObservable::BondsAdded); | 
|---|
| 75 | World::getInstance().signOff(this, World::SelectionChanged); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void GLMoleculeObject_atom::update(Observable *publisher) | 
|---|
| 79 | { | 
|---|
| 80 | #ifdef LOG_OBSERVER | 
|---|
| 81 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from atom "+toString(_atom->getId())+"."; | 
|---|
| 82 | #endif | 
|---|
| 83 | resetProperties(); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void GLMoleculeObject_atom::resetPosition() | 
|---|
| 87 | { | 
|---|
| 88 | const Vector Position = _atom->getPosition(); | 
|---|
| 89 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+"."); | 
|---|
| 90 | setPosition(QVector3D(Position[0], Position[1], Position[2])); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void GLMoleculeObject_atom::resetElement() | 
|---|
| 94 | { | 
|---|
| 95 | size_t elementno = 0; | 
|---|
| 96 | if (_atom->getType() != NULL) { | 
|---|
| 97 | elementno = _atom->getType()->getAtomicNumber(); | 
|---|
| 98 | } else { // if no element yet, set to hydrogen | 
|---|
| 99 | elementno = 1; | 
|---|
| 100 | } | 
|---|
| 101 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+"."); | 
|---|
| 102 |  | 
|---|
| 103 | // set materials | 
|---|
| 104 | QGLMaterial *elementmaterial = getMaterial(elementno); | 
|---|
| 105 | ASSERT(elementmaterial != NULL, | 
|---|
| 106 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL."); | 
|---|
| 107 | setMaterial(elementmaterial); | 
|---|
| 108 |  | 
|---|
| 109 | // set scale | 
|---|
| 110 | double radius = 0.; | 
|---|
| 111 | if (_atom->getType() != NULL) { | 
|---|
| 112 | radius = _atom->getType()->getVanDerWaalsRadius(); | 
|---|
| 113 | } else { | 
|---|
| 114 | radius = 0.5; | 
|---|
| 115 | } | 
|---|
| 116 | setScale( radius / 4. ); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void GLMoleculeObject_atom::resetIndex() | 
|---|
| 120 | { | 
|---|
| 121 | const size_t atomno = _atom->getId(); | 
|---|
| 122 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+"."); | 
|---|
| 123 | setObjectId(atomno); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | void GLMoleculeObject_atom::resetProperties() | 
|---|
| 127 | { | 
|---|
| 128 | // set position | 
|---|
| 129 | resetPosition(); | 
|---|
| 130 |  | 
|---|
| 131 | // set element | 
|---|
| 132 | resetElement(); | 
|---|
| 133 |  | 
|---|
| 134 | // set the object's id | 
|---|
| 135 | resetIndex(); | 
|---|
| 136 |  | 
|---|
| 137 | // selected? | 
|---|
| 138 | setSelected(World::getInstance().isSelected(_atom)); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher) | 
|---|
| 142 | {} | 
|---|
| 143 |  | 
|---|
| 144 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification) | 
|---|
| 145 | { | 
|---|
| 146 | if (publisher == dynamic_cast<const Observable*>(_atom)){ | 
|---|
| 147 | // notofication from atom | 
|---|
| 148 | #ifdef LOG_OBSERVER | 
|---|
| 149 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this) | 
|---|
| 150 | << " received notification from atom " << _atom->getId() << " for channel " | 
|---|
| 151 | << notification->getChannelNo() << "."; | 
|---|
| 152 | #endif | 
|---|
| 153 | switch (notification->getChannelNo()) { | 
|---|
| 154 | case AtomObservable::ElementChanged: | 
|---|
| 155 | resetElement(); | 
|---|
| 156 | emit changed(); | 
|---|
| 157 | break; | 
|---|
| 158 | case AtomObservable::IndexChanged: | 
|---|
| 159 | resetIndex(); | 
|---|
| 160 | break; | 
|---|
| 161 | case AtomObservable::PositionChanged: | 
|---|
| 162 | resetPosition(); | 
|---|
| 163 | emit changed(); | 
|---|
| 164 | break; | 
|---|
| 165 | case AtomObservable::BondsAdded: | 
|---|
| 166 | { | 
|---|
| 167 | ASSERT(!_atom->getListOfBonds().empty(), | 
|---|
| 168 | "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty."); | 
|---|
| 169 | const bond * _bond = *(_atom->getListOfBonds().rbegin()); | 
|---|
| 170 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ? | 
|---|
| 171 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right; | 
|---|
| 172 | emit BondsInserted(_bond, side); | 
|---|
| 173 | break; | 
|---|
| 174 | } | 
|---|
| 175 | default: | 
|---|
| 176 | //setProperties(); | 
|---|
| 177 | break; | 
|---|
| 178 | } | 
|---|
| 179 | }else{ | 
|---|
| 180 | // notification from world | 
|---|
| 181 | #ifdef LOG_OBSERVER | 
|---|
| 182 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this) | 
|---|
| 183 | << " received notification from world for channel " | 
|---|
| 184 | << notification->getChannelNo() << "."; | 
|---|
| 185 | #endif | 
|---|
| 186 | switch (notification->getChannelNo()) { | 
|---|
| 187 | case World::SelectionChanged: | 
|---|
| 188 | setSelected(World::getInstance().isSelected(_atom)); | 
|---|
| 189 | break; | 
|---|
| 190 | default: | 
|---|
| 191 | break; | 
|---|
| 192 | } | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | void GLMoleculeObject_atom::wasClicked() | 
|---|
| 197 | { | 
|---|
| 198 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked"); | 
|---|
| 199 | emit clicked(_atom->getId()); | 
|---|
| 200 | } | 
|---|