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