source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.cpp@ d1831e

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

QtMoleculeItem_.. now shout when they receive subjectKilled().

  • this way we do not have this double system of both QtMoleculeList (in a lazy fashion) and each QtMoleculeList listens for moleculeRemoved (one through the Observer channel, the other through subjectKilled()).
  • we simply count the number of QtMoleculeItems that received subjectKilled() and if max is reached, the row is removed.
  • to prevent molecule id clash, insertion (of same id) after removal is ok. This works cause we first remove and then insert new item. Hence, having the same id is possible.
  • Property mode set to 100644
File size: 5.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 * QtMoleculeItem.cpp
25 *
26 * Created on: Jan 17, 2015
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "QtMoleculeItem.hpp"
36
37#include <QModelIndex>
38
39#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
40
41#include "CodePatterns/MemDebug.hpp"
42
43#include "CodePatterns/Assert.hpp"
44#include "CodePatterns/toString.hpp"
45
46#include "CodePatterns/Observer/Notification.hpp"
47
48#include <algorithm>
49
50#include <boost/thread/locks.hpp>
51
52#include "Descriptors/MoleculeIdDescriptor.hpp"
53#include "molecule.hpp"
54#include "World.hpp"
55
56QtMoleculeItem::QtMoleculeItem(
57 const moleculeId_t _molid,
58 const channellist_t &_channellist,
59 const enum MoveTypes _movetype,
60 const emitDirtyState_t _emitDirtyState,
61 const emitSubjectKilledState_t _emitSubjectKilledState) :
62 Observer("QtMoleculeItem"),
63 molid(_molid),
64 movetype(_movetype),
65 channellist(_channellist),
66 owner(NULL),
67 dirty(true),
68 emitDirtyState(_emitDirtyState),
69 emitSubjectKilledState(_emitSubjectKilledState)
70{
71 // BUG: We cannot sign on here as then and again we catch "pure virtual function called"
72 // this makes no sense as both the Observer part (which also has a recievedNotification())
73 // nor QtMoleculeItem's vtable have been fully created.
74// signOnToMolecule();
75 setFlags(flags() | Qt::ItemIsSelectable);
76}
77
78void QtMoleculeItem::signOnToMolecule()
79{
80 // count the number of threads entering the function
81 std::cout << "QtMoleculeItem::signOnToMolecule() called for " << this << std::endl;
82 const molecule * const mol = const_cast<const World &>(World::getInstance()).
83 getMolecule(MoleculeById(molid));
84 if (mol != NULL) {
85 owner_mutex.lock();
86 if (owner == NULL) {
87 owner_mutex.unlock();
88 const Observable *owner_copy = static_cast<const Observable *>(mol);
89 for (channellist_t::const_iterator channeliter = channellist.begin();
90 channeliter != channellist.end(); ++channeliter)
91 owner_copy->signOn(this, *channeliter);
92 owner_mutex.lock();
93 owner = owner_copy;
94 owner_mutex.unlock();
95 std::cout << "QtMoleculeItem " << this << " has signed on to " << owner << std::endl;
96 } else
97 owner_mutex.unlock();
98 }
99}
100
101void QtMoleculeItem::signOffFromMolecule()
102{
103 // prevent via owner that another thread enters the function again
104 // (re-entrancy)
105 const Observable *owner_copy = NULL;
106 {
107 boost::recursive_mutex::scoped_lock lock(owner_mutex);
108 if (owner != NULL) {
109 owner_copy = owner;
110 owner = NULL;
111 }
112 }
113 std::cout << "QtMoleculeItem::signOffFromMolecule() called for " << this << std::endl;
114 if (owner_copy != NULL) {
115 for (channellist_t::const_iterator channeliter = channellist.begin();
116 channeliter != channellist.end(); ++channeliter)
117 owner_copy->signOff(this, *channeliter);
118 std::cout << "QtMoleculeItem " << this << " has signed off from " << owner_copy << std::endl;
119 }
120}
121
122QtMoleculeItem::~QtMoleculeItem()
123{
124 // BUG: We cannot sign off here as then and again we catch "pure virtual function called"
125 // this makes no sense as neither the Observer part (which also has a recievedNotification())
126 // nor QtMoleculeItem's vtable have yet been destroyed.
127// signOffFromMolecule();
128}
129
130void QtMoleculeItem::updateState()
131{
132 if (dirty) {
133 internal_updateState();
134 dirty = false;
135 }
136}
137
138void QtMoleculeItem::update(Observable *publisher)
139{}
140
141void QtMoleculeItem::recieveNotification(Observable *publisher, Notification_ptr notification)
142{
143 if (dynamic_cast<molecule *>(publisher) != NULL) {
144 if (notification->getChannelNo() == molecule::AboutToBeRemoved) {
145 signOffFromMolecule();
146 // prevent any remaining updates from accessing the molecule
147 //molid = -1;
148 dirty = false;
149 } else {
150 channellist_t::const_iterator iter =
151 std::find(channellist.begin(), channellist.end(), notification->getChannelNo());
152 if (iter != channellist.end()) {
153 dirty = true;
154 emitDirtyState(getMoleculeId(), getType(), movetype);
155 } else {
156 ASSERT(0,
157 "QtMoleculeItem::recieveNotification() - received notification to channel "
158 +toString(notification->getChannelNo())+" we are not subscribed to.");
159 }
160 }
161 }
162}
163
164void QtMoleculeItem::subjectKilled(Observable *publisher)
165{
166 signOffFromMolecule();
167 dirty = false;
168 emitSubjectKilledState(getMoleculeId());
169}
170
171const molecule * const QtMoleculeItem::getMolecule() const
172{
173 const molecule * const mol = const_cast<const World &>(World::getInstance()).getMolecule(MoleculeById(molid));
174// ASSERT (mol != NULL,
175// "QtMoleculeItem::getMolecule() - mol with "+toString(molid)+" is gone.");
176 return mol;
177}
Note: See TracBrowser for help on using the repository browser.