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