[907636] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[907636] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * GLWorldScene.cpp
|
---|
| 10 | *
|
---|
| 11 | * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
|
---|
| 12 | *
|
---|
| 13 | * Created on: Aug 17, 2011
|
---|
| 14 | * Author: heber
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | // include config.h
|
---|
| 18 | #ifdef HAVE_CONFIG_H
|
---|
| 19 | #include <config.h>
|
---|
| 20 | #endif
|
---|
| 21 |
|
---|
| 22 | #include "GLWorldScene.hpp"
|
---|
[d1196d] | 23 | #include <Qt3D/qglview.h>
|
---|
[907636] | 24 |
|
---|
| 25 | #include "GLMoleculeObject.hpp"
|
---|
[7188b1] | 26 | #include "GLMoleculeObject_atom.hpp"
|
---|
| 27 | #include "GLMoleculeObject_bond.hpp"
|
---|
[c67518] | 28 | #include "GLMoleculeObject_molecule.hpp"
|
---|
[907636] | 29 |
|
---|
| 30 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 31 |
|
---|
[7188b1] | 32 | #include "CodePatterns/Log.hpp"
|
---|
| 33 |
|
---|
[0e9ffe] | 34 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
---|
[89643d] | 35 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
---|
[6f0841] | 36 | #include "Atom/atom.hpp"
|
---|
[7188b1] | 37 | #include "Bond/bond.hpp"
|
---|
[89643d] | 38 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[37b2575] | 39 | #include "Helpers/helpers.hpp"
|
---|
[907636] | 40 | #include "molecule.hpp"
|
---|
| 41 | #include "World.hpp"
|
---|
| 42 |
|
---|
[2ad1ec] | 43 | #include <iostream>
|
---|
| 44 |
|
---|
[ce7fdc] | 45 | using namespace MoleCuilder;
|
---|
[907636] | 46 |
|
---|
[2ad1ec] | 47 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
---|
| 48 | {
|
---|
| 49 | ost << t.first << "," << t.second;
|
---|
| 50 | return ost;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[907636] | 53 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
| 54 | : QObject(parent)
|
---|
| 55 | {
|
---|
| 56 | init();
|
---|
| 57 |
|
---|
| 58 | //changeMaterials(false);
|
---|
[45ea59] | 59 | domainBoxMaterial = new QGLMaterial;
|
---|
| 60 | domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
|
---|
| 61 | domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
|
---|
| 62 | domainBoxMaterial->setEmittedLight(QColor(150,160,200,255));
|
---|
[d1196d] | 63 |
|
---|
| 64 | dreiBeinMaterial = new QGLMaterial;
|
---|
| 65 | dreiBeinMaterial->setAmbientColor(QColor(0,0,0,255));
|
---|
| 66 | dreiBeinMaterial->setDiffuseColor(QColor(0,0,0,255));
|
---|
| 67 | dreiBeinMaterial->setEmittedLight(QColor(150,200,150,255));
|
---|
[907636] | 68 | }
|
---|
| 69 |
|
---|
| 70 | GLWorldScene::~GLWorldScene()
|
---|
[7188b1] | 71 | {
|
---|
| 72 | // remove all elements
|
---|
| 73 | GLMoleculeObject::cleanMaterialMap();
|
---|
[45ea59] | 74 |
|
---|
| 75 | delete(domainBoxMaterial);
|
---|
[d1196d] | 76 | delete(dreiBeinMaterial);
|
---|
[7188b1] | 77 | }
|
---|
[907636] | 78 |
|
---|
| 79 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
| 80 | *
|
---|
| 81 | */
|
---|
| 82 | void GLWorldScene::init()
|
---|
| 83 | {
|
---|
| 84 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
| 85 |
|
---|
| 86 | if (molecules.size() > 0) {
|
---|
| 87 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
| 88 | Runner != molecules.end();
|
---|
| 89 | Runner++) {
|
---|
[c67518] | 90 |
|
---|
[7188b1] | 91 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
| 92 | atomiter != (*Runner)->end();
|
---|
| 93 | ++atomiter) {
|
---|
| 94 | // create atom objects in scene
|
---|
| 95 | atomInserted(*atomiter);
|
---|
| 96 |
|
---|
[9c18e4] | 97 | // create bond objects in scene
|
---|
[2ad1ec] | 98 | const BondList &bondlist = (*atomiter)->getListOfBonds();
|
---|
| 99 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
| 100 | bonditer != bondlist.end();
|
---|
| 101 | ++bonditer) {
|
---|
| 102 | const bond *_bond = *bonditer;
|
---|
| 103 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
|
---|
| 104 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
| 105 | bondInserted(_bond, side);
|
---|
| 106 | }
|
---|
[7188b1] | 107 | }
|
---|
[907636] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[7188b1] | 112 | /** Adds an atom to the scene.
|
---|
| 113 | *
|
---|
| 114 | * @param _atom atom to add
|
---|
| 115 | */
|
---|
| 116 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
[907636] | 117 | {
|
---|
[57a770] | 118 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
[7188b1] | 119 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
---|
[37b2575] | 120 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
| 121 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
[7188b1] | 122 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
[37b2575] | 123 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
[7188b1] | 124 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
[5a2a06] | 125 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
[a099d3] | 126 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
[5a2a06] | 127 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
[2ad1ec] | 128 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
---|
| 129 | //bondsChanged(_atom);
|
---|
[65487f] | 130 | emit changeOccured();
|
---|
[7188b1] | 131 | }
|
---|
| 132 |
|
---|
| 133 | /** Removes an atom from the scene.
|
---|
| 134 | *
|
---|
| 135 | * @param _atom atom to remove
|
---|
| 136 | */
|
---|
| 137 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
| 138 | {
|
---|
[57a770] | 139 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
[2ad1ec] | 140 | // bonds are removed by signal coming from ~bond
|
---|
[7188b1] | 141 | // remove atoms
|
---|
[37b2575] | 142 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
| 143 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
[7188b1] | 144 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
| 145 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
| 146 | atomObject->disconnect();
|
---|
[37b2575] | 147 | AtomsinSceneMap.erase(iter);
|
---|
[7188b1] | 148 | delete atomObject;
|
---|
[65487f] | 149 | emit changeOccured();
|
---|
[907636] | 150 | }
|
---|
| 151 |
|
---|
[3927ef] | 152 | /** ....
|
---|
[c67518] | 153 | *
|
---|
| 154 | */
|
---|
[3927ef] | 155 | void GLWorldScene::worldSelectionChanged()
|
---|
[c67518] | 156 | {
|
---|
[3927ef] | 157 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
|
---|
| 158 |
|
---|
| 159 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
| 160 |
|
---|
| 161 | if (molecules.size() > 0) {
|
---|
| 162 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
| 163 | Runner != molecules.end();
|
---|
| 164 | Runner++) {
|
---|
| 165 |
|
---|
| 166 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
|
---|
| 167 | bool isSelected = World::getInstance().isSelected(*Runner);
|
---|
| 168 |
|
---|
| 169 | // molecule selected but not in scene?
|
---|
| 170 | if (isSelected && (iter == MoleculesinSceneMap.end())){
|
---|
| 171 | // -> create new mesh
|
---|
| 172 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(this, *Runner);
|
---|
| 173 | MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
|
---|
| 174 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
| 175 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
| 176 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
| 177 | emit changed();
|
---|
| 178 | emit changeOccured();
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | // molecule not selected but in scene?
|
---|
| 182 | if (!isSelected && (iter != MoleculesinSceneMap.end())){
|
---|
| 183 | // -> remove from scene
|
---|
| 184 | moleculeRemoved(*Runner);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[c67518] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /** Removes a molecule from the scene.
|
---|
| 192 | *
|
---|
| 193 | * @param _molecule molecule to remove
|
---|
| 194 | */
|
---|
| 195 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
|
---|
| 196 | {
|
---|
[3927ef] | 197 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
---|
[c67518] | 198 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
---|
[3927ef] | 199 |
|
---|
| 200 | // only remove if the molecule is in the scene
|
---|
| 201 | // (= is selected)
|
---|
| 202 | if (iter != MoleculesinSceneMap.end()){
|
---|
| 203 | GLMoleculeObject_molecule *molObject = iter->second;
|
---|
| 204 | molObject->disconnect();
|
---|
| 205 | MoleculesinSceneMap.erase(iter);
|
---|
| 206 | delete molObject;
|
---|
| 207 | emit changed();
|
---|
| 208 | emit changeOccured();
|
---|
| 209 | }
|
---|
[c67518] | 210 | }
|
---|
| 211 |
|
---|
[37b2575] | 212 | /** Adds a bond to the scene.
|
---|
[7188b1] | 213 | *
|
---|
| 214 | * @param _bond bond to add
|
---|
[2ad1ec] | 215 | * @param side which side of the bond (left or right)
|
---|
[7188b1] | 216 | */
|
---|
[2ad1ec] | 217 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
---|
[7188b1] | 218 | {
|
---|
[57a770] | 219 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
[2ad1ec] | 220 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
---|
| 221 |
|
---|
| 222 | BondIds ids;
|
---|
| 223 | switch (side) {
|
---|
| 224 | case GLMoleculeObject_bond::left:
|
---|
| 225 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
---|
| 226 | break;
|
---|
| 227 | case GLMoleculeObject_bond::right:
|
---|
| 228 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
---|
| 229 | break;
|
---|
[7188b1] | 230 | }
|
---|
[2ad1ec] | 231 | #ifndef NDEBUG
|
---|
| 232 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
---|
| 233 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
| 234 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
| 235 | #endif
|
---|
| 236 | GLMoleculeObject_bond *bondObject =
|
---|
[fbb1f1] | 237 | new GLMoleculeObject_bond(this, _bond, side);
|
---|
[2ad1ec] | 238 | connect (
|
---|
| 239 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
| 240 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
[5a2a06] | 241 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
[2ad1ec] | 242 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
| 243 | // BondIdsinSceneMap.insert( Leftids );
|
---|
[65487f] | 244 | emit changeOccured();
|
---|
[7188b1] | 245 | }
|
---|
[907636] | 246 |
|
---|
[37b2575] | 247 | /** Removes a bond from the scene.
|
---|
[7188b1] | 248 | *
|
---|
| 249 | * @param _bond bond to remove
|
---|
| 250 | */
|
---|
[37b2575] | 251 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
[907636] | 252 | {
|
---|
[2ad1ec] | 253 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
[7188b1] | 254 | {
|
---|
| 255 | // left bond
|
---|
[37b2575] | 256 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
| 257 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
| 258 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
| 259 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
| 260 | +toString(rightnr)+" not on display.");
|
---|
[2ad1ec] | 261 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
[37b2575] | 262 | BondsinSceneMap.erase(leftiter);
|
---|
[2ad1ec] | 263 | //delete bondObject; // is done by signal from bond itself
|
---|
| 264 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
[7188b1] | 265 | }
|
---|
[2ad1ec] | 266 |
|
---|
[65487f] | 267 | emit changeOccured();
|
---|
[7188b1] | 268 | }
|
---|
| 269 |
|
---|
| 270 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
| 271 | {
|
---|
| 272 | // Initialize all of the mesh objects that we have as children.
|
---|
| 273 | foreach (QObject *obj, children()) {
|
---|
| 274 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 275 | if (meshobj)
|
---|
| 276 | meshobj->initialize(view, painter);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[45ea59] | 280 | void GLWorldScene::drawDomainBox(QGLPainter *painter) const
|
---|
| 281 | {
|
---|
| 282 | RealSpaceMatrix m = World::getInstance().getDomain().getM();
|
---|
| 283 | painter->modelViewMatrix().push();
|
---|
| 284 | painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
|
---|
| 285 | m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
|
---|
| 286 | m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
|
---|
| 287 | 0.0, 0.0, 0.0, 1.0);
|
---|
| 288 |
|
---|
| 289 | painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
|
---|
| 290 | //glEnable(GL_LINE_SMOOTH);
|
---|
| 291 | QVector3DArray array;
|
---|
| 292 | array.append(0, 0, 0); array.append(1, 0, 0);
|
---|
| 293 | array.append(1, 0, 0); array.append(1, 1, 0);
|
---|
| 294 | array.append(1, 1, 0); array.append(0, 1, 0);
|
---|
| 295 | array.append(0, 1, 0); array.append(0, 0, 0);
|
---|
| 296 |
|
---|
| 297 | array.append(0, 0, 1); array.append(1, 0, 1);
|
---|
| 298 | array.append(1, 0, 1); array.append(1, 1, 1);
|
---|
| 299 | array.append(1, 1, 1); array.append(0, 1, 1);
|
---|
| 300 | array.append(0, 1, 1); array.append(0, 0, 1);
|
---|
| 301 |
|
---|
| 302 | array.append(0, 0, 0); array.append(0, 0, 1);
|
---|
| 303 | array.append(1, 0, 0); array.append(1, 0, 1);
|
---|
| 304 | array.append(0, 1, 0); array.append(0, 1, 1);
|
---|
| 305 | array.append(1, 1, 0); array.append(1, 1, 1);
|
---|
| 306 | painter->clearAttributes();
|
---|
| 307 | painter->setVertexAttribute(QGL::Position, array);
|
---|
| 308 | painter->draw(QGL::Lines, 24);
|
---|
| 309 | painter->modelViewMatrix().pop();
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[d1196d] | 312 | void GLWorldScene::drawDreiBein(QGLPainter *painter) const
|
---|
| 313 | {
|
---|
| 314 | QGLView *view = dynamic_cast<QGLView*>(this->parent());
|
---|
| 315 | painter->modelViewMatrix().push();
|
---|
| 316 | painter->modelViewMatrix().translate(view->camera()->center());
|
---|
| 317 | painter->setFaceMaterial(QGL::AllFaces, dreiBeinMaterial);
|
---|
| 318 | QVector3DArray array;
|
---|
| 319 | array.append(0, 0, 0); array.append(1, 0, 0);
|
---|
| 320 | array.append(0, 0, 0); array.append(0, 1, 0);
|
---|
| 321 | array.append(0, 0, 0); array.append(0, 0, 1);
|
---|
| 322 | painter->clearAttributes();
|
---|
| 323 | painter->setVertexAttribute(QGL::Position, array);
|
---|
| 324 | painter->draw(QGL::Lines, 6);
|
---|
| 325 | painter->modelViewMatrix().pop();
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[7188b1] | 328 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
| 329 | {
|
---|
| 330 | // Draw all of the mesh objects that we have as children.
|
---|
| 331 | foreach (QObject *obj, children()) {
|
---|
| 332 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 333 | if (meshobj)
|
---|
| 334 | meshobj->draw(painter);
|
---|
| 335 | }
|
---|
[45ea59] | 336 |
|
---|
| 337 | drawDomainBox(painter);
|
---|
[d1196d] | 338 | drawDreiBein(painter);
|
---|
[907636] | 339 | }
|
---|
[06ebf5] | 340 |
|
---|
[7188b1] | 341 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
[907636] | 342 | {
|
---|
[57a770] | 343 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
---|
[89643d] | 344 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
---|
| 345 | if (!World::getInstance().isSelected(Walker))
|
---|
[0e9ffe] | 346 | SelectionAtomById(no);
|
---|
[89643d] | 347 | else
|
---|
| 348 | SelectionNotAtomById(no);
|
---|
[7188b1] | 349 | emit clicked(no);
|
---|
[907636] | 350 | }
|
---|
[029bb4] | 351 |
|
---|