Ignore:
Timestamp:
Feb 12, 2016, 11:15:23 PM (9 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
494478
Parents:
41e287
git-author:
Frederik Heber <heber@…> (10/29/15 10:46:45)
git-committer:
Frederik Heber <heber@…> (02/12/16 23:15:23)
Message:

Extracted all ObservedValue functionality out of GLMoleculeObject_atom.

  • i.e. GLMoleculeObject_atom is reduced to simple displaying, updated via Qt signals. These signals are connected to a QtObservedAtom which in turn now contains all the ObservedValue functionality.
  • this is preparatory for having a single (Observed)Atom as representer of its associated World's atom in the QtGui part of the code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.cpp

    r41e287 r65c323  
    4545
    4646#include <algorithm>
    47 #include <boost/assign.hpp>
    4847
    4948#include "Atom/atom.hpp"
    5049#include "Bond/bond.hpp"
    51 #include "Descriptors/AtomIdDescriptor.hpp"
    5250#include "Element/element.hpp"
    5351#include "Element/periodentafel.hpp"
     
    5654#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
    5755#include "World.hpp"
    58 #include "WorldTime.hpp"
    59 
    60 #include "ObservedValue_wCallback.hpp"
    61 
    62 using namespace boost::assign;
    63 
    64 static const Observable::channels_t getAtomBondsChannels()
    65 {
    66   Observable::channels_t channels;
    67   channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
    68   return channels;
    69 }
    70 
    71 static const Observable::channels_t getAllObservedChannels()
    72 {
    73   Observable::channels_t channels;
    74   channels +=
    75       AtomObservable::IndexChanged,
    76       AtomObservable::PositionChanged,
    77       AtomObservable::ElementChanged,
    78       AtomObservable::BondsAdded,
    79       AtomObservable::BondsRemoved;
    80   return channels;
    81 }
    82 
    83 // static entities
    84 const Observable::channels_t
    85 GLMoleculeObject_atom::AtomIndexChannels(1, AtomObservable::IndexChanged);
    86 const Observable::channels_t
    87 GLMoleculeObject_atom::AtomPositionChannels(1, AtomObservable::PositionChanged);
    88 const Observable::channels_t
    89 GLMoleculeObject_atom::AtomElementChannels(1, AtomObservable::ElementChanged);
    90 const Observable::channels_t
    91 GLMoleculeObject_atom::AtomBondsChannels(getAtomBondsChannels());
    9256
    9357GLMoleculeObject_atom::GLMoleculeObject_atom(
    9458    QGLSceneNode *mesh[],
    9559    QObject *parent,
    96     const atomId_t _id,
    97     QtObservedInstanceBoard * _board,
    98     std::vector<boost::any> _ObservedValues) :
     60    QtObservedAtom::ptr _ObservedAtom) :
    9961  GLMoleculeObject(mesh, parent),
    100   Observer(std::string("GLMoleculeObject_atom")+toString(_id)),
    101   atomref(getAtom(_id)),
    102   ObservedValues(_ObservedValues),
    103   subjectKilledCount(0),
    104   owner(NULL),
    105   AllsignedOnChannels(getAllObservedChannels().size()),
    106   signedOnChannels(0),
    107   board(_board)
     62  ObservedAtom(_ObservedAtom)
    10863{
    109   init(_id);
     64  init(ObservedAtom->getAtomIndex());
    11065}
    11166
     
    11873  m_selected = const_cast<const World &>(World::getInstance()).isAtomSelected(_id);
    11974
    120   // sign On
    121   activateObserver();
    122 
    123   // atomref is only used for caching the ref, it must be used elswhere
    124   const_cast<atom *&>(atomref) = NULL;
    125 
    12675  connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
    127   connect( this, SIGNAL(idChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
    128   connect( this, SIGNAL(elementChanged()), this, SLOT(resetElement()), Qt::QueuedConnection);
    129   connect( this, SIGNAL(positionChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
    130   connect( this, SIGNAL(bondsChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
     76  connect( ObservedAtom.get(), SIGNAL(indexChanged()), this, SLOT(resetIndex()));
     77  connect( ObservedAtom.get(), SIGNAL(elementChanged()), this, SLOT(resetElement()));
     78  connect( ObservedAtom.get(), SIGNAL(positionChanged()), this, SLOT(resetPosition()));
     79  connect( ObservedAtom.get(), SIGNAL(bondsChanged()), this, SLOT(resetPosition()));
    13180
    13281  // use that ObservedValues::AtomBonds is always up-to-date
     
    13483}
    13584
    136 void GLMoleculeObject_atom::activateObserver()
    137 {
    138   if (atomref != NULL) {
    139     Observable::channels_t channels = getAllObservedChannels();
    140     owner = static_cast<const Observable *>(atomref);
    141     for (Observable::channels_t::const_iterator iter = channels.begin();
    142         iter != channels.end(); ++iter)
    143       owner->signOn(this, *iter);
    144   }
    145 }
    146 
    147 
    148 void GLMoleculeObject_atom::deactivateObserver()
    149 {
    150   // sign Off
    151   if (owner != NULL) {
    152     Observable::channels_t channels = getAllObservedChannels();
    153     for (Observable::channels_t::const_iterator iter = channels.begin();
    154         iter != channels.end(); ++iter)
    155       owner->signOff(this, *iter);
    156     owner = NULL;
    157     signedOnChannels = 0;
    158   }
    159 }
    160 
    16185GLMoleculeObject_atom::~GLMoleculeObject_atom()
    162 {
    163   deactivateObserver();
    164   board->returnAtomObservedValues(getAtomIndex(), ObservedValues);
    165 }
     86{}
    16687
    16788void GLMoleculeObject_atom::resetIndex()
    16889{
    169   const atomId_t newId = getAtomIndex();
     90  const atomId_t newId = ObservedAtom->getAtomIndex();
    17091  const size_t oldId = objectId();
    17192  ASSERT( newId != oldId,
     
    17394  LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(newId)+".");
    17495  setObjectId(newId);
    175 
    176   emit indexChanged(this, oldId, newId);
    17796}
    17897
    17998void GLMoleculeObject_atom::resetPosition()
    18099{
    181   const Vector Position = getAtomPosition();
     100  const Vector Position = ObservedAtom->getAtomPosition();
    182101  LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
    183102  setPosition(QVector3D(Position[0], Position[1], Position[2]));
     
    188107  size_t elementno = 0;
    189108  const element * const _type = World::getInstance().
    190       getPeriode()->FindElement(getAtomElement());
     109      getPeriode()->FindElement(ObservedAtom->getAtomElement());
    191110  if (_type != NULL) {
    192111    elementno = _type->getAtomicNumber();
     
    214133void GLMoleculeObject_atom::resetBonds()
    215134{
    216   ListOfBonds_t ListOfBonds_new = getAtomBonds();
     135  QtObservedAtom::ListOfBonds_t ListOfBonds_new = ObservedAtom->getAtomBonds();
    217136  std::sort(ListOfBonds_new.begin(), ListOfBonds_new.end());
    218   ListOfBonds_t BondsToAdd;
     137  QtObservedAtom::ListOfBonds_t BondsToAdd;
    219138  std::set_difference(
    220139      ListOfBonds_new.begin(), ListOfBonds_new.end(),
    221140      ListOfBonds.begin(), ListOfBonds.end(),
    222141      std::back_inserter(BondsToAdd));
    223   ListOfBonds_t BondsToRemove;
     142  QtObservedAtom::ListOfBonds_t BondsToRemove;
    224143  std::set_difference(
    225144      ListOfBonds.begin(), ListOfBonds.end(),
    226145      ListOfBonds_new.begin(), ListOfBonds_new.end(),
    227146      std::back_inserter(BondsToRemove));
    228   for (ListOfBonds_t::const_iterator iter = BondsToAdd.begin();
     147  for (QtObservedAtom::ListOfBonds_t::const_iterator iter = BondsToAdd.begin();
    229148      iter != BondsToAdd.end();
    230149      ++iter) {
    231     const GLMoleculeObject_bond::SideOfBond side = (iter->first == getAtomIndex()) ?
     150    const GLMoleculeObject_bond::SideOfBond side = (iter->first == ObservedAtom->getAtomIndex()) ?
    232151        GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
    233152    emit BondsAdded(iter->first, iter->second, side);
    234153  }
    235   for (ListOfBonds_t::const_iterator iter = BondsToRemove.begin();
     154  for (QtObservedAtom::ListOfBonds_t::const_iterator iter = BondsToRemove.begin();
    236155      iter != BondsToRemove.end();
    237156      ++iter) {
     
    268187void GLMoleculeObject_atom::wasClicked()
    269188{
    270   LOG(4, "INFO: GLMoleculeObject_atom: atom " << getAtomIndex() << " has been clicked");
    271   emit clicked(getAtomIndex());
     189  LOG(4, "INFO: GLMoleculeObject_atom: atom "
     190      << ObservedAtom->getAtomIndex() << " has been clicked");
     191  emit clicked(ObservedAtom->getAtomIndex());
    272192}
    273 
    274 const atom * const GLMoleculeObject_atom::getAtomConst(const atomId_t _id)
    275 {
    276   const atom * const _atom = const_cast<const World &>(World::getInstance()).
    277       getAtom(AtomById(_id));
    278   return _atom;
    279 }
    280 
    281 atom * const GLMoleculeObject_atom::getAtom(const atomId_t _id)
    282 {
    283   atom * const _atom = World::getInstance().getAtom(AtomById(_id));
    284   return _atom;
    285 }
    286 
    287 atomId_t GLMoleculeObject_atom::updateIndex()
    288 {
    289   return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
    290 }
    291 
    292 Vector GLMoleculeObject_atom::updatePosition(
    293     const boost::function<const atomId_t ()> &_getAtomIndex)
    294 {
    295   const atom * const _atom = getAtomConst(_getAtomIndex());
    296   if (_atom != NULL) {
    297     return _atom->getPosition();
    298   } else {
    299     return zeroVec;
    300   }
    301 }
    302 
    303 atomicNumber_t GLMoleculeObject_atom::updateElement(
    304     const boost::function<const atomId_t ()> &_getAtomIndex)
    305 {
    306   const atom * const _atom = getAtomConst(_getAtomIndex());
    307   if (_atom != NULL) {
    308     return _atom->getElementNo();
    309   } else {
    310     return (atomicNumber_t)-1;
    311   }
    312 }
    313 
    314 GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::updateBonds(
    315     const boost::function<const atomId_t ()> &_getAtomIndex)
    316 {
    317   ListOfBonds_t ListOfBonds;
    318   const atom * const _atom = getAtomConst(_getAtomIndex());
    319   if (_atom != NULL) {
    320     // make sure bonds is up-to-date
    321     const BondList ListBonds = _atom->getListOfBonds();
    322     for (BondList::const_iterator iter = ListBonds.begin();
    323         iter != ListBonds.end();
    324         ++iter)
    325       ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
    326           (*iter)->leftatom->getId(),
    327           (*iter)->rightatom->getId()) );
    328   } else {
    329     ELOG(2, "Atom with id "+toString(_getAtomIndex())+" is already gone.");
    330   }
    331   return ListOfBonds;
    332 }
    333 
    334 void GLMoleculeObject_atom::update(Observable *publisher)
    335 {
    336   ASSERT(0, "GLMoleculeObject_atom::update() - we are not signed on for global updates.");
    337 }
    338 
    339 void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
    340 {
    341   ++signedOnChannels;
    342 
    343   if (signedOnChannels == AllsignedOnChannels) {
    344     // remove owner: no more signOff needed
    345     owner = NULL;
    346 
    347     board->atomcountsubjectKilled(getAtomIndex());
    348   }
    349 }
    350 
    351 void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
    352 {
    353   // ObservedValues have been updated before, hence convert updates to Qt's signals
    354   atom * const _atom = dynamic_cast<atom *>(publisher);
    355   if (_atom != NULL) {
    356     switch (notification->getChannelNo()) {
    357       case AtomObservable::IndexChanged:
    358         emit idChanged();
    359         break;
    360       case AtomObservable::PositionChanged:
    361         emit positionChanged();
    362         break;
    363       case AtomObservable::ElementChanged:
    364         emit elementChanged();
    365         break;
    366       case AtomObservable::BondsAdded:
    367       case AtomObservable::BondsRemoved:
    368         emit bondsChanged();
    369         break;
    370       default:
    371         ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - we are not signed on to channel "
    372             +toString(notification->getChannelNo())+" of the atom.");
    373         break;
    374     }
    375   } else
    376     ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - received notification from unexpected source.");
    377 }
    378 
    379 void GLMoleculeObject_atom::initObservedValues(
    380     std::vector<boost::any> &_ObservedValues,
    381     const atomId_t _id,
    382     const atom * const _atomref,
    383     const boost::function<void(const atomId_t)> &_subjectKilled)
    384 {
    385   /* This is an old note from when the code was still part of cstor's initializer body.
    386    * TODO: Probably does not apply anymore but has not yet been tested.
    387    *
    388    * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
    389    * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
    390    * the class has not been fully constructed yet. "This" itself seems to be working fine.
    391    */
    392 
    393   ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
    394       "GLMoleculeObject_atom::initObservedValues() - given ObservedValues has not correct size.");
    395 
    396   // fill ObservedValues: index first
    397   const boost::function<atomId_t ()> AtomIndexUpdater(
    398       boost::bind(&GLMoleculeObject_atom::updateIndex));
    399 
    400   ObservedValue_wCallback<atomId_t> * const IndexObservable =
    401       new ObservedValue_wCallback<atomId_t>(
    402           _atomref,
    403           boost::bind(&GLMoleculeObject_atom::updateIndex),
    404           "AtomIndex_"+toString(_id),
    405           _id,
    406           AtomIndexChannels,
    407           _subjectKilled);
    408   _ObservedValues[AtomIndex] = IndexObservable;
    409 
    410   const boost::function<const atomId_t ()> AtomIndexGetter =
    411       boost::bind(&ObservedValue_wCallback<atomId_t>::get,
    412           IndexObservable);
    413 
    414   // fill ObservedValues: then all the other that need index
    415   const boost::function<Vector ()> AtomPositionUpdater(
    416       boost::bind(&GLMoleculeObject_atom::updatePosition, AtomIndexGetter));
    417   const boost::function<atomicNumber_t ()> AtomElementUpdater(
    418       boost::bind(&GLMoleculeObject_atom::updateElement, AtomIndexGetter));
    419   const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
    420       boost::bind(&GLMoleculeObject_atom::updateBonds, AtomIndexGetter));
    421 
    422   _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, atomId_t>(
    423       _atomref,
    424       AtomPositionUpdater,
    425       "AtomPosition_"+toString(_id),
    426       AtomPositionUpdater(),
    427       AtomPositionChannels,
    428       _subjectKilled,
    429       AtomIndexGetter);
    430   _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, atomId_t>(
    431       _atomref,
    432       AtomElementUpdater,
    433       "AtomElement"+toString(_id),
    434       AtomElementUpdater(),
    435       AtomElementChannels,
    436       _subjectKilled,
    437       AtomIndexGetter);
    438   _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, atomId_t>(
    439       _atomref,
    440       AtomBondsUpdater,
    441       "AtomBonds_"+toString(_id),
    442       AtomBondsUpdater(),
    443       AtomBondsChannels,
    444       _subjectKilled,
    445       AtomIndexGetter);
    446 }
    447 
    448 void GLMoleculeObject_atom::destroyObservedValues(
    449     std::vector<boost::any> &_ObservedValues)
    450 {
    451   delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[AtomIndex]);
    452   delete boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(_ObservedValues[AtomPosition]);
    453   delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(_ObservedValues[AtomElement]);
    454   delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(_ObservedValues[AtomBonds]);
    455   _ObservedValues.clear();
    456 }
    457 
    458 atomId_t GLMoleculeObject_atom::getAtomIndex() const
    459 {
    460   return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->get();
    461 }
    462 
    463 Vector GLMoleculeObject_atom::getAtomPosition() const
    464 {
    465   return boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[AtomPosition])->get();
    466 }
    467 
    468 atomicNumber_t GLMoleculeObject_atom::getAtomElement() const
    469 {
    470   return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[AtomElement])->get();
    471 }
    472 
    473 GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::getAtomBonds() const
    474 {
    475   return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(ObservedValues[AtomBonds])->get();
    476 }
Note: See TracChangeset for help on using the changeset viewer.