source: src/UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.cpp@ ee3fb8

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 ee3fb8 was 3054f4a, checked in by Frederik Heber <heber@…>, 9 years ago

QtObserved...Observed now get index on ...Removed() signal.

  • Property mode set to 100644
File size: 12.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 * QtObservedInstanceBoard.cpp
25 *
26 * Created on: Oct 17, 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 "QtObservedInstanceBoard.hpp"
37
38#include <QtCore/QMetaType>
39
40#include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp"
41#include "UIElements/Qt4/InstanceBoard/QtObservedMolecule.hpp"
42
43#include "CodePatterns/MemDebug.hpp"
44
45#include <boost/bind.hpp>
46
47#include "CodePatterns/Log.hpp"
48
49#include "Atom/atom.hpp"
50#include "Descriptors/AtomIdDescriptor.hpp"
51#include "Descriptors/MoleculeIdDescriptor.hpp"
52#include "molecule.hpp"
53#include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer_impl.hpp"
54#include "World.hpp"
55
56QtObservedInstanceBoard::QtObservedInstanceBoard(QWidget * _parent) :
57 QWidget(_parent),
58 Observer("QtObservedInstanceBoard"),
59 WorldSignedOn(false),
60 atomObservedValues(
61 "atom",
62 *this,
63 boost::bind(&QtObservedInstanceBoard::atomcountsubjectKilled, this, _1)),
64 moleculeObservedValues(
65 "molecule",
66 *this,
67 boost::bind(&QtObservedInstanceBoard::moleculecountsubjectKilled, this, _1))
68{
69 qRegisterMetaType<ObservedValue_Index_t>("ObservedValue_Index_t");
70 qRegisterMetaType<QtObservedAtom::ptr>("QtObservedAtom::ptr");
71 qRegisterMetaType<QtObservedMolecule::ptr>("QtObservedMolecule::ptr");
72
73 // be first (besides ObservedValues to know about new insertions)
74 World::getInstance().signOn(this, World::AtomInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
75 World::getInstance().signOn(this, World::MoleculeInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
76 WorldSignedOn = true;
77}
78
79QtObservedInstanceBoard::~QtObservedInstanceBoard()
80{
81 if (WorldSignedOn) {
82 World::getInstance().signOff(this, World::AtomInserted);
83 World::getInstance().signOff(this, World::MoleculeInserted);
84 }
85 // sign off from all remaining molecules and atoms
86 for (SignedOn_t::iterator iter = AtomSignedOn.begin(); !AtomSignedOn.empty();
87 iter = AtomSignedOn.begin()) {
88 (*iter)->signOff(this, atom::IndexChanged);
89 AtomSignedOn.erase(iter);
90 }
91
92 for (SignedOn_t::iterator iter = MoleculeSignedOn.begin(); !MoleculeSignedOn.empty();
93 iter = MoleculeSignedOn.begin()) {
94 (*iter)->signOff(this, molecule::IndexChanged);
95 MoleculeSignedOn.erase(iter);
96 }
97}
98
99void QtObservedInstanceBoard::update(Observable *publisher)
100{
101 ASSERT(0,
102 "QtObservedInstanceBoard::update() - we are not signed on to general updates.");
103}
104
105void QtObservedInstanceBoard::subjectKilled(Observable *publisher)
106{
107 SignedOn_t::iterator iter = AtomSignedOn.find(publisher);
108 if ( iter != AtomSignedOn.end()) {
109 LOG(3, "DEBUG: InstanceBoard got subjectKilled() from atom " << publisher);
110 AtomSignedOn.erase(iter);
111 } else {
112 iter = MoleculeSignedOn.find(publisher);
113 if ( iter != MoleculeSignedOn.end()) {
114 LOG(3, "DEBUG: InstanceBoard got subjectKilled() from molecule " << publisher);
115 MoleculeSignedOn.erase(iter);
116 } else {
117 ASSERT(0,
118 "QtObservedInstanceBoard::subjectKilled() - could not find signedOn for atom/molecule "+toString(publisher));
119 }
120 }
121}
122
123void QtObservedInstanceBoard::recieveNotification(Observable *publisher, Notification_ptr notification)
124{
125 if (static_cast<World *>(publisher) == World::getPointer()) {
126 switch (notification->getChannelNo()) {
127 case World::MoleculeInserted:
128 {
129 const moleculeId_t _id = const_cast<const World &>(World::getInstance()).lastChangedMolId();
130 #ifdef LOG_OBSERVER
131 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been inserted.";
132 #endif
133 LOG(3, "DEBUG: InformationBoard got moleculeInserted signal for molecule " << _id);
134 const molecule * const _molecule = const_cast<const World &>(World::getInstance()).
135 getMolecule(MoleculeById(_id));
136 if (_molecule != NULL) {
137 LOG(3, "DEBUG: InformationBoard initializes QtObservedMolecule for " << _id);
138 QtObservedMolecule::ptr observedmolecule(
139 new QtObservedMolecule(
140 _id,
141 _molecule,
142 *this));
143 observedmolecule->setSelfRef(observedmolecule);
144 const ObservedValue_Index_t index = static_cast<const Observable *>(_molecule);
145 moleculeObservedValues.insert(index, observedmolecule);
146 moleculeids_lookup.left.insert( std::make_pair(_id, index) );
147 // we need to check for index changes
148 LOG(3, "DEBUG: InformationBoard signOn()s to molecule " << _id);
149 _molecule->signOn(this, molecule::IndexChanged);
150 MoleculeSignedOn.insert( static_cast<Observable *>(const_cast<molecule *>(_molecule)) );
151
152 emit moleculeInserted(observedmolecule);
153 } else {
154 ELOG(1, "QtObservedInstanceBoard got MoleculeInserted for unknown molecule id " << _id);
155 }
156 break;
157 }
158 case World::AtomInserted:
159 {
160 const atomId_t _id = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
161 #ifdef LOG_OBSERVER
162 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
163 #endif
164 LOG(3, "DEBUG: InformationBoard got atomInserted signal for atom " << _id);
165 const atom * const _atom = const_cast<const World &>(World::getInstance()).
166 getAtom(AtomById(_id));
167 if (_atom!= NULL) {
168 LOG(3, "DEBUG: InformationBoard initializes QtObservedAtom for " << _id);
169 QtObservedAtom::ptr observedatom(
170 new QtObservedAtom(_id, _atom, *this));
171 observedatom->setSelfRef(observedatom);
172 const ObservedValue_Index_t index = static_cast<const Observable *>(_atom);
173 atomObservedValues.insert(index, observedatom);
174 atomids_lookup.left.insert( std::make_pair(_id, index) );
175 // we need to check for index changes
176 LOG(3, "DEBUG: InformationBoard signOn()s to atom " << _id);
177 _atom->signOn(this, atom::IndexChanged);
178 AtomSignedOn.insert( static_cast<Observable *>(const_cast<atom *>(_atom)) );
179 emit atomInserted(observedatom);
180 } else {
181 ELOG(1, "QtObservedInstanceBoard got AtomInserted for unknown atom id " << _id);
182 }
183 break;
184 }
185 default:
186 ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here for World.");
187 break;
188 }
189 } else if (dynamic_cast<molecule *>(publisher) != NULL) {
190 const moleculeId_t molid = const_cast<const World &>(World::getInstance()).lastChangedMolId();
191 switch (notification->getChannelNo()) {
192 case molecule::IndexChanged:
193 {
194 // molecule has changed its index
195 const moleculeId_t newmoleculeId = dynamic_cast<molecule *>(publisher)->getId();
196 LOG(3, "DEBUG: InformationBoard got IndexChanged from molecule " << molid << " to " << newmoleculeId);
197 {
198 typename IdtoIndex_t<moleculeId_t>::left_iterator iter = moleculeids_lookup.left.find(molid);
199 ASSERT( iter != moleculeids_lookup.left.end(),
200 "QtObservedInstanceBoard::recieveNotification() - mol id "
201 +toString(molid)+" unknown to lookup.");
202 const ObservedValue_Index_t index = iter->second;
203 moleculeids_lookup.left.erase(iter);
204 moleculeids_lookup.left.insert( std::pair<moleculeId_t, ObservedValue_Index_t>(newmoleculeId, index) );
205 }
206 // no need update SignedOn, ref does not change
207 emit moleculeIndexChanged(molid, newmoleculeId);
208 break;
209 }
210 default:
211 ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
212 break;
213 }
214 } else if (dynamic_cast<atom *>(publisher) != NULL) {
215 const atomId_t oldatomId = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
216 switch (notification->getChannelNo()) {
217 case AtomObservable::IndexChanged:
218 {
219 const atomId_t newatomId = dynamic_cast<atom *>(publisher)->getId();
220 LOG(3, "DEBUG: InformationBoard got IndexChanged from atom " << oldatomId << " to " << newatomId);
221 {
222 typename IdtoIndex_t<atomId_t>::left_iterator iter = atomids_lookup.left.find(oldatomId);
223 ASSERT( iter != atomids_lookup.left.end(),
224 "QtObservedInstanceBoard::recieveNotification() - atom id "
225 +toString(oldatomId)+" unknown to lookup.");
226 const ObservedValue_Index_t index = iter->second;
227 atomids_lookup.left.erase(iter);
228 atomids_lookup.left.insert( std::pair<atomId_t, ObservedValue_Index_t>(newatomId, index) );
229 }
230 // no need update SignedOn, ref does not change
231 emit atomIndexChanged(oldatomId, newatomId);
232 break;
233 }
234 default:
235 ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
236 break;
237 }
238 } else {
239 ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - notification from unknown source.");
240 }
241}
242
243void QtObservedInstanceBoard::atomcountsubjectKilled(ObservedValue_Index_t _id)
244{
245 LOG(3, "DEBUG: InstanceBoard emits atomRemoved for " << _id);
246 emit atomRemoved(_id);
247}
248
249void QtObservedInstanceBoard::moleculecountsubjectKilled(ObservedValue_Index_t _id)
250{
251 LOG(3, "DEBUG: InstanceBoard emits moleculeRemoved for " << _id);
252 emit moleculeRemoved(_id);
253}
254
255const atomId_t QtObservedInstanceBoard::getAtomIdToIndex(ObservedValue_Index_t _id) const
256{
257 typename IdtoIndex_t<atomId_t>::right_const_iterator iter = atomids_lookup.right.find(_id);
258 ASSERT( iter != atomids_lookup.right.end(),
259 "QtObservedInstanceBoard::getAtomIdToIndex() - index "
260 +toString(_id)+" is unknown.");
261 return iter->second;
262}
263
264const moleculeId_t QtObservedInstanceBoard::getMoleculeIdToIndex(ObservedValue_Index_t _id) const
265{
266 typename IdtoIndex_t<moleculeId_t>::right_const_iterator iter = moleculeids_lookup.right.find(_id);
267 ASSERT( iter != moleculeids_lookup.right.end(),
268 "QtObservedInstanceBoard::getMoleculeIdToIndex() - index "
269 +toString(_id)+" is unknown.");
270 return iter->second;
271}
272
273QtObservedAtom::ptr QtObservedInstanceBoard::getObservedAtom(const atomId_t _id)
274{
275 return atomObservedValues.get(atomids_lookup.left.at(_id));
276}
277
278QtObservedAtom::ptr QtObservedInstanceBoard::getObservedAtom(ObservedValue_Index_t _id)
279{
280 return atomObservedValues.get(_id);
281}
282
283QtObservedMolecule::ptr QtObservedInstanceBoard::getObservedMolecule(const moleculeId_t _id)
284{
285 return moleculeObservedValues.get(moleculeids_lookup.left.at(_id));
286}
287
288QtObservedMolecule::ptr QtObservedInstanceBoard::getObservedMolecule(ObservedValue_Index_t _id)
289{
290 return moleculeObservedValues.get(_id);
291}
292
293void QtObservedInstanceBoard::markObservedAtomAsConnected(ObservedValue_Index_t _id)
294{
295 atomObservedValues.markObservedValuesAsConnected(_id);
296}
297
298void QtObservedInstanceBoard::markObservedAtomAsDisconnected(ObservedValue_Index_t _id)
299{
300 atomObservedValues.markObservedValuesAsDisconnected(_id);
301}
302
303void QtObservedInstanceBoard::markObservedAtomForErase(ObservedValue_Index_t _id)
304{
305 atomObservedValues.eraseObservedValues(_id);
306 atomids_lookup.right.erase(_id);
307}
308
309void QtObservedInstanceBoard::markObservedMoleculeAsConnected(ObservedValue_Index_t _id)
310{
311 moleculeObservedValues.markObservedValuesAsConnected(_id);
312}
313
314void QtObservedInstanceBoard::markObservedMoleculeAsDisconnected(ObservedValue_Index_t _id)
315{
316 moleculeObservedValues.markObservedValuesAsDisconnected(_id);
317}
318
319void QtObservedInstanceBoard::markObservedMoleculeForErase(ObservedValue_Index_t _id)
320{
321 moleculeObservedValues.eraseObservedValues(_id);
322 moleculeids_lookup.right.erase(_id);
323}
Note: See TracBrowser for help on using the repository browser.