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 | * GLWorldView.cpp
|
---|
26 | *
|
---|
27 | * Created on: Aug 1, 2010
|
---|
28 | * Author: heber
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "GLWorldView.hpp"
|
---|
37 |
|
---|
38 | #include <Qt/qevent.h>
|
---|
39 | #include <Qt/qaction.h>
|
---|
40 | #include <QtGui/QMenu>
|
---|
41 | #include <QtGui/QToolBar>
|
---|
42 | #include <QtGui/QToolButton>
|
---|
43 | #include <Qt/qtimer.h>
|
---|
44 | #include <Qt/qsettings.h>
|
---|
45 | #include <Qt3D/qglbuilder.h>
|
---|
46 | #include <Qt3D/qglscenenode.h>
|
---|
47 | #include <Qt3D/qglsphere.h>
|
---|
48 | #include <Qt3D/qglcylinder.h>
|
---|
49 | #include <Qt3D/qglcube.h>
|
---|
50 |
|
---|
51 | #include "GLWorldScene.hpp"
|
---|
52 |
|
---|
53 | #include "CodePatterns/MemDebug.hpp"
|
---|
54 |
|
---|
55 | #include "Atom/AtomObserver.hpp"
|
---|
56 | #include "Atom/atom_observable.hpp"
|
---|
57 | #include "Box.hpp"
|
---|
58 | #include "CodePatterns/Log.hpp"
|
---|
59 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
60 | #include "CodePatterns/Observer/ObserverLog.hpp"
|
---|
61 | #include "molecule.hpp"
|
---|
62 | #include "Shapes/ShapeRegistry.hpp"
|
---|
63 | #include "World.hpp"
|
---|
64 | #include "WorldTime.hpp"
|
---|
65 |
|
---|
66 | GLWorldView::GLWorldView(QWidget *parent)
|
---|
67 | : QGLView(parent), Observer("GLWorldView"), worldscene(NULL), changesPresent(false), needsRedraw(false)
|
---|
68 | {
|
---|
69 | worldscene = new GLWorldScene(this);
|
---|
70 |
|
---|
71 | setOption(QGLView::ObjectPicking, true);
|
---|
72 | setOption(QGLView::CameraNavigation, false);
|
---|
73 | setFocusPolicy(Qt::StrongFocus);
|
---|
74 | setCameraControlMode(Rotate);
|
---|
75 | defaultEyeSeparation = 4.0;
|
---|
76 |
|
---|
77 | createDomainBox();
|
---|
78 | createDreiBein();
|
---|
79 | //changeMaterials(false);
|
---|
80 |
|
---|
81 | qRegisterMetaType<atomId_t>("atomId_t");
|
---|
82 | qRegisterMetaType<moleculeId_t>("moleculeId_t");
|
---|
83 |
|
---|
84 | connect(this, SIGNAL(ShapeAdded()), worldscene, SLOT(addShape()));
|
---|
85 | connect(this, SIGNAL(ShapeRemoved()), worldscene, SLOT(removeShape()));
|
---|
86 | connect(this, SIGNAL(TimeChanged()), worldscene, SIGNAL(updated()));
|
---|
87 | connect(worldscene, SIGNAL(changeOccured()), this, SLOT(changeSignalled()));
|
---|
88 | connect(worldscene, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
89 | connect(worldscene, SIGNAL(hoverChanged(const atom &)), this, SLOT(sceneHoverSignalled(const atom &)));
|
---|
90 | connect(worldscene, SIGNAL(hoverChanged(const molecule &, int)), this, SLOT(sceneHoverSignalled(const molecule &, int)));
|
---|
91 | connect(this, SIGNAL(worldSelectionChanged()), worldscene, SLOT(worldSelectionChanged()));
|
---|
92 | connect(this, SIGNAL(moleculeRemoved(const moleculeId_t)), worldscene, SLOT(moleculeRemoved(const moleculeId_t)));
|
---|
93 | connect(this, SIGNAL(moleculeInserted(const moleculeId_t)), worldscene, SLOT(moleculeInserted(const moleculeId_t)));
|
---|
94 | //connect(this, SIGNAL(changed()), this, SLOT(updateGL()));
|
---|
95 | connect(this, SIGNAL(changed()), this, SLOT(sceneChangeSignalled()));
|
---|
96 | connect(this, SIGNAL(moleculesVisibilityChanged(const moleculeId_t,bool)), worldscene, SLOT(moleculesVisibilityChanged(const moleculeId_t,bool)));
|
---|
97 |
|
---|
98 | // sign on to changes in the world
|
---|
99 | World::getInstance().signOn(this);
|
---|
100 | World::getInstance().signOn(this, World::MoleculeInserted);
|
---|
101 | World::getInstance().signOn(this, World::MoleculeRemoved);
|
---|
102 | World::getInstance().signOn(this, World::SelectionChanged);
|
---|
103 | WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
|
---|
104 | AtomObserver::getInstance().signOn(this, AtomObservable::PositionChanged);
|
---|
105 |
|
---|
106 | ShapeRegistry::getInstance().signOn(this);
|
---|
107 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted);
|
---|
108 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved);
|
---|
109 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged);
|
---|
110 |
|
---|
111 | redrawTimer = new QTimer(this);
|
---|
112 | }
|
---|
113 |
|
---|
114 | GLWorldView::~GLWorldView()
|
---|
115 | {
|
---|
116 | QSettings settings;
|
---|
117 | settings.beginGroup("WorldView");
|
---|
118 | settings.setValue("domainBoxEnabled", (meshDomainBox->options() & QGLSceneNode::HideNode) == 0);
|
---|
119 | settings.setValue("dreiBeinEnabled", (meshDreiBein->options() & QGLSceneNode::HideNode) == 0);
|
---|
120 | settings.endGroup();
|
---|
121 |
|
---|
122 |
|
---|
123 | World::getInstance().signOff(this);
|
---|
124 | World::getInstance().signOff(this, World::AtomInserted);
|
---|
125 | World::getInstance().signOff(this, World::AtomRemoved);
|
---|
126 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
127 | World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
128 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
129 | WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
|
---|
130 | AtomObserver::getInstance().signOff(this, AtomObservable::PositionChanged);
|
---|
131 | ShapeRegistry::getInstance().signOff(this);
|
---|
132 | ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeInserted);
|
---|
133 | ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeRemoved);
|
---|
134 | ShapeRegistry::getInstance().signOff(this, ShapeRegistry::SelectionChanged);
|
---|
135 | delete worldscene;
|
---|
136 |
|
---|
137 | delete(domainBoxMaterial);
|
---|
138 | for (int i=0;i<3;i++)
|
---|
139 | delete(dreiBeinMaterial[i]);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Add some widget specific actions to the toolbar:
|
---|
145 | * - camera rotation/translation mode
|
---|
146 | * - camera fit to domain
|
---|
147 | */
|
---|
148 | void GLWorldView::addToolBarActions(QToolBar *toolbar)
|
---|
149 | {
|
---|
150 | // camera control mode
|
---|
151 | toolbar->addSeparator();
|
---|
152 | QAction *transAction = new QAction(QIcon::fromTheme("forward"), tr("camera translation mode"), this);
|
---|
153 | connect(transAction, SIGNAL(triggered()), this, SLOT(setCameraControlModeTranslation()));
|
---|
154 | toolbar->addAction(transAction);
|
---|
155 | QAction *rotAction = new QAction(QIcon::fromTheme("object-rotate-left"), tr("camera rotation mode"), this);
|
---|
156 | connect(rotAction, SIGNAL(triggered()), this, SLOT(setCameraControlModeRotation()));
|
---|
157 | toolbar->addAction(rotAction);
|
---|
158 | QAction *fitAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("camera fit to domain"), this);
|
---|
159 | connect(fitAction, SIGNAL(triggered()), this, SLOT(fitCameraToDomain()));
|
---|
160 | toolbar->addAction(fitAction);
|
---|
161 |
|
---|
162 | // stereo mode
|
---|
163 | QToolButton *stereoButton = new QToolButton(toolbar);
|
---|
164 | QMenu *stereoMenu = new QMenu();
|
---|
165 | QAction *stereoDisableAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("disable"), this);
|
---|
166 | connect(stereoDisableAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeDisable()));
|
---|
167 | stereoMenu->addAction(stereoDisableAction);
|
---|
168 | QAction *stereoHardwareAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("hardware"), this);
|
---|
169 | connect(stereoHardwareAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeHardware()));
|
---|
170 | stereoMenu->addAction(stereoHardwareAction);
|
---|
171 | QAction *stereoLeftRightAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("left right"), this);
|
---|
172 | connect(stereoLeftRightAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeLeftRight()));
|
---|
173 | stereoMenu->addAction(stereoLeftRightAction);
|
---|
174 | QAction *stereoRightLeftAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("right left"), this);
|
---|
175 | connect(stereoRightLeftAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeRightLeft()));
|
---|
176 | stereoMenu->addAction(stereoRightLeftAction);
|
---|
177 | QAction *stereoTopBottomAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("top bottom"), this);
|
---|
178 | connect(stereoTopBottomAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeTopBottom()));
|
---|
179 | stereoMenu->addAction(stereoTopBottomAction);
|
---|
180 | QAction *stereoBottomTopAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("bottom top"), this);
|
---|
181 | connect(stereoBottomTopAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeBottomTop()));
|
---|
182 | stereoMenu->addAction(stereoBottomTopAction);
|
---|
183 | QAction *stereoAnaglyphAction = new QAction(QIcon::fromTheme("zoom-best-fit"), tr("anaglyph"), this);
|
---|
184 | connect(stereoAnaglyphAction, SIGNAL(triggered()), this, SLOT(setCameraStereoModeAnaglyph()));
|
---|
185 | stereoMenu->addAction(stereoAnaglyphAction);
|
---|
186 | stereoButton->setMenu(stereoMenu);
|
---|
187 | stereoButton->setIcon(QIcon(QPixmap(":/icon_view_stereo.png")));
|
---|
188 | stereoButton->setPopupMode(QToolButton::InstantPopup);
|
---|
189 | toolbar->addWidget(stereoButton);
|
---|
190 |
|
---|
191 | // selection mode
|
---|
192 | toolbar->addSeparator();
|
---|
193 | QAction *selAtomAction = new QAction(QIcon(QPixmap(":/icon_select_atom.png")), tr("select atom by clicking"), this);
|
---|
194 | connect(selAtomAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeAtom()));
|
---|
195 | toolbar->addAction(selAtomAction);
|
---|
196 | QAction *selMolAction = new QAction(QIcon(QPixmap(":/icon_select_molecule.png")), tr("select molecule by clicking"), this);
|
---|
197 | connect(selMolAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeMolecule()));
|
---|
198 | toolbar->addAction(selMolAction);
|
---|
199 |
|
---|
200 | // dreiBein/domain enabler
|
---|
201 | toolbar->addSeparator();
|
---|
202 | QAction *seldreiBein = new QAction(QIcon(QPixmap(":/icon_dreiBein.png")), tr("enable/disable dreiBein"), this);
|
---|
203 | connect(seldreiBein, SIGNAL(triggered()), this, SLOT(changeDreiBein()));
|
---|
204 | toolbar->addAction(seldreiBein);
|
---|
205 | QAction *seldomain = new QAction(QIcon(QPixmap(":/icon_domain.png")), tr("enable/disable domain box"), this);
|
---|
206 | connect(seldomain, SIGNAL(triggered()), this, SLOT(changeDomain()));
|
---|
207 | toolbar->addAction(seldomain);
|
---|
208 | }
|
---|
209 |
|
---|
210 | void GLWorldView::createDomainBox()
|
---|
211 | {
|
---|
212 | QSettings settings;
|
---|
213 | settings.beginGroup("WorldView");
|
---|
214 | QColor colorFrame = settings.value("domainBoxColorFrame", QColor(150,160,200,255)).value<QColor>();
|
---|
215 | QColor colorAmbient = settings.value("domainBoxColorAmbient", QColor(50,60,100,255)).value<QColor>();
|
---|
216 | QColor colorDiffuse = settings.value("domainBoxColorDiffuse", QColor(150,160,200,180)).value<QColor>();
|
---|
217 | settings.setValue("domainBoxColorFrame", colorFrame);
|
---|
218 | settings.setValue("domainBoxColorAmbient", colorAmbient);
|
---|
219 | settings.setValue("domainBoxColorDiffuse", colorDiffuse);
|
---|
220 | const bool status = settings.value("domainBoxEnabled").toBool();
|
---|
221 | settings.endGroup();
|
---|
222 |
|
---|
223 | domainBoxMaterial = new QGLMaterial;
|
---|
224 | domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
|
---|
225 | domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
|
---|
226 | domainBoxMaterial->setEmittedLight(colorFrame);
|
---|
227 |
|
---|
228 |
|
---|
229 | QGLMaterial *material = new QGLMaterial;
|
---|
230 | material->setAmbientColor(colorAmbient);
|
---|
231 | material->setDiffuseColor(colorDiffuse);
|
---|
232 |
|
---|
233 | QGLBuilder builder;
|
---|
234 | builder << QGL::Faceted;
|
---|
235 | builder << QGLCube(-1.0); // "inverted" => inside faces are used as front.
|
---|
236 | meshDomainBox = builder.finalizedSceneNode();
|
---|
237 | QMatrix4x4 mat;
|
---|
238 | mat.translate(0.5f, 0.5f, 0.5f);
|
---|
239 | meshDomainBox->setLocalTransform(mat);
|
---|
240 | meshDomainBox->setMaterial(material);
|
---|
241 |
|
---|
242 | setDomainStatus( status );
|
---|
243 | }
|
---|
244 |
|
---|
245 | void GLWorldView::createDreiBein()
|
---|
246 | {
|
---|
247 | QSettings settings;
|
---|
248 | settings.beginGroup("WorldView");
|
---|
249 | QColor colorX = settings.value("dreiBeinColorX", QColor(255,50,50,255)).value<QColor>();
|
---|
250 | QColor colorY = settings.value("dreiBeinColorY", QColor(50,255,50,255)).value<QColor>();
|
---|
251 | QColor colorZ = settings.value("dreiBeinColorZ", QColor(50,50,255,255)).value<QColor>();
|
---|
252 | settings.setValue("dreiBeinColorX", colorX);
|
---|
253 | settings.setValue("dreiBeinColorY", colorY);
|
---|
254 | settings.setValue("dreiBeinColorZ", colorZ);
|
---|
255 | const bool status = settings.value("dreiBeinEnabled").toBool();
|
---|
256 | settings.endGroup();
|
---|
257 |
|
---|
258 | // Create 3 color for the 3 axes.
|
---|
259 | dreiBeinMaterial[0] = new QGLMaterial;
|
---|
260 | dreiBeinMaterial[0]->setColor(colorX);
|
---|
261 | dreiBeinMaterial[1] = new QGLMaterial;
|
---|
262 | dreiBeinMaterial[1]->setColor(colorY);
|
---|
263 | dreiBeinMaterial[2] = new QGLMaterial;
|
---|
264 | dreiBeinMaterial[2]->setColor(colorZ);
|
---|
265 |
|
---|
266 | // Create the basic meshes (cylinder and cone).
|
---|
267 | QGLBuilder builderCyl;
|
---|
268 | builderCyl << QGLCylinder(.15,.15,1.6,16);
|
---|
269 | QGLSceneNode *cyl = builderCyl.finalizedSceneNode();
|
---|
270 |
|
---|
271 | QGLBuilder builderCone;
|
---|
272 | builderCone << QGLCylinder(0,.4,0.4,16);
|
---|
273 | QGLSceneNode *cone = builderCone.finalizedSceneNode();
|
---|
274 | {
|
---|
275 | QMatrix4x4 mat;
|
---|
276 | mat.translate(0.0f, 0.0f, 1.0f);
|
---|
277 | cone->setLocalTransform(mat);
|
---|
278 | }
|
---|
279 |
|
---|
280 | // Create a scene node from the 3 axes.
|
---|
281 | meshDreiBein = new QGLSceneNode(this);
|
---|
282 |
|
---|
283 | // X-direction
|
---|
284 | QGLSceneNode *node = new QGLSceneNode(meshDreiBein);
|
---|
285 | node->setMaterial(dreiBeinMaterial[0]);
|
---|
286 | node->addNode(cyl);
|
---|
287 | node->setPosition(QVector3D(.8f, 0.f, 0.f));
|
---|
288 | node->addNode(cone);
|
---|
289 | {
|
---|
290 | QMatrix4x4 mat;
|
---|
291 | mat.rotate(90, 0.0f, 1.0f, 0.0f);
|
---|
292 | node->setLocalTransform(mat);
|
---|
293 | }
|
---|
294 |
|
---|
295 | // Y-direction
|
---|
296 | node = new QGLSceneNode(meshDreiBein);
|
---|
297 | node->setMaterial(dreiBeinMaterial[1]);
|
---|
298 | node->addNode(cyl);
|
---|
299 | node->addNode(cone);
|
---|
300 | {
|
---|
301 | QMatrix4x4 mat;
|
---|
302 | mat.rotate(-90, 1.0f, 0.0f, 0.0f);
|
---|
303 | node->setLocalTransform(mat);
|
---|
304 | }
|
---|
305 | node->setPosition(QVector3D(0.f, .8f, 0.f));
|
---|
306 |
|
---|
307 | // Z-direction
|
---|
308 | node = new QGLSceneNode(meshDreiBein);
|
---|
309 | node->setMaterial(dreiBeinMaterial[2]);
|
---|
310 | node->addNode(cyl);
|
---|
311 | node->addNode(cone);
|
---|
312 | node->setPosition(QVector3D(0.f, 0.f, .8f));
|
---|
313 |
|
---|
314 | setdreiBeinStatus( status );
|
---|
315 | }
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Update operation which can be invoked by the observable (which should be the
|
---|
319 | * change tracker here).
|
---|
320 | */
|
---|
321 | void GLWorldView::update(Observable *publisher)
|
---|
322 | {
|
---|
323 | emit changed();
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * The observable can tell when it dies.
|
---|
328 | */
|
---|
329 | void GLWorldView::subjectKilled(Observable *publisher) {}
|
---|
330 |
|
---|
331 | /** Listen to specific changes to the world.
|
---|
332 | *
|
---|
333 | * @param publisher ref to observable.
|
---|
334 | * @param notification type of notification
|
---|
335 | */
|
---|
336 | void GLWorldView::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
337 | {
|
---|
338 | if (static_cast<World *>(publisher) == World::getPointer()) {
|
---|
339 | switch (notification->getChannelNo()) {
|
---|
340 | case World::SelectionChanged:
|
---|
341 | {
|
---|
342 | #ifdef LOG_OBSERVER
|
---|
343 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that selection has changed.";
|
---|
344 | #endif
|
---|
345 | emit worldSelectionChanged();
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | case World::MoleculeInserted:
|
---|
349 | {
|
---|
350 | const moleculeId_t _id = World::getInstance().lastChanged<molecule>()->getId();
|
---|
351 | #ifdef LOG_OBSERVER
|
---|
352 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been removed.";
|
---|
353 | #endif
|
---|
354 | emit moleculeInserted(_id);
|
---|
355 | break;
|
---|
356 | }
|
---|
357 | case World::MoleculeRemoved:
|
---|
358 | {
|
---|
359 | const moleculeId_t _id = World::getInstance().lastChanged<molecule>()->getId();
|
---|
360 | #ifdef LOG_OBSERVER
|
---|
361 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been removed.";
|
---|
362 | #endif
|
---|
363 | emit moleculeRemoved(_id);
|
---|
364 | break;
|
---|
365 | }
|
---|
366 | default:
|
---|
367 | ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for World.");
|
---|
368 | break;
|
---|
369 | }
|
---|
370 | } else if (static_cast<WorldTime *>(publisher) == WorldTime::getPointer()) {
|
---|
371 | switch (notification->getChannelNo()) {
|
---|
372 | case WorldTime::TimeChanged:
|
---|
373 | {
|
---|
374 | #ifdef LOG_OBSERVER
|
---|
375 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that WorldTime's time has changed.";
|
---|
376 | #endif
|
---|
377 | emit changed();
|
---|
378 | emit TimeChanged();
|
---|
379 | break;
|
---|
380 | }
|
---|
381 | default:
|
---|
382 | ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for WorldTime.");
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | } else if (dynamic_cast<AtomObservable *>(publisher) != NULL) {
|
---|
386 | switch (notification->getChannelNo()) {
|
---|
387 | case AtomObservable::PositionChanged:
|
---|
388 | {
|
---|
389 | #ifdef LOG_OBSERVER
|
---|
390 | const atom *_atom = dynamic_cast<const atom *>(publisher);
|
---|
391 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_atom->getId())+" has changed its position.";
|
---|
392 | #endif
|
---|
393 | emit changed();
|
---|
394 | break;
|
---|
395 | }
|
---|
396 | default:
|
---|
397 | ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for AtomObservable.");
|
---|
398 | break;
|
---|
399 | }
|
---|
400 | } else if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
|
---|
401 | switch (notification->getChannelNo()) {
|
---|
402 | case ShapeRegistry::ShapeInserted:
|
---|
403 | {
|
---|
404 | emit ShapeAdded();
|
---|
405 | break;
|
---|
406 | }
|
---|
407 | case ShapeRegistry::ShapeRemoved:
|
---|
408 | {
|
---|
409 | emit ShapeRemoved();
|
---|
410 | break;
|
---|
411 | }
|
---|
412 | case ShapeRegistry::SelectionChanged:
|
---|
413 | {
|
---|
414 | worldscene->updateSelectedShapes();
|
---|
415 | break;
|
---|
416 | }
|
---|
417 | default:
|
---|
418 | ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for ShapeRegistry.");
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | } else
|
---|
422 | ASSERT(0, "GLWorldView::recieveNotification() - received notification from unknown source.");
|
---|
423 | }
|
---|
424 |
|
---|
425 | void GLWorldView::checkChanges()
|
---|
426 | {
|
---|
427 | updateGL();
|
---|
428 | needsRedraw = false;
|
---|
429 | }
|
---|
430 |
|
---|
431 | void GLWorldView::changeDreiBein()
|
---|
432 | {
|
---|
433 | // invert to new status
|
---|
434 | const bool status = ((meshDreiBein->options() & QGLSceneNode::HideNode) == 0);
|
---|
435 | setdreiBeinStatus(!status);
|
---|
436 | // realize
|
---|
437 | updateGL();
|
---|
438 | needsRedraw = true;
|
---|
439 | }
|
---|
440 |
|
---|
441 | void GLWorldView::setdreiBeinStatus(const bool status)
|
---|
442 | {
|
---|
443 | if (status)
|
---|
444 | meshDreiBein->setOptions( meshDreiBein->options() & (255-QGLSceneNode::HideNode) );
|
---|
445 | else
|
---|
446 | meshDreiBein->setOptions( meshDreiBein->options() | QGLSceneNode::HideNode );
|
---|
447 | }
|
---|
448 |
|
---|
449 | void GLWorldView::changeDomain()
|
---|
450 | {
|
---|
451 | // invert to new status
|
---|
452 | const bool status = ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0);
|
---|
453 | setDomainStatus(!status);
|
---|
454 | // realize
|
---|
455 | updateGL();
|
---|
456 | needsRedraw = true;
|
---|
457 | }
|
---|
458 |
|
---|
459 | void GLWorldView::setDomainStatus(const bool status)
|
---|
460 | {
|
---|
461 | if (status)
|
---|
462 | meshDomainBox->setOptions( meshDomainBox->options() & (255-QGLSceneNode::HideNode) );
|
---|
463 | else
|
---|
464 | meshDomainBox->setOptions( meshDomainBox->options() | QGLSceneNode::HideNode );
|
---|
465 | }
|
---|
466 |
|
---|
467 | void GLWorldView::sceneChangeSignalled()
|
---|
468 | {
|
---|
469 | if (!needsRedraw){
|
---|
470 | redrawTimer->singleShot(0, this, SLOT(checkChanges()));
|
---|
471 | needsRedraw = true;
|
---|
472 | redrawTimer->start();
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | void GLWorldView::initializeGL(QGLPainter *painter)
|
---|
477 | {
|
---|
478 | worldscene->initialize(this, painter);
|
---|
479 | changesPresent = false;
|
---|
480 | }
|
---|
481 |
|
---|
482 | void GLWorldView::paintGL(QGLPainter *painter)
|
---|
483 | {
|
---|
484 | if (changesPresent) {
|
---|
485 | initializeGL(painter);
|
---|
486 | changesPresent = false;
|
---|
487 | }
|
---|
488 |
|
---|
489 | QVector3D cameraDir = camera()->center() - camera()->eye();
|
---|
490 | cameraDir.normalize();
|
---|
491 | QVector4D cameraPlane(cameraDir, QVector3D::dotProduct(cameraDir, camera()->eye()));
|
---|
492 | worldscene->draw(painter, cameraPlane);
|
---|
493 |
|
---|
494 | drawDreiBein(painter);
|
---|
495 |
|
---|
496 | // Domain box has to be last because of its transparency.
|
---|
497 | drawDomainBox(painter);
|
---|
498 | }
|
---|
499 |
|
---|
500 | void GLWorldView::keyPressEvent(QKeyEvent *e)
|
---|
501 | {
|
---|
502 | // Find the distance between the eye and focus point.
|
---|
503 | QVector3D d = camera()->eye() - camera()->center();
|
---|
504 | // LOG(1, "Distance vector eye and center is "
|
---|
505 | // << d.x() << "," << d.y() << "," << d.z());
|
---|
506 | // scale the move unit by the eye <-> domain center distance
|
---|
507 | const double key_move_unit = 0.04*(d.length()/50.);
|
---|
508 |
|
---|
509 | if (e->key() == Qt::Key_Tab) {
|
---|
510 | // The Tab key turns the ShowPicking option on and off,
|
---|
511 | // which helps show what the pick buffer looks like.
|
---|
512 | setOption(QGLView::ShowPicking, ((options() & QGLView::ShowPicking) == 0));
|
---|
513 | updateGL();
|
---|
514 | } else if ((e->key() == Qt::Key_Minus) || (e->key() == Qt::Key_Plus)) {
|
---|
515 | // Scale the distance.
|
---|
516 | if (e->key() == Qt::Key_Minus)
|
---|
517 | d *= 1.2;
|
---|
518 | else if (e->key() == Qt::Key_Plus)
|
---|
519 | d /= 1.2;
|
---|
520 | // Set new eye position.
|
---|
521 | camera()->setEye(camera()->center() + d);
|
---|
522 | } else if ((e->key() == Qt::Key_Left) || (e->key() == Qt::Key_Right)) {
|
---|
523 | // Translate the camera.
|
---|
524 | const double d = (e->key() == Qt::Key_Left) ? -key_move_unit : key_move_unit;
|
---|
525 | camera()->translateCenter( d, 0., 0);
|
---|
526 | camera()->translateEye( d, 0., 0);
|
---|
527 | } else if ((e->key() == Qt::Key_Up) || (e->key() == Qt::Key_Down)) {
|
---|
528 | // Translate the camera.
|
---|
529 | const double d = (e->key() == Qt::Key_Up) ? -key_move_unit : key_move_unit;
|
---|
530 | camera()->translateCenter( 0., d, 0);
|
---|
531 | camera()->translateEye( 0., d, 0);
|
---|
532 | } else if ((e->key() == Qt::Key_PageUp) || (e->key() == Qt::Key_PageDown)) {
|
---|
533 | // Translate the camera.
|
---|
534 | const double d = (e->key() == Qt::Key_PageUp) ? -key_move_unit : key_move_unit;
|
---|
535 | camera()->translateCenter( 0., 0., d);
|
---|
536 | camera()->translateEye( 0., 0., d);
|
---|
537 | }
|
---|
538 | QGLView::keyPressEvent(e);
|
---|
539 | }
|
---|
540 |
|
---|
541 | void GLWorldView::changeSignalled()
|
---|
542 | {
|
---|
543 | changesPresent = true;
|
---|
544 | }
|
---|
545 |
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Set the current camera control mode.
|
---|
549 | */
|
---|
550 | void GLWorldView::setCameraControlMode(GLWorldView::CameraControlModeType mode)
|
---|
551 | {
|
---|
552 | cameraControlMode = mode;
|
---|
553 | }
|
---|
554 |
|
---|
555 | void GLWorldView::setCameraControlModeRotation()
|
---|
556 | {
|
---|
557 | setCameraControlMode(Rotate);
|
---|
558 | }
|
---|
559 |
|
---|
560 | void GLWorldView::setCameraControlModeTranslation()
|
---|
561 | {
|
---|
562 | setCameraControlMode(Translate);
|
---|
563 | }
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Returns the current camera control mode.
|
---|
567 | * This needs to be invertable (rotation - translation), if the shift key is pressed.
|
---|
568 | */
|
---|
569 | GLWorldView::CameraControlModeType GLWorldView::getCameraControlMode(bool inverted)
|
---|
570 | {
|
---|
571 | if (inverted){
|
---|
572 | if (cameraControlMode == Rotate)
|
---|
573 | return Translate;
|
---|
574 | if (cameraControlMode == Translate)
|
---|
575 | return Rotate;
|
---|
576 | return Rotate;
|
---|
577 | }else
|
---|
578 | return cameraControlMode;
|
---|
579 | }
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Set the camera so it can oversee the whole domain.
|
---|
583 | */
|
---|
584 | void GLWorldView::fitCameraToDomain()
|
---|
585 | {
|
---|
586 | // Move the camera focus point to the center of the domain box.
|
---|
587 | Vector v = World::getInstance().getDomain().translateIn(Vector(0.5, 0.5, 0.5));
|
---|
588 | camera()->setCenter(QVector3D(v[0], v[1], v[2]));
|
---|
589 |
|
---|
590 | // Guess some eye distance.
|
---|
591 | double dist = v.Norm() * 3;
|
---|
592 | camera()->setEye(QVector3D(v[0], v[1], v[2] + dist));
|
---|
593 | camera()->setUpVector(QVector3D(0, 1, 0));
|
---|
594 | }
|
---|
595 |
|
---|
596 | void GLWorldView::setCameraStereoModeDisable()
|
---|
597 | {
|
---|
598 | setStereoType(QGLView::Hardware);
|
---|
599 | camera()->setEyeSeparation(0.0);
|
---|
600 | updateGL();
|
---|
601 | }
|
---|
602 |
|
---|
603 | void GLWorldView::setCameraStereoModeHardware()
|
---|
604 | {
|
---|
605 | setStereoType(QGLView::Hardware);
|
---|
606 | camera()->setEyeSeparation(defaultEyeSeparation);
|
---|
607 | updateGL();
|
---|
608 | }
|
---|
609 |
|
---|
610 | void GLWorldView::setCameraStereoModeLeftRight()
|
---|
611 | {
|
---|
612 | setStereoType(QGLView::LeftRight);
|
---|
613 | camera()->setEyeSeparation(defaultEyeSeparation);
|
---|
614 | updateGL();
|
---|
615 | }
|
---|
616 |
|
---|
617 | void GLWorldView::setCameraStereoModeRightLeft()
|
---|
618 | {
|
---|
619 | setStereoType(QGLView::RightLeft);
|
---|
620 | camera()->setEyeSeparation(defaultEyeSeparation);
|
---|
621 | updateGL();
|
---|
622 | }
|
---|
623 |
|
---|
624 | void GLWorldView::setCameraStereoModeTopBottom()
|
---|
625 | {
|
---|
626 | setStereoType(QGLView::TopBottom);
|
---|
627 | camera()->setEyeSeparation(defaultEyeSeparation);
|
---|
628 | updateGL();
|
---|
629 | }
|
---|
630 |
|
---|
631 | void GLWorldView::setCameraStereoModeBottomTop()
|
---|
632 | {
|
---|
633 | setStereoType(QGLView::BottomTop);
|
---|
634 | camera()->setEyeSeparation(defaultEyeSeparation);
|
---|
635 | updateGL();
|
---|
636 | }
|
---|
637 |
|
---|
638 | void GLWorldView::setCameraStereoModeAnaglyph()
|
---|
639 | {
|
---|
640 | setStereoType(QGLView::RedCyanAnaglyph);
|
---|
641 | camera()->setEyeSeparation(defaultEyeSeparation);
|
---|
642 | updateGL();
|
---|
643 | }
|
---|
644 |
|
---|
645 | void GLWorldView::mousePressEvent(QMouseEvent *event)
|
---|
646 | {
|
---|
647 | QGLView::mousePressEvent(event);
|
---|
648 |
|
---|
649 | // Reset the saved mouse position.
|
---|
650 | lastMousePos = event->posF();
|
---|
651 | }
|
---|
652 |
|
---|
653 | /**
|
---|
654 | * Handle a mouse move event.
|
---|
655 | * This is used to control the camera (rotation and translation) when the left button is being pressed.
|
---|
656 | */
|
---|
657 | void GLWorldView::mouseMoveEvent(QMouseEvent *event)
|
---|
658 | {
|
---|
659 | if (event->buttons() & Qt::LeftButton){
|
---|
660 | // Find the mouse distance since the last event.
|
---|
661 | QPointF d = event->posF() - lastMousePos;
|
---|
662 | lastMousePos = event->posF();
|
---|
663 |
|
---|
664 | // Rotate or translate? (inverted by shift key)
|
---|
665 | CameraControlModeType mode = getCameraControlMode(event->modifiers() & Qt::ShiftModifier);
|
---|
666 |
|
---|
667 | if (mode == Rotate){
|
---|
668 | // Rotate the camera.
|
---|
669 | d *= 0.3;
|
---|
670 | camera()->tiltPanRollCenter(- d.y(), - d.x(), 0);
|
---|
671 | }else if (mode == Translate){
|
---|
672 | // Translate the camera.
|
---|
673 | d *= 0.02;
|
---|
674 | camera()->translateCenter(- d.x(), d.y(), 0);
|
---|
675 | camera()->translateEye(- d.x(), d.y(), 0);
|
---|
676 | }
|
---|
677 | }else{
|
---|
678 | // Without this Qt would not test for hover events (i.e. mouse over an atom).
|
---|
679 | QGLView::mouseMoveEvent(event);
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * When the mouse wheel is used, zoom in or out.
|
---|
685 | */
|
---|
686 | void GLWorldView::wheelEvent(QWheelEvent *event)
|
---|
687 | {
|
---|
688 | // Find the distance between the eye and focus point.
|
---|
689 | QVector3D d = camera()->eye() - camera()->center();
|
---|
690 |
|
---|
691 | // Scale the distance.
|
---|
692 | if (event->delta() < 0)
|
---|
693 | d *= 1.2;
|
---|
694 | else if (event->delta() > 0)
|
---|
695 | d /= 1.2;
|
---|
696 |
|
---|
697 | // Set new eye position.
|
---|
698 | camera()->setEye(camera()->center() + d);
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * Draw a transparent cube representing the domain.
|
---|
703 | */
|
---|
704 | void GLWorldView::drawDomainBox(QGLPainter *painter) const
|
---|
705 | {
|
---|
706 | // Apply the domain matrix.
|
---|
707 | RealSpaceMatrix m = World::getInstance().getDomain().getM();
|
---|
708 | painter->modelViewMatrix().push();
|
---|
709 | painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
|
---|
710 | m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
|
---|
711 | m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
|
---|
712 | 0.0, 0.0, 0.0, 1.0);
|
---|
713 |
|
---|
714 | // Draw the transparent cube.
|
---|
715 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
716 | glCullFace(GL_BACK);
|
---|
717 | glEnable(GL_CULL_FACE);
|
---|
718 | glEnable(GL_BLEND);
|
---|
719 | glDepthMask(0);
|
---|
720 | //glDisable(GL_DEPTH_TEST);
|
---|
721 | meshDomainBox->draw(painter);
|
---|
722 | //glEnable(GL_DEPTH_TEST);
|
---|
723 | glDepthMask(1);
|
---|
724 | glDisable(GL_BLEND);
|
---|
725 | glDisable(GL_CULL_FACE);
|
---|
726 |
|
---|
727 | // Draw the outlines (if we have drawn the box itself)
|
---|
728 | if ((meshDomainBox->options() & QGLSceneNode::HideNode) == 0) {
|
---|
729 | painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
|
---|
730 | //glEnable(GL_LINE_SMOOTH);
|
---|
731 | QVector3DArray array;
|
---|
732 | array.append(0, 0, 0); array.append(1, 0, 0);
|
---|
733 | array.append(1, 0, 0); array.append(1, 1, 0);
|
---|
734 | array.append(1, 1, 0); array.append(0, 1, 0);
|
---|
735 | array.append(0, 1, 0); array.append(0, 0, 0);
|
---|
736 |
|
---|
737 | array.append(0, 0, 1); array.append(1, 0, 1);
|
---|
738 | array.append(1, 0, 1); array.append(1, 1, 1);
|
---|
739 | array.append(1, 1, 1); array.append(0, 1, 1);
|
---|
740 | array.append(0, 1, 1); array.append(0, 0, 1);
|
---|
741 |
|
---|
742 | array.append(0, 0, 0); array.append(0, 0, 1);
|
---|
743 | array.append(1, 0, 0); array.append(1, 0, 1);
|
---|
744 | array.append(0, 1, 0); array.append(0, 1, 1);
|
---|
745 | array.append(1, 1, 0); array.append(1, 1, 1);
|
---|
746 | painter->clearAttributes();
|
---|
747 | painter->setVertexAttribute(QGL::Position, array);
|
---|
748 | painter->draw(QGL::Lines, 24);
|
---|
749 | }
|
---|
750 |
|
---|
751 | painter->modelViewMatrix().pop();
|
---|
752 | }
|
---|
753 |
|
---|
754 | void GLWorldView::drawDreiBein(QGLPainter *painter)
|
---|
755 | {
|
---|
756 | painter->modelViewMatrix().push();
|
---|
757 | painter->modelViewMatrix().translate(camera()->center());
|
---|
758 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
759 | painter->setFaceMaterial(QGL::FrontFaces, NULL);
|
---|
760 | meshDreiBein->draw(painter);
|
---|
761 | painter->modelViewMatrix().pop();
|
---|
762 | }
|
---|
763 |
|
---|
764 | void GLWorldView::sceneHoverSignalled(const atom &_atom)
|
---|
765 | {
|
---|
766 | emit hoverChanged(_atom);
|
---|
767 | }
|
---|
768 |
|
---|
769 | void GLWorldView::sceneHoverSignalled(const molecule &_mol, int _i)
|
---|
770 | {
|
---|
771 | emit hoverChanged(_mol, _i);
|
---|
772 | }
|
---|