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