1 | /*
|
---|
2 | * QtObservedBond.hpp
|
---|
3 | *
|
---|
4 | * Created on: Mar 03, 2016
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | #ifndef QTOBSERVEDBOND_HPP_
|
---|
10 | #define QTOBSERVEDBOND_HPP_
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include <QtGui/QWidget>
|
---|
18 |
|
---|
19 | #include <boost/function.hpp>
|
---|
20 | #include <boost/shared_ptr.hpp>
|
---|
21 |
|
---|
22 | #include "CodePatterns/Observer/Observable.hpp"
|
---|
23 | #include "CodePatterns/Observer/Observer.hpp"
|
---|
24 |
|
---|
25 | #include "LinearAlgebra/Vector.hpp"
|
---|
26 |
|
---|
27 | #include "Bond/bond.hpp"
|
---|
28 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp"
|
---|
29 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp"
|
---|
30 | #include "types.hpp"
|
---|
31 |
|
---|
32 | class atom;
|
---|
33 | class molecule;
|
---|
34 | class QtObservedInstanceBoard;
|
---|
35 | class QtObservedMolecule;
|
---|
36 |
|
---|
37 | /** This instance is the ObservedValue representation of a World's bond.
|
---|
38 | *
|
---|
39 | * Due to the signal/slot mechanism and its delays, lifetime of objects in the
|
---|
40 | * World and their QtGui representation cannot directly be related (without
|
---|
41 | * slowing down Actions just for having the representation up to speed).
|
---|
42 | * Hence, the required information for displaying and representing these
|
---|
43 | * objects must be contained in an extra instance.
|
---|
44 | *
|
---|
45 | * This is the instance for information about a particular bond.
|
---|
46 | *
|
---|
47 | * Essentially, this is the interface between molecuilder's World (and a
|
---|
48 | * particular bond) and the QtGui part of the code.
|
---|
49 | */
|
---|
50 | class QtObservedBond : public QWidget, public Observer
|
---|
51 | {
|
---|
52 | Q_OBJECT
|
---|
53 |
|
---|
54 | public:
|
---|
55 | typedef std::pair<atomId_t, atomId_t> bondId_t;
|
---|
56 |
|
---|
57 | //!> typedef for instance wrapped in shared ptr
|
---|
58 | typedef boost::shared_ptr<QtObservedBond> ptr;
|
---|
59 |
|
---|
60 | //!> typedef for instance wrapped in weak shared ptr
|
---|
61 | typedef boost::weak_ptr<QtObservedBond> weak_ptr;
|
---|
62 |
|
---|
63 | private:
|
---|
64 | //!> ObservedValuesContainer needs to access private cstor and dstor
|
---|
65 | friend class ObservedValuesContainer<QtObservedBond, ObservedValue_Index_t>;
|
---|
66 | //!> QtObservedInstanceBoard needs to access private cstor and dstor
|
---|
67 | friend class QtObservedInstanceBoard;
|
---|
68 |
|
---|
69 | /** Cstor of QtObservedBond.
|
---|
70 | *
|
---|
71 | * \param _id id of observed bond
|
---|
72 | * \param _bond ref to observed bond
|
---|
73 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled()
|
---|
74 | * \param _parent Qt parent to automatically destroy when parent is destroyed
|
---|
75 | */
|
---|
76 | QtObservedBond(
|
---|
77 | const bondId_t _id,
|
---|
78 | const bond::ptr _bond,
|
---|
79 | QtObservedInstanceBoard &_board,
|
---|
80 | QWidget * _parent=0);
|
---|
81 |
|
---|
82 | public:
|
---|
83 |
|
---|
84 | /** Dstor of QtObservedBond.
|
---|
85 | *
|
---|
86 | */
|
---|
87 | ~QtObservedBond();
|
---|
88 |
|
---|
89 | // Observer functions
|
---|
90 | void update(Observable *publisher);
|
---|
91 | void subjectKilled(Observable *publisher);
|
---|
92 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
---|
93 |
|
---|
94 | /** Getter for a permanent and unique index of this instance.
|
---|
95 | *
|
---|
96 | * \note ALWAYS use this index if you need to store and identifier to this
|
---|
97 | * instance which you might need to retrieve at some later date.
|
---|
98 | *
|
---|
99 | * \warning DO NOT STORE the QtObserved...:ptr directly. This will possibly
|
---|
100 | * prevent their eventual destruction. Only store the ObservedValue_Index_t
|
---|
101 | * as means to obtain the ptr from the QtObservedInstanceBoard.
|
---|
102 | *
|
---|
103 | * \return returns a unique and permanent index that can be used to retrieve this
|
---|
104 | * instance from the QtObservedInstanceBoard as it must not be stored.
|
---|
105 | */
|
---|
106 | ObservedValue_Index_t getIndex() const;
|
---|
107 |
|
---|
108 | /** Getter to bond's index pair contained in \a ObservedValues.
|
---|
109 | *
|
---|
110 | * \return bond's index pair
|
---|
111 | */
|
---|
112 | const bondId_t getBondIndex() const;
|
---|
113 |
|
---|
114 | /** Getter to bond's degree contained in \a ObservedValues.
|
---|
115 | *
|
---|
116 | * \return bond's degree
|
---|
117 | */
|
---|
118 | const int& getBondDegree() const;
|
---|
119 |
|
---|
120 | /** Getter to bond's left atom index contained in \a ObservedValues.
|
---|
121 | *
|
---|
122 | * \return bond's left atom index
|
---|
123 | */
|
---|
124 | const atomId_t& getLeftAtomIndex() const;
|
---|
125 |
|
---|
126 | /** Getter to bond's left atom element contained in \a ObservedValues.
|
---|
127 | *
|
---|
128 | * \return bond's left atom element
|
---|
129 | */
|
---|
130 | const atomicNumber_t& getLeftAtomElement() const;
|
---|
131 |
|
---|
132 | /** Getter to bond's right atom position contained in \a ObservedValues.
|
---|
133 | *
|
---|
134 | * \return bond's left atom position
|
---|
135 | */
|
---|
136 | const Vector& getLeftAtomPosition() const;
|
---|
137 |
|
---|
138 | /** Getter to bond's right atom index contained in \a ObservedValues.
|
---|
139 | *
|
---|
140 | * \return bond's right atom index
|
---|
141 | */
|
---|
142 | const atomId_t& getRightAtomIndex() const;
|
---|
143 |
|
---|
144 | /** Getter to bond's right atom element contained in \a ObservedValues.
|
---|
145 | *
|
---|
146 | * \return bond's right atom element
|
---|
147 | */
|
---|
148 | const atomicNumber_t& getRightAtomElement() const;
|
---|
149 |
|
---|
150 | /** Getter to bond's right atom position contained in \a ObservedValues.
|
---|
151 | *
|
---|
152 | * \return bond's right atom position
|
---|
153 | */
|
---|
154 | const Vector& getRightAtomPosition() const;
|
---|
155 |
|
---|
156 | /** Getter to bond's molecule index contained in \a ObservedValues.
|
---|
157 | *
|
---|
158 | * \return bond's molecule index
|
---|
159 | */
|
---|
160 | const moleculeId_t& getMoleculeIndex() const;
|
---|
161 |
|
---|
162 | //!> typedef for internal observable counter maps
|
---|
163 | typedef std::map<Observable * const, unsigned int> ObservableCount_t;
|
---|
164 |
|
---|
165 | signals:
|
---|
166 |
|
---|
167 | void indexChanged(bondId_t, bondId_t);
|
---|
168 | void degreeChanged();
|
---|
169 | void leftAtomIndexChanged(atomId_t, atomId_t);
|
---|
170 | void leftAtomElementChanged();
|
---|
171 | void leftAtomPositionChanged();
|
---|
172 | void rightAtomIndexChanged(atomId_t, atomId_t);
|
---|
173 | void rightAtomElementChanged();
|
---|
174 | void rightAtomPositionChanged();
|
---|
175 | void moleculeIndexChanged(moleculeId_t, moleculeId_t);
|
---|
176 | void bondRemoved();
|
---|
177 |
|
---|
178 | //private slots:
|
---|
179 |
|
---|
180 | private:
|
---|
181 | void activateObserver();
|
---|
182 | void deactivateObserver();
|
---|
183 |
|
---|
184 | static const atom * const getAtomConst(const atomId_t _id);
|
---|
185 | static atom * const getAtom(const atomId_t _id);
|
---|
186 |
|
---|
187 | private:
|
---|
188 | static int updateDegree(const bond &_bond);
|
---|
189 | static atomId_t updateLeftAtomIndex(const atom &_atomref);
|
---|
190 | static atomicNumber_t updateLeftAtomElement(const atom &_atomref);
|
---|
191 | static Vector updateLeftAtomPosition(const atom &_atomref);
|
---|
192 | static atomId_t updateRightAtomIndex(const atom &_atomref);
|
---|
193 | static atomicNumber_t updateRightAtomElement(const atom &_atomref);
|
---|
194 | static Vector updateRightAtomPosition(const atom &_atomref);
|
---|
195 | static moleculeId_t updateMoleculeIndex(const molecule &_molref);
|
---|
196 |
|
---|
197 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
198 | enum ObservedTypes {
|
---|
199 | //!> contains the current bond degree
|
---|
200 | BondDegree,
|
---|
201 | //!> contains the index of the left atom
|
---|
202 | leftAtomIndex,
|
---|
203 | //!> contains the position of the left atom
|
---|
204 | leftAtomPosition,
|
---|
205 | //!> contains the element of the left atom
|
---|
206 | leftAtomElement,
|
---|
207 | //!> contains the index of the right atom
|
---|
208 | rightAtomIndex,
|
---|
209 | //!> contains the position of the right atom
|
---|
210 | rightAtomPosition,
|
---|
211 | //!> contains the element of the right atom
|
---|
212 | rightAtomElement,
|
---|
213 | //!> contains the id of the molecule this bond is associated with
|
---|
214 | moleculeIndex,
|
---|
215 | //!> gives the size of the enumeration
|
---|
216 | MAX_ObservedTypes
|
---|
217 | };
|
---|
218 |
|
---|
219 | /** Initializes all \a ObservedValues entries.
|
---|
220 | *
|
---|
221 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
222 | * \param _id bond id
|
---|
223 | * \param _bondref reference to bond
|
---|
224 | * \param _bondsubjectKilled ref to function to call on subjectKilled() from bond
|
---|
225 | * \param _leftatomsubjectKilled ref to function to call on subjectKilled() from leftatom
|
---|
226 | * \param _rightatomsubjectKilled ref to function to call on subjectKilled() from rightatom
|
---|
227 | * \param _moleculesubjectKilled ref to function to call on subjectKilled() from molecule
|
---|
228 | */
|
---|
229 | void initObservedValues(
|
---|
230 | ObservedValues_t &_ObservedValues,
|
---|
231 | const bondId_t _id,
|
---|
232 | const bond::ptr _bondref,
|
---|
233 | const boost::function<void()> &_bondsubjectKilled,
|
---|
234 | const boost::function<void()> &_leftatomsubjectKilled,
|
---|
235 | const boost::function<void()> &_rightatomsubjectKilled,
|
---|
236 | const boost::function<void()> &_moleculesubjectKilled);
|
---|
237 |
|
---|
238 | /** Destroys all \a ObservedValues entries.
|
---|
239 | *
|
---|
240 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
241 | */
|
---|
242 | static void destroyObservedValues(
|
---|
243 | std::vector<boost::any> &_ObservedValues);
|
---|
244 |
|
---|
245 | /** Function is called by InstanceBoard to inform about its destruction.
|
---|
246 | *
|
---|
247 | * \note callbacks must not be used after this
|
---|
248 | */
|
---|
249 | void noteBoardIsGone()
|
---|
250 | { BoardIsGone = true; }
|
---|
251 |
|
---|
252 | /** Counts the number of subject killed received from the observed values.
|
---|
253 | *
|
---|
254 | * \param _id id to check against ours
|
---|
255 | * \param _counter counter to decrease
|
---|
256 | */
|
---|
257 | void countValuesSubjectKilled(ObservedValue_Index_t _id, unsigned int &_counter);
|
---|
258 |
|
---|
259 | //!> counts how many ObservedValues have already been subjectKilled() for a given observable
|
---|
260 | mutable ObservableCount_t subjectKilledCount;
|
---|
261 |
|
---|
262 | /** Helper function to check that all subjectKilled have been received for both
|
---|
263 | * this instance and all its internal observed values.
|
---|
264 | *
|
---|
265 | * \param _id id to check against ours
|
---|
266 | */
|
---|
267 | void checkForRemoval(ObservedValue_Index_t _id);
|
---|
268 |
|
---|
269 | private:
|
---|
270 |
|
---|
271 | //!> list of channels when index needs to update
|
---|
272 | static const Observable::channels_t BondDegreeChannels;
|
---|
273 |
|
---|
274 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
275 | const ObservableCount_t AllsignedOnChannels;
|
---|
276 | ObservableCount_t signedOffChannels;
|
---|
277 |
|
---|
278 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
279 | const Observable *bondowner;
|
---|
280 | const Observable *leftatomowner;
|
---|
281 | const Observable *rightatomowner;
|
---|
282 | const Observable *moleculeowner;
|
---|
283 |
|
---|
284 | private:
|
---|
285 |
|
---|
286 | /** Internal setter for the weak shared_ptr instance that we sometimes
|
---|
287 | * need to convert the ref to this instance into an shared ptr instance that
|
---|
288 | * is safe to hand around.
|
---|
289 | *
|
---|
290 | * \param _selfref ref to shared ptr instance that is internally stored
|
---|
291 | */
|
---|
292 | void setSelfRef(const weak_ptr &_selfref)
|
---|
293 | { const_cast<weak_ptr &>(selfref) = _selfref; }
|
---|
294 |
|
---|
295 | //!> reference to this instance wrapped in a shared ptr for handing around
|
---|
296 | const weak_ptr selfref;
|
---|
297 |
|
---|
298 | public:
|
---|
299 |
|
---|
300 | /** Getter for this instance safely wrapped in a shared ptr instance for
|
---|
301 | * handing arount.
|
---|
302 | *
|
---|
303 | * \return shared ptr of this instance
|
---|
304 | */
|
---|
305 | ptr getRef() const
|
---|
306 | { return ptr(selfref); }
|
---|
307 |
|
---|
308 | private:
|
---|
309 | //!> contains still the old id after the index of the bond changed
|
---|
310 | bondId_t oldbondId;
|
---|
311 |
|
---|
312 | //!> contains still the old id after the index of the left atom changed
|
---|
313 | atomId_t oldleftatomId;
|
---|
314 |
|
---|
315 | //!> contains still the old id after the index of the right atom changed
|
---|
316 | atomId_t oldrightatomId;
|
---|
317 |
|
---|
318 | //!> contains still the old id after the index of the molecule changed
|
---|
319 | moleculeId_t oldmoleculeId;
|
---|
320 |
|
---|
321 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
322 | QtObservedInstanceBoard & board;
|
---|
323 |
|
---|
324 | //!> is board still alive or not, impacts callbacks
|
---|
325 | bool BoardIsGone;
|
---|
326 |
|
---|
327 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
328 | ObservedValues_t ObservedValues;
|
---|
329 | };
|
---|
330 |
|
---|
331 |
|
---|
332 | #endif /* QTOBSERVEDBOND_HPP_ */
|
---|