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 | * QtObservedMolecule.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 "QtObservedMolecule.hpp"
|
---|
37 |
|
---|
38 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
|
---|
39 |
|
---|
40 | #include "CodePatterns/MemDebug.hpp"
|
---|
41 | #include "CodePatterns/Assert.hpp"
|
---|
42 | #include "CodePatterns/Log.hpp"
|
---|
43 |
|
---|
44 | #include <boost/assign.hpp>
|
---|
45 | #include <boost/bind.hpp>
|
---|
46 |
|
---|
47 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp"
|
---|
48 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_UpdateAtoms.hpp"
|
---|
49 |
|
---|
50 | #include "Atom/atom.hpp"
|
---|
51 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
52 | #include "World.hpp"
|
---|
53 |
|
---|
54 | using namespace boost::assign;
|
---|
55 |
|
---|
56 | static const Observable::channels_t getAllObservedChannels()
|
---|
57 | {
|
---|
58 | Observable::channels_t channels;
|
---|
59 | channels +=
|
---|
60 | molecule::AtomInserted,
|
---|
61 | molecule::AtomRemoved,
|
---|
62 | molecule::FormulaChanged,
|
---|
63 | molecule::IndexChanged,
|
---|
64 | molecule::MoleculeNameChanged,
|
---|
65 | molecule::BoundingBoxChanged;
|
---|
66 | return channels;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static const Observable::channels_t getAllAtomCountChannels()
|
---|
70 | {
|
---|
71 | Observable::channels_t channels;
|
---|
72 | channels +=
|
---|
73 | molecule::AtomInserted,
|
---|
74 | molecule::AtomRemoved;
|
---|
75 | return channels;
|
---|
76 | }
|
---|
77 |
|
---|
78 | // static instances
|
---|
79 | const Observable::channels_t QtObservedMolecule::AtomCountChannels(getAllAtomCountChannels());
|
---|
80 | const Observable::channels_t QtObservedMolecule::BoundingBoxChannels(1, molecule::BoundingBoxChanged);
|
---|
81 | const Observable::channels_t QtObservedMolecule::FormulaStringChannels(1, molecule::FormulaChanged);
|
---|
82 | const Observable::channels_t QtObservedMolecule::IndexChannels(1, molecule::IndexChanged);
|
---|
83 | const Observable::channels_t QtObservedMolecule::NameChannels(1, molecule::MoleculeNameChanged);
|
---|
84 |
|
---|
85 | QtObservedMolecule::QtObservedMolecule(
|
---|
86 | const ObservedValues_t &_ObservedValues,
|
---|
87 | QtObservedInstanceBoard &_board,
|
---|
88 | QWidget * _parent) :
|
---|
89 | QWidget(_parent),
|
---|
90 | Observer("QtObservedMolecule"),
|
---|
91 | subjectKilledCount(0),
|
---|
92 | AllsignedOnChannels(getAllObservedChannels().size()),
|
---|
93 | signedOffChannels(0),
|
---|
94 | owner(NULL),
|
---|
95 | board(_board),
|
---|
96 | ObservedValues(_ObservedValues)
|
---|
97 | {
|
---|
98 | // activating Observer is done by ObservedValueContainer when it's prepared
|
---|
99 | }
|
---|
100 |
|
---|
101 | QtObservedMolecule::~QtObservedMolecule()
|
---|
102 | {
|
---|
103 | deactivateObserver();
|
---|
104 | }
|
---|
105 |
|
---|
106 | void QtObservedMolecule::deactivateObserver()
|
---|
107 | {
|
---|
108 | if (owner != NULL) {
|
---|
109 | Observable::channels_t channels = getAllObservedChannels();
|
---|
110 | for (Observable::channels_t::const_iterator iter = channels.begin();
|
---|
111 | iter != channels.end(); ++iter)
|
---|
112 | owner->signOff(this, *iter);
|
---|
113 | owner = NULL;
|
---|
114 | signedOffChannels = AllsignedOnChannels;
|
---|
115 | board.markObservedMoleculeAsDisconnected(getMolIndex());
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | void QtObservedMolecule::activateObserver()
|
---|
120 | {
|
---|
121 | // sign on as observer (obtain non-const instance before)
|
---|
122 | const molecule * const _molecule = getMolecule(getMolIndex());
|
---|
123 | if (_molecule != NULL) {
|
---|
124 | Observable::channels_t channels = getAllObservedChannels();
|
---|
125 | owner = static_cast<const Observable *>(_molecule);
|
---|
126 | for (Observable::channels_t::const_iterator iter = channels.begin();
|
---|
127 | iter != channels.end(); ++iter)
|
---|
128 | owner->signOn(this, *iter);
|
---|
129 | } else
|
---|
130 | signedOffChannels = AllsignedOnChannels;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void QtObservedMolecule::update(Observable *publisher)
|
---|
134 | {
|
---|
135 | ASSERT(0,
|
---|
136 | "QtObservedMolecule::update() - general update from unexpected source.");
|
---|
137 | }
|
---|
138 |
|
---|
139 | void QtObservedMolecule::subjectKilled(Observable *publisher)
|
---|
140 | {
|
---|
141 | ++signedOffChannels;
|
---|
142 |
|
---|
143 | if (signedOffChannels == AllsignedOnChannels) {
|
---|
144 | // remove owner: no more signOff needed
|
---|
145 | owner = NULL;
|
---|
146 |
|
---|
147 | board.markObservedMoleculeAsDisconnected(getMolIndex());
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | void QtObservedMolecule::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
152 | {
|
---|
153 | const molecule * const _molecule = getMolecule(getMolIndex());
|
---|
154 | // when molecule is NULL we will soon get destroyed anyway
|
---|
155 | if (_molecule == NULL)
|
---|
156 | return;
|
---|
157 | if (publisher == dynamic_cast<const Observable*>(_molecule)){
|
---|
158 | // notification from atom
|
---|
159 | #ifdef LOG_OBSERVER
|
---|
160 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
161 | << " received notification from molecule " << getMolIndex() << " for channel "
|
---|
162 | << notification->getChannelNo() << ".";
|
---|
163 | #endif
|
---|
164 | switch (notification->getChannelNo()) {
|
---|
165 | case molecule::AtomInserted:
|
---|
166 | {
|
---|
167 | const atomId_t _id = _molecule->lastChangedAtomId();
|
---|
168 | emit atomcountChanged();
|
---|
169 | emit atomInserted(_id);
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | case molecule::AtomRemoved:
|
---|
173 | {
|
---|
174 | const atomId_t _id = _molecule->lastChangedAtomId();
|
---|
175 | emit atomcountChanged();
|
---|
176 | emit atomRemoved(_id);
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | case molecule::BoundingBoxChanged:
|
---|
180 | {
|
---|
181 | #ifdef LOG_OBSERVER
|
---|
182 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that bounding box has changed.";
|
---|
183 | #endif
|
---|
184 | emit tesselationhullChanged();
|
---|
185 | emit boundingboxChanged();
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | case molecule::FormulaChanged:
|
---|
189 | {
|
---|
190 | emit formulaChanged();
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | case molecule::IndexChanged:
|
---|
194 | {
|
---|
195 | #ifdef LOG_OBSERVER
|
---|
196 | const atomId_t _id = _molecule->lastChangedAtomId();
|
---|
197 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+"'s index has changed.";
|
---|
198 | #endif
|
---|
199 | emit indexChanged();
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | case molecule::MoleculeNameChanged:
|
---|
203 | {
|
---|
204 | #ifdef LOG_OBSERVER
|
---|
205 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that name has changed.";
|
---|
206 | #endif
|
---|
207 | emit nameChanged();
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | default:
|
---|
211 | break;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | const molecule * const QtObservedMolecule::getMolecule(const moleculeId_t _id)
|
---|
217 | {
|
---|
218 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
219 | getMolecule(MoleculeById(_id));
|
---|
220 | return mol;
|
---|
221 | }
|
---|
222 |
|
---|
223 | static molecule::BoundingBoxInfo initBoundingBox()
|
---|
224 | {
|
---|
225 | molecule::BoundingBoxInfo info;
|
---|
226 | info.position = zeroVec;
|
---|
227 | info.radius = 0.;
|
---|
228 | return info;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void QtObservedMolecule::initObservedValues(
|
---|
232 | ObservedValues_t &_ObservedValues,
|
---|
233 | const moleculeId_t _molid,
|
---|
234 | const molecule * const _molref,
|
---|
235 | const boost::function<void(const moleculeId_t)> &_subjectKilled)
|
---|
236 | {
|
---|
237 | /* This is an old note from when the code was still part of cstor's initializer body.
|
---|
238 | * TODO: Probably does not apply anymore but has not yet been tested.
|
---|
239 | *
|
---|
240 | * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
|
---|
241 | * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
|
---|
242 | * the class has not been fully constructed yet. "This" itself seems to be working fine.
|
---|
243 | */
|
---|
244 |
|
---|
245 | ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
|
---|
246 | "QtObservedMolecule::initObservedValues() - given ObservedValues has not correct size.");
|
---|
247 |
|
---|
248 | // fill ObservedValues: index first
|
---|
249 | const boost::function<moleculeId_t ()> MolIndexUpdater(
|
---|
250 | boost::bind(&QtObservedMolecule::updateIndex));
|
---|
251 |
|
---|
252 | ObservedValue_wCallback<moleculeId_t> * const IndexObservable =
|
---|
253 | new ObservedValue_wCallback<moleculeId_t>(
|
---|
254 | _molref,
|
---|
255 | MolIndexUpdater,
|
---|
256 | "MoleculeIndex_"+toString(_molid),
|
---|
257 | _molid,
|
---|
258 | IndexChannels,
|
---|
259 | _subjectKilled);
|
---|
260 | _ObservedValues[MolIndex] = IndexObservable;
|
---|
261 |
|
---|
262 | const boost::function<const moleculeId_t ()> MolIndexGetter =
|
---|
263 | boost::bind(&ObservedValue_wCallback<moleculeId_t>::get,
|
---|
264 | IndexObservable);
|
---|
265 |
|
---|
266 | // fill ObservedValues: then all the other that need index
|
---|
267 | const boost::function<int ()> AtomCountUpdater(
|
---|
268 | boost::bind(&QtObservedMolecule::updateAtomCount, MolIndexGetter));
|
---|
269 | const boost::function<std::string ()> MolFormulaUpdater(
|
---|
270 | boost::bind(&QtObservedMolecule::updateFormulaString, MolIndexGetter));
|
---|
271 | const boost::function<std::string ()> MolNameUpdater(
|
---|
272 | boost::bind(&QtObservedMolecule::updateName, MolIndexGetter));
|
---|
273 | const boost::function<molecule::BoundingBoxInfo ()> BoundingBoxUpdater(
|
---|
274 | boost::bind(&QtObservedMolecule::updateBoundingBox, MolIndexGetter));
|
---|
275 |
|
---|
276 | _ObservedValues[AtomCount] = new ObservedValue_wCallback<int, moleculeId_t>(
|
---|
277 | _molref,
|
---|
278 | AtomCountUpdater,
|
---|
279 | "MoleculeAtomCount_"+toString(_molid),
|
---|
280 | AtomCountUpdater(),
|
---|
281 | AtomCountChannels,
|
---|
282 | _subjectKilled,
|
---|
283 | MolIndexGetter);
|
---|
284 | _ObservedValues[BoundingBox] = new ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t>(
|
---|
285 | _molref,
|
---|
286 | BoundingBoxUpdater,
|
---|
287 | "MoleculeBoundingBox_"+toString(_molid),
|
---|
288 | initBoundingBox(),
|
---|
289 | BoundingBoxChannels,
|
---|
290 | _subjectKilled,
|
---|
291 | MolIndexGetter);
|
---|
292 | _ObservedValues[FormulaString] = new ObservedValue_wCallback<std::string, moleculeId_t>(
|
---|
293 | _molref,
|
---|
294 | MolFormulaUpdater,
|
---|
295 | "MoleculeFormula_"+toString(_molid),
|
---|
296 | MolFormulaUpdater(),
|
---|
297 | FormulaStringChannels,
|
---|
298 | _subjectKilled,
|
---|
299 | MolIndexGetter);
|
---|
300 | _ObservedValues[MolName] = new ObservedValue_wCallback<std::string, moleculeId_t>(
|
---|
301 | _molref,
|
---|
302 | MolNameUpdater,
|
---|
303 | "MoleculeName_"+toString(_molid),
|
---|
304 | MolNameUpdater(),
|
---|
305 | NameChannels,
|
---|
306 | _subjectKilled,
|
---|
307 | MolIndexGetter);
|
---|
308 | }
|
---|
309 |
|
---|
310 | void QtObservedMolecule::destroyObservedValues(
|
---|
311 | std::vector<boost::any> &_ObservedValues)
|
---|
312 | {
|
---|
313 | delete boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(_ObservedValues[MolIndex]);
|
---|
314 | delete boost::any_cast<ObservedValue_wCallback<int, moleculeId_t> *>(_ObservedValues[AtomCount]);
|
---|
315 | delete boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(_ObservedValues[FormulaString]);
|
---|
316 | delete boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(_ObservedValues[BoundingBox]);
|
---|
317 | delete boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(_ObservedValues[MolName]);
|
---|
318 | _ObservedValues.clear();
|
---|
319 | }
|
---|
320 |
|
---|
321 | int QtObservedMolecule::updateAtomCount(
|
---|
322 | const boost::function<const moleculeId_t ()> &_getMolIndex)
|
---|
323 | {
|
---|
324 | const molecule * const mol = getMolecule(_getMolIndex());
|
---|
325 | if (mol != NULL)
|
---|
326 | return mol->getAtomCount();
|
---|
327 | else
|
---|
328 | return (int)0;
|
---|
329 | }
|
---|
330 |
|
---|
331 | molecule::BoundingBoxInfo QtObservedMolecule::updateBoundingBox(
|
---|
332 | const boost::function<const moleculeId_t ()> &_getMolIndex)
|
---|
333 | {
|
---|
334 | const molecule * const mol = getMolecule(_getMolIndex());
|
---|
335 | if (mol != NULL)
|
---|
336 | return mol->getBoundingBox();
|
---|
337 | else
|
---|
338 | return molecule::BoundingBoxInfo();
|
---|
339 | }
|
---|
340 |
|
---|
341 | std::string QtObservedMolecule::updateFormulaString(
|
---|
342 | const boost::function<const moleculeId_t ()> &_getMolIndex)
|
---|
343 | {
|
---|
344 | const molecule * const mol = getMolecule(_getMolIndex());
|
---|
345 | if (mol != NULL)
|
---|
346 | return mol->getFormula().toString();
|
---|
347 | else
|
---|
348 | return std::string("");
|
---|
349 | }
|
---|
350 |
|
---|
351 | moleculeId_t QtObservedMolecule::updateIndex()
|
---|
352 | {
|
---|
353 | return const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
354 | }
|
---|
355 |
|
---|
356 | std::string QtObservedMolecule::updateName(
|
---|
357 | const boost::function<const moleculeId_t ()> &_getMolIndex)
|
---|
358 | {
|
---|
359 | const molecule * const mol = getMolecule(_getMolIndex());
|
---|
360 | if (mol != NULL)
|
---|
361 | return mol->getName();
|
---|
362 | else
|
---|
363 | return std::string("");
|
---|
364 | }
|
---|
365 |
|
---|
366 | int QtObservedMolecule::getAtomCount() const
|
---|
367 | {
|
---|
368 | return boost::any_cast<ObservedValue_wCallback<int, moleculeId_t> *>(ObservedValues[AtomCount])->get();
|
---|
369 | }
|
---|
370 |
|
---|
371 | std::string QtObservedMolecule::getMolFormula() const
|
---|
372 | {
|
---|
373 | return boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(ObservedValues[FormulaString])->get();
|
---|
374 | }
|
---|
375 |
|
---|
376 | moleculeId_t QtObservedMolecule::getMolIndex() const
|
---|
377 | {
|
---|
378 | return boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(ObservedValues[MolIndex])->get();
|
---|
379 | }
|
---|
380 |
|
---|
381 | std::string QtObservedMolecule::getMolName() const
|
---|
382 | {
|
---|
383 | return boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(ObservedValues[MolName])->get();
|
---|
384 | }
|
---|
385 |
|
---|
386 | molecule::BoundingBoxInfo QtObservedMolecule::getBoundingBox() const
|
---|
387 | {
|
---|
388 | return boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(ObservedValues[BoundingBox])->get();
|
---|
389 | }
|
---|