| [907636] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [907636] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * GLMoleculeObject_atom.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Aug 17, 2011
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include "GLMoleculeObject_atom.hpp"
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | #include <Qt3D/qglscenenode.h>
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [7188b1] | 42 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [02ce36] | 43 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| [907636] | 44 | 
 | 
|---|
| [2ad1ec] | 45 | #include "Atom/atom.hpp"
 | 
|---|
 | 46 | #include "Bond/bond.hpp"
 | 
|---|
| [7188b1] | 47 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [3bdb6d] | 48 | #include "Element/element.hpp"
 | 
|---|
| [907636] | 49 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [2ad1ec] | 50 | #include "GLMoleculeObject_bond.hpp"
 | 
|---|
| [7188b1] | 51 | #include "World.hpp"
 | 
|---|
| [907636] | 52 | 
 | 
|---|
| [72a4c1] | 53 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atom *atomref) :
 | 
|---|
| [bca99d] | 54 |   GLMoleculeObject(mesh, parent),
 | 
|---|
| [2ad1ec] | 55 |   Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())),
 | 
|---|
 | 56 |   _atom(atomref)
 | 
|---|
| [7188b1] | 57 | {
 | 
|---|
 | 58 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
| [02ce36] | 59 |   atomref->signOn(this, AtomObservable::IndexChanged);
 | 
|---|
 | 60 |   atomref->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
 | 61 |   atomref->signOn(this, AtomObservable::ElementChanged);
 | 
|---|
| [2ad1ec] | 62 |   atomref->signOn(this, AtomObservable::BondsAdded);
 | 
|---|
| [d53902] | 63 |   World::getInstance().signOn(this, World::SelectionChanged);
 | 
|---|
| [7188b1] | 64 | 
 | 
|---|
 | 65 |   // set the object's id
 | 
|---|
 | 66 |   resetProperties();
 | 
|---|
 | 67 | 
 | 
|---|
| [57a770] | 68 |   LOG(2, "INFO: Created sphere for atom " << _atom->getId() << ".");
 | 
|---|
| [7188b1] | 69 | 
 | 
|---|
 | 70 |   connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
 | 
|---|
 | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
| [9c18e4] | 73 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
 | 
|---|
 | 74 | {
 | 
|---|
| [c736fe] | 75 |   if (_atom){
 | 
|---|
 | 76 |     _atom->signOff(this, AtomObservable::IndexChanged);
 | 
|---|
 | 77 |     _atom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
 | 78 |     _atom->signOff(this, AtomObservable::ElementChanged);
 | 
|---|
 | 79 |     _atom->signOff(this, AtomObservable::BondsAdded);
 | 
|---|
 | 80 |   }
 | 
|---|
| [d53902] | 81 |   World::getInstance().signOff(this, World::SelectionChanged);
 | 
|---|
| [9c18e4] | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
| [7188b1] | 84 | void GLMoleculeObject_atom::update(Observable *publisher)
 | 
|---|
 | 85 | {
 | 
|---|
| [57a770] | 86 | #ifdef LOG_OBSERVER
 | 
|---|
 | 87 |   observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from atom "+toString(_atom->getId())+".";
 | 
|---|
 | 88 | #endif
 | 
|---|
| [7188b1] | 89 |   resetProperties();
 | 
|---|
 | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | void GLMoleculeObject_atom::resetPosition()
 | 
|---|
| [907636] | 93 | {
 | 
|---|
 | 94 |   const Vector Position = _atom->getPosition();
 | 
|---|
| [57a770] | 95 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
 | 
|---|
| [7188b1] | 96 |   setPosition(QVector3D(Position[0], Position[1], Position[2]));
 | 
|---|
 | 97 | }
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | void GLMoleculeObject_atom::resetElement()
 | 
|---|
 | 100 | {
 | 
|---|
 | 101 |   size_t elementno = 0;
 | 
|---|
 | 102 |   if (_atom->getType() != NULL) {
 | 
|---|
| [ed26ae] | 103 |     elementno = _atom->getType()->getAtomicNumber();
 | 
|---|
| [7188b1] | 104 |   } else { // if no element yet, set to hydrogen
 | 
|---|
 | 105 |     elementno = 1;
 | 
|---|
 | 106 |   }
 | 
|---|
| [57a770] | 107 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
 | 
|---|
| [907636] | 108 | 
 | 
|---|
 | 109 |   // set materials
 | 
|---|
 | 110 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
 | 111 |   ASSERT(elementmaterial != NULL,
 | 
|---|
 | 112 |       "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
 | 
|---|
 | 113 |   setMaterial(elementmaterial);
 | 
|---|
 | 114 | 
 | 
|---|
| [7188b1] | 115 |   // set scale
 | 
|---|
 | 116 |   double radius = 0.;
 | 
|---|
 | 117 |   if (_atom->getType() != NULL) {
 | 
|---|
 | 118 |     radius = _atom->getType()->getVanDerWaalsRadius();
 | 
|---|
 | 119 |   } else {
 | 
|---|
 | 120 |     radius = 0.5;
 | 
|---|
 | 121 |   }
 | 
|---|
| [3927ef] | 122 |   setScale( radius / 4. );
 | 
|---|
| [7188b1] | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | void GLMoleculeObject_atom::resetIndex()
 | 
|---|
 | 126 | {
 | 
|---|
| [30cd0d] | 127 |   int oldId = objectId();
 | 
|---|
| [7188b1] | 128 |   const size_t atomno = _atom->getId();
 | 
|---|
| [57a770] | 129 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+".");
 | 
|---|
| [7188b1] | 130 |   setObjectId(atomno);
 | 
|---|
| [30cd0d] | 131 | 
 | 
|---|
 | 132 |   emit indexChanged(this, oldId, atomno);
 | 
|---|
| [7188b1] | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | void GLMoleculeObject_atom::resetProperties()
 | 
|---|
 | 136 | {
 | 
|---|
| [907636] | 137 |   // set position
 | 
|---|
| [7188b1] | 138 |   resetPosition();
 | 
|---|
| [907636] | 139 | 
 | 
|---|
| [7188b1] | 140 |   // set element
 | 
|---|
 | 141 |   resetElement();
 | 
|---|
| [907636] | 142 | 
 | 
|---|
 | 143 |   // set the object's id
 | 
|---|
| [7188b1] | 144 |   resetIndex();
 | 
|---|
| [d53902] | 145 | 
 | 
|---|
 | 146 |   // selected?
 | 
|---|
 | 147 |   setSelected(World::getInstance().isSelected(_atom));
 | 
|---|
| [7188b1] | 148 | }
 | 
|---|
| [907636] | 149 | 
 | 
|---|
| [7188b1] | 150 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
 | 
|---|
| [c736fe] | 151 | {
 | 
|---|
 | 152 |   _atom = NULL;
 | 
|---|
 | 153 | }
 | 
|---|
| [06ebf5] | 154 | 
 | 
|---|
| [7188b1] | 155 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
 | 156 | {
 | 
|---|
| [d53902] | 157 |   if (publisher == dynamic_cast<const Observable*>(_atom)){
 | 
|---|
 | 158 |     // notofication from atom
 | 
|---|
| [57a770] | 159 | #ifdef LOG_OBSERVER
 | 
|---|
| [d53902] | 160 |     observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
 | 
|---|
 | 161 |           << " received notification from atom " << _atom->getId() << " for channel "
 | 
|---|
 | 162 |           << notification->getChannelNo() << ".";
 | 
|---|
| [57a770] | 163 | #endif
 | 
|---|
| [d53902] | 164 |     switch (notification->getChannelNo()) {
 | 
|---|
 | 165 |       case AtomObservable::ElementChanged:
 | 
|---|
 | 166 |         resetElement();
 | 
|---|
| [5a2a06] | 167 |         emit changed();
 | 
|---|
| [d53902] | 168 |         break;
 | 
|---|
 | 169 |       case AtomObservable::IndexChanged:
 | 
|---|
 | 170 |         resetIndex();
 | 
|---|
 | 171 |         break;
 | 
|---|
 | 172 |       case AtomObservable::PositionChanged:
 | 
|---|
 | 173 |         resetPosition();
 | 
|---|
| [5a2a06] | 174 |         emit changed();
 | 
|---|
| [d53902] | 175 |         break;
 | 
|---|
 | 176 |       case AtomObservable::BondsAdded:
 | 
|---|
| [2ad1ec] | 177 |       {
 | 
|---|
| [d53902] | 178 |           ASSERT(!_atom->getListOfBonds().empty(),
 | 
|---|
 | 179 |               "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
 | 
|---|
| [88c8ec] | 180 |           const bond::ptr _bond = *(_atom->getListOfBonds().rbegin());
 | 
|---|
| [d53902] | 181 |           const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
 | 
|---|
 | 182 |               GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
 | 
|---|
 | 183 |           emit BondsInserted(_bond, side);
 | 
|---|
 | 184 |           break;
 | 
|---|
 | 185 |         }
 | 
|---|
 | 186 |       default:
 | 
|---|
 | 187 |         //setProperties();
 | 
|---|
 | 188 |         break;
 | 
|---|
 | 189 |     }
 | 
|---|
 | 190 |   }else{
 | 
|---|
 | 191 |     // notification from world
 | 
|---|
 | 192 | #ifdef LOG_OBSERVER
 | 
|---|
 | 193 |     observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
 | 
|---|
 | 194 |           << " received notification from world for channel "
 | 
|---|
 | 195 |           << notification->getChannelNo() << ".";
 | 
|---|
 | 196 | #endif
 | 
|---|
 | 197 |     switch (notification->getChannelNo()) {
 | 
|---|
 | 198 |       case World::SelectionChanged:
 | 
|---|
 | 199 |         setSelected(World::getInstance().isSelected(_atom));
 | 
|---|
 | 200 |         break;
 | 
|---|
 | 201 |       default:
 | 
|---|
| [2ad1ec] | 202 |         break;
 | 
|---|
| [d53902] | 203 |     }
 | 
|---|
| [7188b1] | 204 |   }
 | 
|---|
| [06ebf5] | 205 | }
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 | void GLMoleculeObject_atom::wasClicked()
 | 
|---|
 | 208 | {
 | 
|---|
| [57a770] | 209 |   LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked");
 | 
|---|
| [06ebf5] | 210 |   emit clicked(_atom->getId());
 | 
|---|
| [907636] | 211 | }
 | 
|---|