| [0070aa] | 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 |  | 
|---|
| [65c323] | 19 | #include <boost/function.hpp> | 
|---|
| [98c35c] | 20 | #include <boost/shared_ptr.hpp> | 
|---|
| [0070aa] | 21 |  | 
|---|
| [65c323] | 22 | #include "CodePatterns/Observer/Observable.hpp" | 
|---|
| [0070aa] | 23 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
|  | 24 |  | 
|---|
| [65c323] | 25 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 26 |  | 
|---|
| [98c35c] | 27 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp" | 
|---|
| [41e287] | 28 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp" | 
|---|
| [0070aa] | 29 | #include "types.hpp" | 
|---|
|  | 30 |  | 
|---|
| [65c323] | 31 | class atom; | 
|---|
| [98c35c] | 32 | class QtObservedInstanceBoard; | 
|---|
| [1187c5] | 33 | class QtObservedMolecule; | 
|---|
| [98c35c] | 34 |  | 
|---|
| [0070aa] | 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. | 
|---|
| [65c323] | 44 | * | 
|---|
|  | 45 | * Essentially, this is the interface between molecuilder's World (and a | 
|---|
|  | 46 | * particular atom) and the QtGui part of the code. | 
|---|
| [0070aa] | 47 | */ | 
|---|
|  | 48 | class QtObservedAtom : public QWidget, public Observer | 
|---|
|  | 49 | { | 
|---|
|  | 50 | Q_OBJECT | 
|---|
|  | 51 |  | 
|---|
| [65c323] | 52 | public: | 
|---|
|  | 53 |  | 
|---|
|  | 54 | //!> typedef for instance wrapped in shared ptr | 
|---|
|  | 55 | typedef boost::shared_ptr<QtObservedAtom> ptr; | 
|---|
|  | 56 |  | 
|---|
| [bfd5d5f] | 57 | //!> typedef for instance wrapped in weak shared ptr | 
|---|
|  | 58 | typedef boost::weak_ptr<QtObservedAtom> weak_ptr; | 
|---|
|  | 59 |  | 
|---|
| [65c323] | 60 | private: | 
|---|
| [41e287] | 61 | //!> ObservedValuesContainer needs to access private cstor and dstor | 
|---|
| [59eabc] | 62 | friend class ObservedValuesContainer<QtObservedAtom, ObservedValue_Index_t>; | 
|---|
| [98c35c] | 63 | //!> QtObservedInstanceBoard needs to access private cstor and dstor | 
|---|
|  | 64 | friend class QtObservedInstanceBoard; | 
|---|
|  | 65 |  | 
|---|
| [0070aa] | 66 | /** Cstor of QtObservedAtom. | 
|---|
|  | 67 | * | 
|---|
| [62a0ee] | 68 | * \param _id id of observed atom | 
|---|
|  | 69 | * \param _atom ref to observed atom | 
|---|
| [65c323] | 70 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled() | 
|---|
| [0070aa] | 71 | * \param _parent Qt parent to automatically destroy when parent is destroyed | 
|---|
|  | 72 | */ | 
|---|
| [98c35c] | 73 | QtObservedAtom( | 
|---|
| [62a0ee] | 74 | const atomId_t _id, | 
|---|
|  | 75 | const atom * const _atom, | 
|---|
| [65c323] | 76 | QtObservedInstanceBoard &_board, | 
|---|
| [98c35c] | 77 | QWidget * _parent=0); | 
|---|
|  | 78 |  | 
|---|
|  | 79 | public: | 
|---|
| [0070aa] | 80 |  | 
|---|
|  | 81 | /** Dstor of QtObservedAtom. | 
|---|
|  | 82 | * | 
|---|
|  | 83 | */ | 
|---|
|  | 84 | ~QtObservedAtom(); | 
|---|
|  | 85 |  | 
|---|
| [98c35c] | 86 | // Observer functions | 
|---|
|  | 87 | void update(Observable *publisher); | 
|---|
|  | 88 | void subjectKilled(Observable *publisher); | 
|---|
|  | 89 | void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
|  | 90 |  | 
|---|
| [59eabc] | 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 |  | 
|---|
| [65c323] | 105 | /** Getter to atom index contained in \a ObservedValues. | 
|---|
|  | 106 | * | 
|---|
|  | 107 | * \return atom's index | 
|---|
|  | 108 | */ | 
|---|
| [3b9aa1] | 109 | const atomId_t& getAtomIndex() const; | 
|---|
| [65c323] | 110 |  | 
|---|
| [fe493f] | 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. | 
|---|
| [65c323] | 115 | * | 
|---|
| [fe493f] | 116 | * \return atom's bonds | 
|---|
| [65c323] | 117 | */ | 
|---|
| [3b9aa1] | 118 | const ListOfBonds_t& getAtomBonds() const; | 
|---|
| [65c323] | 119 |  | 
|---|
|  | 120 | /** Getter to atom element contained in \a ObservedValues. | 
|---|
|  | 121 | * | 
|---|
| [fe493f] | 122 | * \return atom's element | 
|---|
| [65c323] | 123 | */ | 
|---|
| [3b9aa1] | 124 | const atomicNumber_t& getAtomElement() const; | 
|---|
| [65c323] | 125 |  | 
|---|
| [fe493f] | 126 | /** Getter to atom name contained in \a ObservedValues. | 
|---|
|  | 127 | * | 
|---|
|  | 128 | * \return atom's name | 
|---|
|  | 129 | */ | 
|---|
| [3b9aa1] | 130 | const std::string& getAtomName() const; | 
|---|
| [65c323] | 131 |  | 
|---|
| [fe493f] | 132 | /** Getter to atom position contained in \a ObservedValues. | 
|---|
| [65c323] | 133 | * | 
|---|
| [fe493f] | 134 | * \return atom's position | 
|---|
| [65c323] | 135 | */ | 
|---|
| [3b9aa1] | 136 | const Vector& getAtomPosition() const; | 
|---|
| [fe493f] | 137 |  | 
|---|
| [897a01] | 138 | /** Getter to atom velocity contained in \a ObservedValues. | 
|---|
|  | 139 | * | 
|---|
|  | 140 | * \return atom's velocity | 
|---|
|  | 141 | */ | 
|---|
|  | 142 | const Vector& getAtomVelocity() const; | 
|---|
|  | 143 |  | 
|---|
|  | 144 | /** Getter to atom force contained in \a ObservedValues. | 
|---|
|  | 145 | * | 
|---|
|  | 146 | * \return atom's force | 
|---|
|  | 147 | */ | 
|---|
|  | 148 | const Vector& getAtomForce() const; | 
|---|
|  | 149 |  | 
|---|
| [273c8a] | 150 | /** Getter to the observed state of the associated molecule. | 
|---|
| [fe493f] | 151 | * | 
|---|
| [273c8a] | 152 | * \return const ref to observed state of molecule | 
|---|
| [fe493f] | 153 | */ | 
|---|
| [273c8a] | 154 | const QtObservedMolecule* const getMoleculeRef() const; | 
|---|
| [65c323] | 155 |  | 
|---|
| [9e9100] | 156 | /** Getter to atom's selected status. | 
|---|
|  | 157 | * | 
|---|
|  | 158 | * \return true - atom selected, false - else | 
|---|
|  | 159 | */ | 
|---|
|  | 160 | const bool getAtomSelected() const; | 
|---|
|  | 161 |  | 
|---|
| [0070aa] | 162 | signals: | 
|---|
| [522c45] | 163 | void indexChanged(); | 
|---|
| [65c323] | 164 | void bondsChanged(); | 
|---|
| [fe493f] | 165 | void elementChanged(); | 
|---|
| [1187c5] | 166 | void moleculeChanged(); | 
|---|
| [fe493f] | 167 | void nameChanged(); | 
|---|
|  | 168 | void positionChanged(); | 
|---|
| [897a01] | 169 | void velocityChanged(); | 
|---|
|  | 170 | void forceChanged(); | 
|---|
| [fe493f] | 171 | void atomRemoved(); | 
|---|
| [9e9100] | 172 | void selectedChanged(); | 
|---|
| [65c323] | 173 |  | 
|---|
|  | 174 | //private slots: | 
|---|
| [0070aa] | 175 |  | 
|---|
| [65c323] | 176 | private: | 
|---|
|  | 177 | void activateObserver(); | 
|---|
|  | 178 | void deactivateObserver(); | 
|---|
| [0070aa] | 179 |  | 
|---|
| [65c323] | 180 | static const atom * const getAtomConst(const atomId_t _id); | 
|---|
|  | 181 | static atom * const getAtom(const atomId_t _id); | 
|---|
| [0070aa] | 182 |  | 
|---|
|  | 183 | private: | 
|---|
| [f35f7e] | 184 | static atomId_t updateIndex(const atom &_atomref); | 
|---|
|  | 185 | static ListOfBonds_t updateBonds(const atom &_atom); | 
|---|
|  | 186 | static atomicNumber_t updateElement(const atom &_atom); | 
|---|
| [273c8a] | 187 | QtObservedMolecule* updateMoleculeRef(const atom &_atom); | 
|---|
| [f35f7e] | 188 | static std::string updateName(const atom &_atom); | 
|---|
|  | 189 | static Vector updatePosition(const atom &_atom); | 
|---|
| [897a01] | 190 | static Vector updateVelocity(const atom &_atom); | 
|---|
|  | 191 | static Vector updateForce(const atom &_atom); | 
|---|
| [f35f7e] | 192 | static bool updateSelected(const atom &_atom); | 
|---|
| [65c323] | 193 |  | 
|---|
|  | 194 | //!> enumeration of observed values to match with entries in ObservedValues | 
|---|
|  | 195 | enum ObservedTypes { | 
|---|
|  | 196 | //!> contains the current atom index | 
|---|
|  | 197 | AtomIndex, | 
|---|
| [fe493f] | 198 | //!> contains the current set of bonds atoms for the atom | 
|---|
|  | 199 | AtomBonds, | 
|---|
| [65c323] | 200 | //!> contains the current atom element | 
|---|
|  | 201 | AtomElement, | 
|---|
| [fe493f] | 202 | //!> contains the current atom position | 
|---|
|  | 203 | AtomName, | 
|---|
|  | 204 | //!> contains the current atom position | 
|---|
|  | 205 | AtomPosition, | 
|---|
| [897a01] | 206 | //!> contains the current atom velocity | 
|---|
|  | 207 | AtomVelocity, | 
|---|
|  | 208 | //!> contains the current atom force | 
|---|
|  | 209 | AtomForce, | 
|---|
| [9e9100] | 210 | //!> contains the current atom's selection status | 
|---|
|  | 211 | AtomSelected, | 
|---|
| [273c8a] | 212 | //!> contains the current atom's molecule index | 
|---|
|  | 213 | MoleculeRef, | 
|---|
| [65c323] | 214 | //!> gives the size of the enumeration | 
|---|
|  | 215 | MAX_ObservedTypes | 
|---|
|  | 216 | }; | 
|---|
|  | 217 |  | 
|---|
|  | 218 | /** Initializes all \a ObservedValues entries. | 
|---|
|  | 219 | * | 
|---|
|  | 220 | * \param _ObservedValues vector of ObservedValue to be filled | 
|---|
|  | 221 | * \param _id atom id | 
|---|
|  | 222 | * \param _atomref reference to atom | 
|---|
|  | 223 | * \param _subjectKilled ref to function to call on subjectKilled() | 
|---|
|  | 224 | */ | 
|---|
| [62a0ee] | 225 | void initObservedValues( | 
|---|
| [65c323] | 226 | ObservedValues_t &_ObservedValues, | 
|---|
|  | 227 | const atomId_t _id, | 
|---|
|  | 228 | const atom * const _atomref, | 
|---|
| [d48a16] | 229 | const boost::function<void()> &_subjectKilled); | 
|---|
| [65c323] | 230 |  | 
|---|
|  | 231 | /** Destroys all \a ObservedValues entries. | 
|---|
|  | 232 | * | 
|---|
|  | 233 | * \param _ObservedValues vector of ObservedValue to be destroyed | 
|---|
|  | 234 | */ | 
|---|
| [c44763] | 235 | void destroyObservedValues( | 
|---|
| [65c323] | 236 | std::vector<boost::any> &_ObservedValues); | 
|---|
|  | 237 |  | 
|---|
| [04c3a3] | 238 | /** Function is called by InstanceBoard to inform about its destruction. | 
|---|
|  | 239 | * | 
|---|
|  | 240 | * \note callbacks must not be used after this | 
|---|
|  | 241 | */ | 
|---|
|  | 242 | void noteBoardIsGone() | 
|---|
|  | 243 | { BoardIsGone = true; } | 
|---|
|  | 244 |  | 
|---|
| [62a0ee] | 245 | /** Counts the number of subject killed received from the observed values. | 
|---|
|  | 246 | * | 
|---|
|  | 247 | * \param _id id to check against ours | 
|---|
|  | 248 | */ | 
|---|
| [ee3fb8] | 249 | void countValuesSubjectKilled(ObservedValue_Index_t _id); | 
|---|
| [62a0ee] | 250 |  | 
|---|
| [65c323] | 251 | //!> counts how many ObservedValues have already been subjectKilled() | 
|---|
|  | 252 | mutable size_t subjectKilledCount; | 
|---|
|  | 253 |  | 
|---|
| [62a0ee] | 254 | /** Helper function to check that all subjectKilled have been received for both | 
|---|
|  | 255 | * this instance and all its internal observed values. | 
|---|
|  | 256 | * | 
|---|
| [ee3fb8] | 257 | * \param _id id to check against ours | 
|---|
| [62a0ee] | 258 | */ | 
|---|
| [ee3fb8] | 259 | void checkForRemoval(ObservedValue_Index_t _id); | 
|---|
| [62a0ee] | 260 |  | 
|---|
| [65c323] | 261 | private: | 
|---|
|  | 262 |  | 
|---|
|  | 263 | //!> list of channels when index needs to update | 
|---|
|  | 264 | static const Observable::channels_t AtomIndexChannels; | 
|---|
|  | 265 | //!> list of channels when bonds needs to update | 
|---|
|  | 266 | static const Observable::channels_t AtomBondsChannels; | 
|---|
| [fe493f] | 267 | //!> list of channels when element needs to update | 
|---|
|  | 268 | static const Observable::channels_t AtomElementChannels; | 
|---|
|  | 269 | //!> list of channels when molecule index needs to update | 
|---|
| [273c8a] | 270 | static const Observable::channels_t MoleculeChangedChannels; | 
|---|
| [fe493f] | 271 | //!> list of channels when name needs to update | 
|---|
|  | 272 | static const Observable::channels_t AtomNameChannels; | 
|---|
|  | 273 | //!> list of channels when position needs to update | 
|---|
|  | 274 | static const Observable::channels_t AtomPositionChannels; | 
|---|
| [897a01] | 275 | //!> list of channels when velocity needs to update | 
|---|
|  | 276 | static const Observable::channels_t AtomVelocityChannels; | 
|---|
|  | 277 | //!> list of channels when force needs to update | 
|---|
|  | 278 | static const Observable::channels_t AtomForceChannels; | 
|---|
| [9e9100] | 279 | //!> list of channels when selection needs to update | 
|---|
|  | 280 | static const Observable::channels_t AtomSelectedChannels; | 
|---|
| [65c323] | 281 |  | 
|---|
|  | 282 | //!> we get multiple subjectKilled(), count and call callback() only on last | 
|---|
|  | 283 | const unsigned int AllsignedOnChannels; | 
|---|
|  | 284 | unsigned int signedOffChannels; | 
|---|
|  | 285 |  | 
|---|
|  | 286 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL) | 
|---|
|  | 287 | const Observable *owner; | 
|---|
|  | 288 |  | 
|---|
| [bfd5d5f] | 289 | private: | 
|---|
|  | 290 |  | 
|---|
|  | 291 | /** Internal setter for the weak shared_ptr instance that we sometimes | 
|---|
|  | 292 | * need to convert the ref to this instance into an shared ptr instance that | 
|---|
|  | 293 | * is safe to hand around. | 
|---|
|  | 294 | * | 
|---|
|  | 295 | * \param _selfref ref to shared ptr instance that is internally stored | 
|---|
|  | 296 | */ | 
|---|
|  | 297 | void setSelfRef(const weak_ptr &_selfref) | 
|---|
|  | 298 | { const_cast<weak_ptr &>(selfref) = _selfref; } | 
|---|
|  | 299 |  | 
|---|
|  | 300 | //!> reference to this instance wrapped in a shared ptr for handing around | 
|---|
|  | 301 | const weak_ptr selfref; | 
|---|
|  | 302 |  | 
|---|
|  | 303 | public: | 
|---|
|  | 304 |  | 
|---|
|  | 305 | /** Getter for this instance safely wrapped in a shared ptr instance for | 
|---|
|  | 306 | * handing arount. | 
|---|
|  | 307 | * | 
|---|
|  | 308 | * \return shared ptr of this instance | 
|---|
|  | 309 | */ | 
|---|
|  | 310 | ptr getRef() const | 
|---|
|  | 311 | { return ptr(selfref); } | 
|---|
|  | 312 |  | 
|---|
| [65c323] | 313 | private: | 
|---|
| [498c678] | 314 | //!> unique index among all observed instances | 
|---|
|  | 315 | const ObservedValue_Index_t index; | 
|---|
|  | 316 |  | 
|---|
| [65c323] | 317 | //!> reference to InstanceBoard for callbacks on subjectKilled() | 
|---|
|  | 318 | QtObservedInstanceBoard & board; | 
|---|
|  | 319 |  | 
|---|
| [04c3a3] | 320 | //!> is board still alive or not, impacts callbacks | 
|---|
|  | 321 | bool BoardIsGone; | 
|---|
|  | 322 |  | 
|---|
| [98c35c] | 323 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard | 
|---|
| [65c323] | 324 | ObservedValues_t ObservedValues; | 
|---|
| [0070aa] | 325 | }; | 
|---|
|  | 326 |  | 
|---|
|  | 327 |  | 
|---|
|  | 328 | #endif /* QTOBSERVEDATOM_HPP_ */ | 
|---|