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 | private:
|
---|
58 | //!> ObservedValuesContainer needs to access private cstor and dstor
|
---|
59 | friend class ObservedValuesContainer<QtObservedAtom, atomId_t>;
|
---|
60 | //!> QtObservedInstanceBoard needs to access private cstor and dstor
|
---|
61 | friend class QtObservedInstanceBoard;
|
---|
62 |
|
---|
63 | /** Cstor of QtObservedAtom.
|
---|
64 | *
|
---|
65 | * \param _id id of observed atom
|
---|
66 | * \param _atom ref to observed atom
|
---|
67 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled()
|
---|
68 | * \param _parent Qt parent to automatically destroy when parent is destroyed
|
---|
69 | */
|
---|
70 | QtObservedAtom(
|
---|
71 | const atomId_t _id,
|
---|
72 | const atom * const _atom,
|
---|
73 | QtObservedInstanceBoard &_board,
|
---|
74 | QWidget * _parent=0);
|
---|
75 |
|
---|
76 | public:
|
---|
77 |
|
---|
78 | /** Dstor of QtObservedAtom.
|
---|
79 | *
|
---|
80 | */
|
---|
81 | ~QtObservedAtom();
|
---|
82 |
|
---|
83 | // Observer functions
|
---|
84 | void update(Observable *publisher);
|
---|
85 | void subjectKilled(Observable *publisher);
|
---|
86 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
---|
87 |
|
---|
88 | /** Getter to atom index contained in \a ObservedValues.
|
---|
89 | *
|
---|
90 | * \return atom's index
|
---|
91 | */
|
---|
92 | const atomId_t& getAtomIndex() const;
|
---|
93 |
|
---|
94 | //!> typedef for list of bonds, defined by pairs of atom ids
|
---|
95 | typedef std::vector< std::pair<atomId_t, atomId_t> > ListOfBonds_t;
|
---|
96 |
|
---|
97 | /** Getter to atom bonds contained in \a ObservedValues.
|
---|
98 | *
|
---|
99 | * \return atom's bonds
|
---|
100 | */
|
---|
101 | const ListOfBonds_t& getAtomBonds() const;
|
---|
102 |
|
---|
103 | /** Getter to atom element contained in \a ObservedValues.
|
---|
104 | *
|
---|
105 | * \return atom's element
|
---|
106 | */
|
---|
107 | const atomicNumber_t& getAtomElement() const;
|
---|
108 |
|
---|
109 | /** Getter to atom name contained in \a ObservedValues.
|
---|
110 | *
|
---|
111 | * \return atom's name
|
---|
112 | */
|
---|
113 | const std::string& getAtomName() const;
|
---|
114 |
|
---|
115 | /** Getter to atom position contained in \a ObservedValues.
|
---|
116 | *
|
---|
117 | * \return atom's position
|
---|
118 | */
|
---|
119 | const Vector& getAtomPosition() const;
|
---|
120 |
|
---|
121 | /** Getter to atom's molecule index contained in \a ObservedValues.
|
---|
122 | *
|
---|
123 | * \return atom's molecule index
|
---|
124 | */
|
---|
125 | QtObservedMolecule* const getAtomMolecule() const;
|
---|
126 |
|
---|
127 | /** Getter to atom's selected status.
|
---|
128 | *
|
---|
129 | * \return true - atom selected, false - else
|
---|
130 | */
|
---|
131 | const bool getAtomSelected() const;
|
---|
132 |
|
---|
133 | signals:
|
---|
134 | void indexChanged(const atomId_t, const atomId_t);
|
---|
135 | void bondsChanged();
|
---|
136 | void elementChanged();
|
---|
137 | void moleculeChanged();
|
---|
138 | void nameChanged();
|
---|
139 | void positionChanged();
|
---|
140 | void atomRemoved();
|
---|
141 | void selectedChanged();
|
---|
142 |
|
---|
143 | //private slots:
|
---|
144 |
|
---|
145 | private:
|
---|
146 | void activateObserver();
|
---|
147 | void deactivateObserver();
|
---|
148 |
|
---|
149 | static const atom * const getAtomConst(const atomId_t _id);
|
---|
150 | static atom * const getAtom(const atomId_t _id);
|
---|
151 |
|
---|
152 | private:
|
---|
153 | static atomId_t updateIndex();
|
---|
154 | static ListOfBonds_t updateBonds(
|
---|
155 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
156 | static atomicNumber_t updateElement(
|
---|
157 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
158 | QtObservedMolecule* updateMoleculeIndex(
|
---|
159 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
160 | static std::string updateName(
|
---|
161 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
162 | static Vector updatePosition(
|
---|
163 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
164 | static bool updateSelected(
|
---|
165 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
166 |
|
---|
167 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
168 | enum ObservedTypes {
|
---|
169 | //!> contains the current atom index
|
---|
170 | AtomIndex,
|
---|
171 | //!> contains the current set of bonds atoms for the atom
|
---|
172 | AtomBonds,
|
---|
173 | //!> contains the current atom element
|
---|
174 | AtomElement,
|
---|
175 | //!> contains the current atom's molecule index
|
---|
176 | AtomMolecule,
|
---|
177 | //!> contains the current atom position
|
---|
178 | AtomName,
|
---|
179 | //!> contains the current atom position
|
---|
180 | AtomPosition,
|
---|
181 | //!> contains the current atom's selection status
|
---|
182 | AtomSelected,
|
---|
183 | //!> gives the size of the enumeration
|
---|
184 | MAX_ObservedTypes
|
---|
185 | };
|
---|
186 |
|
---|
187 | /** Initializes all \a ObservedValues entries.
|
---|
188 | *
|
---|
189 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
190 | * \param _id atom id
|
---|
191 | * \param _atomref reference to atom
|
---|
192 | * \param _subjectKilled ref to function to call on subjectKilled()
|
---|
193 | */
|
---|
194 | void initObservedValues(
|
---|
195 | ObservedValues_t &_ObservedValues,
|
---|
196 | const atomId_t _id,
|
---|
197 | const atom * const _atomref,
|
---|
198 | const boost::function<void(const atomId_t)> &_subjectKilled);
|
---|
199 |
|
---|
200 | /** Destroys all \a ObservedValues entries.
|
---|
201 | *
|
---|
202 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
203 | */
|
---|
204 | void destroyObservedValues(
|
---|
205 | std::vector<boost::any> &_ObservedValues);
|
---|
206 |
|
---|
207 | /** Function is called by InstanceBoard to inform about its destruction.
|
---|
208 | *
|
---|
209 | * \note callbacks must not be used after this
|
---|
210 | */
|
---|
211 | void noteBoardIsGone()
|
---|
212 | { BoardIsGone = true; }
|
---|
213 |
|
---|
214 | /** Counts the number of subject killed received from the observed values.
|
---|
215 | *
|
---|
216 | * \param _id id to check against ours
|
---|
217 | */
|
---|
218 | void countValuesSubjectKilled(const atomId_t _id);
|
---|
219 |
|
---|
220 | //!> counts how many ObservedValues have already been subjectKilled()
|
---|
221 | mutable size_t subjectKilledCount;
|
---|
222 |
|
---|
223 | /** Helper function to check that all subjectKilled have been received for both
|
---|
224 | * this instance and all its internal observed values.
|
---|
225 | *
|
---|
226 | */
|
---|
227 | void checkForRemoval();
|
---|
228 |
|
---|
229 | private:
|
---|
230 |
|
---|
231 | //!> list of channels when index needs to update
|
---|
232 | static const Observable::channels_t AtomIndexChannels;
|
---|
233 | //!> list of channels when bonds needs to update
|
---|
234 | static const Observable::channels_t AtomBondsChannels;
|
---|
235 | //!> list of channels when element needs to update
|
---|
236 | static const Observable::channels_t AtomElementChannels;
|
---|
237 | //!> list of channels when molecule index needs to update
|
---|
238 | static const Observable::channels_t AtomMoleculeChannels;
|
---|
239 | //!> list of channels when name needs to update
|
---|
240 | static const Observable::channels_t AtomNameChannels;
|
---|
241 | //!> list of channels when position needs to update
|
---|
242 | static const Observable::channels_t AtomPositionChannels;
|
---|
243 | //!> list of channels when selection needs to update
|
---|
244 | static const Observable::channels_t AtomSelectedChannels;
|
---|
245 |
|
---|
246 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
247 | const unsigned int AllsignedOnChannels;
|
---|
248 | unsigned int signedOffChannels;
|
---|
249 |
|
---|
250 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
251 | const Observable *owner;
|
---|
252 |
|
---|
253 | private:
|
---|
254 | //!> contains still the old id after the index of the atom changed
|
---|
255 | atomId_t oldId;
|
---|
256 |
|
---|
257 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
258 | QtObservedInstanceBoard & board;
|
---|
259 |
|
---|
260 | //!> is board still alive or not, impacts callbacks
|
---|
261 | bool BoardIsGone;
|
---|
262 |
|
---|
263 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
264 | ObservedValues_t ObservedValues;
|
---|
265 | };
|
---|
266 |
|
---|
267 |
|
---|
268 | #endif /* QTOBSERVEDATOM_HPP_ */
|
---|