source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp@ 897a01

Candidate_v1.6.1 ChemicalSpaceEvaluator Gui_displays_atomic_force_velocity PythonUI_with_named_parameters TremoloParser_IncreasedPrecision
Last change on this file since 897a01 was 897a01, checked in by Frederik Heber <frederik.heber@…>, 7 years ago

Arrows display velocity and force of each atom.

  • Property mode set to 100644
File size: 18.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
380void QtObservedAtom::initObservedValues(
381 ObservedValues_t &_ObservedValues,
382 const atomId_t _id,
383 const atom * const _atomref,
384 const boost::function<void()> &_subjectKilled)
385{
386 ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
387 "QtObservedAtom::initObservedValues() - given ObservedValues has not correct size.");
388
389 // fill ObservedValues: then all the other that need index
390 const boost::function<atomId_t ()> AtomIndexUpdater(
391 boost::bind(&QtObservedAtom::updateIndex, boost::cref(*_atomref)));
392 const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
393 boost::bind(&QtObservedAtom::updateBonds, boost::cref(*_atomref)));
394 const boost::function<atomicNumber_t ()> AtomElementUpdater(
395 boost::bind(&QtObservedAtom::updateElement, boost::cref(*_atomref)));
396 const boost::function<std::string ()> AtomNameUpdater(
397 boost::bind(&QtObservedAtom::updateName, boost::cref(*_atomref)));
398 const boost::function<Vector ()> AtomPositionUpdater(
399 boost::bind(&QtObservedAtom::updatePosition, boost::cref(*_atomref)));
400 const boost::function<Vector ()> AtomVelocityUpdater(
401 boost::bind(&QtObservedAtom::updateVelocity, boost::cref(*_atomref)));
402 const boost::function<Vector ()> AtomForceUpdater(
403 boost::bind(&QtObservedAtom::updateForce, boost::cref(*_atomref)));
404 const boost::function<bool ()> AtomSelectedUpdater(
405 boost::bind(&QtObservedAtom::updateSelected, boost::cref(*_atomref)));
406 const boost::function<QtObservedMolecule* ()> MoleculeRefUpdater(
407 boost::bind(&QtObservedAtom::updateMoleculeRef, this, boost::cref(*_atomref)));
408
409 _ObservedValues[AtomIndex] = new ObservedValue_wCallback<atomId_t, ObservedValue_Index_t>(
410 _atomref,
411 AtomIndexUpdater,
412 "AtomIndex_"+toString(_id),
413 _id,
414 AtomIndexChannels,
415 _subjectKilled);
416 _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t>(
417 _atomref,
418 AtomBondsUpdater,
419 "AtomBonds_"+toString(_id),
420 AtomBondsUpdater(),
421 AtomBondsChannels,
422 _subjectKilled);
423 _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t>(
424 _atomref,
425 AtomElementUpdater,
426 "AtomElement"+toString(_id),
427 AtomElementUpdater(),
428 AtomElementChannels,
429 _subjectKilled);
430 _ObservedValues[AtomName] = new ObservedValue_wCallback<std::string, ObservedValue_Index_t>(
431 _atomref,
432 AtomNameUpdater,
433 "AtomName"+toString(_id),
434 AtomNameUpdater(),
435 AtomNameChannels,
436 _subjectKilled);
437 _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
438 _atomref,
439 AtomPositionUpdater,
440 "AtomPosition_"+toString(_id),
441 AtomPositionUpdater(),
442 AtomPositionChannels,
443 _subjectKilled);
444 _ObservedValues[AtomVelocity] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
445 _atomref,
446 AtomVelocityUpdater,
447 "AtomVelocity_"+toString(_id),
448 AtomVelocityUpdater(),
449 AtomVelocityChannels,
450 _subjectKilled);
451 _ObservedValues[AtomForce] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
452 _atomref,
453 AtomForceUpdater,
454 "AtomForce_"+toString(_id),
455 AtomForceUpdater(),
456 AtomForceChannels,
457 _subjectKilled);
458 _ObservedValues[AtomSelected] = new ObservedValue_wCallback<bool, ObservedValue_Index_t>(
459 _atomref,
460 AtomSelectedUpdater,
461 "AtomSelected_"+toString(_id),
462 AtomSelectedUpdater(),
463 AtomSelectedChannels,
464 _subjectKilled);
465 _ObservedValues[MoleculeRef] = new ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t>(
466 _atomref,
467 MoleculeRefUpdater,
468 "AtomMoleculeIndex"+toString(_id),
469 MoleculeRefUpdater(),
470 MoleculeChangedChannels,
471 _subjectKilled);
472}
473
474void QtObservedAtom::destroyObservedValues(
475 std::vector<boost::any> &_ObservedValues)
476{
477 delete boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(_ObservedValues[AtomIndex]);
478 delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(_ObservedValues[AtomBonds]);
479 delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(_ObservedValues[AtomElement]);
480 delete boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(_ObservedValues[AtomName]);
481 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomPosition]);
482 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomVelocity]);
483 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomForce]);
484 delete boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(_ObservedValues[AtomSelected]);
485 delete boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(_ObservedValues[MoleculeRef]);
486 _ObservedValues.clear();
487}
488
489ObservedValue_Index_t QtObservedAtom::getIndex() const
490{
491 ASSERT( index != NULL,
492 "QtObservedAtom::getIndex() - index is NULL");
493 return index;
494}
495
496const atomId_t& QtObservedAtom::getAtomIndex() const
497{
498 return boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(ObservedValues[AtomIndex])->get();
499}
500
501const QtObservedAtom::ListOfBonds_t& QtObservedAtom::getAtomBonds() const
502{
503 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(ObservedValues[AtomBonds])->get();
504}
505
506const atomicNumber_t& QtObservedAtom::getAtomElement() const
507{
508 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(ObservedValues[AtomElement])->get();
509}
510
511const std::string& QtObservedAtom::getAtomName() const
512{
513 return boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(ObservedValues[AtomName])->get();
514}
515
516const Vector& QtObservedAtom::getAtomPosition() const
517{
518 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomPosition])->get();
519}
520
521const Vector& QtObservedAtom::getAtomVelocity() const
522{
523 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomVelocity])->get();
524}
525
526const Vector& QtObservedAtom::getAtomForce() const
527{
528 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomForce])->get();
529}
530
531const bool QtObservedAtom::getAtomSelected() const
532{
533 return boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(ObservedValues[AtomSelected])->get();
534}
535
536const QtObservedMolecule* const QtObservedAtom::getMoleculeRef() const
537{
538 return boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(ObservedValues[MoleculeRef])->get();
539}
Note: See TracBrowser for help on using the repository browser.