source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp@ 3e334e

Candidate_v1.6.1 ChemicalSpaceEvaluator
Last change on this file since 3e334e was 4dfe724, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

FIX: QtGui again reacts to WorldTime changes.

  • QtObservedAtom now observes WorldTime and uses new forceUpdate() for position and molecule ObservedValues to get information of new time step.
  • Note: As WorldTime is a singleton living till the end of the program we do not need any fancy taking care of subjectKilled() in a correct manner.
  • Property mode set to 100644
File size: 15.5 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::AtomSelectedChannels(1, AtomObservable::SelectionChanged);
92
93QtObservedAtom::QtObservedAtom(
94 const atomId_t _id,
95 const atom * const _atom,
96 QtObservedInstanceBoard &_board,
97 QWidget * _parent) :
98 QWidget(_parent),
99 Observer("QtObservedAtom"),
100 subjectKilledCount(0),
101 AllsignedOnChannels(getAllObservedChannels().size()),
102 signedOffChannels(0),
103 owner(NULL),
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 if (publisher == WorldTime::getPointer()) {
268 // force update of all changes that may occur from one time step to another
269 boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(
270 ObservedValues[AtomPosition])->forceUpdate();
271 boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(
272 ObservedValues[MoleculeRef])->forceUpdate();
273 // then tell Qt part about updates
274 emit positionChanged();
275 emit moleculeChanged();
276 } else {
277 switch (notification->getChannelNo()) {
278 case AtomObservable::IndexChanged:
279 {
280 emit indexChanged();
281 break;
282 }
283 case AtomObservable::BondsAdded:
284 case AtomObservable::BondsRemoved:
285 emit bondsChanged();
286 break;
287 case AtomObservable::ElementChanged:
288 emit elementChanged();
289 break;
290 case AtomObservable::MoleculeChanged:
291 emit moleculeChanged();
292 break;
293 case AtomObservable::NameChanged:
294 emit nameChanged();
295 break;
296 case AtomObservable::PositionChanged:
297 emit positionChanged();
298 break;
299 case AtomObservable::SelectionChanged:
300 emit selectedChanged();
301 break;
302 default:
303 ASSERT(0, "QtObservedAtom::recieveNotification() - we are not signed on to channel "
304 +toString(notification->getChannelNo())+" of the atom.");
305 break;
306 }
307 }
308}
309
310void QtObservedAtom::activateObserver()
311{
312 atom * atomref = getAtom(getAtomIndex());
313 if (atomref != NULL) {
314 Observable::channels_t channels = getAllObservedChannels();
315 owner = static_cast<const Observable *>(atomref);
316 for (Observable::channels_t::const_iterator iter = channels.begin();
317 iter != channels.end(); ++iter)
318 owner->signOn(this, *iter);
319 if (!BoardIsGone)
320 board.markObservedAtomAsConnected(getIndex());
321 } else
322 signedOffChannels = AllsignedOnChannels;
323
324 WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
325}
326
327void QtObservedAtom::deactivateObserver()
328{
329 // sign Off
330 if (owner != NULL) {
331 Observable::channels_t channels = getAllObservedChannels();
332 for (Observable::channels_t::const_iterator iter = channels.begin();
333 iter != channels.end(); ++iter)
334 owner->signOff(this, *iter);
335 owner = NULL;
336 signedOffChannels = AllsignedOnChannels;
337 if (!BoardIsGone)
338 board.markObservedAtomAsDisconnected(getIndex());
339 }
340
341 WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
342}
343
344void QtObservedAtom::initObservedValues(
345 ObservedValues_t &_ObservedValues,
346 const atomId_t _id,
347 const atom * const _atomref,
348 const boost::function<void()> &_subjectKilled)
349{
350 ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
351 "QtObservedAtom::initObservedValues() - given ObservedValues has not correct size.");
352
353 // fill ObservedValues: then all the other that need index
354 const boost::function<atomId_t ()> AtomIndexUpdater(
355 boost::bind(&QtObservedAtom::updateIndex, boost::cref(*_atomref)));
356 const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
357 boost::bind(&QtObservedAtom::updateBonds, boost::cref(*_atomref)));
358 const boost::function<atomicNumber_t ()> AtomElementUpdater(
359 boost::bind(&QtObservedAtom::updateElement, boost::cref(*_atomref)));
360 const boost::function<std::string ()> AtomNameUpdater(
361 boost::bind(&QtObservedAtom::updateName, boost::cref(*_atomref)));
362 const boost::function<Vector ()> AtomPositionUpdater(
363 boost::bind(&QtObservedAtom::updatePosition, boost::cref(*_atomref)));
364 const boost::function<bool ()> AtomSelectedUpdater(
365 boost::bind(&QtObservedAtom::updateSelected, boost::cref(*_atomref)));
366 const boost::function<QtObservedMolecule* ()> MoleculeRefUpdater(
367 boost::bind(&QtObservedAtom::updateMoleculeRef, this, boost::cref(*_atomref)));
368
369 _ObservedValues[AtomIndex] = new ObservedValue_wCallback<atomId_t, ObservedValue_Index_t>(
370 _atomref,
371 AtomIndexUpdater,
372 "AtomIndex_"+toString(_id),
373 _id,
374 AtomIndexChannels,
375 _subjectKilled);
376 _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t>(
377 _atomref,
378 AtomBondsUpdater,
379 "AtomBonds_"+toString(_id),
380 AtomBondsUpdater(),
381 AtomBondsChannels,
382 _subjectKilled);
383 _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t>(
384 _atomref,
385 AtomElementUpdater,
386 "AtomElement"+toString(_id),
387 AtomElementUpdater(),
388 AtomElementChannels,
389 _subjectKilled);
390 _ObservedValues[AtomName] = new ObservedValue_wCallback<std::string, ObservedValue_Index_t>(
391 _atomref,
392 AtomNameUpdater,
393 "AtomName"+toString(_id),
394 AtomNameUpdater(),
395 AtomNameChannels,
396 _subjectKilled);
397 _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, ObservedValue_Index_t>(
398 _atomref,
399 AtomPositionUpdater,
400 "AtomPosition_"+toString(_id),
401 AtomPositionUpdater(),
402 AtomPositionChannels,
403 _subjectKilled);
404 _ObservedValues[AtomSelected] = new ObservedValue_wCallback<bool, ObservedValue_Index_t>(
405 _atomref,
406 AtomSelectedUpdater,
407 "AtomSelected_"+toString(_id),
408 AtomSelectedUpdater(),
409 AtomSelectedChannels,
410 _subjectKilled);
411 _ObservedValues[MoleculeRef] = new ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t>(
412 _atomref,
413 MoleculeRefUpdater,
414 "AtomMoleculeIndex"+toString(_id),
415 MoleculeRefUpdater(),
416 MoleculeChangedChannels,
417 _subjectKilled);
418}
419
420void QtObservedAtom::destroyObservedValues(
421 std::vector<boost::any> &_ObservedValues)
422{
423 delete boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(_ObservedValues[AtomIndex]);
424 delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(_ObservedValues[AtomBonds]);
425 delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(_ObservedValues[AtomElement]);
426 delete boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(_ObservedValues[AtomName]);
427 delete boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(_ObservedValues[AtomPosition]);
428 delete boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(_ObservedValues[AtomSelected]);
429 delete boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(_ObservedValues[MoleculeRef]);
430 _ObservedValues.clear();
431}
432
433ObservedValue_Index_t QtObservedAtom::getIndex() const
434{
435 ASSERT( index != NULL,
436 "QtObservedAtom::getIndex() - index is NULL");
437 return index;
438}
439
440const atomId_t& QtObservedAtom::getAtomIndex() const
441{
442 return boost::any_cast<ObservedValue_wCallback<atomId_t, ObservedValue_Index_t> *>(ObservedValues[AtomIndex])->get();
443}
444
445const QtObservedAtom::ListOfBonds_t& QtObservedAtom::getAtomBonds() const
446{
447 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, ObservedValue_Index_t> *>(ObservedValues[AtomBonds])->get();
448}
449
450const atomicNumber_t& QtObservedAtom::getAtomElement() const
451{
452 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, ObservedValue_Index_t> *>(ObservedValues[AtomElement])->get();
453}
454
455const std::string& QtObservedAtom::getAtomName() const
456{
457 return boost::any_cast<ObservedValue_wCallback<std::string, ObservedValue_Index_t> *>(ObservedValues[AtomName])->get();
458}
459
460const Vector& QtObservedAtom::getAtomPosition() const
461{
462 return boost::any_cast<ObservedValue_wCallback<Vector, ObservedValue_Index_t> *>(ObservedValues[AtomPosition])->get();
463}
464
465const bool QtObservedAtom::getAtomSelected() const
466{
467 return boost::any_cast<ObservedValue_wCallback<bool, ObservedValue_Index_t> *>(ObservedValues[AtomSelected])->get();
468}
469
470const QtObservedMolecule* const QtObservedAtom::getMoleculeRef() const
471{
472 return boost::any_cast<ObservedValue_wCallback<QtObservedMolecule*, ObservedValue_Index_t> *>(ObservedValues[MoleculeRef])->get();
473}
Note: See TracBrowser for help on using the repository browser.