1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
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/>.
|
---|
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"
|
---|
43 | #include "CodePatterns/Log.hpp"
|
---|
44 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
45 |
|
---|
46 | #include "Atom/atom.hpp"
|
---|
47 | #include "Bond/bond.hpp"
|
---|
48 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
49 | #include "Element/element.hpp"
|
---|
50 | #include "LinearAlgebra/Vector.hpp"
|
---|
51 | #include "GLMoleculeObject_bond.hpp"
|
---|
52 | #include "World.hpp"
|
---|
53 | #include "WorldTime.hpp"
|
---|
54 |
|
---|
55 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atom *atomref) :
|
---|
56 | GLMoleculeObject(mesh, parent),
|
---|
57 | Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())),
|
---|
58 | _atom(atomref)
|
---|
59 | {
|
---|
60 | // sign on as observer (obtain non-const instance before)
|
---|
61 | atomref->signOn(this, AtomObservable::IndexChanged);
|
---|
62 | atomref->signOn(this, AtomObservable::PositionChanged);
|
---|
63 | atomref->signOn(this, AtomObservable::ElementChanged);
|
---|
64 | atomref->signOn(this, AtomObservable::BondsAdded);
|
---|
65 | World::getInstance().signOn(this, World::SelectionChanged);
|
---|
66 | WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
|
---|
67 |
|
---|
68 | // set the object's id
|
---|
69 | resetProperties();
|
---|
70 |
|
---|
71 | LOG(2, "INFO: Created sphere for atom " << _atom->getId() << ".");
|
---|
72 |
|
---|
73 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
74 | }
|
---|
75 |
|
---|
76 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
|
---|
77 | {
|
---|
78 | if (_atom){
|
---|
79 | _atom->signOff(this, AtomObservable::IndexChanged);
|
---|
80 | _atom->signOff(this, AtomObservable::PositionChanged);
|
---|
81 | _atom->signOff(this, AtomObservable::ElementChanged);
|
---|
82 | _atom->signOff(this, AtomObservable::BondsAdded);
|
---|
83 | }
|
---|
84 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
85 | WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void GLMoleculeObject_atom::update(Observable *publisher)
|
---|
89 | {
|
---|
90 | #ifdef LOG_OBSERVER
|
---|
91 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from atom "+toString(_atom->getId())+".";
|
---|
92 | #endif
|
---|
93 | resetProperties();
|
---|
94 | }
|
---|
95 |
|
---|
96 | void GLMoleculeObject_atom::resetPosition()
|
---|
97 | {
|
---|
98 | const Vector Position = _atom->getPosition();
|
---|
99 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
|
---|
100 | setPosition(QVector3D(Position[0], Position[1], Position[2]));
|
---|
101 | }
|
---|
102 |
|
---|
103 | void GLMoleculeObject_atom::resetElement()
|
---|
104 | {
|
---|
105 | size_t elementno = 0;
|
---|
106 | if (_atom->getType() != NULL) {
|
---|
107 | elementno = _atom->getType()->getAtomicNumber();
|
---|
108 | } else { // if no element yet, set to hydrogen
|
---|
109 | elementno = 1;
|
---|
110 | }
|
---|
111 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
|
---|
112 |
|
---|
113 | // set materials
|
---|
114 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
115 | ASSERT(elementmaterial != NULL,
|
---|
116 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
|
---|
117 | setMaterial(elementmaterial);
|
---|
118 |
|
---|
119 | // set scale
|
---|
120 | double radius = 0.;
|
---|
121 | if (_atom->getType() != NULL) {
|
---|
122 | radius = _atom->getType()->getVanDerWaalsRadius();
|
---|
123 | } else {
|
---|
124 | radius = 0.5;
|
---|
125 | }
|
---|
126 | setScale( radius / 4. );
|
---|
127 | }
|
---|
128 |
|
---|
129 | void GLMoleculeObject_atom::resetIndex()
|
---|
130 | {
|
---|
131 | int oldId = objectId();
|
---|
132 | const size_t atomno = _atom->getId();
|
---|
133 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+".");
|
---|
134 | setObjectId(atomno);
|
---|
135 |
|
---|
136 | emit indexChanged(this, oldId, atomno);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void GLMoleculeObject_atom::resetProperties()
|
---|
140 | {
|
---|
141 | // set position
|
---|
142 | resetPosition();
|
---|
143 |
|
---|
144 | // set element
|
---|
145 | resetElement();
|
---|
146 |
|
---|
147 | // set the object's id
|
---|
148 | resetIndex();
|
---|
149 |
|
---|
150 | // selected?
|
---|
151 | setSelected(World::getInstance().isSelected(_atom));
|
---|
152 | }
|
---|
153 |
|
---|
154 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
|
---|
155 | {
|
---|
156 | _atom = NULL;
|
---|
157 | }
|
---|
158 |
|
---|
159 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
160 | {
|
---|
161 | if (publisher == dynamic_cast<const Observable*>(_atom)){
|
---|
162 | // notofication from atom
|
---|
163 | #ifdef LOG_OBSERVER
|
---|
164 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
165 | << " received notification from atom " << _atom->getId() << " for channel "
|
---|
166 | << notification->getChannelNo() << ".";
|
---|
167 | #endif
|
---|
168 | switch (notification->getChannelNo()) {
|
---|
169 | case AtomObservable::ElementChanged:
|
---|
170 | resetElement();
|
---|
171 | emit changed();
|
---|
172 | break;
|
---|
173 | case AtomObservable::IndexChanged:
|
---|
174 | resetIndex();
|
---|
175 | break;
|
---|
176 | case AtomObservable::PositionChanged:
|
---|
177 | resetPosition();
|
---|
178 | emit changed();
|
---|
179 | break;
|
---|
180 | case AtomObservable::BondsAdded:
|
---|
181 | {
|
---|
182 | ASSERT(!_atom->getListOfBonds().empty(),
|
---|
183 | "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
|
---|
184 | const bond::ptr _bond = *(_atom->getListOfBonds().rbegin());
|
---|
185 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
|
---|
186 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
187 | emit BondsInserted(_bond, side);
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | default:
|
---|
191 | //setProperties();
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }else if (static_cast<World *>(publisher) == World::getPointer()) {
|
---|
195 | // notification from world
|
---|
196 | #ifdef LOG_OBSERVER
|
---|
197 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
198 | << " received notification from world for channel "
|
---|
199 | << notification->getChannelNo() << ".";
|
---|
200 | #endif
|
---|
201 | switch (notification->getChannelNo()) {
|
---|
202 | case World::SelectionChanged:
|
---|
203 | setSelected(World::getInstance().isSelected(_atom));
|
---|
204 | break;
|
---|
205 | default:
|
---|
206 | break;
|
---|
207 | }
|
---|
208 | } else {
|
---|
209 | // notification from world
|
---|
210 | #ifdef LOG_OBSERVER
|
---|
211 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
212 | << " received notification from Worldtime for channel "
|
---|
213 | << notification->getChannelNo() << ".";
|
---|
214 | #endif
|
---|
215 | switch (notification->getChannelNo()) {
|
---|
216 | case WorldTime::TimeChanged:
|
---|
217 | resetPosition();
|
---|
218 | emit changed();
|
---|
219 | break;
|
---|
220 | default:
|
---|
221 | break;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | void GLMoleculeObject_atom::wasClicked()
|
---|
227 | {
|
---|
228 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked");
|
---|
229 | emit clicked(_atom->getId());
|
---|
230 | }
|
---|