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