Index: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp
===================================================================
--- src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp	(revision 65c3231ff9731698df9e1c840c3c6b5d4c56b2cf)
+++ src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp	(revision 494478e91be04149753f2f153a7dfad4d8911944)
@@ -40,4 +40,6 @@
 #include <Qt3D/qglbuilder.h>
 
+#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.hpp"
+
 #include "CodePatterns/MemDebug.hpp"
 
@@ -47,12 +49,5 @@
 #include "CodePatterns/IteratorAdaptors.hpp"
 #include "CodePatterns/Log.hpp"
-#include "CodePatterns/Observer/Notification.hpp"
-#include "CodePatterns/Observer/ObserverLog.hpp"
-
-#include "Atom/atom.hpp"
-#include "molecule.hpp"
-#include "Descriptors/AtomIdDescriptor.hpp"
-#include "Descriptors/MoleculeIdDescriptor.hpp"
-#include "Element/element.hpp"
+
 #include "LinearAlgebra/Vector.hpp"
 #include "LinkedCell/PointCloudAdaptor.hpp"
@@ -66,10 +61,5 @@
 #include "World.hpp"
 
-#include "ObservedValue_wCallback.hpp"
-#include "ObservedValue_UpdateAtoms.hpp"
-
 using namespace boost::assign;
-
-#include "GLMoleculeObject_atom.hpp"
 
 static Observable::channels_t getAllAtomicChangesChannels()
@@ -80,16 +70,5 @@
 }
 
-static Observable::channels_t getAllAtomicObservedChannels()
-{
-  Observable::channels_t channels;
-  channels += molecule::AtomInserted, molecule::AtomRemoved, molecule::AtomMoved;
-  return channels;
-}
-
-// static instances
 const Observable::channels_t GLMoleculeObject_molecule::HullChannels(getAllAtomicChangesChannels());
-const Observable::channels_t GLMoleculeObject_molecule::BoundingBoxChannels(1, molecule::BoundingBoxChanged);
-const Observable::channels_t GLMoleculeObject_molecule::IndexChannels(1, molecule::IndexChanged);
-const Observable::channels_t GLMoleculeObject_molecule::NameChannels(1, molecule::MoleculeNameChanged);
 
 static QGLSceneNode *createMoleculeMesh(const QGeometryData &_geo)
@@ -104,17 +83,13 @@
 GLMoleculeObject_molecule::GLMoleculeObject_molecule(
     QObject *parent,
-    const moleculeId_t _molid,
-    QtObservedInstanceBoard * _board,
-    std::vector<boost::any> _ObservedValues) :
+    QtObservedInstanceBoard &_board,
+    QtObservedMolecule::ptr _ObservedMolecule) :
   GLMoleculeObject((QGLSceneNode *)NULL, parent),
-  Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
   owner(NULL),
-  molref(getMolecule(_molid)),
+  molref(QtObservedMolecule::getMolecule(_ObservedMolecule->getMolIndex())),
   /* We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
    * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
    * the class has not been fully constructed yet. "This" itself seems to be working fine.
    */
-  ObservedValues(_ObservedValues),
-  subjectKilledCount(0),
   TesselationHullUpdater(
       boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
@@ -123,12 +98,11 @@
       molref,
       TesselationHullUpdater,
-      "MoleculeTesselationHull_"+toString(_molid),
+      "MoleculeTesselationHull_"+toString(_ObservedMolecule->getMolIndex()),
       HullChannels),
   hoverAtomId(-1),
-  AllsignedOnChannels(getAllAtomicObservedChannels().size()),
-  signedOnChannels(0),
-  board(_board)
-{
-  init(_molid);
+  board(_board),
+  ObservedMolecule(_ObservedMolecule)
+{
+  init(ObservedMolecule->getMolIndex());
 }
 
@@ -136,17 +110,13 @@
     QGLSceneNode *mesh[],
     QObject *parent,
-    const moleculeId_t _molid,
-    QtObservedInstanceBoard * _board,
-    std::vector<boost::any> _ObservedValues) :
+    QtObservedInstanceBoard &_board,
+    QtObservedMolecule::ptr _ObservedMolecule) :
   GLMoleculeObject(mesh, parent),
-  Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
   owner(NULL),
-  molref(getMolecule(_molid)),
+  molref(QtObservedMolecule::getMolecule(_ObservedMolecule->getMolIndex())),
   /* We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
    * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
    * the class has not been fully constructed yet. "This" itself seems to be working fine.
    */
-  ObservedValues(_ObservedValues),
-  subjectKilledCount(0),
   TesselationHullUpdater(
       boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
@@ -155,12 +125,11 @@
       molref,
       TesselationHullUpdater,
-      "MoleculeTesselationHull_"+toString(_molid),
+      "MoleculeTesselationHull_"+toString(_ObservedMolecule->getMolIndex()),
       HullChannels),
   hoverAtomId(-1),
-  AllsignedOnChannels(getAllAtomicObservedChannels().size()),
-  signedOnChannels(0),
-  board(_board)
-{
-  init(_molid);
+  board(_board),
+  ObservedMolecule(_ObservedMolecule)
+{
+  init(ObservedMolecule->getMolIndex());
 }
 
@@ -175,19 +144,11 @@
   m_visible = false;
 
-  activateObserver();
-
   connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
   connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
-  connect (this, SIGNAL(TesselationHullChanged()), this, SLOT(resetTesselationHull()), Qt::QueuedConnection);
-  connect (this, SIGNAL(BoundingBoxChanged()), this, SLOT(resetBoundingBox()), Qt::QueuedConnection);
-  connect (this, SIGNAL(IdChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
-  connect (this, SIGNAL(AtomInserted(const atomId_t)), this, SLOT(atomInserted(const atomId_t)), Qt::QueuedConnection);
-  connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(atomRemoved(const atomId_t)), Qt::QueuedConnection);
-  if (board != NULL) {
-    connect(board, SIGNAL(atomInserted(const atomId_t)),
-        this, SLOT(atomInserted(const atomId_t)));
-    connect(board, SIGNAL(atomRemoved(const atomId_t)),
-        this, SLOT(atomRemoved(const atomId_t)));
-  }
+  connect (ObservedMolecule.get(), SIGNAL(tesselationhullChanged()), this, SLOT(resetTesselationHull()));
+  connect (ObservedMolecule.get(), SIGNAL(boundingboxChanged()), this, SLOT(resetBoundingBox()));
+  connect (ObservedMolecule.get(), SIGNAL(indexChanged()), this, SLOT(resetIndex()));
+  connect (ObservedMolecule.get(), SIGNAL(atomInserted(const atomId_t)), this, SLOT(atomInserted(const atomId_t)));
+  connect (ObservedMolecule.get(), SIGNAL(atomRemoved(const atomId_t)), this, SLOT(atomRemoved(const atomId_t)));
 
   connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
@@ -195,36 +156,5 @@
 
 GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
-{
-  deactivateObserver();
-  board->returnMoleculeObservedValues(getMolIndex(), ObservedValues);
-}
-
-void GLMoleculeObject_molecule::deactivateObserver()
-{
-  if (owner != NULL) {
-    Observable::channels_t channels = getAllAtomicObservedChannels();
-    for (Observable::channels_t::const_iterator iter = channels.begin();
-        iter != channels.end(); ++iter)
-      owner->signOff(this, *iter);
-    owner = NULL;
-    signedOnChannels = 0;
-  }
-}
-
-void GLMoleculeObject_molecule::activateObserver()
-{
-  // sign on as observer (obtain non-const instance before)
-  const molecule * const _molecule = getMolecule(getMolIndex());
-  if (_molecule != NULL) {
-    Observable::channels_t channels = getAllAtomicObservedChannels();
-    owner = static_cast<const Observable *>(_molecule);
-    for (Observable::channels_t::const_iterator iter = channels.begin();
-        iter != channels.end(); ++iter)
-      owner->signOn(this, *iter);
-  } else {
-    ELOG(1, "GLMoleculeObject_molecule() - added null object for not present mol id " << getMolIndex());
-  }
-
-}
+{}
 
 void GLMoleculeObject_molecule::addAtomBonds(
@@ -249,7 +179,9 @@
   QGeometryData geo;
 
-  const molecule * const molref = getMolecule(getMolIndex());
+  const molecule * const molref =
+      QtObservedMolecule::getMolecule(ObservedMolecule->getMolIndex());
   if (molref == NULL) {
-    ELOG(1, "Could not createMoleculeMesh, molecule with id " << getMolIndex() << " already gone.");
+    ELOG(1, "Could not createMoleculeMesh, molecule with id "
+        << ObservedMolecule->getMolIndex() << " already gone.");
     return geo;
   }
@@ -273,5 +205,7 @@
     // Tesselate the points.
     Tesselation T;
-    PointCloudAdaptor<molecule> cloud(const_cast<molecule *>(molref), getMolName());
+    PointCloudAdaptor<molecule> cloud(
+        const_cast<molecule *>(molref),
+        ObservedMolecule->getMolName());
     T(cloud, minradius);
 
@@ -320,31 +254,4 @@
 }
 
-molecule::BoundingBoxInfo GLMoleculeObject_molecule::initBoundingBox()
-{
-  molecule::BoundingBoxInfo info;
-  info.position = zeroVec;
-  info.radius = 0.;
-  return info;
-}
-
-molecule::BoundingBoxInfo GLMoleculeObject_molecule::updateBoundingBox(
-    const boost::function<const moleculeId_t ()> &_getMolIndex)
-{
-  const molecule * const mol = getMolecule(_getMolIndex());
-  return mol->getBoundingBox();
-}
-
-moleculeId_t GLMoleculeObject_molecule::updateIndex()
-{
-  return const_cast<const World &>(World::getInstance()).lastChangedMolId();
-}
-
-std::string GLMoleculeObject_molecule::updateName(
-    const boost::function<const moleculeId_t ()> &_getMolIndex)
-{
-  const molecule * const mol = getMolecule(_getMolIndex());
-  return mol->getName();
-}
-
 void GLMoleculeObject_molecule::resetTesselationHull()
 {
@@ -356,5 +263,5 @@
 void GLMoleculeObject_molecule::resetBoundingBox()
 {
-  molecule::BoundingBoxInfo info = getBoundingBox();
+  molecule::BoundingBoxInfo info = ObservedMolecule->getBoundingBox();
   setPosition(QVector3D(info.position[0], info.position[1], info.position[2]));
   setScale(info.radius + 0.3); // getBoundingSphere() only sees atoms as points, so make the box a bit bigger
@@ -363,5 +270,5 @@
 void GLMoleculeObject_molecule::resetIndex()
 {
-  const atomId_t newId = getMolIndex();
+  const atomId_t newId = ObservedMolecule->getMolIndex();
   const size_t oldId = objectId();
   ASSERT( newId != oldId,
@@ -371,8 +278,4 @@
 
   emit indexChanged(this, oldId, newId);
-}
-
-void GLMoleculeObject_molecule::resetName()
-{
 }
 
@@ -418,61 +321,4 @@
 }
 
-void GLMoleculeObject_molecule::update(Observable *publisher)
-{
-  ASSERT(0,
-      "GLMoleculeObject_molecule::update() - general update from unexpected source.");
-}
-
-void GLMoleculeObject_molecule::subjectKilled(Observable *publisher)
-{
-  ++signedOnChannels;
-
-  if (signedOnChannels == AllsignedOnChannels) {
-    // remove owner: no more signOff needed
-    owner = NULL;
-
-    board->moleculecountsubjectKilled(getMolIndex());
-  }
-}
-
-void GLMoleculeObject_molecule::recieveNotification(Observable *publisher, Notification_ptr notification)
-{
-  const molecule * const _molecule = getMolecule(getMolIndex());
-  // when molecule is NULL we will soon get destroyed anyway
-  if (_molecule == NULL)
-    return;
-  if (publisher == dynamic_cast<const Observable*>(_molecule)){
-    // notification from atom
-#ifdef LOG_OBSERVER
-    observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
-          << " received notification from molecule " << getMolIndex() << " for channel "
-          << notification->getChannelNo() << ".";
-#endif
-    switch (notification->getChannelNo()) {
-      case molecule::AtomInserted:
-      case molecule::AtomRemoved:
-      case molecule::AtomMoved:
-      {
-  #ifdef LOG_OBSERVER
-        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
-  #endif
-        emit TesselationHullChanged();
-        emit BoundingBoxChanged();
-        break;
-      }
-      case molecule::IndexChanged:
-      {
-  #ifdef LOG_OBSERVER
-        const atomId_t _id = _molecule->lastChangedAtomId();
-        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+"'s index has changed.";
-  #endif
-        emit IdChanged();
-        break;
-      }
-      default:
-        break;
-    }
-  }
-}
 
 void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter)
@@ -573,5 +419,5 @@
           GLMoleculeObject::meshSphere,
           this,
-          board->getObservedAtom(_id));
+          board.getObservedAtom(_id));
   ASSERT( atomObject != NULL,
       "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(_id));
@@ -582,5 +428,4 @@
 
   qRegisterMetaType<atomId_t>("atomId_t");
-  qRegisterMetaType<bond::ptr>("bond::ptr");
   qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond");
   connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t)));
@@ -645,5 +490,5 @@
     if (moleculeObject == this){
       // Propagate signal.
-      emit hoverChanged(getMolIndex(), 0);
+      emit hoverChanged(ObservedMolecule->getMolIndex(), 0);
     }
   }
@@ -748,6 +593,7 @@
 void GLMoleculeObject_molecule::wasClicked()
 {
-  LOG(4, "INFO: GLMoleculeObject_molecule: atom " << getMolIndex() << " has been clicked");
-  emit moleculeClicked(getMolIndex());
+  LOG(4, "INFO: GLMoleculeObject_molecule: atom "
+      << ObservedMolecule->getMolIndex() << " has been clicked");
+  emit moleculeClicked(ObservedMolecule->getMolIndex());
 }
 
@@ -777,93 +623,4 @@
 }
 
-const molecule * const GLMoleculeObject_molecule::getMolecule(const moleculeId_t _id)
-{
-  const molecule * const mol = const_cast<const World &>(World::getInstance()).
-      getMolecule(MoleculeById(_id));
-  return mol;
-}
-
-void GLMoleculeObject_molecule::initObservedValues(
-    std::vector<boost::any> &_ObservedValues,
-    const moleculeId_t _molid,
-    const molecule * const _molref,
-    const boost::function<void(const moleculeId_t)> &_subjectKilled)
-{
-  /* This is an old note from when the code was still part of cstor's initializer body.
-   * TODO: Probably does not apply anymore but has not yet been tested.
-   *
-   * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
-   * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
-   * the class has not been fully constructed yet. "This" itself seems to be working fine.
-   */
-
-  ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
-      "GLMoleculeObject_molecule::initObservedValues() - given ObservedValues has not correct size.");
-
-  // fill ObservedValues: index first
-  const boost::function<moleculeId_t ()> MolIndexUpdater(
-      boost::bind(&GLMoleculeObject_molecule::updateIndex));
-
-  ObservedValue_wCallback<moleculeId_t> * const IndexObservable =
-      new ObservedValue_wCallback<moleculeId_t>(
-        _molref,
-        MolIndexUpdater,
-        "MoleculeIndex_"+toString(_molid),
-        _molid,
-        IndexChannels,
-        _subjectKilled);
-  _ObservedValues[MolIndex] = IndexObservable;
-
-  const boost::function<const moleculeId_t ()> MolIndexGetter =
-      boost::bind(&ObservedValue_wCallback<moleculeId_t>::get,
-          IndexObservable);
-
-  // fill ObservedValues: then all the other that need index
-  const boost::function<std::string ()> MolNameUpdater(
-      boost::bind(&GLMoleculeObject_molecule::updateName, MolIndexGetter));
-  const boost::function<molecule::BoundingBoxInfo ()> BoundingBoxUpdater(
-      boost::bind(&GLMoleculeObject_molecule::updateBoundingBox, MolIndexGetter));
-
-  _ObservedValues[MolName] = new ObservedValue_wCallback<std::string, moleculeId_t>(
-      _molref,
-      MolNameUpdater,
-      "MoleculeName_"+toString(_molid),
-      MolNameUpdater(),
-      NameChannels,
-      _subjectKilled,
-      MolIndexGetter);
-  _ObservedValues[BoundingBox] = new ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t>(
-      _molref,
-      BoundingBoxUpdater,
-      "MoleculeBoundingBox_"+toString(_molid),
-      initBoundingBox(),
-      BoundingBoxChannels,
-      _subjectKilled,
-      MolIndexGetter);
-}
-
-void GLMoleculeObject_molecule::destroyObservedValues(
-    std::vector<boost::any> &_ObservedValues)
-{
-  delete boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(_ObservedValues[MolIndex]);
-  delete boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(_ObservedValues[MolName]);
-  delete boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(_ObservedValues[BoundingBox]);
-  _ObservedValues.clear();
-}
-
-moleculeId_t GLMoleculeObject_molecule::getMolIndex() const
-{
-  return boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(ObservedValues[MolIndex])->get();
-}
-
-std::string GLMoleculeObject_molecule::getMolName() const
-{
-  return boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(ObservedValues[MolName])->get();
-}
-
-molecule::BoundingBoxInfo GLMoleculeObject_molecule::getBoundingBox() const
-{
-  return boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(ObservedValues[BoundingBox])->get();
-}
 
 GLMoleculeObject_molecule::atoms_t GLMoleculeObject_molecule::getPresentAtoms() const
