| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 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"
 | 
|---|
| 42 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 43 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "Atom/atom.hpp"
 | 
|---|
| 46 | #include "Bond/bond.hpp"
 | 
|---|
| 47 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 48 | #include "Element/element.hpp"
 | 
|---|
| 49 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 50 | #include "GLMoleculeObject_bond.hpp"
 | 
|---|
| 51 | #include "World.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atom *atomref) :
 | 
|---|
| 54 |   GLMoleculeObject(mesh, parent),
 | 
|---|
| 55 |   Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())),
 | 
|---|
| 56 |   _atom(atomref)
 | 
|---|
| 57 | {
 | 
|---|
| 58 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
| 59 |   atomref->signOn(this, AtomObservable::IndexChanged);
 | 
|---|
| 60 |   atomref->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
| 61 |   atomref->signOn(this, AtomObservable::ElementChanged);
 | 
|---|
| 62 |   atomref->signOn(this, AtomObservable::BondsAdded);
 | 
|---|
| 63 |   World::getInstance().signOn(this, World::SelectionChanged);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   // set the object's id
 | 
|---|
| 66 |   resetProperties();
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   LOG(2, "INFO: Created sphere for atom " << _atom->getId() << ".");
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
 | 
|---|
| 74 | {
 | 
|---|
| 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 |   }
 | 
|---|
| 81 |   World::getInstance().signOff(this, World::SelectionChanged);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | void GLMoleculeObject_atom::update(Observable *publisher)
 | 
|---|
| 85 | {
 | 
|---|
| 86 | #ifdef LOG_OBSERVER
 | 
|---|
| 87 |   observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from atom "+toString(_atom->getId())+".";
 | 
|---|
| 88 | #endif
 | 
|---|
| 89 |   resetProperties();
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void GLMoleculeObject_atom::resetPosition()
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   const Vector Position = _atom->getPosition();
 | 
|---|
| 95 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
 | 
|---|
| 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) {
 | 
|---|
| 103 |     elementno = _atom->getType()->getAtomicNumber();
 | 
|---|
| 104 |   } else { // if no element yet, set to hydrogen
 | 
|---|
| 105 |     elementno = 1;
 | 
|---|
| 106 |   }
 | 
|---|
| 107 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
 | 
|---|
| 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 | 
 | 
|---|
| 115 |   // set scale
 | 
|---|
| 116 |   double radius = 0.;
 | 
|---|
| 117 |   if (_atom->getType() != NULL) {
 | 
|---|
| 118 |     radius = _atom->getType()->getVanDerWaalsRadius();
 | 
|---|
| 119 |   } else {
 | 
|---|
| 120 |     radius = 0.5;
 | 
|---|
| 121 |   }
 | 
|---|
| 122 |   setScale( radius / 4. );
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | void GLMoleculeObject_atom::resetIndex()
 | 
|---|
| 126 | {
 | 
|---|
| 127 |   int oldId = objectId();
 | 
|---|
| 128 |   const size_t atomno = _atom->getId();
 | 
|---|
| 129 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+".");
 | 
|---|
| 130 |   setObjectId(atomno);
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   emit indexChanged(this, oldId, atomno);
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | void GLMoleculeObject_atom::resetProperties()
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   // set position
 | 
|---|
| 138 |   resetPosition();
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   // set element
 | 
|---|
| 141 |   resetElement();
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   // set the object's id
 | 
|---|
| 144 |   resetIndex();
 | 
|---|
| 145 | 
 | 
|---|
| 146 |   // selected?
 | 
|---|
| 147 |   setSelected(World::getInstance().isSelected(_atom));
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
 | 
|---|
| 151 | {
 | 
|---|
| 152 |   _atom = NULL;
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   if (publisher == dynamic_cast<const Observable*>(_atom)){
 | 
|---|
| 158 |     // notofication from atom
 | 
|---|
| 159 | #ifdef LOG_OBSERVER
 | 
|---|
| 160 |     observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
 | 
|---|
| 161 |           << " received notification from atom " << _atom->getId() << " for channel "
 | 
|---|
| 162 |           << notification->getChannelNo() << ".";
 | 
|---|
| 163 | #endif
 | 
|---|
| 164 |     switch (notification->getChannelNo()) {
 | 
|---|
| 165 |       case AtomObservable::ElementChanged:
 | 
|---|
| 166 |         resetElement();
 | 
|---|
| 167 |         emit changed();
 | 
|---|
| 168 |         break;
 | 
|---|
| 169 |       case AtomObservable::IndexChanged:
 | 
|---|
| 170 |         resetIndex();
 | 
|---|
| 171 |         break;
 | 
|---|
| 172 |       case AtomObservable::PositionChanged:
 | 
|---|
| 173 |         resetPosition();
 | 
|---|
| 174 |         emit changed();
 | 
|---|
| 175 |         break;
 | 
|---|
| 176 |       case AtomObservable::BondsAdded:
 | 
|---|
| 177 |       {
 | 
|---|
| 178 |           ASSERT(!_atom->getListOfBonds().empty(),
 | 
|---|
| 179 |               "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
 | 
|---|
| 180 |           const bond * _bond = *(_atom->getListOfBonds().rbegin());
 | 
|---|
| 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:
 | 
|---|
| 202 |         break;
 | 
|---|
| 203 |     }
 | 
|---|
| 204 |   }
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | void GLMoleculeObject_atom::wasClicked()
 | 
|---|
| 208 | {
 | 
|---|
| 209 |   LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked");
 | 
|---|
| 210 |   emit clicked(_atom->getId());
 | 
|---|
| 211 | }
 | 
|---|