[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"
|
---|
[907636] | 53 |
|
---|
[72a4c1] | 54 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atom *atomref) :
|
---|
[bca99d] | 55 | GLMoleculeObject(mesh, parent),
|
---|
[2ad1ec] | 56 | Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())),
|
---|
| 57 | _atom(atomref)
|
---|
[7188b1] | 58 | {
|
---|
| 59 | // sign on as observer (obtain non-const instance before)
|
---|
[02ce36] | 60 | atomref->signOn(this, AtomObservable::IndexChanged);
|
---|
| 61 | atomref->signOn(this, AtomObservable::PositionChanged);
|
---|
| 62 | atomref->signOn(this, AtomObservable::ElementChanged);
|
---|
[2ad1ec] | 63 | atomref->signOn(this, AtomObservable::BondsAdded);
|
---|
[d53902] | 64 | World::getInstance().signOn(this, World::SelectionChanged);
|
---|
[7188b1] | 65 |
|
---|
| 66 | // set the object's id
|
---|
| 67 | resetProperties();
|
---|
| 68 |
|
---|
[57a770] | 69 | LOG(2, "INFO: Created sphere for atom " << _atom->getId() << ".");
|
---|
[7188b1] | 70 |
|
---|
| 71 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[9c18e4] | 74 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
|
---|
| 75 | {
|
---|
[c736fe] | 76 | if (_atom){
|
---|
| 77 | _atom->signOff(this, AtomObservable::IndexChanged);
|
---|
| 78 | _atom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 79 | _atom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 80 | _atom->signOff(this, AtomObservable::BondsAdded);
|
---|
| 81 | }
|
---|
[d53902] | 82 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
[9c18e4] | 83 | }
|
---|
| 84 |
|
---|
[7188b1] | 85 | void GLMoleculeObject_atom::update(Observable *publisher)
|
---|
| 86 | {
|
---|
[57a770] | 87 | #ifdef LOG_OBSERVER
|
---|
[708277] | 88 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from atom "+toString(_atom->getId())+".";
|
---|
[57a770] | 89 | #endif
|
---|
[7188b1] | 90 | resetProperties();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | void GLMoleculeObject_atom::resetPosition()
|
---|
[907636] | 94 | {
|
---|
| 95 | const Vector Position = _atom->getPosition();
|
---|
[57a770] | 96 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
|
---|
[7188b1] | 97 | setPosition(QVector3D(Position[0], Position[1], Position[2]));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void GLMoleculeObject_atom::resetElement()
|
---|
| 101 | {
|
---|
| 102 | size_t elementno = 0;
|
---|
| 103 | if (_atom->getType() != NULL) {
|
---|
[ed26ae] | 104 | elementno = _atom->getType()->getAtomicNumber();
|
---|
[7188b1] | 105 | } else { // if no element yet, set to hydrogen
|
---|
| 106 | elementno = 1;
|
---|
| 107 | }
|
---|
[57a770] | 108 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
|
---|
[907636] | 109 |
|
---|
| 110 | // set materials
|
---|
| 111 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 112 | ASSERT(elementmaterial != NULL,
|
---|
| 113 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
|
---|
| 114 | setMaterial(elementmaterial);
|
---|
| 115 |
|
---|
[7188b1] | 116 | // set scale
|
---|
| 117 | double radius = 0.;
|
---|
| 118 | if (_atom->getType() != NULL) {
|
---|
| 119 | radius = _atom->getType()->getVanDerWaalsRadius();
|
---|
| 120 | } else {
|
---|
| 121 | radius = 0.5;
|
---|
| 122 | }
|
---|
[3927ef] | 123 | setScale( radius / 4. );
|
---|
[7188b1] | 124 | }
|
---|
| 125 |
|
---|
| 126 | void GLMoleculeObject_atom::resetIndex()
|
---|
| 127 | {
|
---|
[30cd0d] | 128 | int oldId = objectId();
|
---|
[7188b1] | 129 | const size_t atomno = _atom->getId();
|
---|
[57a770] | 130 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+".");
|
---|
[7188b1] | 131 | setObjectId(atomno);
|
---|
[30cd0d] | 132 |
|
---|
| 133 | emit indexChanged(this, oldId, atomno);
|
---|
[7188b1] | 134 | }
|
---|
| 135 |
|
---|
| 136 | void GLMoleculeObject_atom::resetProperties()
|
---|
| 137 | {
|
---|
[907636] | 138 | // set position
|
---|
[7188b1] | 139 | resetPosition();
|
---|
[907636] | 140 |
|
---|
[7188b1] | 141 | // set element
|
---|
| 142 | resetElement();
|
---|
[907636] | 143 |
|
---|
| 144 | // set the object's id
|
---|
[7188b1] | 145 | resetIndex();
|
---|
[d53902] | 146 |
|
---|
| 147 | // selected?
|
---|
| 148 | setSelected(World::getInstance().isSelected(_atom));
|
---|
[7188b1] | 149 | }
|
---|
[907636] | 150 |
|
---|
[7188b1] | 151 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
|
---|
[c736fe] | 152 | {
|
---|
| 153 | _atom = NULL;
|
---|
| 154 | }
|
---|
[06ebf5] | 155 |
|
---|
[7188b1] | 156 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 157 | {
|
---|
[d53902] | 158 | if (publisher == dynamic_cast<const Observable*>(_atom)){
|
---|
| 159 | // notofication from atom
|
---|
[57a770] | 160 | #ifdef LOG_OBSERVER
|
---|
[708277] | 161 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
[d53902] | 162 | << " received notification from atom " << _atom->getId() << " for channel "
|
---|
| 163 | << notification->getChannelNo() << ".";
|
---|
[57a770] | 164 | #endif
|
---|
[d53902] | 165 | switch (notification->getChannelNo()) {
|
---|
| 166 | case AtomObservable::ElementChanged:
|
---|
| 167 | resetElement();
|
---|
[5a2a06] | 168 | emit changed();
|
---|
[d53902] | 169 | break;
|
---|
| 170 | case AtomObservable::IndexChanged:
|
---|
| 171 | resetIndex();
|
---|
| 172 | break;
|
---|
| 173 | case AtomObservable::PositionChanged:
|
---|
| 174 | resetPosition();
|
---|
[5a2a06] | 175 | emit changed();
|
---|
[d53902] | 176 | break;
|
---|
| 177 | case AtomObservable::BondsAdded:
|
---|
[2ad1ec] | 178 | {
|
---|
[d53902] | 179 | ASSERT(!_atom->getListOfBonds().empty(),
|
---|
| 180 | "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
|
---|
[88c8ec] | 181 | const bond::ptr _bond = *(_atom->getListOfBonds().rbegin());
|
---|
[d53902] | 182 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
|
---|
| 183 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
| 184 | emit BondsInserted(_bond, side);
|
---|
| 185 | break;
|
---|
| 186 | }
|
---|
| 187 | default:
|
---|
| 188 | //setProperties();
|
---|
| 189 | break;
|
---|
| 190 | }
|
---|
| 191 | }else{
|
---|
| 192 | // notification from world
|
---|
| 193 | #ifdef LOG_OBSERVER
|
---|
[708277] | 194 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
[d53902] | 195 | << " received notification from world for channel "
|
---|
| 196 | << notification->getChannelNo() << ".";
|
---|
| 197 | #endif
|
---|
| 198 | switch (notification->getChannelNo()) {
|
---|
| 199 | case World::SelectionChanged:
|
---|
| 200 | setSelected(World::getInstance().isSelected(_atom));
|
---|
| 201 | break;
|
---|
| 202 | default:
|
---|
[2ad1ec] | 203 | break;
|
---|
[d53902] | 204 | }
|
---|
[7188b1] | 205 | }
|
---|
[06ebf5] | 206 | }
|
---|
| 207 |
|
---|
| 208 | void GLMoleculeObject_atom::wasClicked()
|
---|
| 209 | {
|
---|
[57a770] | 210 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked");
|
---|
[06ebf5] | 211 | emit clicked(_atom->getId());
|
---|
[907636] | 212 | }
|
---|