source: src/UIElements/Views/Qt4/Qt3D/GLWorldView.cpp@ 795c0f

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 795c0f was 704d59, checked in by Frederik Heber <heber@…>, 10 years ago

Changed hoverChanged() from mol* to moleculeId_t.

  • also changed signatures for atom.
  • Property mode set to 100644
File size: 26.1 KB
RevLine 
[d238e7]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]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/>.
[d238e7]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>
[0e5d14]39#include <Qt/qaction.h>
[8e7dd9]40#include <QtGui/QMenu>
[0e5d14]41#include <QtGui/QToolBar>
[8e7dd9]42#include <QtGui/QToolButton>
[585f78]43#include <Qt/qtimer.h>
[c7e000]44#include <Qt/qsettings.h>
[26ed25]45#include <Qt3D/qglbuilder.h>
46#include <Qt3D/qglscenenode.h>
47#include <Qt3D/qglsphere.h>
48#include <Qt3D/qglcylinder.h>
[02b2d3]49#include <Qt3D/qglcube.h>
[d238e7]50
[907636]51#include "GLWorldScene.hpp"
[d238e7]52
53#include "CodePatterns/MemDebug.hpp"
54
[53059e]55#include "Atom/AtomObserver.hpp"
56#include "Atom/atom_observable.hpp"
[1d02c2d]57#include "Box.hpp"
[7188b1]58#include "CodePatterns/Log.hpp"
[02ce36]59#include "CodePatterns/Observer/Notification.hpp"
[856d05]60#include "CodePatterns/Observer/ObserverLog.hpp"
[1d02c2d]61#include "molecule.hpp"
[284551]62#include "Shapes/ShapeRegistry.hpp"
[7188b1]63#include "World.hpp"
[89b992]64#include "WorldTime.hpp"
[7188b1]65
[d238e7]66GLWorldView::GLWorldView(QWidget *parent)
[585f78]67 : QGLView(parent), Observer("GLWorldView"), worldscene(NULL), changesPresent(false), needsRedraw(false)
[d238e7]68{
[65487f]69 worldscene = new GLWorldScene(this);
[d238e7]70
[65487f]71 setOption(QGLView::ObjectPicking, true);
[8880c9]72 setOption(QGLView::CameraNavigation, false);
[b6d92e]73 setFocusPolicy(Qt::StrongFocus);
[8880c9]74 setCameraControlMode(Rotate);
[8e7dd9]75 defaultEyeSeparation = 4.0;
[d238e7]76
[26ed25]77 createDomainBox();
78 createDreiBein();
[e8c636]79 //changeMaterials(false);
80
[8c001a]81 qRegisterMetaType<atomId_t>("atomId_t");
82 qRegisterMetaType<moleculeId_t>("moleculeId_t");
[c48ac12]83
[8481f5]84 connect(this, SIGNAL(ShapeAdded()), worldscene, SLOT(addShape()));
85 connect(this, SIGNAL(ShapeRemoved()), worldscene, SLOT(removeShape()));
[ce4126]86 connect(this, SIGNAL(TimeChanged()), worldscene, SIGNAL(updated()));
[65487f]87 connect(worldscene, SIGNAL(changeOccured()), this, SLOT(changeSignalled()));
88 connect(worldscene, SIGNAL(changed()), this, SIGNAL(changed()));
[704d59]89 connect(worldscene, SIGNAL(hoverChanged(const atomId_t)), this, SLOT(sceneHoverSignalled(const atomId_t)));
90 connect(worldscene, SIGNAL(hoverChanged(const moleculeId_t, int)), this, SLOT(sceneHoverSignalled(const moleculeId_t, int)));
[3927ef]91 connect(this, SIGNAL(worldSelectionChanged()), worldscene, SLOT(worldSelectionChanged()));
[8c001a]92 connect(this, SIGNAL(moleculeRemoved(const moleculeId_t)), worldscene, SLOT(moleculeRemoved(const moleculeId_t)));
[ef3013]93 connect(this, SIGNAL(moleculeInserted(const moleculeId_t)), worldscene, SLOT(moleculeInserted(const moleculeId_t)));
[585f78]94 //connect(this, SIGNAL(changed()), this, SLOT(updateGL()));
95 connect(this, SIGNAL(changed()), this, SLOT(sceneChangeSignalled()));
[739ee9]96 connect(this, SIGNAL(moleculesVisibilityChanged(const moleculeId_t,bool)), worldscene, SLOT(moleculesVisibilityChanged(const moleculeId_t,bool)));
[7188b1]97
[65487f]98 // sign on to changes in the world
99 World::getInstance().signOn(this);
[53059e]100 World::getInstance().signOn(this, World::MoleculeInserted);
[c67518]101 World::getInstance().signOn(this, World::MoleculeRemoved);
[3927ef]102 World::getInstance().signOn(this, World::SelectionChanged);
[89b992]103 WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
[53059e]104 AtomObserver::getInstance().signOn(this, AtomObservable::PositionChanged);
[585f78]105
[284551]106 ShapeRegistry::getInstance().signOn(this);
[85c36d]107 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted);
[284551]108 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved);
109 ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged);
110
[585f78]111 redrawTimer = new QTimer(this);
[d238e7]112}
113
[04f017]114GLWorldView::~GLWorldView()
115{
[592d42]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
[9c18e4]123 World::getInstance().signOff(this);
[02ce36]124 World::getInstance().signOff(this, World::AtomInserted);
125 World::getInstance().signOff(this, World::AtomRemoved);
[53059e]126 World::getInstance().signOff(this, World::MoleculeInserted);
[c67518]127 World::getInstance().signOff(this, World::MoleculeRemoved);
[3927ef]128 World::getInstance().signOff(this, World::SelectionChanged);
[89b992]129 WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
[53059e]130 AtomObserver::getInstance().signOff(this, AtomObservable::PositionChanged);
[284551]131 ShapeRegistry::getInstance().signOff(this);
[85c36d]132 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeInserted);
[284551]133 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeRemoved);
134 ShapeRegistry::getInstance().signOff(this, ShapeRegistry::SelectionChanged);
[04f017]135 delete worldscene;
[e8c636]136
137 delete(domainBoxMaterial);
138 for (int i=0;i<3;i++)
139 delete(dreiBeinMaterial[i]);
[04f017]140}
141
[0e5d14]142
143/**
144 * Add some widget specific actions to the toolbar:
145 * - camera rotation/translation mode
146 * - camera fit to domain
147 */
148void GLWorldView::addToolBarActions(QToolBar *toolbar)
149{
[f31b34]150 // camera control mode
[0e5d14]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);
[8e7dd9]161
[f31b34]162 // stereo mode
[8e7dd9]163 QToolButton *stereoButton = new QToolButton(toolbar);
164 QMenu *stereoMenu = new QMenu();
[a8fc70]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);
[8e7dd9]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);
[5b991a]187 stereoButton->setIcon(QIcon(QPixmap(":/icon_view_stereo.png")));
[8e7dd9]188 stereoButton->setPopupMode(QToolButton::InstantPopup);
189 toolbar->addWidget(stereoButton);
[f31b34]190
191 // selection mode
192 toolbar->addSeparator();
[5b991a]193 QAction *selAtomAction = new QAction(QIcon(QPixmap(":/icon_select_atom.png")), tr("select atom by clicking"), this);
[f31b34]194 connect(selAtomAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeAtom()));
195 toolbar->addAction(selAtomAction);
[5b991a]196 QAction *selMolAction = new QAction(QIcon(QPixmap(":/icon_select_molecule.png")), tr("select molecule by clicking"), this);
[f31b34]197 connect(selMolAction, SIGNAL(triggered()), worldscene, SLOT(setSelectionModeMolecule()));
198 toolbar->addAction(selMolAction);
[592d42]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);
[0e5d14]208}
209
[26ed25]210void GLWorldView::createDomainBox()
211{
[c7e000]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);
[592d42]220 const bool status = settings.value("domainBoxEnabled").toBool();
[c7e000]221 settings.endGroup();
222
[02b2d3]223 domainBoxMaterial = new QGLMaterial;
224 domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
225 domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
[c7e000]226 domainBoxMaterial->setEmittedLight(colorFrame);
[02b2d3]227
[26ed25]228
[02b2d3]229 QGLMaterial *material = new QGLMaterial;
[c7e000]230 material->setAmbientColor(colorAmbient);
231 material->setDiffuseColor(colorDiffuse);
[02b2d3]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);
[592d42]241
242 setDomainStatus( status );
[26ed25]243}
244
245void GLWorldView::createDreiBein()
246{
[c7e000]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);
[592d42]255 const bool status = settings.value("dreiBeinEnabled").toBool();
[c7e000]256 settings.endGroup();
257
[02b2d3]258 // Create 3 color for the 3 axes.
[26ed25]259 dreiBeinMaterial[0] = new QGLMaterial;
[c7e000]260 dreiBeinMaterial[0]->setColor(colorX);
[26ed25]261 dreiBeinMaterial[1] = new QGLMaterial;
[c7e000]262 dreiBeinMaterial[1]->setColor(colorY);
[26ed25]263 dreiBeinMaterial[2] = new QGLMaterial;
[c7e000]264 dreiBeinMaterial[2]->setColor(colorZ);
[26ed25]265
[02b2d3]266 // Create the basic meshes (cylinder and cone).
[26ed25]267 QGLBuilder builderCyl;
[37e910]268 builderCyl << QGLCylinder(.15,.15,1.6,16);
[26ed25]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
[02b2d3]280 // Create a scene node from the 3 axes.
[26ed25]281 meshDreiBein = new QGLSceneNode(this);
282
283 // X-direction
284 QGLSceneNode *node = new QGLSceneNode(meshDreiBein);
285 node->setMaterial(dreiBeinMaterial[0]);
286 node->addNode(cyl);
[37e910]287 node->setPosition(QVector3D(.8f, 0.f, 0.f));
[26ed25]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 }
[37e910]305 node->setPosition(QVector3D(0.f, .8f, 0.f));
[26ed25]306
307 // Z-direction
308 node = new QGLSceneNode(meshDreiBein);
309 node->setMaterial(dreiBeinMaterial[2]);
310 node->addNode(cyl);
311 node->addNode(cone);
[37e910]312 node->setPosition(QVector3D(0.f, 0.f, .8f));
[592d42]313
314 setdreiBeinStatus( status );
[26ed25]315}
316
[7188b1]317/**
318 * Update operation which can be invoked by the observable (which should be the
319 * change tracker here).
320 */
321void GLWorldView::update(Observable *publisher)
322{
323 emit changed();
324}
325
326/**
327 * The observable can tell when it dies.
328 */
329void 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 */
336void GLWorldView::recieveNotification(Observable *publisher, Notification_ptr notification)
337{
[53059e]338 if (static_cast<World *>(publisher) == World::getPointer()) {
339 switch (notification->getChannelNo()) {
340 case World::SelectionChanged:
341 {
342 #ifdef LOG_OBSERVER
[708277]343 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that selection has changed.";
[53059e]344 #endif
345 emit worldSelectionChanged();
346 break;
347 }
348 case World::MoleculeInserted:
349 {
[ef3013]350 const moleculeId_t _id = World::getInstance().lastChanged<molecule>()->getId();
[53059e]351 #ifdef LOG_OBSERVER
[eac542]352 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been inserted.";
[53059e]353 #endif
[ef3013]354 emit moleculeInserted(_id);
[53059e]355 break;
356 }
357 case World::MoleculeRemoved:
358 {
[8c001a]359 const moleculeId_t _id = World::getInstance().lastChanged<molecule>()->getId();
[53059e]360 #ifdef LOG_OBSERVER
[8c001a]361 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been removed.";
[53059e]362 #endif
[8c001a]363 emit moleculeRemoved(_id);
[53059e]364 break;
365 }
366 default:
[89b992]367 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for World.");
[53059e]368 break;
[7188b1]369 }
[89b992]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();
[ce4126]378 emit TimeChanged();
[89b992]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) {
[53059e]386 switch (notification->getChannelNo()) {
387 case AtomObservable::PositionChanged:
388 {
389 #ifdef LOG_OBSERVER
[89b992]390 const atom *_atom = dynamic_cast<const atom *>(publisher);
[708277]391 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_atom->getId())+" has changed its position.";
[53059e]392 #endif
393 emit changed();
394 break;
395 }
396 default:
[89b992]397 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for AtomObservable.");
[53059e]398 break;
[7188b1]399 }
[284551]400 } else if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
401 switch (notification->getChannelNo()) {
[85c36d]402 case ShapeRegistry::ShapeInserted:
403 {
[4d6662]404 emit ShapeAdded();
[85c36d]405 break;
406 }
[284551]407 case ShapeRegistry::ShapeRemoved:
408 {
[4d6662]409 emit ShapeRemoved();
[284551]410 break;
411 }
412 case ShapeRegistry::SelectionChanged:
413 {
[85c36d]414 worldscene->updateSelectedShapes();
[284551]415 break;
416 }
417 default:
[89b992]418 ASSERT(0, "GLWorldView::recieveNotification() - we cannot get here for ShapeRegistry.");
[284551]419 break;
420 }
[53059e]421 } else
422 ASSERT(0, "GLWorldView::recieveNotification() - received notification from unknown source.");
[7188b1]423}
[04f017]424
[585f78]425void GLWorldView::checkChanges()
426{
427 updateGL();
428 needsRedraw = false;
429}
430
[592d42]431void 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
441void 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
449void 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
459void 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
[585f78]467void GLWorldView::sceneChangeSignalled()
468{
469 if (!needsRedraw){
470 redrawTimer->singleShot(0, this, SLOT(checkChanges()));
471 needsRedraw = true;
472 redrawTimer->start();
473 }
474}
475
[d238e7]476void GLWorldView::initializeGL(QGLPainter *painter)
477{
[65487f]478 worldscene->initialize(this, painter);
479 changesPresent = false;
[d238e7]480}
481
482void GLWorldView::paintGL(QGLPainter *painter)
483{
[65487f]484 if (changesPresent) {
485 initializeGL(painter);
486 changesPresent = false;
487 }
[72a4c1]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);
[e8c636]493
494 drawDreiBein(painter);
[02b2d3]495
496 // Domain box has to be last because of its transparency.
497 drawDomainBox(painter);
[907636]498}
[d238e7]499
[907636]500void GLWorldView::keyPressEvent(QKeyEvent *e)
501{
[b6d92e]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
[65487f]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();
[b6d92e]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);
[65487f]537 }
538 QGLView::keyPressEvent(e);
539}
540
541void GLWorldView::changeSignalled()
542{
543 changesPresent = true;
[d238e7]544}
545
546
[0e5d14]547/**
548 * Set the current camera control mode.
549 */
[e13b34]550void GLWorldView::setCameraControlMode(GLWorldView::CameraControlModeType mode)
551{
552 cameraControlMode = mode;
553}
554
555void GLWorldView::setCameraControlModeRotation()
556{
557 setCameraControlMode(Rotate);
558}
559
560void GLWorldView::setCameraControlModeTranslation()
561{
562 setCameraControlMode(Translate);
563}
564
[0e5d14]565/**
566 * Returns the current camera control mode.
567 * This needs to be invertable (rotation - translation), if the shift key is pressed.
568 */
[e13b34]569GLWorldView::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
[8880c9]581/**
582 * Set the camera so it can oversee the whole domain.
583 */
[e13b34]584void GLWorldView::fitCameraToDomain()
585{
[8880c9]586 // Move the camera focus point to the center of the domain box.
[3f48c2]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
[8880c9]590 // Guess some eye distance.
[3f48c2]591 double dist = v.Norm() * 3;
592 camera()->setEye(QVector3D(v[0], v[1], v[2] + dist));
593 camera()->setUpVector(QVector3D(0, 1, 0));
[e13b34]594}
595
[8e7dd9]596void GLWorldView::setCameraStereoModeDisable()
597{
598 setStereoType(QGLView::Hardware);
599 camera()->setEyeSeparation(0.0);
600 updateGL();
601}
602
603void GLWorldView::setCameraStereoModeHardware()
604{
605 setStereoType(QGLView::Hardware);
606 camera()->setEyeSeparation(defaultEyeSeparation);
607 updateGL();
608}
609
610void GLWorldView::setCameraStereoModeLeftRight()
611{
612 setStereoType(QGLView::LeftRight);
613 camera()->setEyeSeparation(defaultEyeSeparation);
614 updateGL();
615}
616
617void GLWorldView::setCameraStereoModeRightLeft()
618{
619 setStereoType(QGLView::RightLeft);
620 camera()->setEyeSeparation(defaultEyeSeparation);
621 updateGL();
622}
623
624void GLWorldView::setCameraStereoModeTopBottom()
625{
626 setStereoType(QGLView::TopBottom);
627 camera()->setEyeSeparation(defaultEyeSeparation);
628 updateGL();
629}
630
631void GLWorldView::setCameraStereoModeBottomTop()
632{
633 setStereoType(QGLView::BottomTop);
634 camera()->setEyeSeparation(defaultEyeSeparation);
635 updateGL();
636}
637
638void GLWorldView::setCameraStereoModeAnaglyph()
639{
640 setStereoType(QGLView::RedCyanAnaglyph);
641 camera()->setEyeSeparation(defaultEyeSeparation);
642 updateGL();
643}
644
[8880c9]645void 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 */
657void 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 */
686void 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
[02b2d3]701/**
702 * Draw a transparent cube representing the domain.
703 */
[e8c636]704void GLWorldView::drawDomainBox(QGLPainter *painter) const
705{
[02b2d3]706 // Apply the domain matrix.
[e8c636]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
[02b2d3]714 // Draw the transparent cube.
[073a2ff]715 painter->setStandardEffect(QGL::LitMaterial);
[02b2d3]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
[592d42]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 }
[02b2d3]750
[e8c636]751 painter->modelViewMatrix().pop();
752}
753
754void GLWorldView::drawDreiBein(QGLPainter *painter)
755{
756 painter->modelViewMatrix().push();
757 painter->modelViewMatrix().translate(camera()->center());
[073a2ff]758 painter->setStandardEffect(QGL::LitMaterial);
[02b2d3]759 painter->setFaceMaterial(QGL::FrontFaces, NULL);
[26ed25]760 meshDreiBein->draw(painter);
[e8c636]761 painter->modelViewMatrix().pop();
762}
763
[704d59]764void GLWorldView::sceneHoverSignalled(const atomId_t _id)
[407638e]765{
[704d59]766 emit hoverChanged(_id);
[407638e]767}
[2b596f]768
[704d59]769void GLWorldView::sceneHoverSignalled(const moleculeId_t _id, int _i)
[2b596f]770{
[704d59]771 emit hoverChanged(_id, _i);
[2b596f]772}
Note: See TracBrowser for help on using the repository browser.