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