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 |
|
---|
56 | QtObservedInstanceBoard::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 | lastremovedatom((atomId_t)-1),
|
---|
69 | lastremovedmolecule((moleculeId_t)-1)
|
---|
70 | {
|
---|
71 | qRegisterMetaType<QtObservedAtom::ptr>("QtObservedAtom::ptr");
|
---|
72 | qRegisterMetaType<QtObservedMolecule::ptr>("QtObservedMolecule::ptr");
|
---|
73 |
|
---|
74 | // be first (besides ObservedValues to know about new insertions)
|
---|
75 | World::getInstance().signOn(this, World::AtomInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
76 | World::getInstance().signOn(this, World::AtomRemoved, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
77 | World::getInstance().signOn(this, World::MoleculeInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
78 | World::getInstance().signOn(this, World::MoleculeRemoved, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
79 | WorldSignedOn = true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | QtObservedInstanceBoard::~QtObservedInstanceBoard()
|
---|
83 | {
|
---|
84 | if (WorldSignedOn) {
|
---|
85 | World::getInstance().signOff(this, World::AtomInserted);
|
---|
86 | World::getInstance().signOff(this, World::AtomRemoved);
|
---|
87 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
88 | World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
89 | }
|
---|
90 | // sign off from all remaining molecules and atoms
|
---|
91 | for (SignedOn_t::iterator iter = AtomSignedOn.begin(); !AtomSignedOn.empty();
|
---|
92 | iter = AtomSignedOn.begin()) {
|
---|
93 | (*iter)->signOff(this, atom::IndexChanged);
|
---|
94 | AtomSignedOn.erase(iter);
|
---|
95 | }
|
---|
96 |
|
---|
97 | for (SignedOn_t::iterator iter = MoleculeSignedOn.begin(); !MoleculeSignedOn.empty();
|
---|
98 | iter = MoleculeSignedOn.begin()) {
|
---|
99 | Observable * const _observable = *iter;
|
---|
100 | _observable->signOff(this, molecule::IndexChanged);
|
---|
101 | // remove all three remaining instances in multiset
|
---|
102 | for (SignedOn_t::iterator sameiter = MoleculeSignedOn.find(_observable);
|
---|
103 | sameiter != MoleculeSignedOn.end();
|
---|
104 | sameiter = MoleculeSignedOn.find(_observable))
|
---|
105 | MoleculeSignedOn.erase(*sameiter); //erase all instances
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void QtObservedInstanceBoard::update(Observable *publisher)
|
---|
110 | {
|
---|
111 | ASSERT(0,
|
---|
112 | "QtObservedInstanceBoard::update() - we are not signed on to general updates.");
|
---|
113 | }
|
---|
114 |
|
---|
115 | void QtObservedInstanceBoard::subjectKilled(Observable *publisher)
|
---|
116 | {
|
---|
117 | SignedOn_t::iterator iter = AtomSignedOn.find(publisher);
|
---|
118 | if ( iter != AtomSignedOn.end()) {
|
---|
119 | LOG(3, "DEBUG: InstanceBoard got subjectKilled() from atom " << publisher);
|
---|
120 | AtomSignedOn.erase(iter);
|
---|
121 | } else {
|
---|
122 | iter = MoleculeSignedOn.find(publisher);
|
---|
123 | if ( iter != MoleculeSignedOn.end()) {
|
---|
124 | LOG(3, "DEBUG: InstanceBoard got subjectKilled() from molecule " << publisher);
|
---|
125 | MoleculeSignedOn.erase(iter);
|
---|
126 | } else {
|
---|
127 | ASSERT(0,
|
---|
128 | "QtObservedInstanceBoard::subjectKilled() - could not find signedOn for atom/molecule "+toString(publisher));
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | void QtObservedInstanceBoard::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
134 | {
|
---|
135 | if (static_cast<World *>(publisher) == World::getPointer()) {
|
---|
136 | switch (notification->getChannelNo()) {
|
---|
137 | case World::MoleculeInserted:
|
---|
138 | {
|
---|
139 | const moleculeId_t _id = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
140 | #ifdef LOG_OBSERVER
|
---|
141 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been inserted.";
|
---|
142 | #endif
|
---|
143 | LOG(3, "DEBUG: InformationBoard got moleculeInserted signal for molecule " << _id);
|
---|
144 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()).
|
---|
145 | getMolecule(MoleculeById(_id));
|
---|
146 | if (_molecule != NULL) {
|
---|
147 | LOG(3, "DEBUG: InformationBoard initializes QtObservedMolecule for " << _id);
|
---|
148 | QtObservedMolecule::ptr observedmolecule(
|
---|
149 | new QtObservedMolecule(
|
---|
150 | _id,
|
---|
151 | _molecule,
|
---|
152 | *this));
|
---|
153 | moleculeObservedValues.insert(_id, observedmolecule);
|
---|
154 | // we need to check for index changes
|
---|
155 | LOG(3, "DEBUG: InformationBoard signOn()s to molecule " << _id);
|
---|
156 | _molecule->signOn(this, molecule::IndexChanged);
|
---|
157 | MoleculeSignedOn.insert( static_cast<Observable *>(const_cast<molecule *>(_molecule)) );
|
---|
158 |
|
---|
159 | emit moleculeInserted(observedmolecule);
|
---|
160 | } else {
|
---|
161 | ELOG(1, "QtObservedInstanceBoard got MoleculeInserted for unknown molecule id " << _id);
|
---|
162 | }
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | case World::MoleculeRemoved:
|
---|
166 | {
|
---|
167 | const moleculeId_t _id = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
168 | LOG(3, "DEBUG: InformationBoard got MoleculeRemoved signal for molecule " << _id);
|
---|
169 | // note down such that ObservedValues are simply dropped
|
---|
170 | lastremovedmolecule = _id;
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | case World::AtomInserted:
|
---|
174 | {
|
---|
175 | const atomId_t _id = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
176 | #ifdef LOG_OBSERVER
|
---|
177 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
|
---|
178 | #endif
|
---|
179 | LOG(3, "DEBUG: InformationBoard got atomInserted signal for atom " << _id);
|
---|
180 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
181 | getAtom(AtomById(_id));
|
---|
182 | if (_atom!= NULL) {
|
---|
183 | LOG(3, "DEBUG: InformationBoard initializes QtObservedAtom for " << _id);
|
---|
184 | QtObservedAtom::ptr observedatom(
|
---|
185 | new QtObservedAtom(_id, _atom, *this));
|
---|
186 | atomObservedValues.insert(_id, observedatom);
|
---|
187 | // we need to check for index changes
|
---|
188 | LOG(3, "DEBUG: InformationBoard signOn()s to atom " << _id);
|
---|
189 | _atom->signOn(this, atom::IndexChanged);
|
---|
190 | AtomSignedOn.insert( static_cast<Observable *>(const_cast<atom *>(_atom)) );
|
---|
191 | emit atomInserted(observedatom);
|
---|
192 | } else {
|
---|
193 | ELOG(1, "QtObservedInstanceBoard got AtomInserted for unknown atom id " << _id);
|
---|
194 | }
|
---|
195 | break;
|
---|
196 | }
|
---|
197 | case World::AtomRemoved:
|
---|
198 | {
|
---|
199 | const atomId_t _id = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
200 | LOG(3, "DEBUG: InformationBoard got AtomRemoved signal for atom " << _id);
|
---|
201 | // note down such that ObservedValues are simply dropped
|
---|
202 | lastremovedatom = _id;
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | default:
|
---|
206 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here for World.");
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | } else if (dynamic_cast<molecule *>(publisher) != NULL) {
|
---|
210 | const moleculeId_t molid = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
211 | switch (notification->getChannelNo()) {
|
---|
212 | case molecule::IndexChanged:
|
---|
213 | {
|
---|
214 | // molecule has changed its index
|
---|
215 | const moleculeId_t newmoleculeId = dynamic_cast<molecule *>(publisher)->getId();
|
---|
216 | LOG(3, "DEBUG: InformationBoard got IndexChanged from molecule " << molid << " to " << newmoleculeId);
|
---|
217 | #ifndef NDEBUG
|
---|
218 | bool status =
|
---|
219 | #endif
|
---|
220 | moleculeObservedValues.changeIdentifier(molid, newmoleculeId);
|
---|
221 | ASSERT( status,
|
---|
222 | "QtObservedInstanceBoard::recieveNotification() - cannot change molecule's id "
|
---|
223 | +toString(molid)+" "+toString(newmoleculeId)+" in moleculeObservedValues.");
|
---|
224 | // no need update SignedOn, ref does not change
|
---|
225 | emit moleculeIndexChanged(molid, newmoleculeId);
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | default:
|
---|
229 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
|
---|
230 | break;
|
---|
231 | }
|
---|
232 | } else if (dynamic_cast<atom *>(publisher) != NULL) {
|
---|
233 | const atomId_t oldatomId = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
234 | switch (notification->getChannelNo()) {
|
---|
235 | case AtomObservable::IndexChanged:
|
---|
236 | {
|
---|
237 | const atomId_t newatomId = dynamic_cast<atom *>(publisher)->getId();
|
---|
238 | LOG(3, "DEBUG: InformationBoard got IndexChanged from atom " << oldatomId << " to " << newatomId);
|
---|
239 | #ifndef NDEBUG
|
---|
240 | bool status =
|
---|
241 | #endif
|
---|
242 | atomObservedValues.changeIdentifier(oldatomId, newatomId);
|
---|
243 | ASSERT( status,
|
---|
244 | "QtObservedInstanceBoard::recieveNotification() - cannot change atom's id "
|
---|
245 | +toString(oldatomId)+" "+toString(newatomId)+" in atomObservedValues.");
|
---|
246 | // no need update SignedOn, ref does not change
|
---|
247 | emit atomIndexChanged(oldatomId, newatomId);
|
---|
248 | break;
|
---|
249 | }
|
---|
250 | default:
|
---|
251 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | } else {
|
---|
255 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - notification from unknown source.");
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | void QtObservedInstanceBoard::atomcountsubjectKilled(const atomId_t _atomid)
|
---|
260 | {
|
---|
261 | if ((_atomid == lastremovedatom)) {
|
---|
262 | LOG(3, "DEBUG: InstanceBoard emits atomRemoved for " << _atomid);
|
---|
263 | emit atomRemoved(lastremovedatom);
|
---|
264 | } else
|
---|
265 | ELOG(2, "QtObservedInstanceBoard::atomcountsubjectKilled() - id " << _atomid
|
---|
266 | << " not fitting with " << lastremovedatom);
|
---|
267 | }
|
---|
268 |
|
---|
269 | void QtObservedInstanceBoard::moleculecountsubjectKilled(const moleculeId_t _molid)
|
---|
270 | {
|
---|
271 | if (lastremovedmolecule == _molid) {
|
---|
272 | LOG(3, "DEBUG: InstanceBoard emits moleculeRemoved for " << _molid);
|
---|
273 | emit moleculeRemoved(_molid);
|
---|
274 | } else
|
---|
275 | ELOG(2, "QtObservedInstanceBoard::moleculecountsubjectKilled() - id " << _molid
|
---|
276 | << " not fitting with " << lastremovedmolecule);
|
---|
277 | }
|
---|
278 |
|
---|
279 | QtObservedAtom::ptr QtObservedInstanceBoard::getObservedAtom(const atomId_t _id)
|
---|
280 | {
|
---|
281 | return atomObservedValues.get(_id);
|
---|
282 | }
|
---|
283 |
|
---|
284 | QtObservedMolecule::ptr QtObservedInstanceBoard::getObservedMolecule(const moleculeId_t _id)
|
---|
285 | {
|
---|
286 | return moleculeObservedValues.get(_id);
|
---|
287 | }
|
---|
288 |
|
---|
289 | void QtObservedInstanceBoard::markObservedAtomAsConnected(const atomId_t _id)
|
---|
290 | {
|
---|
291 | atomObservedValues.markObservedValuesAsConnected(_id);
|
---|
292 | }
|
---|
293 |
|
---|
294 | void QtObservedInstanceBoard::markObservedAtomAsDisconnected(const atomId_t _id)
|
---|
295 | {
|
---|
296 | atomObservedValues.markObservedValuesAsDisconnected(_id);
|
---|
297 | }
|
---|
298 |
|
---|
299 | void QtObservedInstanceBoard::markObservedAtomForErase(const atomId_t _id)
|
---|
300 | {
|
---|
301 | atomObservedValues.eraseObservedValues(_id);
|
---|
302 | }
|
---|
303 |
|
---|
304 | void QtObservedInstanceBoard::markObservedMoleculeAsConnected(const moleculeId_t _id)
|
---|
305 | {
|
---|
306 | moleculeObservedValues.markObservedValuesAsConnected(_id);
|
---|
307 | }
|
---|
308 |
|
---|
309 | void QtObservedInstanceBoard::markObservedMoleculeAsDisconnected(const moleculeId_t _id)
|
---|
310 | {
|
---|
311 | moleculeObservedValues.markObservedValuesAsDisconnected(_id);
|
---|
312 | }
|
---|
313 |
|
---|
314 | void QtObservedInstanceBoard::markObservedMoleculeForErase(const moleculeId_t _id)
|
---|
315 | {
|
---|
316 | moleculeObservedValues.eraseObservedValues(_id);
|
---|
317 | }
|
---|