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