source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp@ 1c0961

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 1c0961 was 1c0961, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: indexChanged from QtObserved... transmit both old and new id.

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