source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp@ 08a7ecc

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
Last change on this file since 08a7ecc was 08a7ecc, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: ObservedValue_Index_t is now the memory address of the Observer instance.

  • the atom, bond, or molecule may be removed before and reinstantiated at the same address. Hence, it is not a good unique index. The only sensible index is the memory address of the QtObserved... instance itself.
  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2015 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * QtObservedAtom.cpp
25 *
26 * Created on: Oct 28, 2015
27 * Author: heber
28 */
29
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "QtObservedAtom.hpp"
37
38#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
39
40#include "CodePatterns/MemDebug.hpp"
41
42#include <boost/assign.hpp>
43
44#include "Atom/atom.hpp"
45#include "Bond/bond.hpp"
46#include "Descriptors/AtomIdDescriptor.hpp"
47#include "Element/element.hpp"
48#include "World.hpp"
49
50#include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp"
51
52using namespace boost::assign;
53
54static const Observable::channels_t getAtomBondsChannels()
55{
56 Observable::channels_t channels;
57 channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
58 return channels;
59}
60
61static const Observable::channels_t getAllObservedChannels()
62{
63 Observable::channels_t channels;
64 channels +=
65 AtomObservable::IndexChanged,
66 AtomObservable::BondsAdded,
67 AtomObservable::BondsRemoved,
68 AtomObservable::MoleculeChanged,
69 AtomObservable::NameChanged,
70 AtomObservable::ElementChanged,
71 AtomObservable::PositionChanged,
72 AtomObservable::SelectionChanged;
73 return channels;
74}
75
76// static entities
77const Observable::channels_t
78QtObservedAtom::AtomIndexChannels(1, AtomObservable::IndexChanged);
79const Observable::channels_t
80QtObservedAtom::AtomBondsChannels(getAtomBondsChannels());
81const Observable::channels_t
82QtObservedAtom::AtomElementChannels(1, AtomObservable::ElementChanged);
83const Observable::channels_t
84QtObservedAtom::MoleculeChangedChannels(1, AtomObservable::MoleculeChanged);
85const Observable::channels_t
86QtObservedAtom::AtomNameChannels(1, AtomObservable::NameChanged);
87const Observable::channels_t
88QtObservedAtom::AtomPositionChannels(1, AtomObservable::PositionChanged);
89const Observable::channels_t
90QtObservedAtom::AtomSelectedChannels(1, AtomObservable::SelectionChanged);
91
92QtObservedAtom::QtObservedAtom(
93 const atomId_t _id,
94 const atom * const _atom,
95 QtObservedInstanceBoard &_board,
96 QWidget * _parent) :
97 QWidget(_parent),
98 Observer("QtObservedAtom"),
99 subjectKilledCount(0),
100 AllsignedOnChannels(getAllObservedChannels().size()),
101 signedOffChannels(0),
102 owner(NULL),
103 oldId(_id),
104 index(static_cast<const ObservedValue_Index_t>(const_cast<const QtObservedAtom * const>(this))),
105 board(_board),
106 BoardIsGone(false),
107 ObservedValues(QtObservedAtom::MAX_ObservedTypes)
108{
109 boost::function<void ()> atomSubjectKilled(
110 boost::bind(&QtObservedAtom::countValuesSubjectKilled,
111 boost::ref(*this),
112 boost::bind(&QtObservedAtom::getIndex, boost::ref(*this))));
113 initObservedValues( ObservedValues, _id, _atom, atomSubjectKilled);
114
115 // activating Observer is done by ObservedValueContainer when it's inserted
116}
117
118QtObservedAtom::~QtObservedAtom()
119{
120 boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(ObservedValues[AtomIndex])->noteCallBackIsGone();
121 boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(ObservedValues[AtomBonds])->noteCallBackIsGone();
122 boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(ObservedValues[AtomElement])->noteCallBackIsGone();
123 boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(ObservedValues[MoleculeRef])->noteCallBackIsGone();
124 boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(ObservedValues[AtomName])->noteCallBackIsGone();
125 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomPosition])->noteCallBackIsGone();
126 boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(ObservedValues[AtomSelected])->noteCallBackIsGone();
127
128 deactivateObserver();
129
130 destroyObservedValues(ObservedValues);
131}
132
133const atom * const QtObservedAtom::getAtomConst(const atomId_t _id)
134{
135 const atom * const _atom = const_cast<const World &>(World::getInstance()).
136 getAtom(AtomById(_id));
137 return _atom;
138}
139
140atom * const QtObservedAtom::getAtom(const atomId_t _id)
141{
142 atom * const _atom = World::getInstance().getAtom(AtomById(_id));
143 return _atom;
144}
145
146#ifdef HAVE_INLINE
147inline
148#endif
149atomId_t QtObservedAtom::updateIndex(const atom &_atomref)
150{
151 return _atomref.getId();
152}
153
154QtObservedAtom::ListOfBonds_t QtObservedAtom::updateBonds(
155 const atom &_atom)
156{
157 ListOfBonds_t ListOfBonds;
158 // make sure bonds is up-to-date
159 const BondList ListBonds = _atom.getListOfBonds();
160 for (BondList::const_iterator iter = ListBonds.begin();
161 iter != ListBonds.end();
162 ++iter)
163 ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
164 (*iter)->leftatom->getId(),
165 (*iter)->rightatom->getId()) );
166 return ListOfBonds;
167}
168
169#ifdef HAVE_INLINE
170inline
171#endif
172atomicNumber_t QtObservedAtom::updateElement(
173 const atom &_atom)
174{
175 return _atom.getElementNo();
176}
177
178#ifdef HAVE_INLINE
179inline
180#endif
181QtObservedMolecule* QtObservedAtom::updateMoleculeRef(
182 const atom &_atom)
183{
184 if (_atom.getMolecule() != NULL) {
185 const moleculeId_t molid = _atom.getMolecule()->getId();
186 const QtObservedMolecule::ptr mol = board.getObservedMolecule(molid);
187 ASSERT(mol,
188 "QtObservedAtom::updateMoleculeRef() - could not update the ref, "
189 +toString(molid)+" unknown to Instance board?");
190 return mol.get();
191 } else {
192 return (QtObservedMolecule*)NULL;
193 }
194}
195
196#ifdef HAVE_INLINE
197inline
198#endif
199std::string QtObservedAtom::updateName(
200 const atom &_atom)
201{
202 return _atom.getName();
203}
204
205#ifdef HAVE_INLINE
206inline
207#endif
208Vector QtObservedAtom::updatePosition(
209 const atom &_atom)
210{
211 return _atom.getPosition();
212}
213
214#ifdef HAVE_INLINE
215inline
216#endif
217bool QtObservedAtom::updateSelected(
218 const atom &_atom)
219{
220 return _atom.getSelected();
221}
222
223void QtObservedAtom::update(Observable *publisher)
224{
225 ASSERT(0, "QtObservedAtom::update() - we are not signed on for global updates.");
226}
227
228void QtObservedAtom::subjectKilled(Observable *publisher)
229{
230 ++signedOffChannels;
231
232 checkForRemoval(getIndex());
233}
234
235void QtObservedAtom::countValuesSubjectKilled(ObservedValue_Index_t _id)
236{
237 ASSERT( _id == getIndex(),
238 "QtObservedAtom::countValuesSubjectKilled() - atom "+toString(getIndex())
239 +" received countValuesSubjectKilled for atom id "+toString(_id)+".");
240
241 ++subjectKilledCount;
242
243 checkForRemoval(_id);
244}
245
246#ifdef HAVE_INLINE
247inline
248#endif
249void QtObservedAtom::checkForRemoval(ObservedValue_Index_t _id)
250{
251 if ((signedOffChannels == AllsignedOnChannels) && (subjectKilledCount == MAX_ObservedTypes)) {
252 // remove owner: no more signOff needed
253 owner = NULL;
254
255 emit atomRemoved();
256
257 if (!BoardIsGone) {
258 board.markObservedAtomAsDisconnected(_id);
259 board.markObservedAtomForErase(_id);
260 }
261 }
262}
263
264void QtObservedAtom::recieveNotification(Observable *publisher, Notification_ptr notification)
265{
266 // ObservedValues have been updated before, hence convert updates to Qt's signals
267 switch (notification->getChannelNo()) {
268 case AtomObservable::IndexChanged:
269 {
270 const atomId_t newId = getAtomIndex();
271 emit indexChanged(oldId, newId);
272 oldId = newId;
273 break;
274 }
275 case AtomObservable::BondsAdded:
276 case AtomObservable::BondsRemoved:
277 emit bondsChanged();
278 break;
279 case AtomObservable::ElementChanged:
280 emit elementChanged();
281 break;
282 case AtomObservable::MoleculeChanged:
283 emit moleculeChanged();
284 break;
285 case AtomObservable::NameChanged:
286 emit nameChanged();
287 break;
288 case AtomObservable::PositionChanged:
289 emit positionChanged();
290 break;
291 case AtomObservable::SelectionChanged:
292 emit selectedChanged();
293 break;
294 default:
295 ASSERT(0, "QtObservedAtom::recieveNotification() - we are not signed on to channel "
296 +toString(notification->getChannelNo())+" of the atom.");
297 break;
298 }
299}
300
301void QtObservedAtom::activateObserver()
302{
303 atom * atomref = getAtom(getAtomIndex());
304 if (atomref != NULL) {
305 Observable::channels_t channels = getAllObservedChannels();
306 owner = static_cast<const Observable *>(atomref);
307 for (Observable::channels_t::const_iterator iter = channels.begin();
308 iter != channels.end(); ++iter)
309 owner->signOn(this, *iter);
310 if (!BoardIsGone)
311 board.markObservedAtomAsConnected(getIndex());
312 } else
313 signedOffChannels = AllsignedOnChannels;
314}
315
316void QtObservedAtom::deactivateObserver()
317{
318 // sign Off
319 if (owner != NULL) {
320 Observable::channels_t channels = getAllObservedChannels();
321 for (Observable::channels_t::const_iterator iter = channels.begin();
322 iter != channels.end(); ++iter)
323 owner->signOff(this, *iter);
324 owner = NULL;
325 signedOffChannels = AllsignedOnChannels;
326 if (!BoardIsGone)
327 board.markObservedAtomAsDisconnected(getIndex());
328 }
329}
330
331void QtObservedAtom::initObservedValues(
332 ObservedValues_t &_ObservedValues,
333 const atomId_t _id,
334 const atom * const _atomref,
335 const boost::function<void()> &_subjectKilled)
336{
337 ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
338 "QtObservedAtom::initObservedValues() - given ObservedValues has not correct size.");
339
340 // fill ObservedValues: then all the other that need index
341 const boost::function<atomId_t ()> AtomIndexUpdater(
342 boost::bind(&QtObservedAtom::updateIndex, boost::cref(*_atomref)));
343 const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
344 boost::bind(&QtObservedAtom::updateBonds, boost::cref(*_atomref)));
345 const boost::function<atomicNumber_t ()> AtomElementUpdater(
346 boost::bind(&QtObservedAtom::updateElement, boost::cref(*_atomref)));
347 const boost::function<std::string ()> AtomNameUpdater(
348 boost::bind(&QtObservedAtom::updateName, boost::cref(*_atomref)));
349 const boost::function<Vector ()> AtomPositionUpdater(
350 boost::bind(&QtObservedAtom::updatePosition, boost::cref(*_atomref)));
351 const boost::function<bool ()> AtomSelectedUpdater(
352 boost::bind(&QtObservedAtom::updateSelected, boost::cref(*_atomref)));
353 const boost::function<QtObservedMolecule* ()> MoleculeRefUpdater(
354 boost::bind(&QtObservedAtom::updateMoleculeRef, this, boost::cref(*_atomref)));
355
356 _ObservedValues[AtomIndex] = new ObservedValue_wCallback<atomId_t, ObservedValue_Index_t>(
357 _atomref,
358 AtomIndexUpdater,
359 "AtomIndex_"+toString(_id),
360 _id,
361 AtomIndexChannels,
362 _subjectKilled);
363 _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t>(
364 _atomref,
365 AtomBondsUpdater,
366 "AtomBonds_"+toString(_id),
367 AtomBondsUpdater(),
368 AtomBondsChannels,
369 _subjectKilled);
370 _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t>(
371 _atomref,
372 AtomElementUpdater,
373 "AtomElement"+toString(_id),
374 AtomElementUpdater(),
375 AtomElementChannels,
376 _subjectKilled);
377 _ObservedValues[AtomName] = new ObservedValue_wCallback<std::string, ObservedValue_Index_t>(
378 _atomref,
379 AtomNameUpdater,
380 "AtomName"+toString(_id),
381 AtomNameUpdater(),
382 AtomNameChannels,
383 _subjectKilled);
384 _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
385 _atomref,
386 AtomPositionUpdater,
387 "AtomPosition_"+toString(_id),
388 AtomPositionUpdater(),
389 AtomPositionChannels,
390 _subjectKilled);
391 _ObservedValues[AtomSelected] = new ObservedValue_wCallback<bool, ObservedValue_Index_t>(
392 _atomref,
393 AtomSelectedUpdater,
394 "AtomSelected_"+toString(_id),
395 AtomSelectedUpdater(),
396 AtomSelectedChannels,
397 _subjectKilled);
398 _ObservedValues[MoleculeRef] = new ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t>(
399 _atomref,
400 MoleculeRefUpdater,
401 "AtomMoleculeIndex"+toString(_id),
402 MoleculeRefUpdater(),
403 MoleculeChangedChannels,
404 _subjectKilled);
405}
406
407void QtObservedAtom::destroyObservedValues(
408 std::vector<boost::any> &_ObservedValues)
409{
410 delete boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(_ObservedValues[AtomIndex]);
411 delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(_ObservedValues[AtomBonds]);
412 delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(_ObservedValues[AtomElement]);
413 delete boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(_ObservedValues[AtomName]);
414 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomPosition]);
415 delete boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(_ObservedValues[AtomSelected]);
416 delete boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(_ObservedValues[MoleculeRef]);
417 _ObservedValues.clear();
418}
419
420ObservedValue_Index_t QtObservedAtom::getIndex() const
421{
422 ASSERT( index != NULL,
423 "QtObservedAtom::getIndex() - index is NULL");
424 return index;
425}
426
427const atomId_t& QtObservedAtom::getAtomIndex() const
428{
429 return boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(ObservedValues[AtomIndex])->get();
430}
431
432const QtObservedAtom::ListOfBonds_t& QtObservedAtom::getAtomBonds() const
433{
434 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(ObservedValues[AtomBonds])->get();
435}
436
437const atomicNumber_t& QtObservedAtom::getAtomElement() const
438{
439 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(ObservedValues[AtomElement])->get();
440}
441
442const std::string& QtObservedAtom::getAtomName() const
443{
444 return boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(ObservedValues[AtomName])->get();
445}
446
447const Vector& QtObservedAtom::getAtomPosition() const
448{
449 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomPosition])->get();
450}
451
452const bool QtObservedAtom::getAtomSelected() const
453{
454 return boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(ObservedValues[AtomSelected])->get();
455}
456
457const QtObservedMolecule* const QtObservedAtom::getMoleculeRef() const
458{
459 return boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(ObservedValues[MoleculeRef])->get();
460}
Note: See TracBrowser for help on using the repository browser.