[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 | * GLMoleculeObject_atom.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Aug 17, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include "GLMoleculeObject_atom.hpp"
|
---|
| 21 |
|
---|
| 22 | #include <Qt3D/qglbuilder.h>
|
---|
| 23 | #include <Qt3D/qglscenenode.h>
|
---|
| 24 | #include <Qt3D/qglsphere.h>
|
---|
| 25 |
|
---|
| 26 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 27 |
|
---|
| 28 | #include "CodePatterns/Assert.hpp"
|
---|
[7188b1] | 29 | #include "CodePatterns/Log.hpp"
|
---|
[02ce36] | 30 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
[907636] | 31 |
|
---|
[2ad1ec] | 32 | #include "Atom/atom.hpp"
|
---|
| 33 | #include "Bond/bond.hpp"
|
---|
[7188b1] | 34 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[3bdb6d] | 35 | #include "Element/element.hpp"
|
---|
[907636] | 36 | #include "LinearAlgebra/Vector.hpp"
|
---|
[2ad1ec] | 37 | #include "GLMoleculeObject_bond.hpp"
|
---|
[7188b1] | 38 | #include "World.hpp"
|
---|
[907636] | 39 |
|
---|
| 40 | static QGLSceneNode * createAtom(QObject *parent)
|
---|
| 41 | {
|
---|
| 42 | QGLBuilder builder;
|
---|
| 43 | builder << QGLSphere(0.5);
|
---|
| 44 | QGLSceneNode *n = builder.finalizedSceneNode();
|
---|
| 45 | n->setParent(parent);
|
---|
| 46 | return n;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[06ebf5] | 49 | GLMoleculeObject_atom::GLMoleculeObject_atom(QObject *parent, const atom *atomref) :
|
---|
[2ad1ec] | 50 | GLMoleculeObject(createAtom(parent), parent),
|
---|
| 51 | Observer(std::string("GLMoleculeObject_atom")+toString(atomref->getId())),
|
---|
| 52 | _atom(atomref)
|
---|
[7188b1] | 53 | {
|
---|
| 54 | // sign on as observer (obtain non-const instance before)
|
---|
[02ce36] | 55 | atomref->signOn(this, AtomObservable::IndexChanged);
|
---|
| 56 | atomref->signOn(this, AtomObservable::PositionChanged);
|
---|
| 57 | atomref->signOn(this, AtomObservable::ElementChanged);
|
---|
[2ad1ec] | 58 | atomref->signOn(this, AtomObservable::BondsAdded);
|
---|
[d53902] | 59 | World::getInstance().signOn(this, World::SelectionChanged);
|
---|
[7188b1] | 60 |
|
---|
| 61 | // set the object's id
|
---|
| 62 | resetProperties();
|
---|
| 63 |
|
---|
[57a770] | 64 | LOG(2, "INFO: Created sphere for atom " << _atom->getId() << ".");
|
---|
[7188b1] | 65 |
|
---|
| 66 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[9c18e4] | 69 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
|
---|
| 70 | {
|
---|
[02ce36] | 71 | _atom->signOff(this, AtomObservable::IndexChanged);
|
---|
| 72 | _atom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 73 | _atom->signOff(this, AtomObservable::ElementChanged);
|
---|
[2ad1ec] | 74 | _atom->signOff(this, AtomObservable::BondsAdded);
|
---|
[d53902] | 75 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
[9c18e4] | 76 | }
|
---|
| 77 |
|
---|
[7188b1] | 78 | void GLMoleculeObject_atom::update(Observable *publisher)
|
---|
| 79 | {
|
---|
[57a770] | 80 | #ifdef LOG_OBSERVER
|
---|
| 81 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from atom "+toString(_atom->getId())+".";
|
---|
| 82 | #endif
|
---|
[7188b1] | 83 | resetProperties();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void GLMoleculeObject_atom::resetPosition()
|
---|
[907636] | 87 | {
|
---|
| 88 | const Vector Position = _atom->getPosition();
|
---|
[57a770] | 89 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
|
---|
[7188b1] | 90 | setPosition(QVector3D(Position[0], Position[1], Position[2]));
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | void GLMoleculeObject_atom::resetElement()
|
---|
| 94 | {
|
---|
| 95 | size_t elementno = 0;
|
---|
| 96 | if (_atom->getType() != NULL) {
|
---|
[ed26ae] | 97 | elementno = _atom->getType()->getAtomicNumber();
|
---|
[7188b1] | 98 | } else { // if no element yet, set to hydrogen
|
---|
| 99 | elementno = 1;
|
---|
| 100 | }
|
---|
[57a770] | 101 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
|
---|
[907636] | 102 |
|
---|
| 103 | // set materials
|
---|
| 104 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 105 | ASSERT(elementmaterial != NULL,
|
---|
| 106 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
|
---|
| 107 | setMaterial(elementmaterial);
|
---|
| 108 | QGLMaterial *hovermaterial = getMaterial(0); // 0 is the hover material
|
---|
| 109 | ASSERT(hovermaterial != NULL,
|
---|
| 110 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function for hover is NULL.");
|
---|
| 111 | setHoverMaterial(hovermaterial);
|
---|
[8a77ac] | 112 | QGLMaterial *selectionmaterial = getMaterial(65536); // 65536 is the selection material
|
---|
| 113 | ASSERT(selectionmaterial != NULL,
|
---|
| 114 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function for selection is NULL.");
|
---|
| 115 | setSelectionMaterial(selectionmaterial);
|
---|
[907636] | 116 |
|
---|
[7188b1] | 117 | // set scale
|
---|
| 118 | double radius = 0.;
|
---|
| 119 | if (_atom->getType() != NULL) {
|
---|
| 120 | radius = _atom->getType()->getVanDerWaalsRadius();
|
---|
| 121 | } else {
|
---|
| 122 | radius = 0.5;
|
---|
| 123 | }
|
---|
| 124 | setScale( radius );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void GLMoleculeObject_atom::resetIndex()
|
---|
| 128 | {
|
---|
| 129 | const size_t atomno = _atom->getId();
|
---|
[57a770] | 130 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomno)+".");
|
---|
[7188b1] | 131 | setObjectId(atomno);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void GLMoleculeObject_atom::resetProperties()
|
---|
| 135 | {
|
---|
[907636] | 136 | // set position
|
---|
[7188b1] | 137 | resetPosition();
|
---|
[907636] | 138 |
|
---|
[7188b1] | 139 | // set element
|
---|
| 140 | resetElement();
|
---|
[907636] | 141 |
|
---|
| 142 | // set the object's id
|
---|
[7188b1] | 143 | resetIndex();
|
---|
[d53902] | 144 |
|
---|
| 145 | // selected?
|
---|
| 146 | setSelected(World::getInstance().isSelected(_atom));
|
---|
[7188b1] | 147 | }
|
---|
[907636] | 148 |
|
---|
[7188b1] | 149 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
|
---|
| 150 | {}
|
---|
[06ebf5] | 151 |
|
---|
[7188b1] | 152 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 153 | {
|
---|
[d53902] | 154 | if (publisher == dynamic_cast<const Observable*>(_atom)){
|
---|
| 155 | // notofication from atom
|
---|
[57a770] | 156 | #ifdef LOG_OBSERVER
|
---|
[d53902] | 157 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
|
---|
| 158 | << " received notification from atom " << _atom->getId() << " for channel "
|
---|
| 159 | << notification->getChannelNo() << ".";
|
---|
[57a770] | 160 | #endif
|
---|
[d53902] | 161 | switch (notification->getChannelNo()) {
|
---|
| 162 | case AtomObservable::ElementChanged:
|
---|
| 163 | resetElement();
|
---|
[5a2a06] | 164 | emit changed();
|
---|
[d53902] | 165 | break;
|
---|
| 166 | case AtomObservable::IndexChanged:
|
---|
| 167 | resetIndex();
|
---|
| 168 | break;
|
---|
| 169 | case AtomObservable::PositionChanged:
|
---|
| 170 | resetPosition();
|
---|
[5a2a06] | 171 | emit changed();
|
---|
[d53902] | 172 | break;
|
---|
| 173 | case AtomObservable::BondsAdded:
|
---|
[2ad1ec] | 174 | {
|
---|
[d53902] | 175 | ASSERT(!_atom->getListOfBonds().empty(),
|
---|
| 176 | "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
|
---|
| 177 | const bond * _bond = *(_atom->getListOfBonds().rbegin());
|
---|
| 178 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
|
---|
| 179 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
| 180 | emit BondsInserted(_bond, side);
|
---|
| 181 | break;
|
---|
| 182 | }
|
---|
| 183 | default:
|
---|
| 184 | //setProperties();
|
---|
| 185 | break;
|
---|
| 186 | }
|
---|
| 187 | }else{
|
---|
| 188 | // notification from world
|
---|
| 189 | #ifdef LOG_OBSERVER
|
---|
| 190 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
|
---|
| 191 | << " received notification from world for channel "
|
---|
| 192 | << notification->getChannelNo() << ".";
|
---|
| 193 | #endif
|
---|
| 194 | switch (notification->getChannelNo()) {
|
---|
| 195 | case World::SelectionChanged:
|
---|
| 196 | setSelected(World::getInstance().isSelected(_atom));
|
---|
[5a2a06] | 197 | emit selectionChanged();
|
---|
[d53902] | 198 | break;
|
---|
| 199 | default:
|
---|
[2ad1ec] | 200 | break;
|
---|
[d53902] | 201 | }
|
---|
[7188b1] | 202 | }
|
---|
[06ebf5] | 203 | }
|
---|
| 204 |
|
---|
| 205 | void GLMoleculeObject_atom::wasClicked()
|
---|
| 206 | {
|
---|
[57a770] | 207 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << _atom->getId() << " has been clicked");
|
---|
[06ebf5] | 208 | emit clicked(_atom->getId());
|
---|
[907636] | 209 | }
|
---|