1 | /*
|
---|
2 | * QtObservedMolecule.hpp
|
---|
3 | *
|
---|
4 | * Created on: Oct 28, 2015
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | #ifndef QTOBSERVEDMOLECULE_HPP_
|
---|
10 | #define QTOBSERVEDMOLECULE_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 "molecule.hpp"
|
---|
28 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp"
|
---|
29 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp"
|
---|
30 | #include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp"
|
---|
31 | #include "types.hpp"
|
---|
32 |
|
---|
33 | class QtObservedInstanceBoard;
|
---|
34 |
|
---|
35 | /** This instance is the ObservedValue representation of a World's molecule.
|
---|
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 molecule.
|
---|
44 | */
|
---|
45 | class QtObservedMolecule : public QWidget, public Observer
|
---|
46 | {
|
---|
47 | Q_OBJECT
|
---|
48 |
|
---|
49 | public:
|
---|
50 |
|
---|
51 | //!> typedef for instance wrapped in shared ptr
|
---|
52 | typedef boost::shared_ptr<QtObservedMolecule> ptr;
|
---|
53 |
|
---|
54 | private:
|
---|
55 | //!> ObservedValuesContainer needs to access private cstor and dstor
|
---|
56 | friend class ObservedValuesContainer<QtObservedMolecule, moleculeId_t>;
|
---|
57 | //!> QtObservedInstanceBoard needs to access private cstor and dstor
|
---|
58 | friend class QtObservedInstanceBoard;
|
---|
59 |
|
---|
60 | /** Cstor of QtObservedMolecule.
|
---|
61 | *
|
---|
62 | * \param _id id of observed molecule
|
---|
63 | * \param _mol ref to observed molecule
|
---|
64 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled()
|
---|
65 | * \param _parent Qt parent to automatically destroy when parent is destroyed
|
---|
66 | */
|
---|
67 | QtObservedMolecule(
|
---|
68 | const moleculeId_t _id,
|
---|
69 | const molecule * const _mol,
|
---|
70 | QtObservedInstanceBoard &_board,
|
---|
71 | QWidget * _parent=0);
|
---|
72 |
|
---|
73 | public:
|
---|
74 |
|
---|
75 | /** Dstor of QtObservedMolecule.
|
---|
76 | *
|
---|
77 | */
|
---|
78 | ~QtObservedMolecule();
|
---|
79 |
|
---|
80 | // Observer functions
|
---|
81 | void update(Observable *publisher);
|
---|
82 | void subjectKilled(Observable *publisher);
|
---|
83 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
---|
84 |
|
---|
85 | /** Getter to molecule atom count contained in \a ObservedValues.
|
---|
86 | *
|
---|
87 | * \return molecule's atom count
|
---|
88 | */
|
---|
89 | const int& getAtomCount() const;
|
---|
90 |
|
---|
91 | /** Getter to molecule bond count contained in \a ObservedValues.
|
---|
92 | *
|
---|
93 | * \return molecule's bond count
|
---|
94 | */
|
---|
95 | const int& getBondCount() const;
|
---|
96 |
|
---|
97 | /** Getter to molecule center contained in \a ObservedValues.
|
---|
98 | *
|
---|
99 | * \return molecule's center
|
---|
100 | */
|
---|
101 | const Vector& getMolCenter() const;
|
---|
102 |
|
---|
103 | /** Getter to molecule index contained in \a ObservedValues.
|
---|
104 | *
|
---|
105 | * \return molecule's index
|
---|
106 | */
|
---|
107 | const moleculeId_t& getMolIndex() const;
|
---|
108 |
|
---|
109 | /** Getter to molecule name contained in \a ObservedValues.
|
---|
110 | *
|
---|
111 | * \return molecule's name
|
---|
112 | */
|
---|
113 | const std::string& getMolName() const;
|
---|
114 |
|
---|
115 | /** Getter to molecule formula contained in \a ObservedValues.
|
---|
116 | *
|
---|
117 | * \return molecule's formula
|
---|
118 | */
|
---|
119 | const std::string& getMolFormula() const;
|
---|
120 |
|
---|
121 | /** Getter to molecule non-hydrogen atom count contained in \a ObservedValues.
|
---|
122 | *
|
---|
123 | * \return molecule's non-hydrogen atom count
|
---|
124 | */
|
---|
125 | const int& getNonHydrogenCount() const;
|
---|
126 |
|
---|
127 | /** Getter to molecule's bounding box contained in \a ObservedValues.
|
---|
128 | *
|
---|
129 | * \return molecule's bounding box
|
---|
130 | */
|
---|
131 | const molecule::BoundingBoxInfo& getBoundingBox() const;
|
---|
132 |
|
---|
133 | /** Getter to molecule's selected status.
|
---|
134 | *
|
---|
135 | * \return true - molecule selected, false - else
|
---|
136 | */
|
---|
137 | const bool& getMolSelected() const;
|
---|
138 |
|
---|
139 | static const molecule * const getMolecule(const moleculeId_t _id);
|
---|
140 |
|
---|
141 | signals:
|
---|
142 | void atomcountChanged();
|
---|
143 | void bondcountChanged();
|
---|
144 | void formulaChanged();
|
---|
145 | void indexChanged(const moleculeId_t, const moleculeId_t);
|
---|
146 | void nameChanged();
|
---|
147 | void nononhydrogenChanged();
|
---|
148 | void centerChanged();
|
---|
149 | void tesselationhullChanged();
|
---|
150 | void boundingboxChanged();
|
---|
151 | void atomInserted(QtObservedAtom::ptr, QtObservedMolecule *);
|
---|
152 | void atomRemoved(const atomId_t, QtObservedMolecule *);
|
---|
153 | void moleculeRemoved();
|
---|
154 | void selectedChanged();
|
---|
155 |
|
---|
156 | private:
|
---|
157 |
|
---|
158 | void activateObserver();
|
---|
159 | void deactivateObserver();
|
---|
160 |
|
---|
161 | private:
|
---|
162 | static int updateAtomCount(
|
---|
163 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
164 | static int updateBondCount(
|
---|
165 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
166 | static molecule::BoundingBoxInfo updateBoundingBox(
|
---|
167 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
168 | static std::string updateFormulaString(
|
---|
169 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
170 | static Vector updateCenter(
|
---|
171 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
172 | static moleculeId_t updateIndex();
|
---|
173 | static std::string updateName(
|
---|
174 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
175 | static int updateNonHydrogenCount(
|
---|
176 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
177 | static bool updateSelected(
|
---|
178 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
---|
179 |
|
---|
180 | //!> list of channels when atom count needs to update
|
---|
181 | static const Observable::channels_t AtomCountChannels;
|
---|
182 | //!> list of channels when bond count needs to update
|
---|
183 | static const Observable::channels_t BondCountChannels;
|
---|
184 | //!> list of channels when bounding box needs to update
|
---|
185 | static const Observable::channels_t BoundingBoxChannels;
|
---|
186 | //!> list of channels when formula needs to update
|
---|
187 | static const Observable::channels_t FormulaStringChannels;
|
---|
188 | //!> list of channels when the center needs to update
|
---|
189 | static const Observable::channels_t CenterChannels;
|
---|
190 | //!> list of channels when the index needs to update
|
---|
191 | static const Observable::channels_t IndexChannels;
|
---|
192 | //!> list of channels when the name needs to update
|
---|
193 | static const Observable::channels_t NameChannels;
|
---|
194 | //!> list of channels when the name needs to update
|
---|
195 | static const Observable::channels_t NonHydrogenCountChannels;
|
---|
196 | //!> list of channels when the name needs to update
|
---|
197 | static const Observable::channels_t SelectedChannels;
|
---|
198 |
|
---|
199 | private:
|
---|
200 | /** Observed Values **/
|
---|
201 |
|
---|
202 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
203 | enum ObservedTypes {
|
---|
204 | //!> contains the current molecule index
|
---|
205 | MolIndex,
|
---|
206 | //!> contains the current molecule's atom count
|
---|
207 | AtomCount,
|
---|
208 | //!> contains the current molecule's number of bonds
|
---|
209 | BondCount,
|
---|
210 | //!> contains newest version of the bounding box on request
|
---|
211 | BoundingBox,
|
---|
212 | //!> contains the current molecule's formula
|
---|
213 | FormulaString,
|
---|
214 | //!> contains the current molecule's center
|
---|
215 | MolCenter,
|
---|
216 | //!> contains the current molecule name
|
---|
217 | MolName,
|
---|
218 | //!> contains the current molecule's non-hydrogen atom count
|
---|
219 | NonHydrogenCount,
|
---|
220 | //!> contains the current molecule's selection status
|
---|
221 | MolSelected,
|
---|
222 | //!> gives the size of the enumeration
|
---|
223 | MAX_ObservedTypes
|
---|
224 | };
|
---|
225 |
|
---|
226 | /** Initializes all \a _ObservedValues entries.
|
---|
227 | *
|
---|
228 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
229 | * \param _moid molecule id
|
---|
230 | * \param _molref reference to molecule
|
---|
231 | * \param _subjectKilled ref to function to call on subjectKilled()
|
---|
232 | */
|
---|
233 | static void initObservedValues(
|
---|
234 | ObservedValues_t &_ObservedValues,
|
---|
235 | const moleculeId_t _molid,
|
---|
236 | const molecule * const _molref,
|
---|
237 | const boost::function<void(const moleculeId_t)> &_subjectKilled);
|
---|
238 |
|
---|
239 | /** Destroys all \a ObservedValues entries.
|
---|
240 | *
|
---|
241 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
242 | */
|
---|
243 | static void destroyObservedValues(
|
---|
244 | std::vector<boost::any> &_ObservedValues);
|
---|
245 |
|
---|
246 | /** Function is called by InstanceBoard to inform about its destruction.
|
---|
247 | *
|
---|
248 | * \note callbacks must not be used after this
|
---|
249 | */
|
---|
250 | void noteBoardIsGone()
|
---|
251 | { BoardIsGone = true; }
|
---|
252 |
|
---|
253 |
|
---|
254 | /** Counts the number of subject killed received from the observed values.
|
---|
255 | *
|
---|
256 | * \param _id id to check against ours
|
---|
257 | */
|
---|
258 | void countValuesSubjectKilled(const atomId_t _id);
|
---|
259 |
|
---|
260 | //!> counts how many ObservedValues have already been subjectKilled()
|
---|
261 | mutable size_t subjectKilledCount;
|
---|
262 |
|
---|
263 | /** Helper function to check that all subjectKilled have been received for both
|
---|
264 | * this instance and all its internal observed values.
|
---|
265 | *
|
---|
266 | */
|
---|
267 | void checkForRemoval();
|
---|
268 |
|
---|
269 | private:
|
---|
270 |
|
---|
271 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
272 | const unsigned int AllsignedOnChannels;
|
---|
273 | unsigned int signedOffChannels;
|
---|
274 |
|
---|
275 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
276 | const Observable *owner;
|
---|
277 |
|
---|
278 | private:
|
---|
279 | //!> contains still the old index after the index changed
|
---|
280 | moleculeId_t oldId;
|
---|
281 |
|
---|
282 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
283 | QtObservedInstanceBoard & board;
|
---|
284 |
|
---|
285 | //!> is board still alive or not, impacts callbacks
|
---|
286 | bool BoardIsGone;
|
---|
287 |
|
---|
288 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
289 | ObservedValues_t ObservedValues;
|
---|
290 | };
|
---|
291 |
|
---|
292 |
|
---|
293 | #endif /* QTOBSERVEDMOLECULE_HPP_ */
|
---|