source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp@ b2c2e4

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 b2c2e4 was 3b9aa1, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: All ObservedValue getters now return const references.

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