| [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 | 
 | 
|---|
 | 138 |   /** Getter to atom's molecule index contained in \a ObservedValues.
 | 
|---|
 | 139 |    *
 | 
|---|
 | 140 |    * \return atom's molecule index
 | 
|---|
 | 141 |    */
 | 
|---|
| [1187c5] | 142 |   QtObservedMolecule* const getAtomMolecule() const;
 | 
|---|
| [65c323] | 143 | 
 | 
|---|
| [9e9100] | 144 |   /** Getter to atom's selected status.
 | 
|---|
 | 145 |    *
 | 
|---|
 | 146 |    * \return true - atom selected, false - else
 | 
|---|
 | 147 |    */
 | 
|---|
 | 148 |   const bool getAtomSelected() const;
 | 
|---|
 | 149 | 
 | 
|---|
| [0070aa] | 150 | signals:
 | 
|---|
| [1c0961] | 151 |   void indexChanged(const atomId_t, const atomId_t);
 | 
|---|
| [65c323] | 152 |   void bondsChanged();
 | 
|---|
| [fe493f] | 153 |   void elementChanged();
 | 
|---|
| [1187c5] | 154 |   void moleculeChanged();
 | 
|---|
| [fe493f] | 155 |   void nameChanged();
 | 
|---|
 | 156 |   void positionChanged();
 | 
|---|
 | 157 |   void atomRemoved();
 | 
|---|
| [9e9100] | 158 |   void selectedChanged();
 | 
|---|
| [65c323] | 159 | 
 | 
|---|
 | 160 | //private slots:
 | 
|---|
| [0070aa] | 161 | 
 | 
|---|
| [65c323] | 162 | private:
 | 
|---|
 | 163 |   void activateObserver();
 | 
|---|
 | 164 |   void deactivateObserver();
 | 
|---|
| [0070aa] | 165 | 
 | 
|---|
| [65c323] | 166 |   static const atom * const getAtomConst(const atomId_t _id);
 | 
|---|
 | 167 |   static atom * const getAtom(const atomId_t _id);
 | 
|---|
| [0070aa] | 168 | 
 | 
|---|
 | 169 | private:
 | 
|---|
| [f35f7e] | 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* updateMoleculeIndex(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);
 | 
|---|
| [65c323] | 177 | 
 | 
|---|
 | 178 |   //!> enumeration of observed values to match with entries in ObservedValues
 | 
|---|
 | 179 |   enum ObservedTypes {
 | 
|---|
 | 180 |     //!> contains the current atom index
 | 
|---|
 | 181 |     AtomIndex,
 | 
|---|
| [fe493f] | 182 |     //!> contains the current set of bonds atoms for the atom
 | 
|---|
 | 183 |     AtomBonds,
 | 
|---|
| [65c323] | 184 |     //!> contains the current atom element
 | 
|---|
 | 185 |     AtomElement,
 | 
|---|
| [fe493f] | 186 |     //!> contains the current atom's molecule index
 | 
|---|
| [1187c5] | 187 |     AtomMolecule,
 | 
|---|
| [fe493f] | 188 |     //!> contains the current atom position
 | 
|---|
 | 189 |     AtomName,
 | 
|---|
 | 190 |     //!> contains the current atom position
 | 
|---|
 | 191 |     AtomPosition,
 | 
|---|
| [9e9100] | 192 |     //!> contains the current atom's selection status
 | 
|---|
 | 193 |     AtomSelected,
 | 
|---|
| [65c323] | 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 |    */
 | 
|---|
| [62a0ee] | 205 |   void initObservedValues(
 | 
|---|
| [65c323] | 206 |       ObservedValues_t &_ObservedValues,
 | 
|---|
 | 207 |       const atomId_t _id,
 | 
|---|
 | 208 |       const atom * const _atomref,
 | 
|---|
| [d48a16] | 209 |       const boost::function<void()> &_subjectKilled);
 | 
|---|
| [65c323] | 210 | 
 | 
|---|
 | 211 |   /** Destroys all \a ObservedValues entries.
 | 
|---|
 | 212 |    *
 | 
|---|
 | 213 |    * \param _ObservedValues vector of ObservedValue to be destroyed
 | 
|---|
 | 214 |    */
 | 
|---|
| [c44763] | 215 |   void destroyObservedValues(
 | 
|---|
| [65c323] | 216 |       std::vector<boost::any> &_ObservedValues);
 | 
|---|
 | 217 | 
 | 
|---|
| [04c3a3] | 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 | 
 | 
|---|
| [62a0ee] | 225 |   /** Counts the number of subject killed received from the observed values.
 | 
|---|
 | 226 |    *
 | 
|---|
 | 227 |    * \param _id id to check against ours
 | 
|---|
 | 228 |    */
 | 
|---|
| [ee3fb8] | 229 |   void countValuesSubjectKilled(ObservedValue_Index_t _id);
 | 
|---|
| [62a0ee] | 230 | 
 | 
|---|
| [65c323] | 231 |   //!> counts how many ObservedValues have already been subjectKilled()
 | 
|---|
 | 232 |   mutable size_t subjectKilledCount;
 | 
|---|
 | 233 | 
 | 
|---|
| [62a0ee] | 234 |   /** Helper function to check that all subjectKilled have been received for both
 | 
|---|
 | 235 |    * this instance and all its internal observed values.
 | 
|---|
 | 236 |    *
 | 
|---|
| [ee3fb8] | 237 |    * \param _id id to check against ours
 | 
|---|
| [62a0ee] | 238 |    */
 | 
|---|
| [ee3fb8] | 239 |   void checkForRemoval(ObservedValue_Index_t _id);
 | 
|---|
| [62a0ee] | 240 | 
 | 
|---|
| [65c323] | 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;
 | 
|---|
| [fe493f] | 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
 | 
|---|
| [1187c5] | 250 |   static const Observable::channels_t AtomMoleculeChannels;
 | 
|---|
| [fe493f] | 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;
 | 
|---|
| [9e9100] | 255 |   //!> list of channels when selection needs to update
 | 
|---|
 | 256 |   static const Observable::channels_t AtomSelectedChannels;
 | 
|---|
| [65c323] | 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 | 
 | 
|---|
| [bfd5d5f] | 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 | 
 | 
|---|
| [65c323] | 289 | private:
 | 
|---|
| [1c0961] | 290 |   //!> contains still the old id after the index of the atom changed
 | 
|---|
 | 291 |   atomId_t oldId;
 | 
|---|
| [65c323] | 292 | 
 | 
|---|
 | 293 |   //!> reference to InstanceBoard for callbacks on subjectKilled()
 | 
|---|
 | 294 |   QtObservedInstanceBoard & board;
 | 
|---|
 | 295 | 
 | 
|---|
| [04c3a3] | 296 |   //!> is board still alive or not, impacts callbacks
 | 
|---|
 | 297 |   bool BoardIsGone;
 | 
|---|
 | 298 | 
 | 
|---|
| [98c35c] | 299 |   //!> internal reference to ObservedValues held by QtObservedInstanceBoard
 | 
|---|
| [65c323] | 300 |   ObservedValues_t ObservedValues;
 | 
|---|
| [0070aa] | 301 | };
 | 
|---|
 | 302 | 
 | 
|---|
 | 303 | 
 | 
|---|
 | 304 | #endif /* QTOBSERVEDATOM_HPP_ */
 | 
|---|