| 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 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 | 
 | 
|---|
| 150 |   /** Getter to the observed state of the associated molecule.
 | 
|---|
| 151 |    *
 | 
|---|
| 152 |    * \return const ref to observed state of molecule
 | 
|---|
| 153 |    */
 | 
|---|
| 154 |   const QtObservedMolecule* const getMoleculeRef() const;
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   /** Getter to atom's selected status.
 | 
|---|
| 157 |    *
 | 
|---|
| 158 |    * \return true - atom selected, false - else
 | 
|---|
| 159 |    */
 | 
|---|
| 160 |   const bool getAtomSelected() const;
 | 
|---|
| 161 | 
 | 
|---|
| 162 | signals:
 | 
|---|
| 163 |   void indexChanged();
 | 
|---|
| 164 |   void bondsChanged();
 | 
|---|
| 165 |   void elementChanged();
 | 
|---|
| 166 |   void moleculeChanged();
 | 
|---|
| 167 |   void nameChanged();
 | 
|---|
| 168 |   void positionChanged();
 | 
|---|
| 169 |   void velocityChanged();
 | 
|---|
| 170 |   void forceChanged();
 | 
|---|
| 171 |   void atomRemoved();
 | 
|---|
| 172 |   void selectedChanged();
 | 
|---|
| 173 | 
 | 
|---|
| 174 | //private slots:
 | 
|---|
| 175 | 
 | 
|---|
| 176 | private:
 | 
|---|
| 177 |   void activateObserver();
 | 
|---|
| 178 |   void deactivateObserver();
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   static const atom * const getAtomConst(const atomId_t _id);
 | 
|---|
| 181 |   static atom * const getAtom(const atomId_t _id);
 | 
|---|
| 182 | 
 | 
|---|
| 183 | private:
 | 
|---|
| 184 |   static atomId_t updateIndex(const atom &_atomref);
 | 
|---|
| 185 |   static ListOfBonds_t updateBonds(const atom &_atom);
 | 
|---|
| 186 |   static atomicNumber_t updateElement(const atom &_atom);
 | 
|---|
| 187 |   QtObservedMolecule* updateMoleculeRef(const atom &_atom);
 | 
|---|
| 188 |   static std::string updateName(const atom &_atom);
 | 
|---|
| 189 |   static Vector updatePosition(const atom &_atom);
 | 
|---|
| 190 |   static Vector updateVelocity(const atom &_atom);
 | 
|---|
| 191 |   static Vector updateForce(const atom &_atom);
 | 
|---|
| 192 |   static bool updateSelected(const atom &_atom);
 | 
|---|
| 193 | 
 | 
|---|
| 194 |   //!> enumeration of observed values to match with entries in ObservedValues
 | 
|---|
| 195 |   enum ObservedTypes {
 | 
|---|
| 196 |     //!> contains the current atom index
 | 
|---|
| 197 |     AtomIndex,
 | 
|---|
| 198 |     //!> contains the current set of bonds atoms for the atom
 | 
|---|
| 199 |     AtomBonds,
 | 
|---|
| 200 |     //!> contains the current atom element
 | 
|---|
| 201 |     AtomElement,
 | 
|---|
| 202 |     //!> contains the current atom position
 | 
|---|
| 203 |     AtomName,
 | 
|---|
| 204 |     //!> contains the current atom position
 | 
|---|
| 205 |     AtomPosition,
 | 
|---|
| 206 |     //!> contains the current atom velocity
 | 
|---|
| 207 |     AtomVelocity,
 | 
|---|
| 208 |     //!> contains the current atom force
 | 
|---|
| 209 |     AtomForce,
 | 
|---|
| 210 |     //!> contains the current atom's selection status
 | 
|---|
| 211 |     AtomSelected,
 | 
|---|
| 212 |     //!> contains the current atom's molecule index
 | 
|---|
| 213 |     MoleculeRef,
 | 
|---|
| 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 |    */
 | 
|---|
| 225 |   void initObservedValues(
 | 
|---|
| 226 |       ObservedValues_t &_ObservedValues,
 | 
|---|
| 227 |       const atomId_t _id,
 | 
|---|
| 228 |       const atom * const _atomref,
 | 
|---|
| 229 |       const boost::function<void()> &_subjectKilled);
 | 
|---|
| 230 | 
 | 
|---|
| 231 |   /** Destroys all \a ObservedValues entries.
 | 
|---|
| 232 |    *
 | 
|---|
| 233 |    * \param _ObservedValues vector of ObservedValue to be destroyed
 | 
|---|
| 234 |    */
 | 
|---|
| 235 |   void destroyObservedValues(
 | 
|---|
| 236 |       std::vector<boost::any> &_ObservedValues);
 | 
|---|
| 237 | 
 | 
|---|
| 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 | 
 | 
|---|
| 245 |   /** Counts the number of subject killed received from the observed values.
 | 
|---|
| 246 |    *
 | 
|---|
| 247 |    * \param _id id to check against ours
 | 
|---|
| 248 |    */
 | 
|---|
| 249 |   void countValuesSubjectKilled(ObservedValue_Index_t _id);
 | 
|---|
| 250 | 
 | 
|---|
| 251 |   //!> counts how many ObservedValues have already been subjectKilled()
 | 
|---|
| 252 |   mutable size_t subjectKilledCount;
 | 
|---|
| 253 | 
 | 
|---|
| 254 |   /** Helper function to check that all subjectKilled have been received for both
 | 
|---|
| 255 |    * this instance and all its internal observed values.
 | 
|---|
| 256 |    *
 | 
|---|
| 257 |    * \param _id id to check against ours
 | 
|---|
| 258 |    */
 | 
|---|
| 259 |   void checkForRemoval(ObservedValue_Index_t _id);
 | 
|---|
| 260 | 
 | 
|---|
| 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;
 | 
|---|
| 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
 | 
|---|
| 270 |   static const Observable::channels_t MoleculeChangedChannels;
 | 
|---|
| 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;
 | 
|---|
| 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;
 | 
|---|
| 279 |   //!> list of channels when selection needs to update
 | 
|---|
| 280 |   static const Observable::channels_t AtomSelectedChannels;
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 313 | private:
 | 
|---|
| 314 |   //!> unique index among all observed instances
 | 
|---|
| 315 |   const ObservedValue_Index_t index;
 | 
|---|
| 316 | 
 | 
|---|
| 317 |   //!> reference to InstanceBoard for callbacks on subjectKilled()
 | 
|---|
| 318 |   QtObservedInstanceBoard & board;
 | 
|---|
| 319 | 
 | 
|---|
| 320 |   //!> is board still alive or not, impacts callbacks
 | 
|---|
| 321 |   bool BoardIsGone;
 | 
|---|
| 322 | 
 | 
|---|
| 323 |   //!> internal reference to ObservedValues held by QtObservedInstanceBoard
 | 
|---|
| 324 |   ObservedValues_t ObservedValues;
 | 
|---|
| 325 | };
 | 
|---|
| 326 | 
 | 
|---|
| 327 | 
 | 
|---|
| 328 | #endif /* QTOBSERVEDATOM_HPP_ */
 | 
|---|