Index: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp
===================================================================
--- src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp	(revision 026bef57c11cf9d09d87056dab378f0b357f2a4c)
+++ src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp	(revision 8d5fbf175be3a0dab17997f67f03c1284b6174fb)
@@ -45,4 +45,5 @@
 
 #include "CodePatterns/Assert.hpp"
+#include "CodePatterns/IteratorAdaptors.hpp"
 #include "CodePatterns/Log.hpp"
 #include "CodePatterns/Observer/Notification.hpp"
@@ -61,4 +62,5 @@
 #include "Tesselation/BoundaryTriangleSet.hpp"
 #include "Tesselation/CandidateForTesselation.hpp"
+#include "UIElements/Views/Qt4/QtInstanceInformationBoard.hpp"
 #include "Atom/TesselPoint.hpp"
 #include "World.hpp"
@@ -70,11 +72,4 @@
 
 #include "GLMoleculeObject_atom.hpp"
-
-static Observable::channels_t getAtomsChannels()
-{
-  Observable::channels_t channels;
-  channels += molecule::AtomInserted, molecule::AtomRemoved;
-  return channels;
-}
 
 static Observable::channels_t getAllAtomicChangesChannels()
@@ -85,6 +80,12 @@
 }
 
+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::AtomsChannels(getAtomsChannels());
 const Observable::channels_t GLMoleculeObject_molecule::HullChannels(getAllAtomicChangesChannels());
 const Observable::channels_t GLMoleculeObject_molecule::BoundingBoxChannels(1, molecule::BoundingBoxChanged);
@@ -101,5 +102,9 @@
 }
 
-GLMoleculeObject_molecule::GLMoleculeObject_molecule(QObject *parent, const moleculeId_t _molid) :
+GLMoleculeObject_molecule::GLMoleculeObject_molecule(
+    QObject *parent,
+    const moleculeId_t _molid,
+    QtInstanceInformationBoard * _board,
+    std::vector<boost::any> _ObservedValues) :
   GLMoleculeObject((QGLSceneNode *)NULL, parent),
   Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
@@ -110,5 +115,5 @@
    * the class has not been fully constructed yet. "This" itself seems to be working fine.
    */
-  ObservedValues(MAX_ObservedTypes),
+  ObservedValues(_ObservedValues),
   subjectKilledCount(0),
   TesselationHullUpdater(
@@ -120,10 +125,46 @@
       "MoleculeTesselationHull_"+toString(_molid),
       HullChannels),
-  hoverAtomId(-1)
-{
-  boost::function<void(const moleculeId_t)> subjectKilled =
-      boost::bind(&GLMoleculeObject_molecule::countsubjectKilled, this, _molid);
-  initObservedValues(ObservedValues, _molid, molref, subjectKilled);
-
+  hoverAtomId(-1),
+  AllsignedOnChannels(getAllAtomicObservedChannels().size()),
+  signedOnChannels(0),
+  board(_board)
+{
+  init(_molid);
+}
+
+GLMoleculeObject_molecule::GLMoleculeObject_molecule(
+    QGLSceneNode *mesh[],
+    QObject *parent,
+    const moleculeId_t _molid,
+    QtInstanceInformationBoard * _board,
+    std::vector<boost::any> _ObservedValues) :
+  GLMoleculeObject(mesh, parent),
+  Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
+  owner(NULL),
+  molref(getMolecule(_molid)),
+  /* 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)
+      ),
+  TesselationHull(
+      molref,
+      TesselationHullUpdater,
+      "MoleculeTesselationHull_"+toString(_molid),
+      HullChannels),
+  hoverAtomId(-1),
+  AllsignedOnChannels(getAllAtomicObservedChannels().size()),
+  signedOnChannels(0),
+  board(_board)
+{
+  init(_molid);
+}
+
+void GLMoleculeObject_molecule::init(const moleculeId_t _molid)
+{
   setObjectId(_molid);
   setMaterial(getMaterial(1));
@@ -133,4 +174,6 @@
   // initially, atoms and bonds should be visible
   m_visible = false;
+
+  activateObserver();
 
   connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
@@ -147,57 +190,8 @@
 }
 
-GLMoleculeObject_molecule::GLMoleculeObject_molecule(
-    QGLSceneNode *mesh[],
-    QObject *parent,
-    const moleculeId_t _molid) :
-  GLMoleculeObject(mesh, parent),
-  Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
-  owner(NULL),
-  molref(getMolecule(_molid)),
-  /* 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(MAX_ObservedTypes),
-  subjectKilledCount(0),
-  TesselationHullUpdater(
-      boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
-      ),
-  TesselationHull(
-      molref,
-      TesselationHullUpdater,
-      "MoleculeTesselationHull_"+toString(_molid),
-      HullChannels),
-  hoverAtomId(-1)
-{
-  boost::function<void(const moleculeId_t)> subjectKilled =
-      boost::bind(&GLMoleculeObject_molecule::countsubjectKilled, this, _molid);
-  initObservedValues(ObservedValues, _molid, molref, subjectKilled);
-
-  setObjectId(_molid);
-  setMaterial(getMaterial(1));
-
-  m_selected = const_cast<const World &>(World::getInstance()).isMoleculeSelected(_molid);
-
-  // initially, atoms and bonds should be visible
-  m_visible = false;
-
-  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(AtomInserted(const atomId_t)), this, SLOT(resetAtoms()), Qt::QueuedConnection);
-  connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(resetAtoms()), Qt::QueuedConnection);
-  connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(atomRemoved(const atomId_t)), Qt::QueuedConnection);
-
-  connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
-}
-
 GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
 {
   deactivateObserver();
-  destroyObservedValues(ObservedValues);
+  board->returnMoleculeObservedValues(getMolIndex(), ObservedValues);
 }
 
@@ -205,9 +199,10 @@
 {
   if (owner != NULL) {
-    owner->signOff(this, molecule::AtomInserted);
-    owner->signOff(this, molecule::AtomRemoved);
-    owner->signOff(this, molecule::AtomMoved);
-    owner->signOff(this, molecule::IndexChanged);
+    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;
   }
 }
@@ -218,9 +213,9 @@
   const molecule * const _molecule = getMolecule(getMolIndex());
   if (_molecule != NULL) {
+    Observable::channels_t channels = getAllAtomicObservedChannels();
     owner = static_cast<const Observable *>(_molecule);
-    owner->signOn(this, molecule::AtomInserted);
-    owner->signOn(this, molecule::AtomRemoved);
-    owner->signOn(this, molecule::AtomMoved);
-    owner->signOn(this, molecule::IndexChanged);
+    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());
@@ -244,32 +239,4 @@
     BondsinSceneMap[ids]->resetWidth();
   }
-}
-
-void GLMoleculeObject_molecule::addAtomBonds(
-    const atomId_t _id)
-{
-  const atom * const Walker = const_cast<const World &>(World::getInstance()).
-      getAtom(AtomById(_id));
-  if (Walker != NULL) {
-    const bool atom_present = AtomsinSceneMap.count(_id);
-    const BondList &bondlist = Walker->getListOfBonds();
-    for (BondList::const_iterator bonditer = bondlist.begin();
-        (bonditer != bondlist.end()) && atom_present;
-        ++bonditer) {
-      const bond::ptr _bond = *bonditer;
-      // check if OtherAtom's sphere is already present
-      const atom *OtherAtom = _bond->GetOtherAtom(Walker);
-      const bool otheratom_present = AtomsinSceneMap.count(OtherAtom->getId());
-      if (otheratom_present && atom_present) {
-        const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == Walker) ?
-            GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
-        const GLMoleculeObject_bond::SideOfBond otherside = (_bond->leftatom == Walker) ?
-            GLMoleculeObject_bond::right : GLMoleculeObject_bond::left;
-        addAtomBonds(_bond, side);
-        addAtomBonds(_bond, otherside);
-      }
-    }
-  } else
-    ELOG(1, "GLMoleculeObject_atom disappeared while about to add bonds.");
 }
 
@@ -389,29 +356,4 @@
 }
 
-void GLMoleculeObject_molecule::resetAtoms()
-{
-  const atoms_t atoms = getPresentAtoms();
-  std::vector<atomId_t> InsertedAtoms;
-  std::vector<atomId_t> RemovedAtoms;
-  // obtain all newly inserted and removed atoms
-  std::set_difference(
-      atoms.begin(), atoms.end(),
-      DisplayedAtoms.begin(), DisplayedAtoms.end(),
-      std::back_inserter(InsertedAtoms));
-  std::set_difference(
-      DisplayedAtoms.begin(), DisplayedAtoms.end(),
-      atoms.begin(), atoms.end(),
-      std::back_inserter(RemovedAtoms));
-  // remove the atoms
-  std::for_each(RemovedAtoms.begin(), RemovedAtoms.end(),
-      boost::bind(&GLMoleculeObject_molecule::atomRemoved, this, _1));
-  // insert the atoms
-  std::for_each(InsertedAtoms.begin(), InsertedAtoms.end(),
-      boost::bind(&GLMoleculeObject_molecule::atomInserted, this, _1));
-  DisplayedAtoms = atoms;
-
-  emit changed();
-}
-
 void GLMoleculeObject_molecule::resetIndex()
 {
@@ -479,8 +421,12 @@
 void GLMoleculeObject_molecule::subjectKilled(Observable *publisher)
 {
-  // remove owner: no more signOff needed
-  owner = NULL;
-
-  countsubjectKilled(getMolIndex());
+  ++signedOnChannels;
+
+  if (signedOnChannels == AllsignedOnChannels) {
+    // remove owner: no more signOff needed
+    owner = NULL;
+
+    board->moleculecountsubjectKilled(getMolIndex());
+  }
 }
 
@@ -492,5 +438,5 @@
     return;
   if (publisher == dynamic_cast<const Observable*>(_molecule)){
-    // notofication from atom
+    // notification from atom
 #ifdef LOG_OBSERVER
     observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
@@ -500,28 +446,8 @@
     switch (notification->getChannelNo()) {
       case molecule::AtomInserted:
-      {
-        const atomId_t _id = _molecule->lastChangedAtomId();
-  #ifdef LOG_OBSERVER
-        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
-  #endif
-        emit AtomInserted(_id);
-        emit TesselationHullChanged();
-        emit BoundingBoxChanged();
-        break;
-      }
-      case World::AtomRemoved:
-      {
-//        const atomId_t _id = _molecule->lastChangedAtomId();
-  #ifdef LOG_OBSERVER
-        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been removed.";
-  #endif
-        emit TesselationHullChanged();
-        emit BoundingBoxChanged();
-        break;
-      }
+      case molecule::AtomRemoved:
       case molecule::AtomMoved:
       {
   #ifdef LOG_OBSERVER
-        const atomId_t _id = _molecule->lastChangedAtomId();
         observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
   #endif
@@ -638,5 +564,11 @@
   LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomInserted for atom "+toString(_id)+".");
 
-  GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(GLMoleculeObject::meshSphere, this, _id);
+  GLMoleculeObject_atom *atomObject =
+      new GLMoleculeObject_atom(
+          GLMoleculeObject::meshSphere,
+          this,
+          _id,
+          board,
+          board->getAtomObservedValues(_id));
   ASSERT( atomObject != NULL,
       "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(_id));
@@ -657,12 +589,9 @@
   connect (atomObject, SIGNAL(BondsRemoved(const atomId_t, const atomId_t)), this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
   connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, const atomId_t, const atomId_t)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, const atomId_t, const atomId_t)));
-  connect (atomObject, SIGNAL(InstanceRemoved(const atomId_t)), this, SIGNAL(AtomRemoved(const atomId_t)));
 
   if (m_objectId  == -1)
     setObjectId(_id);
 
-  // add all bonds
-  addAtomBonds(_id);
-
+  emit changed();
   emit changeOccured();
 }
@@ -691,4 +620,5 @@
   delete atomObject;
 
+  emit changed();
   emit changeOccured();
 }
@@ -766,4 +696,5 @@
     iter->second->resetWidth();
   }
+  emit changed();
   emit changeOccured();
 }
@@ -790,4 +721,5 @@
   }
 
+  emit changed();
   emit changeOccured();
 }
@@ -800,4 +732,7 @@
   // then emit onward
   GLMoleculeObject::setVisible(value);
+
+  emit changed();
+  emit changeOccured();
 }
 
@@ -844,12 +779,4 @@
       getMolecule(MoleculeById(_id));
   return mol;
-}
-
-void GLMoleculeObject_molecule::countsubjectKilled(const moleculeId_t)
-{
-  ++subjectKilledCount;
-
-  if (subjectKilledCount > ObservedValues.size())
-    emit InstanceRemoved(getMolIndex());
 }
 
@@ -911,10 +838,4 @@
       _subjectKilled,
       MolIndexGetter);
-  _ObservedValues[PresentAtoms] = new ObservedValue_UpdateAtoms(
-      _molref,
-      "MoleculeAtoms_"+toString(_molid),
-      AtomsChannels,
-      _subjectKilled,
-      MolIndexGetter);
 }
 
@@ -925,5 +846,4 @@
   delete boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(_ObservedValues[MolName]);
   delete boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(_ObservedValues[BoundingBox]);
-  delete boost::any_cast<ObservedValue_UpdateAtoms *>(_ObservedValues[PresentAtoms]);
   _ObservedValues.clear();
 }
@@ -946,4 +866,8 @@
 GLMoleculeObject_molecule::atoms_t GLMoleculeObject_molecule::getPresentAtoms() const
 {
-  return boost::any_cast<ObservedValue_UpdateAtoms *>(ObservedValues[PresentAtoms])->get();
-}
+  atoms_t returnAtomIds;
+  returnAtomIds.insert(
+      MapKeyConstIterator<AtomNodeMap::const_iterator>(const_cast<const AtomNodeMap &>(AtomsinSceneMap).begin()),
+      MapKeyConstIterator<AtomNodeMap::const_iterator>(const_cast<const AtomNodeMap &>(AtomsinSceneMap).end()));
+  return returnAtomIds;
+}
