1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * GLWorldScene.cpp
|
---|
10 | *
|
---|
11 | * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
|
---|
12 | *
|
---|
13 | * Created on: Aug 17, 2011
|
---|
14 | * Author: heber
|
---|
15 | */
|
---|
16 |
|
---|
17 | // include config.h
|
---|
18 | #ifdef HAVE_CONFIG_H
|
---|
19 | #include <config.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include "GLWorldScene.hpp"
|
---|
23 |
|
---|
24 | #include "GLMoleculeObject.hpp"
|
---|
25 | #include "GLMoleculeObject_atom.hpp"
|
---|
26 | #include "GLMoleculeObject_bond.hpp"
|
---|
27 |
|
---|
28 | #include "CodePatterns/MemDebug.hpp"
|
---|
29 |
|
---|
30 | #include "CodePatterns/Log.hpp"
|
---|
31 |
|
---|
32 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
33 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
34 | #include "atom.hpp"
|
---|
35 | #include "Bond/bond.hpp"
|
---|
36 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
37 | #include "Helpers/helpers.hpp"
|
---|
38 | #include "molecule.hpp"
|
---|
39 | #include "World.hpp"
|
---|
40 |
|
---|
41 |
|
---|
42 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
43 | : QObject(parent)
|
---|
44 | {
|
---|
45 | init();
|
---|
46 |
|
---|
47 | //changeMaterials(false);
|
---|
48 | }
|
---|
49 |
|
---|
50 | GLWorldScene::~GLWorldScene()
|
---|
51 | {
|
---|
52 | // remove all elements
|
---|
53 | GLMoleculeObject::cleanMaterialMap();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
57 | *
|
---|
58 | */
|
---|
59 | void GLWorldScene::init()
|
---|
60 | {
|
---|
61 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
62 |
|
---|
63 | if (molecules.size() > 0) {
|
---|
64 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
65 | Runner != molecules.end();
|
---|
66 | Runner++) {
|
---|
67 | // create molecule
|
---|
68 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
69 | atomiter != (*Runner)->end();
|
---|
70 | ++atomiter) {
|
---|
71 | // create atom objects in scene
|
---|
72 | atomInserted(*atomiter);
|
---|
73 |
|
---|
74 | // create bond objects in scene
|
---|
75 | bondsChanged(*atomiter);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Adds an atom to the scene.
|
---|
82 | *
|
---|
83 | * @param _atom atom to add
|
---|
84 | */
|
---|
85 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
86 | {
|
---|
87 | LOG(0, "GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
88 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
---|
89 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
90 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
91 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
92 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
93 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
94 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
95 | connect (atomObject, SIGNAL(BondsChanged(const atom *)), this, SLOT(bondsChanged(const atom *)));
|
---|
96 | bondsChanged(_atom);
|
---|
97 | emit changeOccured();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Removes an atom from the scene.
|
---|
101 | *
|
---|
102 | * @param _atom atom to remove
|
---|
103 | */
|
---|
104 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
105 | {
|
---|
106 | LOG(0, "GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
107 | // remove all its bonds
|
---|
108 | const BondList& bondlist = _atom->getListOfBonds();
|
---|
109 | for (BondList::const_iterator iter = bondlist.begin(); iter != bondlist.end(); ++iter) {
|
---|
110 | bondRemoved((*iter)->leftatom->getId(), (*iter)->rightatom->getId());
|
---|
111 | bondRemoved((*iter)->rightatom->getId(), (*iter)->leftatom->getId());
|
---|
112 | }
|
---|
113 | // remove atoms
|
---|
114 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
115 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
116 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
117 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
118 | atomObject->disconnect();
|
---|
119 | AtomsinSceneMap.erase(iter);
|
---|
120 | delete atomObject;
|
---|
121 | emit changeOccured();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** Updates the bond structure of the signaled \a _atom.
|
---|
125 | *
|
---|
126 | * @param _atom atom whose bonds changed.
|
---|
127 | */
|
---|
128 | void GLWorldScene::bondsChanged(const atom *_atom)
|
---|
129 | {
|
---|
130 | const atomId_t id = _atom->getId();
|
---|
131 |
|
---|
132 | // create list with all present bonds
|
---|
133 | std::set< atomId_t > presentBonds;
|
---|
134 | std::pair< BondIdsMap::const_iterator, BondIdsMap::const_iterator> range =
|
---|
135 | BondIdsinSceneMap.equal_range( id );
|
---|
136 | for (BondIdsMap::const_iterator iter = range.first; iter != range.second; ++iter) {
|
---|
137 | const atomId_t otherid = iter->second;
|
---|
138 | #ifndef NDEBUG
|
---|
139 | std::set< atomId_t >::const_iterator iter = presentBonds.find(otherid);
|
---|
140 | ASSERT(iter == presentBonds.end(),
|
---|
141 | "GLWorldScene::BondsChanged() - bond id "+toString(otherid)+" for atom "
|
---|
142 | +toString(id)+" present twice.");
|
---|
143 | #endif
|
---|
144 | presentBonds.insert( otherid );
|
---|
145 | }
|
---|
146 | LOG(0, "We have the following bonds: "+toString(presentBonds)+".");
|
---|
147 |
|
---|
148 | // search for added bonds
|
---|
149 | const BondList &bondlist = _atom->getListOfBonds();
|
---|
150 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
151 | bonditer != bondlist.end();
|
---|
152 | ++bonditer) {
|
---|
153 | const bond *_bond = *bonditer;
|
---|
154 | const atomId_t otherid = _bond->GetOtherAtom(_atom)->getId();
|
---|
155 | const BondIds ids = std::make_pair( id, otherid );
|
---|
156 | BondNodeMap::const_iterator iter = BondsinSceneMap.find(ids);
|
---|
157 | if (iter != BondsinSceneMap.end()) {
|
---|
158 | // bond is already present
|
---|
159 | std::set< atomId_t >::const_iterator iter = presentBonds.find(otherid);
|
---|
160 | ASSERT(iter != presentBonds.end(),
|
---|
161 | "GLWorldScene::BondsChanged() - other id "+toString(otherid)+" for atom "
|
---|
162 | +toString(_atom->getId())+" not present in BondIdsinSceneMap.");
|
---|
163 | presentBonds.erase(otherid);
|
---|
164 | LOG(0, "Removing "+toString(otherid)+" from presentBonds.");
|
---|
165 | } else {
|
---|
166 | // insert new bond
|
---|
167 | bondInserted(_bond);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | LOG(0, "The following bonds should not be present: "+toString(presentBonds)+".");
|
---|
171 |
|
---|
172 | // remove all still presentBonds
|
---|
173 | for (std::set< atomId_t >::iterator iter = presentBonds.begin();
|
---|
174 | !presentBonds.empty(); iter = presentBonds.begin()) {
|
---|
175 | bondRemoved( id, *iter );
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Adds a bond to the scene.
|
---|
180 | *
|
---|
181 | * @param _bond bond to add
|
---|
182 | */
|
---|
183 | void GLWorldScene::bondInserted(const bond *_bond)
|
---|
184 | {
|
---|
185 | LOG(0, "GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
186 | const double distance =
|
---|
187 | _bond->leftatom->getPosition().distance(_bond->rightatom->getPosition())/2.;
|
---|
188 | {
|
---|
189 | // left bond
|
---|
190 | const BondIds Leftids( make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()) );
|
---|
191 | BondNodeMap::iterator iter = BondsinSceneMap.find(Leftids);
|
---|
192 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
193 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
194 | GLMoleculeObject_bond *bondObject =
|
---|
195 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::left);
|
---|
196 | BondsinSceneMap.insert( make_pair(Leftids, bondObject) );
|
---|
197 | BondIdsinSceneMap.insert( Leftids );
|
---|
198 | }
|
---|
199 | {
|
---|
200 | // right bond
|
---|
201 | const BondIds Rightids( make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()) );
|
---|
202 | BondNodeMap::iterator iter = BondsinSceneMap.find(Rightids);
|
---|
203 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
204 | "GLWorldScene::bondAdded() - same right-sided bond "+toString(*_bond)+" added again.");
|
---|
205 | GLMoleculeObject_bond *bondObject =
|
---|
206 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::right);
|
---|
207 | BondsinSceneMap.insert( make_pair(Rightids, bondObject) );
|
---|
208 | BondIdsinSceneMap.insert( Rightids );
|
---|
209 | }
|
---|
210 | emit changeOccured();
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Removes a bond from the scene.
|
---|
214 | *
|
---|
215 | * @param _bond bond to remove
|
---|
216 | */
|
---|
217 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
218 | {
|
---|
219 | LOG(0, "GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(leftnr)+".");
|
---|
220 | {
|
---|
221 | // left bond
|
---|
222 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
223 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
224 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
225 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
226 | +toString(rightnr)+" not on display.");
|
---|
227 | GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
228 | BondsinSceneMap.erase(leftiter);
|
---|
229 | delete bondObject;
|
---|
230 | }
|
---|
231 | // remove from bond ids
|
---|
232 | std::pair<BondIdsMap::iterator, BondIdsMap::iterator> leftrange =
|
---|
233 | BondIdsinSceneMap.equal_range(leftnr);
|
---|
234 | BondIdsMap::iterator iter;
|
---|
235 | for (iter = leftrange.first; iter != leftrange.second; ++iter) {
|
---|
236 | if (iter->second == rightnr) {
|
---|
237 | BondIdsinSceneMap.erase(iter);
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 | ASSERT(iter != leftrange.second,
|
---|
242 | "GLWorldScene::bondRemoved() - could not find ("
|
---|
243 | +toString(leftnr)+"-"+toString(rightnr)+" in BondIdsinSceneMap.");
|
---|
244 | emit changeOccured();
|
---|
245 | }
|
---|
246 |
|
---|
247 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
248 | {
|
---|
249 | // Initialize all of the mesh objects that we have as children.
|
---|
250 | foreach (QObject *obj, children()) {
|
---|
251 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
252 | if (meshobj)
|
---|
253 | meshobj->initialize(view, painter);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
258 | {
|
---|
259 | // Draw all of the mesh objects that we have as children.
|
---|
260 | foreach (QObject *obj, children()) {
|
---|
261 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
262 | if (meshobj)
|
---|
263 | meshobj->draw(painter);
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
268 | {
|
---|
269 | qDebug("GLWorldScene: atom %d has been clicked.", no);
|
---|
270 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
271 | if (!World::getInstance().isSelected(Walker))
|
---|
272 | SelectionAtomById(no);
|
---|
273 | else
|
---|
274 | SelectionNotAtomById(no);
|
---|
275 | emit clicked(no);
|
---|
276 | }
|
---|
277 |
|
---|