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 |
|
---|
52 | using namespace boost::assign;
|
---|
53 |
|
---|
54 | static const Observable::channels_t getAtomBondsChannels()
|
---|
55 | {
|
---|
56 | Observable::channels_t channels;
|
---|
57 | channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
|
---|
58 | return channels;
|
---|
59 | }
|
---|
60 |
|
---|
61 | static 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 | return channels;
|
---|
73 | }
|
---|
74 |
|
---|
75 | // static entities
|
---|
76 | const Observable::channels_t
|
---|
77 | QtObservedAtom::AtomIndexChannels(1, AtomObservable::IndexChanged);
|
---|
78 | const Observable::channels_t
|
---|
79 | QtObservedAtom::AtomBondsChannels(getAtomBondsChannels());
|
---|
80 | const Observable::channels_t
|
---|
81 | QtObservedAtom::AtomElementChannels(1, AtomObservable::ElementChanged);
|
---|
82 | const Observable::channels_t
|
---|
83 | QtObservedAtom::AtomMoleculeIndexChannels(1, AtomObservable::MoleculeChanged);
|
---|
84 | const Observable::channels_t
|
---|
85 | QtObservedAtom::AtomNameChannels(1, AtomObservable::NameChanged);
|
---|
86 | const Observable::channels_t
|
---|
87 | QtObservedAtom::AtomPositionChannels(1, AtomObservable::PositionChanged);
|
---|
88 |
|
---|
89 | QtObservedAtom::QtObservedAtom(
|
---|
90 | const ObservedValues_t &_ObservedValues,
|
---|
91 | QtObservedInstanceBoard &_board,
|
---|
92 | QWidget * _parent) :
|
---|
93 | QWidget(_parent),
|
---|
94 | Observer("QtObservedAtom"),
|
---|
95 | subjectKilledCount(0),
|
---|
96 | AllsignedOnChannels(getAllObservedChannels().size()),
|
---|
97 | signedOffChannels(0),
|
---|
98 | owner(NULL),
|
---|
99 | board(_board),
|
---|
100 | BoardIsGone(false),
|
---|
101 | ObservedValues(_ObservedValues)
|
---|
102 | {
|
---|
103 | // activating Observer is done by ObservedValueContainer when it's prepared
|
---|
104 | }
|
---|
105 |
|
---|
106 | QtObservedAtom::~QtObservedAtom()
|
---|
107 | {
|
---|
108 | boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->noteCallBackIsGone();
|
---|
109 | boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(ObservedValues[AtomBonds])->noteCallBackIsGone();
|
---|
110 | boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[AtomElement])->noteCallBackIsGone();
|
---|
111 | boost::any_cast<ObservedValue_wCallback<moleculeId_t, atomId_t> *>(ObservedValues[AtomMoleculeIndex])->noteCallBackIsGone();
|
---|
112 | boost::any_cast<ObservedValue_wCallback<std::string, atomId_t> *>(ObservedValues[AtomName])->noteCallBackIsGone();
|
---|
113 | boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[AtomPosition])->noteCallBackIsGone();
|
---|
114 |
|
---|
115 | deactivateObserver();
|
---|
116 | }
|
---|
117 |
|
---|
118 | const atom * const QtObservedAtom::getAtomConst(const atomId_t _id)
|
---|
119 | {
|
---|
120 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
121 | getAtom(AtomById(_id));
|
---|
122 | return _atom;
|
---|
123 | }
|
---|
124 |
|
---|
125 | atom * const QtObservedAtom::getAtom(const atomId_t _id)
|
---|
126 | {
|
---|
127 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
---|
128 | return _atom;
|
---|
129 | }
|
---|
130 |
|
---|
131 | atomId_t QtObservedAtom::updateIndex()
|
---|
132 | {
|
---|
133 | return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
134 | }
|
---|
135 |
|
---|
136 | QtObservedAtom::ListOfBonds_t QtObservedAtom::updateBonds(
|
---|
137 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
---|
138 | {
|
---|
139 | ListOfBonds_t ListOfBonds;
|
---|
140 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
---|
141 | if (_atom != NULL) {
|
---|
142 | // make sure bonds is up-to-date
|
---|
143 | const BondList ListBonds = _atom->getListOfBonds();
|
---|
144 | for (BondList::const_iterator iter = ListBonds.begin();
|
---|
145 | iter != ListBonds.end();
|
---|
146 | ++iter)
|
---|
147 | ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
|
---|
148 | (*iter)->leftatom->getId(),
|
---|
149 | (*iter)->rightatom->getId()) );
|
---|
150 | }
|
---|
151 | return ListOfBonds;
|
---|
152 | }
|
---|
153 |
|
---|
154 | atomicNumber_t QtObservedAtom::updateElement(
|
---|
155 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
---|
156 | {
|
---|
157 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
---|
158 | if (_atom != NULL) {
|
---|
159 | return _atom->getElementNo();
|
---|
160 | } else {
|
---|
161 | return (atomicNumber_t)-1;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | moleculeId_t QtObservedAtom::updateMoleculeIndex(
|
---|
166 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
---|
167 | {
|
---|
168 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
---|
169 | if ((_atom != NULL) && (_atom->getMolecule() != NULL)) {
|
---|
170 | return _atom->getMolecule()->getId();
|
---|
171 | } else {
|
---|
172 | return (moleculeId_t)0;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | std::string QtObservedAtom::updateName(
|
---|
177 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
---|
178 | {
|
---|
179 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
---|
180 | if (_atom != NULL) {
|
---|
181 | return _atom->getName();
|
---|
182 | } else {
|
---|
183 | return std::string("");
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | Vector QtObservedAtom::updatePosition(
|
---|
188 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
---|
189 | {
|
---|
190 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
---|
191 | if (_atom != NULL) {
|
---|
192 | return _atom->getPosition();
|
---|
193 | } else {
|
---|
194 | return zeroVec;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | void QtObservedAtom::update(Observable *publisher)
|
---|
199 | {
|
---|
200 | ASSERT(0, "QtObservedAtom::update() - we are not signed on for global updates.");
|
---|
201 | }
|
---|
202 |
|
---|
203 | void QtObservedAtom::subjectKilled(Observable *publisher)
|
---|
204 | {
|
---|
205 | ++signedOffChannels;
|
---|
206 |
|
---|
207 | if (signedOffChannels == AllsignedOnChannels) {
|
---|
208 | // remove owner: no more signOff needed
|
---|
209 | owner = NULL;
|
---|
210 |
|
---|
211 | emit atomRemoved();
|
---|
212 |
|
---|
213 | if (!BoardIsGone) {
|
---|
214 | board.markObservedAtomAsDisconnected(getAtomIndex());
|
---|
215 | board.markObservedAtomForErase(getAtomIndex());
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | void QtObservedAtom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
221 | {
|
---|
222 | // ObservedValues have been updated before, hence convert updates to Qt's signals
|
---|
223 | switch (notification->getChannelNo()) {
|
---|
224 | case AtomObservable::IndexChanged:
|
---|
225 | emit indexChanged();
|
---|
226 | break;
|
---|
227 | case AtomObservable::BondsAdded:
|
---|
228 | case AtomObservable::BondsRemoved:
|
---|
229 | emit bondsChanged();
|
---|
230 | break;
|
---|
231 | case AtomObservable::ElementChanged:
|
---|
232 | emit elementChanged();
|
---|
233 | break;
|
---|
234 | case AtomObservable::MoleculeChanged:
|
---|
235 | emit moleculeindexChanged();
|
---|
236 | break;
|
---|
237 | case AtomObservable::NameChanged:
|
---|
238 | emit nameChanged();
|
---|
239 | break;
|
---|
240 | case AtomObservable::PositionChanged:
|
---|
241 | emit positionChanged();
|
---|
242 | break;
|
---|
243 | default:
|
---|
244 | ASSERT(0, "QtObservedAtom::recieveNotification() - we are not signed on to channel "
|
---|
245 | +toString(notification->getChannelNo())+" of the atom.");
|
---|
246 | break;
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | void QtObservedAtom::activateObserver()
|
---|
251 | {
|
---|
252 | atom * atomref = getAtom(getAtomIndex());
|
---|
253 | if (atomref != NULL) {
|
---|
254 | Observable::channels_t channels = getAllObservedChannels();
|
---|
255 | owner = static_cast<const Observable *>(atomref);
|
---|
256 | for (Observable::channels_t::const_iterator iter = channels.begin();
|
---|
257 | iter != channels.end(); ++iter)
|
---|
258 | owner->signOn(this, *iter);
|
---|
259 | if (!BoardIsGone)
|
---|
260 | board.markObservedAtomAsConnected(getAtomIndex());
|
---|
261 | } else
|
---|
262 | signedOffChannels = AllsignedOnChannels;
|
---|
263 | }
|
---|
264 |
|
---|
265 | void QtObservedAtom::deactivateObserver()
|
---|
266 | {
|
---|
267 | // sign Off
|
---|
268 | if (owner != NULL) {
|
---|
269 | Observable::channels_t channels = getAllObservedChannels();
|
---|
270 | for (Observable::channels_t::const_iterator iter = channels.begin();
|
---|
271 | iter != channels.end(); ++iter)
|
---|
272 | owner->signOff(this, *iter);
|
---|
273 | owner = NULL;
|
---|
274 | signedOffChannels = AllsignedOnChannels;
|
---|
275 | if (!BoardIsGone)
|
---|
276 | board.markObservedAtomAsDisconnected(getAtomIndex());
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | void QtObservedAtom::initObservedValues(
|
---|
281 | ObservedValues_t &_ObservedValues,
|
---|
282 | const atomId_t _id,
|
---|
283 | const atom * const _atomref,
|
---|
284 | const boost::function<void(const atomId_t)> &_subjectKilled)
|
---|
285 | {
|
---|
286 | /* This is an old note from when the code was still part of cstor's initializer body.
|
---|
287 | * TODO: Probably does not apply anymore but has not yet been tested.
|
---|
288 | *
|
---|
289 | * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
|
---|
290 | * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
|
---|
291 | * the class has not been fully constructed yet. "This" itself seems to be working fine.
|
---|
292 | */
|
---|
293 |
|
---|
294 | ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
|
---|
295 | "QtObservedAtom::initObservedValues() - given ObservedValues has not correct size.");
|
---|
296 |
|
---|
297 | // fill ObservedValues: index first
|
---|
298 | const boost::function<atomId_t ()> AtomIndexUpdater(
|
---|
299 | boost::bind(&QtObservedAtom::updateIndex));
|
---|
300 |
|
---|
301 | ObservedValue_wCallback<atomId_t> * const IndexObservable =
|
---|
302 | new ObservedValue_wCallback<atomId_t>(
|
---|
303 | _atomref,
|
---|
304 | boost::bind(&QtObservedAtom::updateIndex),
|
---|
305 | "AtomIndex_"+toString(_id),
|
---|
306 | _id,
|
---|
307 | AtomIndexChannels,
|
---|
308 | _subjectKilled);
|
---|
309 | _ObservedValues[AtomIndex] = IndexObservable;
|
---|
310 |
|
---|
311 | const boost::function<const atomId_t ()> AtomIndexGetter =
|
---|
312 | boost::bind(&ObservedValue_wCallback<atomId_t>::get,
|
---|
313 | IndexObservable);
|
---|
314 |
|
---|
315 | // fill ObservedValues: then all the other that need index
|
---|
316 | const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
|
---|
317 | boost::bind(&QtObservedAtom::updateBonds, AtomIndexGetter));
|
---|
318 | const boost::function<atomicNumber_t ()> AtomElementUpdater(
|
---|
319 | boost::bind(&QtObservedAtom::updateElement, AtomIndexGetter));
|
---|
320 | const boost::function<moleculeId_t ()> AtomMoleculeIndexUpdater(
|
---|
321 | boost::bind(&QtObservedAtom::updateMoleculeIndex, AtomIndexGetter));
|
---|
322 | const boost::function<std::string ()> AtomNameUpdater(
|
---|
323 | boost::bind(&QtObservedAtom::updateName, AtomIndexGetter));
|
---|
324 | const boost::function<Vector ()> AtomPositionUpdater(
|
---|
325 | boost::bind(&QtObservedAtom::updatePosition, AtomIndexGetter));
|
---|
326 |
|
---|
327 | _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, atomId_t>(
|
---|
328 | _atomref,
|
---|
329 | AtomBondsUpdater,
|
---|
330 | "AtomBonds_"+toString(_id),
|
---|
331 | AtomBondsUpdater(),
|
---|
332 | AtomBondsChannels,
|
---|
333 | _subjectKilled,
|
---|
334 | AtomIndexGetter);
|
---|
335 | _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, atomId_t>(
|
---|
336 | _atomref,
|
---|
337 | AtomElementUpdater,
|
---|
338 | "AtomElement"+toString(_id),
|
---|
339 | AtomElementUpdater(),
|
---|
340 | AtomElementChannels,
|
---|
341 | _subjectKilled,
|
---|
342 | AtomIndexGetter);
|
---|
343 | _ObservedValues[AtomMoleculeIndex] = new ObservedValue_wCallback<moleculeId_t, atomId_t>(
|
---|
344 | _atomref,
|
---|
345 | AtomMoleculeIndexUpdater,
|
---|
346 | "AtomMoleculeIndex"+toString(_id),
|
---|
347 | AtomMoleculeIndexUpdater(),
|
---|
348 | AtomMoleculeIndexChannels,
|
---|
349 | _subjectKilled,
|
---|
350 | AtomIndexGetter);
|
---|
351 | _ObservedValues[AtomName] = new ObservedValue_wCallback<std::string, atomId_t>(
|
---|
352 | _atomref,
|
---|
353 | AtomNameUpdater,
|
---|
354 | "AtomName"+toString(_id),
|
---|
355 | AtomNameUpdater(),
|
---|
356 | AtomNameChannels,
|
---|
357 | _subjectKilled,
|
---|
358 | AtomIndexGetter);
|
---|
359 | _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, atomId_t>(
|
---|
360 | _atomref,
|
---|
361 | AtomPositionUpdater,
|
---|
362 | "AtomPosition_"+toString(_id),
|
---|
363 | AtomPositionUpdater(),
|
---|
364 | AtomPositionChannels,
|
---|
365 | _subjectKilled,
|
---|
366 | AtomIndexGetter);
|
---|
367 | }
|
---|
368 |
|
---|
369 | void QtObservedAtom::destroyObservedValues(
|
---|
370 | std::vector<boost::any> &_ObservedValues)
|
---|
371 | {
|
---|
372 | delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[AtomIndex]);
|
---|
373 | delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(_ObservedValues[AtomBonds]);
|
---|
374 | delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(_ObservedValues[AtomElement]);
|
---|
375 | delete boost::any_cast<ObservedValue_wCallback<moleculeId_t, atomId_t> *>(_ObservedValues[AtomMoleculeIndex]);
|
---|
376 | delete boost::any_cast<ObservedValue_wCallback<std::string, atomId_t> *>(_ObservedValues[AtomName]);
|
---|
377 | delete boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(_ObservedValues[AtomPosition]);
|
---|
378 | _ObservedValues.clear();
|
---|
379 | }
|
---|
380 |
|
---|
381 | const atomId_t& QtObservedAtom::getAtomIndex() const
|
---|
382 | {
|
---|
383 | return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->get();
|
---|
384 | }
|
---|
385 |
|
---|
386 | const QtObservedAtom::ListOfBonds_t& QtObservedAtom::getAtomBonds() const
|
---|
387 | {
|
---|
388 | return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(ObservedValues[AtomBonds])->get();
|
---|
389 | }
|
---|
390 |
|
---|
391 | const atomicNumber_t& QtObservedAtom::getAtomElement() const
|
---|
392 | {
|
---|
393 | return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[AtomElement])->get();
|
---|
394 | }
|
---|
395 |
|
---|
396 | const moleculeId_t& QtObservedAtom::getAtomMoleculeIndex() const
|
---|
397 | {
|
---|
398 | return boost::any_cast<ObservedValue_wCallback<moleculeId_t, atomId_t> *>(ObservedValues[AtomMoleculeIndex])->get();
|
---|
399 | }
|
---|
400 |
|
---|
401 | const std::string& QtObservedAtom::getAtomName() const
|
---|
402 | {
|
---|
403 | return boost::any_cast<ObservedValue_wCallback<std::string, atomId_t> *>(ObservedValues[AtomName])->get();
|
---|
404 | }
|
---|
405 |
|
---|
406 | const Vector& QtObservedAtom::getAtomPosition() const
|
---|
407 | {
|
---|
408 | return boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[AtomPosition])->get();
|
---|
409 | }
|
---|