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 | * GLWorldScene.cpp
|
---|
26 | *
|
---|
27 | * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
|
---|
28 | *
|
---|
29 | * Created on: Aug 17, 2011
|
---|
30 | * Author: heber
|
---|
31 | */
|
---|
32 |
|
---|
33 | // include config.h
|
---|
34 | #ifdef HAVE_CONFIG_H
|
---|
35 | #include <config.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include "GLWorldScene.hpp"
|
---|
39 | #include <Qt3D/qglview.h>
|
---|
40 | #include <Qt3D/qglbuilder.h>
|
---|
41 | #include <Qt3D/qglscenenode.h>
|
---|
42 | #include <Qt3D/qglsphere.h>
|
---|
43 | #include <Qt3D/qglcylinder.h>
|
---|
44 |
|
---|
45 | #include "GLMoleculeObject.hpp"
|
---|
46 | #include "GLMoleculeObject_atom.hpp"
|
---|
47 | #include "GLMoleculeObject_bond.hpp"
|
---|
48 | #include "GLMoleculeObject_molecule.hpp"
|
---|
49 | #include "GLMoleculeObject_shape.hpp"
|
---|
50 |
|
---|
51 | #include "CodePatterns/MemDebug.hpp"
|
---|
52 |
|
---|
53 | #include "CodePatterns/Log.hpp"
|
---|
54 |
|
---|
55 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
56 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
57 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
58 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
59 | #include "Atom/atom.hpp"
|
---|
60 | #include "Bond/bond.hpp"
|
---|
61 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
62 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
63 | #include "Helpers/helpers.hpp"
|
---|
64 | #include "Shapes/ShapeRegistry.hpp"
|
---|
65 | #include "molecule.hpp"
|
---|
66 | #include "World.hpp"
|
---|
67 |
|
---|
68 | #include <iostream>
|
---|
69 |
|
---|
70 | using namespace MoleCuilder;
|
---|
71 |
|
---|
72 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
73 | : QObject(parent)
|
---|
74 | {
|
---|
75 | int sphereDetails[] = {5, 3, 2, 0};
|
---|
76 | int cylinderDetails[] = {16, 8, 6, 3};
|
---|
77 | for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
|
---|
78 | QGLBuilder emptyBuilder;
|
---|
79 | GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
|
---|
80 | QGLBuilder sphereBuilder;
|
---|
81 | sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
|
---|
82 | GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
|
---|
83 | GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
84 | QGLBuilder cylinderBuilder;
|
---|
85 | cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
|
---|
86 | GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
|
---|
87 | GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
|
---|
88 | }
|
---|
89 |
|
---|
90 | connect(this, SIGNAL(updated()), this, SLOT(update()));
|
---|
91 |
|
---|
92 |
|
---|
93 | setSelectionMode(SelectAtom);
|
---|
94 |
|
---|
95 | init();
|
---|
96 | }
|
---|
97 |
|
---|
98 | GLWorldScene::~GLWorldScene()
|
---|
99 | {
|
---|
100 | // remove all elements
|
---|
101 | GLMoleculeObject::cleanMaterialMap();
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
105 | *
|
---|
106 | */
|
---|
107 | void GLWorldScene::init()
|
---|
108 | {
|
---|
109 | const std::vector<const molecule *> &molecules =
|
---|
110 | const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
111 |
|
---|
112 | for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
|
---|
113 | moliter != molecules.end();
|
---|
114 | moliter++) {
|
---|
115 | // create molecule objects in scene
|
---|
116 | moleculeInserted((*moliter)->getId());
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Update the WorldScene with molecules and atoms from World.
|
---|
121 | *
|
---|
122 | * This function should be called after e.g. WorldTime::TimeChanged was
|
---|
123 | * received or after another molecule has been loaded.
|
---|
124 | *
|
---|
125 | */
|
---|
126 | void GLWorldScene::update()
|
---|
127 | {
|
---|
128 | const std::vector<const molecule *> &molecules =
|
---|
129 | const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
130 |
|
---|
131 | for (std::vector<const molecule*>::const_iterator moliter = molecules.begin();
|
---|
132 | moliter != molecules.end();
|
---|
133 | moliter++) {
|
---|
134 | // check whether molecule already exists
|
---|
135 | const moleculeId_t molid = (*moliter)->getId();
|
---|
136 | const bool mol_present = MoleculesinSceneMap.count(molid);
|
---|
137 | if (!mol_present)
|
---|
138 | moleculeInserted((*moliter)->getId());
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
143 | {
|
---|
144 | LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
|
---|
145 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
---|
146 | getAtom(AtomById(no));
|
---|
147 | if (selectionMode == SelectAtom){
|
---|
148 | if (!World::getInstance().isSelected(Walker))
|
---|
149 | SelectionAtomById(std::vector<atomId_t>(1,no));
|
---|
150 | else
|
---|
151 | SelectionNotAtomById(std::vector<atomId_t>(1,no));
|
---|
152 | }else if (selectionMode == SelectMolecule){
|
---|
153 | const molecule *mol = Walker->getMolecule();
|
---|
154 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
155 | molids_t ids(1, mol->getId());
|
---|
156 | if (!World::getInstance().isSelected(mol))
|
---|
157 | SelectionMoleculeById(ids);
|
---|
158 | else
|
---|
159 | SelectionNotMoleculeById(ids);
|
---|
160 | }
|
---|
161 | emit clicked(no);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void GLWorldScene::moleculeClicked(moleculeId_t no)
|
---|
165 | {
|
---|
166 | LOG(3, "INFO: GLMoleculeObject_molecule - mol " << no << " has been clicked.");
|
---|
167 | const molecule * const mol= const_cast<const World &>(World::getInstance()).
|
---|
168 | getMolecule(MoleculeById(no));
|
---|
169 | ASSERT(mol, "Atom without molecule has been clicked.");
|
---|
170 | molids_t ids(1, mol->getId());
|
---|
171 | if (!World::getInstance().isSelected(mol))
|
---|
172 | SelectionMoleculeById(ids);
|
---|
173 | else
|
---|
174 | SelectionNotMoleculeById(ids);
|
---|
175 | emit clicked(no);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** ....
|
---|
179 | *
|
---|
180 | */
|
---|
181 | void GLWorldScene::worldSelectionChanged()
|
---|
182 | {
|
---|
183 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
|
---|
184 |
|
---|
185 | const std::vector<const molecule*> &molecules =
|
---|
186 | const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
187 |
|
---|
188 | if (molecules.size() > 0) {
|
---|
189 | for (std::vector<const molecule*>::const_iterator Runner = molecules.begin();
|
---|
190 | Runner != molecules.end();
|
---|
191 | Runner++) {
|
---|
192 |
|
---|
193 | // molecule selected but not in scene?
|
---|
194 | const bool isSelected =
|
---|
195 | const_cast<const World &>(World::getInstance()).isSelected(*Runner);
|
---|
196 | if (isSelected){
|
---|
197 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
|
---|
198 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
199 | "GLWorldScene::worldSelectionChanged() - selected molecule is unknown.");
|
---|
200 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
201 | // inform molecule object
|
---|
202 | molObject->selected(isSelected);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /** Inserts a molecule into the scene.
|
---|
209 | *
|
---|
210 | * @param _mol molecule to insert
|
---|
211 | */
|
---|
212 | void GLWorldScene::moleculeInserted(const moleculeId_t _id)
|
---|
213 | {
|
---|
214 | LOG(3, "INFO: GLWorldScene: Received signal moleculeInserted for molecule "+toString(_id)+".");
|
---|
215 | MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_id);
|
---|
216 | ASSERT( iter == MoleculesinSceneMap.end(),
|
---|
217 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" already present.");
|
---|
218 |
|
---|
219 | // add new object
|
---|
220 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _id);
|
---|
221 | ASSERT( molObject != NULL,
|
---|
222 | "GLWorldScene::moleculeInserted - could not create molecule object for "+toString(_id));
|
---|
223 | MoleculesinSceneMap.insert( make_pair(_id, molObject) );
|
---|
224 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
225 | connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
|
---|
226 | connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
227 | connect (molObject, SIGNAL(moleculeClicked(moleculeId_t)), this, SLOT(moleculeClicked(moleculeId_t)));
|
---|
228 | connect (molObject, SIGNAL(changeAtomId(GLMoleculeObject_atom *, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom *, int, int)));
|
---|
229 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
230 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
231 | connect (molObject, SIGNAL(hoverChanged(const atomId_t)), this, SIGNAL(hoverChanged(const atomId_t)));
|
---|
232 | connect (molObject, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SIGNAL(hoverChanged(const moleculeId_t, int)));
|
---|
233 | emit changed();
|
---|
234 | emit changeOccured();
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Removes a molecule from the scene.
|
---|
238 | *
|
---|
239 | * @param _id id of molecule to remove
|
---|
240 | */
|
---|
241 | void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
|
---|
242 | {
|
---|
243 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
|
---|
244 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
245 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
246 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
---|
247 |
|
---|
248 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
249 | molObject->disconnect();
|
---|
250 | MoleculesinSceneMap.erase(iter);
|
---|
251 | delete molObject;
|
---|
252 | emit changed();
|
---|
253 | emit changeOccured();
|
---|
254 | }
|
---|
255 |
|
---|
256 | void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
|
---|
257 | {
|
---|
258 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
|
---|
259 | ASSERT( iter != MoleculesinSceneMap.end(),
|
---|
260 | "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
|
---|
261 |
|
---|
262 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
263 | molObject->setVisible(_visible);
|
---|
264 |
|
---|
265 | emit changed();
|
---|
266 | emit changeOccured();
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Adds a shape to the scene.
|
---|
270 | *
|
---|
271 | * uses ShapeRegistry::lastChanged()
|
---|
272 | *
|
---|
273 | */
|
---|
274 | void GLWorldScene::addShape()
|
---|
275 | {
|
---|
276 | Shape &shape = *ShapeRegistry::getInstance().lastChanged();
|
---|
277 | GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
|
---|
278 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
|
---|
279 | ASSERT(iter == ShapesinSceneMap.end(),
|
---|
280 | "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
|
---|
281 | ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
|
---|
282 | }
|
---|
283 |
|
---|
284 | void GLWorldScene::removeShape()
|
---|
285 | {
|
---|
286 | Shape &shape = *ShapeRegistry::getInstance().lastChanged();
|
---|
287 | ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
|
---|
288 | ASSERT(iter != ShapesinSceneMap.end(),
|
---|
289 | "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
|
---|
290 | ShapesinSceneMap.erase(iter);
|
---|
291 | delete(iter->second);
|
---|
292 | }
|
---|
293 |
|
---|
294 | void GLWorldScene::updateSelectedShapes()
|
---|
295 | {
|
---|
296 | foreach (QObject *obj, children()) {
|
---|
297 | GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
|
---|
298 | if (shapeobj){
|
---|
299 | shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
305 | {
|
---|
306 | // Initialize all of the mesh objects that we have as children.
|
---|
307 | foreach (QObject *obj, children()) {
|
---|
308 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
309 | if (meshobj)
|
---|
310 | meshobj->initialize(view, painter);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
|
---|
315 | {
|
---|
316 | // Draw all of the mesh objects that we have as children.
|
---|
317 | foreach (QObject *obj, children()) {
|
---|
318 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
319 | if (meshobj)
|
---|
320 | meshobj->draw(painter, cameraPlane);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | void GLWorldScene::setSelectionMode(SelectionModeType mode)
|
---|
325 | {
|
---|
326 | selectionMode = mode;
|
---|
327 | // TODO send update to toolbar
|
---|
328 | }
|
---|
329 |
|
---|
330 | void GLWorldScene::setSelectionModeAtom()
|
---|
331 | {
|
---|
332 | setSelectionMode(SelectAtom);
|
---|
333 | }
|
---|
334 |
|
---|
335 | void GLWorldScene::setSelectionModeMolecule()
|
---|
336 | {
|
---|
337 | setSelectionMode(SelectMolecule);
|
---|
338 | }
|
---|