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