| 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 | //!> typedef for instance wrapped in shared ptr | 
|---|
| 55 | typedef boost::weak_ptr<QtObservedMolecule> weak_ptr; | 
|---|
| 56 |  | 
|---|
| 57 | private: | 
|---|
| 58 | //!> ObservedValuesContainer needs to access private cstor and dstor | 
|---|
| 59 | friend class ObservedValuesContainer<QtObservedMolecule, ObservedValue_Index_t>; | 
|---|
| 60 | //!> QtObservedInstanceBoard needs to access private cstor and dstor | 
|---|
| 61 | friend class QtObservedInstanceBoard; | 
|---|
| 62 |  | 
|---|
| 63 | /** Cstor of QtObservedMolecule. | 
|---|
| 64 | * | 
|---|
| 65 | * \param _id id of observed molecule | 
|---|
| 66 | * \param _mol ref to observed molecule | 
|---|
| 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 | QtObservedMolecule( | 
|---|
| 71 | const moleculeId_t _id, | 
|---|
| 72 | const molecule * const _mol, | 
|---|
| 73 | QtObservedInstanceBoard &_board, | 
|---|
| 74 | QWidget * _parent=0); | 
|---|
| 75 |  | 
|---|
| 76 | public: | 
|---|
| 77 |  | 
|---|
| 78 | /** Dstor of QtObservedMolecule. | 
|---|
| 79 | * | 
|---|
| 80 | */ | 
|---|
| 81 | ~QtObservedMolecule(); | 
|---|
| 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 for a permanent and unique index of this instance. | 
|---|
| 89 | * | 
|---|
| 90 | * \note ALWAYS use this index if you need to store and identifier to this | 
|---|
| 91 | * instance which you might need to retrieve at some later date. | 
|---|
| 92 | * | 
|---|
| 93 | * \warning DO NOT STORE the QtObserved...:ptr directly. This will possibly | 
|---|
| 94 | * prevent their eventual destruction. Only store the ObservedValue_Index_t | 
|---|
| 95 | * as means to obtain the ptr from the QtObservedInstanceBoard. | 
|---|
| 96 | * | 
|---|
| 97 | * \return returns a unique and permanent index that can be used to retrieve this | 
|---|
| 98 | * instance from the QtObservedInstanceBoard as it must not be stored. | 
|---|
| 99 | */ | 
|---|
| 100 | ObservedValue_Index_t getIndex() const; | 
|---|
| 101 |  | 
|---|
| 102 | /** Getter to molecule atom count contained in \a ObservedValues. | 
|---|
| 103 | * | 
|---|
| 104 | * \return molecule's atom count | 
|---|
| 105 | */ | 
|---|
| 106 | const int& getAtomCount() const; | 
|---|
| 107 |  | 
|---|
| 108 | /** Getter to molecule bond count contained in \a ObservedValues. | 
|---|
| 109 | * | 
|---|
| 110 | * \return molecule's bond count | 
|---|
| 111 | */ | 
|---|
| 112 | const int& getBondCount() const; | 
|---|
| 113 |  | 
|---|
| 114 | /** Getter to molecule center contained in \a ObservedValues. | 
|---|
| 115 | * | 
|---|
| 116 | * \return molecule's center | 
|---|
| 117 | */ | 
|---|
| 118 | const Vector& getMolCenter() const; | 
|---|
| 119 |  | 
|---|
| 120 | /** Getter to molecule index contained in \a ObservedValues. | 
|---|
| 121 | * | 
|---|
| 122 | * \return molecule's index | 
|---|
| 123 | */ | 
|---|
| 124 | const moleculeId_t& getMolIndex() const; | 
|---|
| 125 |  | 
|---|
| 126 | /** Getter to molecule name contained in \a ObservedValues. | 
|---|
| 127 | * | 
|---|
| 128 | * \return molecule's name | 
|---|
| 129 | */ | 
|---|
| 130 | const std::string& getMolName() const; | 
|---|
| 131 |  | 
|---|
| 132 | /** Getter to molecule formula contained in \a ObservedValues. | 
|---|
| 133 | * | 
|---|
| 134 | * \return molecule's formula | 
|---|
| 135 | */ | 
|---|
| 136 | const std::string& getMolFormula() const; | 
|---|
| 137 |  | 
|---|
| 138 | /** Getter to molecule non-hydrogen atom  count contained in \a ObservedValues. | 
|---|
| 139 | * | 
|---|
| 140 | * \return molecule's non-hydrogen atom count | 
|---|
| 141 | */ | 
|---|
| 142 | const int& getNonHydrogenCount() const; | 
|---|
| 143 |  | 
|---|
| 144 | /** Getter to molecule's bounding box contained in \a ObservedValues. | 
|---|
| 145 | * | 
|---|
| 146 | * \return molecule's bounding box | 
|---|
| 147 | */ | 
|---|
| 148 | const molecule::BoundingBoxInfo& getBoundingBox() const; | 
|---|
| 149 |  | 
|---|
| 150 | /** Getter to molecule's selected status. | 
|---|
| 151 | * | 
|---|
| 152 | * \return true - molecule selected, false - else | 
|---|
| 153 | */ | 
|---|
| 154 | const bool& getMolSelected() const; | 
|---|
| 155 |  | 
|---|
| 156 | static const molecule * const getMolecule(const moleculeId_t _id); | 
|---|
| 157 |  | 
|---|
| 158 | signals: | 
|---|
| 159 | void atomcountChanged(); | 
|---|
| 160 | void bondcountChanged(); | 
|---|
| 161 | void formulaChanged(); | 
|---|
| 162 | void indexChanged(); | 
|---|
| 163 | void nameChanged(); | 
|---|
| 164 | void nononhydrogenChanged(); | 
|---|
| 165 | void centerChanged(); | 
|---|
| 166 | void tesselationhullChanged(); | 
|---|
| 167 | void boundingboxChanged(); | 
|---|
| 168 | void atomInserted(QtObservedAtom::ptr, QtObservedMolecule *); | 
|---|
| 169 | void atomRemoved(ObservedValue_Index_t, QtObservedMolecule *); | 
|---|
| 170 | void moleculeRemoved(); | 
|---|
| 171 | void selectedChanged(); | 
|---|
| 172 |  | 
|---|
| 173 | private: | 
|---|
| 174 |  | 
|---|
| 175 | void activateObserver(); | 
|---|
| 176 | void deactivateObserver(); | 
|---|
| 177 |  | 
|---|
| 178 | private: | 
|---|
| 179 | static int updateAtomCount(const molecule &mol); | 
|---|
| 180 | static int updateBondCount(const molecule &mol); | 
|---|
| 181 | static molecule::BoundingBoxInfo updateBoundingBox(const molecule &mol); | 
|---|
| 182 | static std::string updateFormulaString(const molecule &mol); | 
|---|
| 183 | static Vector updateCenter(const molecule &mol); | 
|---|
| 184 | static moleculeId_t updateIndex(const molecule &mol); | 
|---|
| 185 | static std::string updateName(const molecule &mol); | 
|---|
| 186 | static int updateNonHydrogenCount(const molecule &mol); | 
|---|
| 187 | static bool updateSelected(const molecule &mol); | 
|---|
| 188 |  | 
|---|
| 189 | //!> list of channels when atom count needs to update | 
|---|
| 190 | static const Observable::channels_t AtomCountChannels; | 
|---|
| 191 | //!> list of channels when bond count needs to update | 
|---|
| 192 | static const Observable::channels_t BondCountChannels; | 
|---|
| 193 | //!> list of channels when bounding box needs to update | 
|---|
| 194 | static const Observable::channels_t BoundingBoxChannels; | 
|---|
| 195 | //!> list of channels when formula needs to update | 
|---|
| 196 | static const Observable::channels_t FormulaStringChannels; | 
|---|
| 197 | //!> list of channels when the center needs to update | 
|---|
| 198 | static const Observable::channels_t CenterChannels; | 
|---|
| 199 | //!> list of channels when the index needs to update | 
|---|
| 200 | static const Observable::channels_t IndexChannels; | 
|---|
| 201 | //!> list of channels when the name needs to update | 
|---|
| 202 | static const Observable::channels_t NameChannels; | 
|---|
| 203 | //!> list of channels when the name needs to update | 
|---|
| 204 | static const Observable::channels_t NonHydrogenCountChannels; | 
|---|
| 205 | //!> list of channels when the name needs to update | 
|---|
| 206 | static const Observable::channels_t SelectedChannels; | 
|---|
| 207 |  | 
|---|
| 208 | private: | 
|---|
| 209 | /** Observed Values **/ | 
|---|
| 210 |  | 
|---|
| 211 | //!> enumeration of observed values to match with entries in ObservedValues | 
|---|
| 212 | enum ObservedTypes { | 
|---|
| 213 | //!> contains the current molecule index | 
|---|
| 214 | MolIndex, | 
|---|
| 215 | //!> contains the current molecule's atom count | 
|---|
| 216 | AtomCount, | 
|---|
| 217 | //!> contains the current molecule's number of bonds | 
|---|
| 218 | BondCount, | 
|---|
| 219 | //!> contains newest version of the bounding box on request | 
|---|
| 220 | BoundingBox, | 
|---|
| 221 | //!> contains the current molecule's formula | 
|---|
| 222 | FormulaString, | 
|---|
| 223 | //!> contains the current molecule's center | 
|---|
| 224 | MolCenter, | 
|---|
| 225 | //!> contains the current molecule name | 
|---|
| 226 | MolName, | 
|---|
| 227 | //!> contains the current molecule's non-hydrogen atom count | 
|---|
| 228 | NonHydrogenCount, | 
|---|
| 229 | //!> contains the current molecule's selection status | 
|---|
| 230 | MolSelected, | 
|---|
| 231 | //!> gives the size of the enumeration | 
|---|
| 232 | MAX_ObservedTypes | 
|---|
| 233 | }; | 
|---|
| 234 |  | 
|---|
| 235 | /** Initializes all \a _ObservedValues entries. | 
|---|
| 236 | * | 
|---|
| 237 | * \param _ObservedValues vector of ObservedValue to be filled | 
|---|
| 238 | * \param _moid molecule id | 
|---|
| 239 | * \param _molref reference to molecule | 
|---|
| 240 | * \param _subjectKilled ref to function to call on subjectKilled() | 
|---|
| 241 | */ | 
|---|
| 242 | void initObservedValues( | 
|---|
| 243 | ObservedValues_t &_ObservedValues, | 
|---|
| 244 | const moleculeId_t _molid, | 
|---|
| 245 | const molecule * const _molref, | 
|---|
| 246 | const boost::function<void()> &_subjectKilled); | 
|---|
| 247 |  | 
|---|
| 248 | /** Destroys all \a ObservedValues entries. | 
|---|
| 249 | * | 
|---|
| 250 | * \param _ObservedValues vector of ObservedValue to be destroyed | 
|---|
| 251 | */ | 
|---|
| 252 | void destroyObservedValues( | 
|---|
| 253 | std::vector<boost::any> &_ObservedValues); | 
|---|
| 254 |  | 
|---|
| 255 | /** Function is called by InstanceBoard to inform about its destruction. | 
|---|
| 256 | * | 
|---|
| 257 | * \note callbacks must not be used after this | 
|---|
| 258 | */ | 
|---|
| 259 | void noteBoardIsGone() | 
|---|
| 260 | { BoardIsGone = true; } | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 | /** Counts the number of subject killed received from the observed values. | 
|---|
| 264 | * | 
|---|
| 265 | * \param _id id to check against ours | 
|---|
| 266 | */ | 
|---|
| 267 | void countValuesSubjectKilled(ObservedValue_Index_t _id); | 
|---|
| 268 |  | 
|---|
| 269 | //!> counts how many ObservedValues have already been subjectKilled() | 
|---|
| 270 | mutable size_t subjectKilledCount; | 
|---|
| 271 |  | 
|---|
| 272 | /** Helper function to check that all subjectKilled have been received for both | 
|---|
| 273 | * this instance and all its internal observed values. | 
|---|
| 274 | * | 
|---|
| 275 | * \param _id id to check against ours | 
|---|
| 276 | */ | 
|---|
| 277 | void checkForRemoval(ObservedValue_Index_t _id); | 
|---|
| 278 |  | 
|---|
| 279 | private: | 
|---|
| 280 |  | 
|---|
| 281 | //!> we get multiple subjectKilled(), count and call callback() only on last | 
|---|
| 282 | const unsigned int AllsignedOnChannels; | 
|---|
| 283 | unsigned int signedOffChannels; | 
|---|
| 284 |  | 
|---|
| 285 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL) | 
|---|
| 286 | const Observable *owner; | 
|---|
| 287 |  | 
|---|
| 288 | private: | 
|---|
| 289 |  | 
|---|
| 290 | /** Internal setter for the weak shared_ptr instance that we sometimes | 
|---|
| 291 | * need to convert the ref to this instance into an shared ptr instance that | 
|---|
| 292 | * is safe to hand around. | 
|---|
| 293 | * | 
|---|
| 294 | * \param _selfref ref to shared ptr instance that is internally stored | 
|---|
| 295 | */ | 
|---|
| 296 | void setSelfRef(const weak_ptr &_selfref) | 
|---|
| 297 | { const_cast<weak_ptr &>(selfref) = _selfref; } | 
|---|
| 298 |  | 
|---|
| 299 | //!> reference to this instance wrapped in a shared ptr for handing around | 
|---|
| 300 | const weak_ptr selfref; | 
|---|
| 301 |  | 
|---|
| 302 | public: | 
|---|
| 303 |  | 
|---|
| 304 | /** Getter for this instance safely wrapped in a shared ptr instance for | 
|---|
| 305 | * handing arount. | 
|---|
| 306 | * | 
|---|
| 307 | * \return shared ptr of this instance | 
|---|
| 308 | */ | 
|---|
| 309 | ptr getRef() const | 
|---|
| 310 | { return ptr(selfref); } | 
|---|
| 311 |  | 
|---|
| 312 | private: | 
|---|
| 313 | //!> unique index among all observed instances | 
|---|
| 314 | const ObservedValue_Index_t index; | 
|---|
| 315 |  | 
|---|
| 316 | //!> reference to InstanceBoard for callbacks on subjectKilled() | 
|---|
| 317 | QtObservedInstanceBoard & board; | 
|---|
| 318 |  | 
|---|
| 319 | //!> is board still alive or not, impacts callbacks | 
|---|
| 320 | bool BoardIsGone; | 
|---|
| 321 |  | 
|---|
| 322 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard | 
|---|
| 323 | ObservedValues_t ObservedValues; | 
|---|
| 324 | }; | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | #endif /* QTOBSERVEDMOLECULE_HPP_ */ | 
|---|