1 | /*
|
---|
2 | * QtObservedAtom.hpp
|
---|
3 | *
|
---|
4 | * Created on: Oct 28, 2015
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | #ifndef QTOBSERVEDATOM_HPP_
|
---|
10 | #define QTOBSERVEDATOM_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 "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp"
|
---|
28 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp"
|
---|
29 | #include "types.hpp"
|
---|
30 |
|
---|
31 | class atom;
|
---|
32 | class QtObservedInstanceBoard;
|
---|
33 | class QtObservedMolecule;
|
---|
34 |
|
---|
35 | /** This instance is the ObservedValue representation of a World's atom.
|
---|
36 | *
|
---|
37 | * Due to the signal/slot mechanism and its delays, lifetime of objects in the
|
---|
38 | * World and their QtGui representation cannot directly be related (without
|
---|
39 | * slowing down Actions just for having the representation up to speed).
|
---|
40 | * Hence, the required information for displaying and representing these
|
---|
41 | * objects must be contained in an extra instance.
|
---|
42 | *
|
---|
43 | * This is the instance for information about a particular atom.
|
---|
44 | *
|
---|
45 | * Essentially, this is the interface between molecuilder's World (and a
|
---|
46 | * particular atom) and the QtGui part of the code.
|
---|
47 | */
|
---|
48 | class QtObservedAtom : public QWidget, public Observer
|
---|
49 | {
|
---|
50 | Q_OBJECT
|
---|
51 |
|
---|
52 | public:
|
---|
53 |
|
---|
54 | //!> typedef for instance wrapped in shared ptr
|
---|
55 | typedef boost::shared_ptr<QtObservedAtom> ptr;
|
---|
56 |
|
---|
57 | //!> typedef for instance wrapped in weak shared ptr
|
---|
58 | typedef boost::weak_ptr<QtObservedAtom> weak_ptr;
|
---|
59 |
|
---|
60 | private:
|
---|
61 | //!> ObservedValuesContainer needs to access private cstor and dstor
|
---|
62 | friend class ObservedValuesContainer<QtObservedAtom, ObservedValue_Index_t>;
|
---|
63 | //!> QtObservedInstanceBoard needs to access private cstor and dstor
|
---|
64 | friend class QtObservedInstanceBoard;
|
---|
65 |
|
---|
66 | /** Cstor of QtObservedAtom.
|
---|
67 | *
|
---|
68 | * \param _id id of observed atom
|
---|
69 | * \param _atom ref to observed atom
|
---|
70 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled()
|
---|
71 | * \param _parent Qt parent to automatically destroy when parent is destroyed
|
---|
72 | */
|
---|
73 | QtObservedAtom(
|
---|
74 | const atomId_t _id,
|
---|
75 | const atom * const _atom,
|
---|
76 | QtObservedInstanceBoard &_board,
|
---|
77 | QWidget * _parent=0);
|
---|
78 |
|
---|
79 | public:
|
---|
80 |
|
---|
81 | /** Dstor of QtObservedAtom.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | ~QtObservedAtom();
|
---|
85 |
|
---|
86 | // Observer functions
|
---|
87 | void update(Observable *publisher);
|
---|
88 | void subjectKilled(Observable *publisher);
|
---|
89 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
---|
90 |
|
---|
91 | /** Getter for a permanent and unique index of this instance.
|
---|
92 | *
|
---|
93 | * \note ALWAYS use this index if you need to store and identifier to this
|
---|
94 | * instance which you might need to retrieve at some later date.
|
---|
95 | *
|
---|
96 | * \warning DO NOT STORE the QtObserved...:ptr directly. This will possibly
|
---|
97 | * prevent their eventual destruction. Only store the ObservedValue_Index_t
|
---|
98 | * as means to obtain the ptr from the QtObservedInstanceBoard.
|
---|
99 | *
|
---|
100 | * \return returns a unique and permanent index that can be used to retrieve this
|
---|
101 | * instance from the QtObservedInstanceBoard as it must not be stored.
|
---|
102 | */
|
---|
103 | ObservedValue_Index_t getIndex() const;
|
---|
104 |
|
---|
105 | /** Getter to atom index contained in \a ObservedValues.
|
---|
106 | *
|
---|
107 | * \return atom's index
|
---|
108 | */
|
---|
109 | const atomId_t& getAtomIndex() const;
|
---|
110 |
|
---|
111 | //!> typedef for list of bonds, defined by pairs of atom ids
|
---|
112 | typedef std::vector< std::pair<atomId_t, atomId_t> > ListOfBonds_t;
|
---|
113 |
|
---|
114 | /** Getter to atom bonds contained in \a ObservedValues.
|
---|
115 | *
|
---|
116 | * \return atom's bonds
|
---|
117 | */
|
---|
118 | const ListOfBonds_t& getAtomBonds() const;
|
---|
119 |
|
---|
120 | /** Getter to atom element contained in \a ObservedValues.
|
---|
121 | *
|
---|
122 | * \return atom's element
|
---|
123 | */
|
---|
124 | const atomicNumber_t& getAtomElement() const;
|
---|
125 |
|
---|
126 | /** Getter to atom name contained in \a ObservedValues.
|
---|
127 | *
|
---|
128 | * \return atom's name
|
---|
129 | */
|
---|
130 | const std::string& getAtomName() const;
|
---|
131 |
|
---|
132 | /** Getter to atom position contained in \a ObservedValues.
|
---|
133 | *
|
---|
134 | * \return atom's position
|
---|
135 | */
|
---|
136 | const Vector& getAtomPosition() const;
|
---|
137 |
|
---|
138 | /** Getter to the observed state of the associated molecule.
|
---|
139 | *
|
---|
140 | * \return const ref to observed state of molecule
|
---|
141 | */
|
---|
142 | const QtObservedMolecule* const getMoleculeRef() const;
|
---|
143 |
|
---|
144 | /** Getter to atom's selected status.
|
---|
145 | *
|
---|
146 | * \return true - atom selected, false - else
|
---|
147 | */
|
---|
148 | const bool getAtomSelected() const;
|
---|
149 |
|
---|
150 | signals:
|
---|
151 | void indexChanged();
|
---|
152 | void bondsChanged();
|
---|
153 | void elementChanged();
|
---|
154 | void moleculeChanged();
|
---|
155 | void nameChanged();
|
---|
156 | void positionChanged();
|
---|
157 | void atomRemoved();
|
---|
158 | void selectedChanged();
|
---|
159 |
|
---|
160 | //private slots:
|
---|
161 |
|
---|
162 | private:
|
---|
163 | void activateObserver();
|
---|
164 | void deactivateObserver();
|
---|
165 |
|
---|
166 | static const atom * const getAtomConst(const atomId_t _id);
|
---|
167 | static atom * const getAtom(const atomId_t _id);
|
---|
168 |
|
---|
169 | private:
|
---|
170 | static atomId_t updateIndex(const atom &_atomref);
|
---|
171 | static ListOfBonds_t updateBonds(const atom &_atom);
|
---|
172 | static atomicNumber_t updateElement(const atom &_atom);
|
---|
173 | QtObservedMolecule* updateMoleculeRef(const atom &_atom);
|
---|
174 | static std::string updateName(const atom &_atom);
|
---|
175 | static Vector updatePosition(const atom &_atom);
|
---|
176 | static bool updateSelected(const atom &_atom);
|
---|
177 |
|
---|
178 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
179 | enum ObservedTypes {
|
---|
180 | //!> contains the current atom index
|
---|
181 | AtomIndex,
|
---|
182 | //!> contains the current set of bonds atoms for the atom
|
---|
183 | AtomBonds,
|
---|
184 | //!> contains the current atom element
|
---|
185 | AtomElement,
|
---|
186 | //!> contains the current atom position
|
---|
187 | AtomName,
|
---|
188 | //!> contains the current atom position
|
---|
189 | AtomPosition,
|
---|
190 | //!> contains the current atom's selection status
|
---|
191 | AtomSelected,
|
---|
192 | //!> contains the current atom's molecule index
|
---|
193 | MoleculeRef,
|
---|
194 | //!> gives the size of the enumeration
|
---|
195 | MAX_ObservedTypes
|
---|
196 | };
|
---|
197 |
|
---|
198 | /** Initializes all \a ObservedValues entries.
|
---|
199 | *
|
---|
200 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
201 | * \param _id atom id
|
---|
202 | * \param _atomref reference to atom
|
---|
203 | * \param _subjectKilled ref to function to call on subjectKilled()
|
---|
204 | */
|
---|
205 | void initObservedValues(
|
---|
206 | ObservedValues_t &_ObservedValues,
|
---|
207 | const atomId_t _id,
|
---|
208 | const atom * const _atomref,
|
---|
209 | const boost::function<void()> &_subjectKilled);
|
---|
210 |
|
---|
211 | /** Destroys all \a ObservedValues entries.
|
---|
212 | *
|
---|
213 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
214 | */
|
---|
215 | void destroyObservedValues(
|
---|
216 | std::vector<boost::any> &_ObservedValues);
|
---|
217 |
|
---|
218 | /** Function is called by InstanceBoard to inform about its destruction.
|
---|
219 | *
|
---|
220 | * \note callbacks must not be used after this
|
---|
221 | */
|
---|
222 | void noteBoardIsGone()
|
---|
223 | { BoardIsGone = true; }
|
---|
224 |
|
---|
225 | /** Counts the number of subject killed received from the observed values.
|
---|
226 | *
|
---|
227 | * \param _id id to check against ours
|
---|
228 | */
|
---|
229 | void countValuesSubjectKilled(ObservedValue_Index_t _id);
|
---|
230 |
|
---|
231 | //!> counts how many ObservedValues have already been subjectKilled()
|
---|
232 | mutable size_t subjectKilledCount;
|
---|
233 |
|
---|
234 | /** Helper function to check that all subjectKilled have been received for both
|
---|
235 | * this instance and all its internal observed values.
|
---|
236 | *
|
---|
237 | * \param _id id to check against ours
|
---|
238 | */
|
---|
239 | void checkForRemoval(ObservedValue_Index_t _id);
|
---|
240 |
|
---|
241 | private:
|
---|
242 |
|
---|
243 | //!> list of channels when index needs to update
|
---|
244 | static const Observable::channels_t AtomIndexChannels;
|
---|
245 | //!> list of channels when bonds needs to update
|
---|
246 | static const Observable::channels_t AtomBondsChannels;
|
---|
247 | //!> list of channels when element needs to update
|
---|
248 | static const Observable::channels_t AtomElementChannels;
|
---|
249 | //!> list of channels when molecule index needs to update
|
---|
250 | static const Observable::channels_t MoleculeChangedChannels;
|
---|
251 | //!> list of channels when name needs to update
|
---|
252 | static const Observable::channels_t AtomNameChannels;
|
---|
253 | //!> list of channels when position needs to update
|
---|
254 | static const Observable::channels_t AtomPositionChannels;
|
---|
255 | //!> list of channels when selection needs to update
|
---|
256 | static const Observable::channels_t AtomSelectedChannels;
|
---|
257 |
|
---|
258 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
259 | const unsigned int AllsignedOnChannels;
|
---|
260 | unsigned int signedOffChannels;
|
---|
261 |
|
---|
262 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
263 | const Observable *owner;
|
---|
264 |
|
---|
265 | private:
|
---|
266 |
|
---|
267 | /** Internal setter for the weak shared_ptr instance that we sometimes
|
---|
268 | * need to convert the ref to this instance into an shared ptr instance that
|
---|
269 | * is safe to hand around.
|
---|
270 | *
|
---|
271 | * \param _selfref ref to shared ptr instance that is internally stored
|
---|
272 | */
|
---|
273 | void setSelfRef(const weak_ptr &_selfref)
|
---|
274 | { const_cast<weak_ptr &>(selfref) = _selfref; }
|
---|
275 |
|
---|
276 | //!> reference to this instance wrapped in a shared ptr for handing around
|
---|
277 | const weak_ptr selfref;
|
---|
278 |
|
---|
279 | public:
|
---|
280 |
|
---|
281 | /** Getter for this instance safely wrapped in a shared ptr instance for
|
---|
282 | * handing arount.
|
---|
283 | *
|
---|
284 | * \return shared ptr of this instance
|
---|
285 | */
|
---|
286 | ptr getRef() const
|
---|
287 | { return ptr(selfref); }
|
---|
288 |
|
---|
289 | private:
|
---|
290 | //!> unique index among all observed instances
|
---|
291 | const ObservedValue_Index_t index;
|
---|
292 |
|
---|
293 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
294 | QtObservedInstanceBoard & board;
|
---|
295 |
|
---|
296 | //!> is board still alive or not, impacts callbacks
|
---|
297 | bool BoardIsGone;
|
---|
298 |
|
---|
299 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
300 | ObservedValues_t ObservedValues;
|
---|
301 | };
|
---|
302 |
|
---|
303 |
|
---|
304 | #endif /* QTOBSERVEDATOM_HPP_ */
|
---|