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 "atom.hpp"
|
---|
33 | #include "Bond/bond.hpp"
|
---|
34 | #include "molecule.hpp"
|
---|
35 | #include "World.hpp"
|
---|
36 |
|
---|
37 |
|
---|
38 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
39 | : QObject(parent)
|
---|
40 | {
|
---|
41 | init();
|
---|
42 |
|
---|
43 | //changeMaterials(false);
|
---|
44 | }
|
---|
45 |
|
---|
46 | GLWorldScene::~GLWorldScene()
|
---|
47 | {
|
---|
48 | // remove all elements
|
---|
49 | GLMoleculeObject::cleanMaterialMap();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | void GLWorldScene::init()
|
---|
56 | {
|
---|
57 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
58 |
|
---|
59 | if (molecules.size() > 0) {
|
---|
60 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
61 | Runner != molecules.end();
|
---|
62 | Runner++) {
|
---|
63 | // create molecule
|
---|
64 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
65 | atomiter != (*Runner)->end();
|
---|
66 | ++atomiter) {
|
---|
67 | // create atom objects in scene
|
---|
68 | atomInserted(*atomiter);
|
---|
69 |
|
---|
70 | // create its bonds
|
---|
71 | const BondList &bonds = (*atomiter)->getListOfBonds();
|
---|
72 | for (BondList::const_iterator bonditer = bonds.begin();
|
---|
73 | bonditer != bonds.end();
|
---|
74 | ++bonditer) {
|
---|
75 | if ((*bonditer)->leftatom->getId() == (*atomiter)->getId()) {
|
---|
76 | // create bond objects in scene
|
---|
77 | bondInserted(*bonditer);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Adds an atom to the scene.
|
---|
86 | *
|
---|
87 | * @param _atom atom to add
|
---|
88 | */
|
---|
89 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
90 | {
|
---|
91 | LOG(0, "GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
92 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
---|
93 | AtomNodeMap::iterator iter = AtomsinMoleculeMap.find(_atom->getId());
|
---|
94 | ASSERT(iter == AtomsinMoleculeMap.end(),
|
---|
95 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
96 | AtomsinMoleculeMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
97 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
98 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(hoverChanged()));
|
---|
99 | emit changed();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Removes an atom from the scene.
|
---|
103 | *
|
---|
104 | * @param _atom atom to remove
|
---|
105 | */
|
---|
106 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
107 | {
|
---|
108 | LOG(0, "GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
109 | // remove all its bonds
|
---|
110 | const BondList& bondlist = _atom->getListOfBonds();
|
---|
111 | for (BondList::const_iterator iter = bondlist.begin(); iter != bondlist.end(); ++iter) {
|
---|
112 | bondRemoved(*iter);
|
---|
113 | }
|
---|
114 | // remove atoms
|
---|
115 | AtomNodeMap::iterator iter = AtomsinMoleculeMap.find(_atom->getId());
|
---|
116 | ASSERT(iter != AtomsinMoleculeMap.end(),
|
---|
117 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
118 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
119 | atomObject->disconnect();
|
---|
120 | AtomsinMoleculeMap.erase(iter);
|
---|
121 | delete atomObject;
|
---|
122 | emit changed();
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** Adds an bond to the scene.
|
---|
126 | *
|
---|
127 | * @param _bond bond to add
|
---|
128 | */
|
---|
129 | void GLWorldScene::bondInserted(const bond *_bond)
|
---|
130 | {
|
---|
131 | const double distance =
|
---|
132 | _bond->leftatom->getPosition().distance(_bond->rightatom->getPosition())/2.;
|
---|
133 | {
|
---|
134 | // left bond
|
---|
135 | const BondIds Leftids( make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()) );
|
---|
136 | BondNodeMap::iterator iter = BondsinMoleculeMap.find(Leftids);
|
---|
137 | ASSERT(iter == BondsinMoleculeMap.end(),
|
---|
138 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
139 | GLMoleculeObject_bond *bondObject =
|
---|
140 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::left);
|
---|
141 | BondsinMoleculeMap.insert( make_pair(Leftids, bondObject) );
|
---|
142 | }
|
---|
143 | {
|
---|
144 | // right bond
|
---|
145 | const BondIds Rightids( make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()) );
|
---|
146 | BondNodeMap::iterator iter = BondsinMoleculeMap.find(Rightids);
|
---|
147 | ASSERT(iter == BondsinMoleculeMap.end(),
|
---|
148 | "GLWorldScene::bondAdded() - same right-sided bond "+toString(*_bond)+" added again.");
|
---|
149 | GLMoleculeObject_bond *bondObject =
|
---|
150 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::right);
|
---|
151 | BondsinMoleculeMap.insert( make_pair(Rightids, bondObject) );
|
---|
152 | }
|
---|
153 | emit changed();
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** Removes an bond from the scene.
|
---|
157 | *
|
---|
158 | * @param _bond bond to remove
|
---|
159 | */
|
---|
160 | void GLWorldScene::bondRemoved(const bond *_bond)
|
---|
161 | {
|
---|
162 | {
|
---|
163 | // left bond
|
---|
164 | const BondIds Leftids( make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()) );
|
---|
165 | BondNodeMap::iterator leftiter = BondsinMoleculeMap.find( Leftids );
|
---|
166 | ASSERT(leftiter != BondsinMoleculeMap.end(),
|
---|
167 | "GLWorldScene::bondRemoved() - bond "+toString(*_bond)+" not on display.");
|
---|
168 | GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
169 | BondsinMoleculeMap.erase(leftiter);
|
---|
170 | delete bondObject;
|
---|
171 | }
|
---|
172 | {
|
---|
173 | // right bond
|
---|
174 | const BondIds Rightids( make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()) );
|
---|
175 | BondNodeMap::iterator rightiter = BondsinMoleculeMap.find( Rightids );
|
---|
176 | ASSERT(rightiter != BondsinMoleculeMap.end(),
|
---|
177 | "GLWorldScene::bondRemoved() - bond "+toString(*_bond)+" not on display.");
|
---|
178 | GLMoleculeObject_bond *bondObject = rightiter->second;
|
---|
179 | BondsinMoleculeMap.erase(rightiter);
|
---|
180 | delete bondObject;
|
---|
181 | }
|
---|
182 | emit changed();
|
---|
183 | }
|
---|
184 |
|
---|
185 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
186 | {
|
---|
187 | // Initialize all of the mesh objects that we have as children.
|
---|
188 | foreach (QObject *obj, children()) {
|
---|
189 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
190 | if (meshobj)
|
---|
191 | meshobj->initialize(view, painter);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
196 | {
|
---|
197 | // Draw all of the mesh objects that we have as children.
|
---|
198 | foreach (QObject *obj, children()) {
|
---|
199 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
200 | if (meshobj)
|
---|
201 | meshobj->draw(painter);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
206 | {
|
---|
207 | qDebug("GLWorldScene: atom %d has been clicked.", no);
|
---|
208 | emit clicked(no);
|
---|
209 | }
|
---|
210 |
|
---|