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 | * 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 | #include <Qt3D/qglview.h>
|
---|
24 |
|
---|
25 | #include "GLMoleculeObject.hpp"
|
---|
26 | #include "GLMoleculeObject_atom.hpp"
|
---|
27 | #include "GLMoleculeObject_bond.hpp"
|
---|
28 | #include "GLMoleculeObject_molecule.hpp"
|
---|
29 |
|
---|
30 | #include "CodePatterns/MemDebug.hpp"
|
---|
31 |
|
---|
32 | #include "CodePatterns/Log.hpp"
|
---|
33 |
|
---|
34 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
35 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
36 | #include "Atom/atom.hpp"
|
---|
37 | #include "Bond/bond.hpp"
|
---|
38 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
39 | #include "Helpers/helpers.hpp"
|
---|
40 | #include "molecule.hpp"
|
---|
41 | #include "World.hpp"
|
---|
42 |
|
---|
43 | #include <iostream>
|
---|
44 |
|
---|
45 | using namespace MoleCuilder;
|
---|
46 |
|
---|
47 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
48 | {
|
---|
49 | ost << t.first << "," << t.second;
|
---|
50 | return ost;
|
---|
51 | }
|
---|
52 |
|
---|
53 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
54 | : QObject(parent)
|
---|
55 | {
|
---|
56 | init();
|
---|
57 | }
|
---|
58 |
|
---|
59 | GLWorldScene::~GLWorldScene()
|
---|
60 | {
|
---|
61 | // remove all elements
|
---|
62 | GLMoleculeObject::cleanMaterialMap();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
66 | *
|
---|
67 | */
|
---|
68 | void GLWorldScene::init()
|
---|
69 | {
|
---|
70 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
71 |
|
---|
72 | if (molecules.size() > 0) {
|
---|
73 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
74 | Runner != molecules.end();
|
---|
75 | Runner++) {
|
---|
76 |
|
---|
77 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
78 | atomiter != (*Runner)->end();
|
---|
79 | ++atomiter) {
|
---|
80 | // create atom objects in scene
|
---|
81 | atomInserted(*atomiter);
|
---|
82 |
|
---|
83 | // create bond objects in scene
|
---|
84 | const BondList &bondlist = (*atomiter)->getListOfBonds();
|
---|
85 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
86 | bonditer != bondlist.end();
|
---|
87 | ++bonditer) {
|
---|
88 | const bond *_bond = *bonditer;
|
---|
89 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
|
---|
90 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
91 | bondInserted(_bond, side);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Adds an atom to the scene.
|
---|
99 | *
|
---|
100 | * @param _atom atom to add
|
---|
101 | */
|
---|
102 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
103 | {
|
---|
104 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
105 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
---|
106 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
107 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
108 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
109 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
110 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
111 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
112 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
113 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
114 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
115 | //bondsChanged(_atom);
|
---|
116 | emit changeOccured();
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Removes an atom from the scene.
|
---|
120 | *
|
---|
121 | * @param _atom atom to remove
|
---|
122 | */
|
---|
123 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
124 | {
|
---|
125 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
126 | // bonds are removed by signal coming from ~bond
|
---|
127 | // remove atoms
|
---|
128 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
129 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
130 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
131 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
132 | atomObject->disconnect();
|
---|
133 | AtomsinSceneMap.erase(iter);
|
---|
134 | delete atomObject;
|
---|
135 | emit changeOccured();
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** ....
|
---|
139 | *
|
---|
140 | */
|
---|
141 | void GLWorldScene::worldSelectionChanged()
|
---|
142 | {
|
---|
143 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
|
---|
144 |
|
---|
145 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
146 |
|
---|
147 | if (molecules.size() > 0) {
|
---|
148 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
149 | Runner != molecules.end();
|
---|
150 | Runner++) {
|
---|
151 |
|
---|
152 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
|
---|
153 | bool isSelected = World::getInstance().isSelected(*Runner);
|
---|
154 |
|
---|
155 | // molecule selected but not in scene?
|
---|
156 | if (isSelected && (iter == MoleculesinSceneMap.end())){
|
---|
157 | // -> create new mesh
|
---|
158 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(this, *Runner);
|
---|
159 | MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
|
---|
160 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
161 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
162 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
163 | emit changed();
|
---|
164 | emit changeOccured();
|
---|
165 | }
|
---|
166 |
|
---|
167 | // molecule not selected but in scene?
|
---|
168 | if (!isSelected && (iter != MoleculesinSceneMap.end())){
|
---|
169 | // -> remove from scene
|
---|
170 | moleculeRemoved(*Runner);
|
---|
171 | }
|
---|
172 |
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Removes a molecule from the scene.
|
---|
178 | *
|
---|
179 | * @param _molecule molecule to remove
|
---|
180 | */
|
---|
181 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
|
---|
182 | {
|
---|
183 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
184 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
185 |
|
---|
186 | // only remove if the molecule is in the scene
|
---|
187 | // (= is selected)
|
---|
188 | if (iter != MoleculesinSceneMap.end()){
|
---|
189 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
190 | molObject->disconnect();
|
---|
191 | MoleculesinSceneMap.erase(iter);
|
---|
192 | delete molObject;
|
---|
193 | emit changed();
|
---|
194 | emit changeOccured();
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Adds a bond to the scene.
|
---|
199 | *
|
---|
200 | * @param _bond bond to add
|
---|
201 | * @param side which side of the bond (left or right)
|
---|
202 | */
|
---|
203 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
204 | {
|
---|
205 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
206 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
---|
207 |
|
---|
208 | BondIds ids;
|
---|
209 | switch (side) {
|
---|
210 | case GLMoleculeObject_bond::left:
|
---|
211 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
---|
212 | break;
|
---|
213 | case GLMoleculeObject_bond::right:
|
---|
214 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | #ifndef NDEBUG
|
---|
218 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
---|
219 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
220 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
221 | #endif
|
---|
222 | GLMoleculeObject_bond *bondObject =
|
---|
223 | new GLMoleculeObject_bond(this, _bond, side);
|
---|
224 | connect (
|
---|
225 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
226 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
227 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
228 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
229 | // BondIdsinSceneMap.insert( Leftids );
|
---|
230 | emit changeOccured();
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Removes a bond from the scene.
|
---|
234 | *
|
---|
235 | * @param _bond bond to remove
|
---|
236 | */
|
---|
237 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
238 | {
|
---|
239 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
240 | {
|
---|
241 | // left bond
|
---|
242 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
243 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
244 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
245 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
246 | +toString(rightnr)+" not on display.");
|
---|
247 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
248 | BondsinSceneMap.erase(leftiter);
|
---|
249 | //delete bondObject; // is done by signal from bond itself
|
---|
250 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
251 | }
|
---|
252 |
|
---|
253 | emit changeOccured();
|
---|
254 | }
|
---|
255 |
|
---|
256 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
257 | {
|
---|
258 | // Initialize all of the mesh objects that we have as children.
|
---|
259 | foreach (QObject *obj, children()) {
|
---|
260 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
261 | if (meshobj)
|
---|
262 | meshobj->initialize(view, painter);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
267 | {
|
---|
268 | // Draw all of the mesh objects that we have as children.
|
---|
269 | foreach (QObject *obj, children()) {
|
---|
270 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
271 | if (meshobj)
|
---|
272 | meshobj->draw(painter);
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
277 | {
|
---|
278 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
279 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
280 | if (!World::getInstance().isSelected(Walker))
|
---|
281 | SelectionAtomById(no);
|
---|
282 | else
|
---|
283 | SelectionNotAtomById(no);
|
---|
284 | emit clicked(no);
|
---|
285 | }
|
---|
286 |
|
---|