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