source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp

Candidate_v1.7.1 stable v1.7.1
Last change on this file was 29ba9a, checked in by Frederik Heber <frederik.heber@…>, 2 weeks ago

FIX: QtObservedAtom avoids using atomRef on first subjectKilled.

  • Property mode set to 100644
File size: 19.0 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#include "WorldTime.hpp"
50
51#include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp"
52
53using namespace boost::assign;
54
55static const Observable::channels_t getAtomBondsChannels()
56{
57 Observable::channels_t channels;
58 channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
59 return channels;
60}
61
62static const Observable::channels_t getAllObservedChannels()
63{
64 Observable::channels_t channels;
65 channels +=
66 AtomObservable::IndexChanged,
67 AtomObservable::BondsAdded,
68 AtomObservable::BondsRemoved,
69 AtomObservable::MoleculeChanged,
70 AtomObservable::NameChanged,
71 AtomObservable::ElementChanged,
72 AtomObservable::PositionChanged,
73 AtomObservable::SelectionChanged;
74 return channels;
75}
76
77// static entities
78const Observable::channels_t
79QtObservedAtom::AtomIndexChannels(1, AtomObservable::IndexChanged);
80const Observable::channels_t
81QtObservedAtom::AtomBondsChannels(getAtomBondsChannels());
82const Observable::channels_t
83QtObservedAtom::AtomElementChannels(1, AtomObservable::ElementChanged);
84const Observable::channels_t
85QtObservedAtom::MoleculeChangedChannels(1, AtomObservable::MoleculeChanged);
86const Observable::channels_t
87QtObservedAtom::AtomNameChannels(1, AtomObservable::NameChanged);
88const Observable::channels_t
89QtObservedAtom::AtomPositionChannels(1, AtomObservable::PositionChanged);
90const Observable::channels_t
91QtObservedAtom::AtomVelocityChannels(1, AtomObservable::VelocityChanged);
92const Observable::channels_t
93QtObservedAtom::AtomForceChannels(1, AtomObservable::ForceChanged);
94const Observable::channels_t
95QtObservedAtom::AtomSelectedChannels(1, AtomObservable::SelectionChanged);
96
97QtObservedAtom::QtObservedAtom(
98 const atomId_t _id,
99 const atom * const _atom,
100 QtObservedInstanceBoard &_board,
101 QWidget * _parent) :
102 QWidget(_parent),
103 Observer("QtObservedAtom"),
104 subjectKilledCount(0),
105 AllsignedOnChannels(getAllObservedChannels().size()),
106 signedOffChannels(0),
107 owner(NULL),
108 index(static_cast<const ObservedValue_Index_t>(const_cast<const QtObservedAtom * const>(this))),
109 board(_board),
110 BoardIsGone(false),
111 ObservedValues(QtObservedAtom::MAX_ObservedTypes)
112{
113 boost::function<void ()> atomSubjectKilled(
114 boost::bind(&QtObservedAtom::countValuesSubjectKilled,
115 boost::ref(*this),
116 boost::bind(&QtObservedAtom::getIndex, boost::ref(*this))));
117 initObservedValues( ObservedValues, _id, _atom, atomSubjectKilled);
118
119 // activating Observer is done by ObservedValueContainer when it's inserted
120}
121
122QtObservedAtom::~QtObservedAtom()
123{
124 boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(ObservedValues[AtomIndex])->noteCallBackIsGone();
125 boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(ObservedValues[AtomBonds])->noteCallBackIsGone();
126 boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(ObservedValues[AtomElement])->noteCallBackIsGone();
127 boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(ObservedValues[MoleculeRef])->noteCallBackIsGone();
128 boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(ObservedValues[AtomName])->noteCallBackIsGone();
129 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomPosition])->noteCallBackIsGone();
130 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomVelocity])->noteCallBackIsGone();
131 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomForce])->noteCallBackIsGone();
132 boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(ObservedValues[AtomSelected])->noteCallBackIsGone();
133
134 deactivateObserver();
135
136 destroyObservedValues(ObservedValues);
137}
138
139const atom * const QtObservedAtom::getAtomConst(const atomId_t _id)
140{
141 const atom * const _atom = const_cast<const World &>(World::getInstance()).
142 getAtom(AtomById(_id));
143 return _atom;
144}
145
146atom * const QtObservedAtom::getAtom(const atomId_t _id)
147{
148 atom * const _atom = World::getInstance().getAtom(AtomById(_id));
149 return _atom;
150}
151
152#ifdef HAVE_INLINE
153inline
154#endif
155atomId_t QtObservedAtom::updateIndex(const atom &_atomref)
156{
157 return _atomref.getId();
158}
159
160QtObservedAtom::ListOfBonds_t QtObservedAtom::updateBonds(
161 const atom &_atom)
162{
163 ListOfBonds_t ListOfBonds;
164 // make sure bonds is up-to-date
165 const BondList ListBonds = _atom.getListOfBonds();
166 for (BondList::const_iterator iter = ListBonds.begin();
167 iter != ListBonds.end();
168 ++iter)
169 ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
170 (*iter)->leftatom->getId(),
171 (*iter)->rightatom->getId()) );
172 return ListOfBonds;
173}
174
175#ifdef HAVE_INLINE
176inline
177#endif
178atomicNumber_t QtObservedAtom::updateElement(
179 const atom &_atom)
180{
181 return _atom.getElementNo();
182}
183
184#ifdef HAVE_INLINE
185inline
186#endif
187QtObservedMolecule* QtObservedAtom::updateMoleculeRef(
188 const atom &_atom)
189{
190 if (_atom.getMolecule() != NULL) {
191 const moleculeId_t molid = _atom.getMolecule()->getId();
192 const QtObservedMolecule::ptr mol = board.getObservedMolecule(molid);
193 ASSERT(mol,
194 "QtObservedAtom::updateMoleculeRef() - could not update the ref, "
195 +toString(molid)+" unknown to Instance board?");
196 return mol.get();
197 } else {
198 return (QtObservedMolecule*)NULL;
199 }
200}
201
202#ifdef HAVE_INLINE
203inline
204#endif
205std::string QtObservedAtom::updateName(
206 const atom &_atom)
207{
208 return _atom.getName();
209}
210
211#ifdef HAVE_INLINE
212inline
213#endif
214Vector QtObservedAtom::updatePosition(
215 const atom &_atom)
216{
217 return _atom.getPosition();
218}
219
220#ifdef HAVE_INLINE
221inline
222#endif
223Vector QtObservedAtom::updateVelocity(
224 const atom &_atom)
225{
226 return _atom.getAtomicVelocity();
227}
228
229#ifdef HAVE_INLINE
230inline
231#endif
232Vector QtObservedAtom::updateForce(
233 const atom &_atom)
234{
235 return _atom.getAtomicForce();
236}
237
238#ifdef HAVE_INLINE
239inline
240#endif
241bool QtObservedAtom::updateSelected(
242 const atom &_atom)
243{
244 return _atom.getSelected();
245}
246
247void QtObservedAtom::update(Observable *publisher)
248{
249 ASSERT(0, "QtObservedAtom::update() - we are not signed on for global updates.");
250}
251
252void QtObservedAtom::subjectKilled(Observable *publisher)
253{
254 ++signedOffChannels;
255
256 checkForRemoval(getIndex());
257}
258
259void QtObservedAtom::countValuesSubjectKilled(ObservedValue_Index_t _id)
260{
261 ASSERT( _id == getIndex(),
262 "QtObservedAtom::countValuesSubjectKilled() - atom "+toString(getIndex())
263 +" received countValuesSubjectKilled for atom id "+toString(_id)+".");
264
265 ++subjectKilledCount;
266
267 checkForRemoval(_id);
268}
269
270#ifdef HAVE_INLINE
271inline
272#endif
273void QtObservedAtom::checkForRemoval(ObservedValue_Index_t _id)
274{
275 if ((signedOffChannels == AllsignedOnChannels) && (subjectKilledCount == MAX_ObservedTypes)) {
276 // remove owner: no more signOff needed
277 owner = NULL;
278
279 emit atomRemoved();
280
281 if (!BoardIsGone) {
282 board.markObservedAtomAsDisconnected(_id);
283 board.markObservedAtomForErase(_id);
284 }
285 }
286}
287
288void QtObservedAtom::recieveNotification(Observable *publisher, Notification_ptr notification)
289{
290 // ObservedValues have been updated before, hence convert updates to Qt's signals
291 if (publisher == WorldTime::getPointer()) {
292 // force update of all changes that may occur from one time step to another
293 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(
294 ObservedValues[AtomPosition])->forceUpdate();
295 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(
296 ObservedValues[AtomVelocity])->forceUpdate();
297 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(
298 ObservedValues[AtomForce])->forceUpdate();
299 boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(
300 ObservedValues[MoleculeRef])->forceUpdate();
301 // then tell Qt part about updates
302 emit positionChanged();
303 emit velocityChanged();
304 emit forceChanged();
305 emit moleculeChanged();
306 } else {
307 switch (notification->getChannelNo()) {
308 case AtomObservable::IndexChanged:
309 {
310 emit indexChanged();
311 break;
312 }
313 case AtomObservable::BondsAdded:
314 case AtomObservable::BondsRemoved:
315 emit bondsChanged();
316 break;
317 case AtomObservable::ElementChanged:
318 emit elementChanged();
319 break;
320 case AtomObservable::MoleculeChanged:
321 emit moleculeChanged();
322 break;
323 case AtomObservable::NameChanged:
324 emit nameChanged();
325 break;
326 case AtomObservable::PositionChanged:
327 emit positionChanged();
328 break;
329 case AtomObservable::VelocityChanged:
330 emit velocityChanged();
331 break;
332 case AtomObservable::ForceChanged:
333 emit forceChanged();
334 break;
335 case AtomObservable::SelectionChanged:
336 emit selectedChanged();
337 break;
338 default:
339 ASSERT(0, "QtObservedAtom::recieveNotification() - we are not signed on to channel "
340 +toString(notification->getChannelNo())+" of the atom.");
341 break;
342 }
343 }
344}
345
346void QtObservedAtom::activateObserver()
347{
348 atom * atomref = getAtom(getAtomIndex());
349 if (atomref != NULL) {
350 Observable::channels_t channels = getAllObservedChannels();
351 owner = static_cast<const Observable *>(atomref);
352 for (Observable::channels_t::const_iterator iter = channels.begin();
353 iter != channels.end(); ++iter)
354 owner->signOn(this, *iter);
355 if (!BoardIsGone)
356 board.markObservedAtomAsConnected(getIndex());
357 } else
358 signedOffChannels = AllsignedOnChannels;
359
360 WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
361}
362
363void QtObservedAtom::deactivateObserver()
364{
365 // sign Off
366 if (owner != NULL) {
367 Observable::channels_t channels = getAllObservedChannels();
368 for (Observable::channels_t::const_iterator iter = channels.begin();
369 iter != channels.end(); ++iter)
370 owner->signOff(this, *iter);
371 owner = NULL;
372 signedOffChannels = AllsignedOnChannels;
373 if (!BoardIsGone)
374 board.markObservedAtomAsDisconnected(getIndex());
375 }
376
377 WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
378}
379
380template <typename T>
381T safelyAccessUpdater(
382 const size_t& _subjectKilledCount,
383 const boost::function<T ()> _updater,
384 const T _defaultValue
385) {
386 if (_subjectKilledCount == 0)
387 return _updater();
388 return _defaultValue;
389}
390
391void QtObservedAtom::initObservedValues(
392 ObservedValues_t &_ObservedValues,
393 const atomId_t _id,
394 const atom * const _atomref,
395 const boost::function<void()> &_subjectKilled)
396{
397 ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
398 "QtObservedAtom::initObservedValues() - given ObservedValues has not correct size.");
399
400 // fill ObservedValues: then all the other that need index
401
402 const boost::function<atomId_t ()> AtomIndexUpdater(
403 boost::bind<atomId_t>(&QtObservedAtom::updateIndex, boost::cref(*_atomref)));
404 const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
405 boost::bind(&QtObservedAtom::updateBonds, boost::cref(*_atomref)));
406 const boost::function<atomicNumber_t ()> AtomElementUpdater(
407 boost::bind(&QtObservedAtom::updateElement, boost::cref(*_atomref)));
408 const boost::function<std::string ()> AtomNameUpdater(
409 boost::bind(&QtObservedAtom::updateName, boost::cref(*_atomref)));
410 const boost::function<Vector ()> AtomPositionUpdater(
411 boost::bind(&QtObservedAtom::updatePosition, boost::cref(*_atomref)));
412 const boost::function<Vector ()> AtomVelocityUpdater(
413 boost::bind(&QtObservedAtom::updateVelocity, boost::cref(*_atomref)));
414 const boost::function<Vector ()> AtomForceUpdater(
415 boost::bind(&QtObservedAtom::updateForce, boost::cref(*_atomref)));
416 const boost::function<bool ()> AtomSelectedUpdater(
417 boost::bind(&QtObservedAtom::updateSelected, boost::cref(*_atomref)));
418 const boost::function<QtObservedMolecule* ()> MoleculeRefUpdater(
419 boost::bind(&QtObservedAtom::updateMoleculeRef, this, boost::cref(*_atomref)));
420
421 _ObservedValues[AtomIndex] = new ObservedValue_wCallback<atomId_t, ObservedValue_Index_t>(
422 _atomref,
423 boost::bind(safelyAccessUpdater<atomId_t>, boost::cref(subjectKilledCount), AtomIndexUpdater, (atomId_t)0),
424 "AtomIndex_"+toString(_id),
425 _id,
426 AtomIndexChannels,
427 _subjectKilled);
428 _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t>(
429 _atomref,
430 boost::bind(safelyAccessUpdater<ListOfBonds_t>, boost::cref(subjectKilledCount), AtomBondsUpdater, ListOfBonds_t()),
431 "AtomBonds_"+toString(_id),
432 AtomBondsUpdater(),
433 AtomBondsChannels,
434 _subjectKilled);
435 _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t>(
436 _atomref,
437 boost::bind(safelyAccessUpdater<atomicNumber_t>, boost::cref(subjectKilledCount), AtomElementUpdater, (atomicNumber_t)0),
438 "AtomElement"+toString(_id),
439 AtomElementUpdater(),
440 AtomElementChannels,
441 _subjectKilled);
442 _ObservedValues[AtomName] = new ObservedValue_wCallback<std::string, ObservedValue_Index_t>(
443 _atomref,
444 boost::bind(safelyAccessUpdater<std::string>, boost::cref(subjectKilledCount), AtomNameUpdater, ""),
445 "AtomName"+toString(_id),
446 AtomNameUpdater(),
447 AtomNameChannels,
448 _subjectKilled);
449 _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
450 _atomref,
451 boost::bind(safelyAccessUpdater<Vector>, boost::cref(subjectKilledCount), AtomPositionUpdater, zeroVec),
452 "AtomPosition_"+toString(_id),
453 AtomPositionUpdater(),
454 AtomPositionChannels,
455 _subjectKilled);
456 _ObservedValues[AtomVelocity] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
457 _atomref,
458 boost::bind(safelyAccessUpdater<Vector>, boost::cref(subjectKilledCount), AtomVelocityUpdater, zeroVec),
459 "AtomVelocity_"+toString(_id),
460 AtomVelocityUpdater(),
461 AtomVelocityChannels,
462 _subjectKilled);
463 _ObservedValues[AtomForce] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
464 _atomref,
465 boost::bind(safelyAccessUpdater<Vector>, boost::cref(subjectKilledCount), AtomForceUpdater, zeroVec),
466 "AtomForce_"+toString(_id),
467 AtomForceUpdater(),
468 AtomForceChannels,
469 _subjectKilled);
470 _ObservedValues[AtomSelected] = new ObservedValue_wCallback<bool, ObservedValue_Index_t>(
471 _atomref,
472 boost::bind(safelyAccessUpdater<bool>, boost::cref(subjectKilledCount), AtomSelectedUpdater, false),
473 "AtomSelected_"+toString(_id),
474 AtomSelectedUpdater(),
475 AtomSelectedChannels,
476 _subjectKilled);
477 _ObservedValues[MoleculeRef] = new ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t>(
478 _atomref,
479 boost::bind(safelyAccessUpdater<QtObservedMolecule*>, boost::cref(subjectKilledCount), MoleculeRefUpdater, (QtObservedMolecule*)NULL),
480 "AtomMoleculeIndex"+toString(_id),
481 MoleculeRefUpdater(),
482 MoleculeChangedChannels,
483 _subjectKilled);
484}
485
486void QtObservedAtom::destroyObservedValues(
487 std::vector<boost::any> &_ObservedValues)
488{
489 delete boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(_ObservedValues[AtomIndex]);
490 delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(_ObservedValues[AtomBonds]);
491 delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(_ObservedValues[AtomElement]);
492 delete boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(_ObservedValues[AtomName]);
493 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomPosition]);
494 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomVelocity]);
495 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomForce]);
496 delete boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(_ObservedValues[AtomSelected]);
497 delete boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(_ObservedValues[MoleculeRef]);
498 _ObservedValues.clear();
499}
500
501ObservedValue_Index_t QtObservedAtom::getIndex() const
502{
503 ASSERT( index != NULL,
504 "QtObservedAtom::getIndex() - index is NULL");
505 return index;
506}
507
508const atomId_t& QtObservedAtom::getAtomIndex() const
509{
510 return boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(ObservedValues[AtomIndex])->get();
511}
512
513const QtObservedAtom::ListOfBonds_t& QtObservedAtom::getAtomBonds() const
514{
515 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(ObservedValues[AtomBonds])->get();
516}
517
518const atomicNumber_t& QtObservedAtom::getAtomElement() const
519{
520 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(ObservedValues[AtomElement])->get();
521}
522
523const std::string& QtObservedAtom::getAtomName() const
524{
525 return boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(ObservedValues[AtomName])->get();
526}
527
528const Vector& QtObservedAtom::getAtomPosition() const
529{
530 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomPosition])->get();
531}
532
533const Vector& QtObservedAtom::getAtomVelocity() const
534{
535 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomVelocity])->get();
536}
537
538const Vector& QtObservedAtom::getAtomForce() const
539{
540 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomForce])->get();
541}
542
543const bool QtObservedAtom::getAtomSelected() const
544{
545 return boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(ObservedValues[AtomSelected])->get();
546}
547
548const QtObservedMolecule* const QtObservedAtom::getMoleculeRef() const
549{
550 return boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(ObservedValues[MoleculeRef])->get();
551}
Note: See TracBrowser for help on using the repository browser.