Ignore:
Timestamp:
Feb 12, 2016, 11:15:24 PM (10 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, Candidate_v1.7.0, Candidate_v1.7.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:
90821d
Parents:
65c323
git-author:
Frederik Heber <heber@…> (10/29/15 12:52:29)
git-committer:
Frederik Heber <heber@…> (02/12/16 23:15:24)
Message:

Extracted all ObservedValue functionality out of GLMoleculeObject_molecule.

  • see previous commit for explanations.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/Qt4/InstanceBoard/QtObservedMolecule.cpp

    r65c323 r494478  
    3636#include "QtObservedMolecule.hpp"
    3737
     38#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
     39
    3840#include "CodePatterns/MemDebug.hpp"
     41#include "CodePatterns/Assert.hpp"
     42#include "CodePatterns/Log.hpp"
     43
     44#include <boost/assign.hpp>
     45#include <boost/bind.hpp>
     46
     47#include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp"
     48#include "UIElements/Qt4/InstanceBoard/ObservedValue_UpdateAtoms.hpp"
     49
     50#include "Atom/atom.hpp"
     51#include "Descriptors/MoleculeIdDescriptor.hpp"
     52#include "World.hpp"
     53
     54using namespace boost::assign;
     55
     56static const Observable::channels_t getAllObservedChannels()
     57{
     58  Observable::channels_t channels;
     59  channels +=
     60      molecule::AtomInserted,
     61      molecule::AtomRemoved,
     62      molecule::IndexChanged,
     63      molecule::MoleculeNameChanged,
     64      molecule::BoundingBoxChanged;
     65  return channels;
     66}
     67
     68// static instances
     69const Observable::channels_t QtObservedMolecule::BoundingBoxChannels(1, molecule::BoundingBoxChanged);
     70const Observable::channels_t QtObservedMolecule::IndexChannels(1, molecule::IndexChanged);
     71const Observable::channels_t QtObservedMolecule::NameChannels(1, molecule::MoleculeNameChanged);
    3972
    4073QtObservedMolecule::QtObservedMolecule(
    41     const ObservedValues_t &_obsvalues,
     74    const ObservedValues_t &_ObservedValues,
     75    QtObservedInstanceBoard &_board,
    4276    QWidget * _parent) :
    4377  QWidget(_parent),
    4478  Observer("QtObservedMolecule"),
    45   obsvalues(_obsvalues)
    46 {}
     79  subjectKilledCount(0),
     80  AllsignedOnChannels(getAllObservedChannels().size()),
     81  signedOffChannels(0),
     82  owner(NULL),
     83  board(_board),
     84  ObservedValues(_ObservedValues)
     85{
     86  activateObserver();
     87}
    4788
    4889QtObservedMolecule::~QtObservedMolecule()
    49 {}
     90{
     91  deactivateObserver();
     92}
     93
     94void QtObservedMolecule::deactivateObserver()
     95{
     96  if (owner != NULL) {
     97    Observable::channels_t channels = getAllObservedChannels();
     98    for (Observable::channels_t::const_iterator iter = channels.begin();
     99        iter != channels.end(); ++iter)
     100      owner->signOff(this, *iter);
     101    owner = NULL;
     102    signedOffChannels = AllsignedOnChannels;
     103  }
     104}
     105
     106void QtObservedMolecule::activateObserver()
     107{
     108  // sign on as observer (obtain non-const instance before)
     109  const molecule * const _molecule = getMolecule(getMolIndex());
     110  if (_molecule != NULL) {
     111    Observable::channels_t channels = getAllObservedChannels();
     112    owner = static_cast<const Observable *>(_molecule);
     113    for (Observable::channels_t::const_iterator iter = channels.begin();
     114        iter != channels.end(); ++iter)
     115      owner->signOn(this, *iter);
     116  } else
     117    signedOffChannels = AllsignedOnChannels;
     118}
    50119
    51120void QtObservedMolecule::update(Observable *publisher)
    52 {}
     121{
     122  ASSERT(0,
     123      "QtObservedMolecule::update() - general update from unexpected source.");
     124}
    53125
    54126void QtObservedMolecule::subjectKilled(Observable *publisher)
    55 {}
     127{
     128  ++signedOffChannels;
     129
     130  if (signedOffChannels == AllsignedOnChannels) {
     131    // remove owner: no more signOff needed
     132    owner = NULL;
     133
     134    board.moleculecountsubjectKilled(getMolIndex());
     135  }
     136}
    56137
    57138void QtObservedMolecule::recieveNotification(Observable *publisher, Notification_ptr notification)
    58 {}
     139{
     140  const molecule * const _molecule = getMolecule(getMolIndex());
     141  // when molecule is NULL we will soon get destroyed anyway
     142  if (_molecule == NULL)
     143    return;
     144  if (publisher == dynamic_cast<const Observable*>(_molecule)){
     145    // notification from atom
     146#ifdef LOG_OBSERVER
     147    observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
     148          << " received notification from molecule " << getMolIndex() << " for channel "
     149          << notification->getChannelNo() << ".";
     150#endif
     151    switch (notification->getChannelNo()) {
     152      case molecule::AtomInserted:
     153      {
     154        const atomId_t _id = _molecule->lastChangedAtomId();
     155        emit atomInserted(_id);
     156        break;
     157      }
     158      case molecule::AtomRemoved:
     159      {
     160        const atomId_t _id = _molecule->lastChangedAtomId();
     161        emit atomRemoved(_id);
     162        break;
     163      }
     164      case molecule::BoundingBoxChanged:
     165      {
     166  #ifdef LOG_OBSERVER
     167        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that bounding box has changed.";
     168  #endif
     169        emit tesselationhullChanged();
     170        emit boundingboxChanged();
     171        break;
     172      }
     173      case molecule::IndexChanged:
     174      {
     175  #ifdef LOG_OBSERVER
     176        const atomId_t _id = _molecule->lastChangedAtomId();
     177        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+"'s index has changed.";
     178  #endif
     179        emit indexChanged();
     180        break;
     181      }
     182      case molecule::MoleculeNameChanged:
     183      {
     184  #ifdef LOG_OBSERVER
     185        observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that name has changed.";
     186  #endif
     187        emit nameChanged();
     188        break;
     189      }
     190      default:
     191        break;
     192    }
     193  }
     194}
     195
     196const molecule * const QtObservedMolecule::getMolecule(const moleculeId_t _id)
     197{
     198  const molecule * const mol = const_cast<const World &>(World::getInstance()).
     199      getMolecule(MoleculeById(_id));
     200  return mol;
     201}
     202
     203static molecule::BoundingBoxInfo initBoundingBox()
     204{
     205  molecule::BoundingBoxInfo info;
     206  info.position = zeroVec;
     207  info.radius = 0.;
     208  return info;
     209}
     210
     211void QtObservedMolecule::initObservedValues(
     212    ObservedValues_t &_ObservedValues,
     213    const moleculeId_t _molid,
     214    const molecule * const _molref,
     215    const boost::function<void(const moleculeId_t)> &_subjectKilled)
     216{
     217  /* This is an old note from when the code was still part of cstor's initializer body.
     218   * TODO: Probably does not apply anymore but has not yet been tested.
     219   *
     220   * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
     221   * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
     222   * the class has not been fully constructed yet. "This" itself seems to be working fine.
     223   */
     224
     225  ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
     226      "QtObservedMolecule::initObservedValues() - given ObservedValues has not correct size.");
     227
     228  // fill ObservedValues: index first
     229  const boost::function<moleculeId_t ()> MolIndexUpdater(
     230      boost::bind(&QtObservedMolecule::updateIndex));
     231
     232  ObservedValue_wCallback<moleculeId_t> * const IndexObservable =
     233      new ObservedValue_wCallback<moleculeId_t>(
     234        _molref,
     235        MolIndexUpdater,
     236        "MoleculeIndex_"+toString(_molid),
     237        _molid,
     238        IndexChannels,
     239        _subjectKilled);
     240  _ObservedValues[MolIndex] = IndexObservable;
     241
     242  const boost::function<const moleculeId_t ()> MolIndexGetter =
     243      boost::bind(&ObservedValue_wCallback<moleculeId_t>::get,
     244          IndexObservable);
     245
     246  // fill ObservedValues: then all the other that need index
     247  const boost::function<std::string ()> MolNameUpdater(
     248      boost::bind(&QtObservedMolecule::updateName, MolIndexGetter));
     249  const boost::function<molecule::BoundingBoxInfo ()> BoundingBoxUpdater(
     250      boost::bind(&QtObservedMolecule::updateBoundingBox, MolIndexGetter));
     251
     252  _ObservedValues[MolName] = new ObservedValue_wCallback<std::string, moleculeId_t>(
     253      _molref,
     254      MolNameUpdater,
     255      "MoleculeName_"+toString(_molid),
     256      MolNameUpdater(),
     257      NameChannels,
     258      _subjectKilled,
     259      MolIndexGetter);
     260  _ObservedValues[BoundingBox] = new ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t>(
     261      _molref,
     262      BoundingBoxUpdater,
     263      "MoleculeBoundingBox_"+toString(_molid),
     264      initBoundingBox(),
     265      BoundingBoxChannels,
     266      _subjectKilled,
     267      MolIndexGetter);
     268}
     269
     270void QtObservedMolecule::destroyObservedValues(
     271    std::vector<boost::any> &_ObservedValues)
     272{
     273  delete boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(_ObservedValues[MolIndex]);
     274  delete boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(_ObservedValues[MolName]);
     275  delete boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(_ObservedValues[BoundingBox]);
     276  _ObservedValues.clear();
     277}
     278
     279molecule::BoundingBoxInfo QtObservedMolecule::updateBoundingBox(
     280    const boost::function<const moleculeId_t ()> &_getMolIndex)
     281{
     282  const molecule * const mol = getMolecule(_getMolIndex());
     283  if (mol != NULL)
     284    return mol->getBoundingBox();
     285  else
     286    return molecule::BoundingBoxInfo();
     287}
     288
     289moleculeId_t QtObservedMolecule::updateIndex()
     290{
     291  return const_cast<const World &>(World::getInstance()).lastChangedMolId();
     292}
     293
     294std::string QtObservedMolecule::updateName(
     295    const boost::function<const moleculeId_t ()> &_getMolIndex)
     296{
     297  const molecule * const mol = getMolecule(_getMolIndex());
     298  if (mol != NULL)
     299    return mol->getName();
     300  else
     301    return std::string("");
     302}
     303
     304moleculeId_t QtObservedMolecule::getMolIndex() const
     305{
     306  return boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(ObservedValues[MolIndex])->get();
     307}
     308
     309std::string QtObservedMolecule::getMolName() const
     310{
     311  return boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(ObservedValues[MolName])->get();
     312}
     313
     314molecule::BoundingBoxInfo QtObservedMolecule::getBoundingBox() const
     315{
     316  return boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(ObservedValues[BoundingBox])->get();
     317}
Note: See TracChangeset for help on using the changeset viewer.