source: src/UIElements/Qt4/InstanceBoard/QtObservedAtom.cpp@ 5cd3a33

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 5cd3a33 was 9e9100, checked in by Frederik Heber <heber@…>, 9 years ago

Added ObservedValue for selection status of atoms and molecules to QtObserved...

  • Property mode set to 100644
File size: 14.6 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 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
109QtObservedAtom::~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
122const 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
129atom * const QtObservedAtom::getAtom(const atomId_t _id)
130{
131 atom * const _atom = World::getInstance().getAtom(AtomById(_id));
132 return _atom;
133}
134
135atomId_t QtObservedAtom::updateIndex()
136{
137 return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
138}
139
140QtObservedAtom::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
158atomicNumber_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
169moleculeId_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
180std::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
191Vector 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
202bool 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
213void QtObservedAtom::update(Observable *publisher)
214{
215 ASSERT(0, "QtObservedAtom::update() - we are not signed on for global updates.");
216}
217
218void 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
235void 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
268void 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
283void 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
298void 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
397void 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
410const atomId_t& QtObservedAtom::getAtomIndex() const
411{
412 return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->get();
413}
414
415const QtObservedAtom::ListOfBonds_t& QtObservedAtom::getAtomBonds() const
416{
417 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(ObservedValues[AtomBonds])->get();
418}
419
420const atomicNumber_t& QtObservedAtom::getAtomElement() const
421{
422 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[AtomElement])->get();
423}
424
425const moleculeId_t& QtObservedAtom::getAtomMoleculeIndex() const
426{
427 return boost::any_cast<ObservedValue_wCallback<moleculeId_t, atomId_t> *>(ObservedValues[AtomMoleculeIndex])->get();
428}
429
430const std::string& QtObservedAtom::getAtomName() const
431{
432 return boost::any_cast<ObservedValue_wCallback<std::string, atomId_t> *>(ObservedValues[AtomName])->get();
433}
434
435const Vector& QtObservedAtom::getAtomPosition() const
436{
437 return boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[AtomPosition])->get();
438}
439
440const bool QtObservedAtom::getAtomSelected() const
441{
442 return boost::any_cast<ObservedValue_wCallback<bool, atomId_t> *>(ObservedValues[AtomSelected])->get();
443}
Note: See TracBrowser for help on using the repository browser.