1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * GLMoleculeObject_atom.cpp
|
---|
10 | *
|
---|
11 | * Created on: Aug 17, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "GLMoleculeObject_atom.hpp"
|
---|
21 |
|
---|
22 | #include <Qt3D/qglbuilder.h>
|
---|
23 | #include <Qt3D/qglscenenode.h>
|
---|
24 | #include <Qt3D/qglsphere.h>
|
---|
25 |
|
---|
26 | #include "CodePatterns/MemDebug.hpp"
|
---|
27 |
|
---|
28 | #include "CodePatterns/Assert.hpp"
|
---|
29 | #include "CodePatterns/Log.hpp"
|
---|
30 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
31 |
|
---|
32 | #include "Atom/atom.hpp"
|
---|
33 | #include "Bond/bond.hpp"
|
---|
34 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
35 | #include "Element/element.hpp"
|
---|
36 | #include "LinearAlgebra/Vector.hpp"
|
---|
37 | #include "GLMoleculeObject_bond.hpp"
|
---|
38 | #include "World.hpp"
|
---|
39 |
|
---|
40 | static QGLSceneNode * createAtom(QObject *parent)
|
---|
41 | {
|
---|
42 | QGLBuilder builder;
|
---|
43 | builder << QGLSphere(0.5);
|
---|
44 | QGLSceneNode *n = builder.finalizedSceneNode();
|
---|
45 | n->setParent(parent);
|
---|
46 | return n;
|
---|
47 | }
|
---|
48 |
|
---|
49 | GLMoleculeObject_atom::GLMoleculeObject_atom(QObject *parent, const atom *atomref) :
|
---|
50 | GLMoleculeObject(createAtom(parent), parent),
|
---|
51 | Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())),
|
---|
52 | _atom(atomref)
|
---|
53 | {
|
---|
54 | // sign on as observer (obtain non-const instance before)
|
---|
55 | atomref->signOn(this, AtomObservable::IndexChanged);
|
---|
56 | atomref->signOn(this, AtomObservable::PositionChanged);
|
---|
57 | atomref->signOn(this, AtomObservable::ElementChanged);
|
---|
58 | atomref->signOn(this, AtomObservable::BondsAdded);
|
---|
59 |
|
---|
60 | // set the object's id
|
---|
61 | resetProperties();
|
---|
62 |
|
---|
63 | LOG(2, "INFO: Created sphere for atom " << _atom->getId() << ".");
|
---|
64 |
|
---|
65 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
66 | }
|
---|
67 |
|
---|
68 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
|
---|
69 | {
|
---|
70 | _atom->signOff(this, AtomObservable::IndexChanged);
|
---|
71 | _atom->signOff(this, AtomObservable::PositionChanged);
|
---|
72 | _atom->signOff(this, AtomObservable::ElementChanged);
|
---|
73 | _atom->signOff(this, AtomObservable::BondsAdded);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void GLMoleculeObject_atom::update(Observable *publisher)
|
---|
77 | {
|
---|
78 | #ifdef LOG_OBSERVER
|
---|
79 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from atom "+toString(_atom->getId())+".";
|
---|
80 | #endif
|
---|
81 | resetProperties();
|
---|
82 | }
|
---|
83 |
|
---|
84 | void GLMoleculeObject_atom::resetPosition()
|
---|
85 | {
|
---|
86 | const Vector Position = _atom->getPosition();
|
---|
87 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
|
---|
88 | setPosition(QVector3D(Position[0], Position[1], Position[2]));
|
---|
89 | }
|
---|
90 |
|
---|
91 | void GLMoleculeObject_atom::resetElement()
|
---|
92 | {
|
---|
93 | size_t elementno = 0;
|
---|
94 | if (_atom->getType() != NULL) {
|
---|
95 | elementno = _atom->getType()->getNumber();
|
---|
96 | } else { // if no element yet, set to hydrogen
|
---|
97 | elementno = 1;
|
---|
98 | }
|
---|
99 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
|
---|
100 |
|
---|
101 | // set materials
|
---|
102 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
103 | ASSERT(elementmaterial != NULL,
|
---|
104 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
|
---|
105 | setMaterial(elementmaterial);
|
---|
106 | QGLMaterial *hovermaterial = getMaterial(0); // 0 is the hover material
|
---|
107 | ASSERT(hovermaterial != NULL,
|
---|
108 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function for hover is NULL.");
|
---|
109 | setHoverMaterial(hovermaterial);
|
---|
110 |
|
---|
111 | // set scale
|
---|
112 | double radius = 0.;
|
---|
113 | if (_atom->getType() != NULL) {
|
---|
114 | radius = _atom->getType()->getVanDerWaalsRadius();
|
---|
115 | } else {
|
---|
116 | radius = 0.5;
|
---|
117 | }
|
---|
118 | setScale( radius );
|
---|
119 | }
|
---|
120 |
|
---|
121 | void GLMoleculeObject_atom::resetIndex()
|
---|
122 | {
|
---|
123 | const size_t atomno = _atom->getId();
|
---|
124 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+".");
|
---|
125 | setObjectId(atomno);
|
---|
126 | }
|
---|
127 |
|
---|
128 | void GLMoleculeObject_atom::resetProperties()
|
---|
129 | {
|
---|
130 | // set position
|
---|
131 | resetPosition();
|
---|
132 |
|
---|
133 | // set element
|
---|
134 | resetElement();
|
---|
135 |
|
---|
136 | // set the object's id
|
---|
137 | resetIndex();
|
---|
138 | }
|
---|
139 |
|
---|
140 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
|
---|
141 | {}
|
---|
142 |
|
---|
143 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
144 | {
|
---|
145 | #ifdef LOG_OBSERVER
|
---|
146 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
|
---|
147 | << " received notification from atom " << _atom->getId() << " for channel "
|
---|
148 | << notification->getChannelNo() << ".";
|
---|
149 | #endif
|
---|
150 | switch (notification->getChannelNo()) {
|
---|
151 | case AtomObservable::ElementChanged:
|
---|
152 | resetElement();
|
---|
153 | break;
|
---|
154 | case AtomObservable::IndexChanged:
|
---|
155 | resetIndex();
|
---|
156 | break;
|
---|
157 | case AtomObservable::PositionChanged:
|
---|
158 | resetPosition();
|
---|
159 | break;
|
---|
160 | case AtomObservable::BondsAdded:
|
---|
161 | {
|
---|
162 | ASSERT(!_atom->getListOfBonds().empty(),
|
---|
163 | "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
|
---|
164 | const bond * _bond = *(_atom->getListOfBonds().rbegin());
|
---|
165 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
|
---|
166 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
167 | emit BondsInserted(_bond, side);
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | default:
|
---|
171 | //setProperties();
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | void GLMoleculeObject_atom::wasClicked()
|
---|
177 | {
|
---|
178 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked");
|
---|
179 | emit clicked(_atom->getId());
|
---|
180 | }
|
---|