| [0070aa] | 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 |  | 
|---|
| [494478] | 19 | #include <boost/function.hpp> | 
|---|
| [98c35c] | 20 | #include <boost/shared_ptr.hpp> | 
|---|
| [0070aa] | 21 |  | 
|---|
| [494478] | 22 | #include "CodePatterns/Observer/Observable.hpp" | 
|---|
| [0070aa] | 23 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
|  | 24 |  | 
|---|
| [5a9e34] | 25 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 26 |  | 
|---|
| [494478] | 27 | #include "molecule.hpp" | 
|---|
| [98c35c] | 28 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp" | 
|---|
| [41e287] | 29 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp" | 
|---|
| [1b07b1] | 30 | #include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp" | 
|---|
| [0070aa] | 31 | #include "types.hpp" | 
|---|
|  | 32 |  | 
|---|
| [98c35c] | 33 | class QtObservedInstanceBoard; | 
|---|
|  | 34 |  | 
|---|
| [0070aa] | 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 |  | 
|---|
| [494478] | 49 | public: | 
|---|
|  | 50 |  | 
|---|
|  | 51 | //!> typedef for instance wrapped in shared ptr | 
|---|
|  | 52 | typedef boost::shared_ptr<QtObservedMolecule> ptr; | 
|---|
|  | 53 |  | 
|---|
| [bfd5d5f] | 54 | //!> typedef for instance wrapped in shared ptr | 
|---|
|  | 55 | typedef boost::weak_ptr<QtObservedMolecule> weak_ptr; | 
|---|
|  | 56 |  | 
|---|
| [494478] | 57 | private: | 
|---|
| [41e287] | 58 | //!> ObservedValuesContainer needs to access private cstor and dstor | 
|---|
| [59eabc] | 59 | friend class ObservedValuesContainer<QtObservedMolecule, ObservedValue_Index_t>; | 
|---|
| [98c35c] | 60 | //!> QtObservedInstanceBoard needs to access private cstor and dstor | 
|---|
|  | 61 | friend class QtObservedInstanceBoard; | 
|---|
|  | 62 |  | 
|---|
| [0070aa] | 63 | /** Cstor of QtObservedMolecule. | 
|---|
|  | 64 | * | 
|---|
| [62a0ee] | 65 | * \param _id id of observed molecule | 
|---|
|  | 66 | * \param _mol ref to observed molecule | 
|---|
| [494478] | 67 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled() | 
|---|
| [0070aa] | 68 | * \param _parent Qt parent to automatically destroy when parent is destroyed | 
|---|
|  | 69 | */ | 
|---|
| [98c35c] | 70 | QtObservedMolecule( | 
|---|
| [62a0ee] | 71 | const moleculeId_t _id, | 
|---|
|  | 72 | const molecule * const _mol, | 
|---|
| [494478] | 73 | QtObservedInstanceBoard &_board, | 
|---|
| [98c35c] | 74 | QWidget * _parent=0); | 
|---|
|  | 75 |  | 
|---|
|  | 76 | public: | 
|---|
| [0070aa] | 77 |  | 
|---|
|  | 78 | /** Dstor of QtObservedMolecule. | 
|---|
|  | 79 | * | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | ~QtObservedMolecule(); | 
|---|
|  | 82 |  | 
|---|
| [98c35c] | 83 | // Observer functions | 
|---|
|  | 84 | void update(Observable *publisher); | 
|---|
|  | 85 | void subjectKilled(Observable *publisher); | 
|---|
|  | 86 | void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
|  | 87 |  | 
|---|
| [59eabc] | 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 |  | 
|---|
| [1b6415a] | 102 | /** Getter to molecule atom count contained in \a ObservedValues. | 
|---|
|  | 103 | * | 
|---|
|  | 104 | * \return molecule's atom count | 
|---|
|  | 105 | */ | 
|---|
| [3b9aa1] | 106 | const int& getAtomCount() const; | 
|---|
| [1b6415a] | 107 |  | 
|---|
| [5a9e34] | 108 | /** Getter to molecule bond count contained in \a ObservedValues. | 
|---|
|  | 109 | * | 
|---|
|  | 110 | * \return molecule's bond count | 
|---|
|  | 111 | */ | 
|---|
| [3b9aa1] | 112 | const int& getBondCount() const; | 
|---|
| [5a9e34] | 113 |  | 
|---|
|  | 114 | /** Getter to molecule center contained in \a ObservedValues. | 
|---|
|  | 115 | * | 
|---|
|  | 116 | * \return molecule's center | 
|---|
|  | 117 | */ | 
|---|
| [3b9aa1] | 118 | const Vector& getMolCenter() const; | 
|---|
| [5a9e34] | 119 |  | 
|---|
| [494478] | 120 | /** Getter to molecule index contained in \a ObservedValues. | 
|---|
|  | 121 | * | 
|---|
|  | 122 | * \return molecule's index | 
|---|
|  | 123 | */ | 
|---|
| [3b9aa1] | 124 | const moleculeId_t& getMolIndex() const; | 
|---|
| [494478] | 125 |  | 
|---|
|  | 126 | /** Getter to molecule name contained in \a ObservedValues. | 
|---|
|  | 127 | * | 
|---|
|  | 128 | * \return molecule's name | 
|---|
|  | 129 | */ | 
|---|
| [3b9aa1] | 130 | const std::string& getMolName() const; | 
|---|
| [494478] | 131 |  | 
|---|
| [1b6415a] | 132 | /** Getter to molecule formula contained in \a ObservedValues. | 
|---|
|  | 133 | * | 
|---|
|  | 134 | * \return molecule's formula | 
|---|
|  | 135 | */ | 
|---|
| [3b9aa1] | 136 | const std::string& getMolFormula() const; | 
|---|
| [1b6415a] | 137 |  | 
|---|
| [5a9e34] | 138 | /** Getter to molecule non-hydrogen atom  count contained in \a ObservedValues. | 
|---|
|  | 139 | * | 
|---|
|  | 140 | * \return molecule's non-hydrogen atom count | 
|---|
|  | 141 | */ | 
|---|
| [3b9aa1] | 142 | const int& getNonHydrogenCount() const; | 
|---|
| [5a9e34] | 143 |  | 
|---|
| [494478] | 144 | /** Getter to molecule's bounding box contained in \a ObservedValues. | 
|---|
|  | 145 | * | 
|---|
|  | 146 | * \return molecule's bounding box | 
|---|
|  | 147 | */ | 
|---|
| [3b9aa1] | 148 | const molecule::BoundingBoxInfo& getBoundingBox() const; | 
|---|
| [494478] | 149 |  | 
|---|
| [9e9100] | 150 | /** Getter to molecule's selected status. | 
|---|
|  | 151 | * | 
|---|
|  | 152 | * \return true - molecule selected, false - else | 
|---|
|  | 153 | */ | 
|---|
|  | 154 | const bool& getMolSelected() const; | 
|---|
|  | 155 |  | 
|---|
| [494478] | 156 | static const molecule * const getMolecule(const moleculeId_t _id); | 
|---|
|  | 157 |  | 
|---|
| [0070aa] | 158 | signals: | 
|---|
| [1b6415a] | 159 | void atomcountChanged(); | 
|---|
| [5a9e34] | 160 | void bondcountChanged(); | 
|---|
| [1b6415a] | 161 | void formulaChanged(); | 
|---|
| [522c45] | 162 | void indexChanged(); | 
|---|
| [494478] | 163 | void nameChanged(); | 
|---|
| [5a9e34] | 164 | void nononhydrogenChanged(); | 
|---|
|  | 165 | void centerChanged(); | 
|---|
| [494478] | 166 | void tesselationhullChanged(); | 
|---|
|  | 167 | void boundingboxChanged(); | 
|---|
| [96e145] | 168 | void atomInserted(QtObservedAtom::ptr, QtObservedMolecule *); | 
|---|
| [f91ef6] | 169 | void atomRemoved(ObservedValue_Index_t, QtObservedMolecule *); | 
|---|
| [5a9e34] | 170 | void moleculeRemoved(); | 
|---|
| [9e9100] | 171 | void selectedChanged(); | 
|---|
| [494478] | 172 |  | 
|---|
|  | 173 | private: | 
|---|
|  | 174 |  | 
|---|
|  | 175 | void activateObserver(); | 
|---|
|  | 176 | void deactivateObserver(); | 
|---|
|  | 177 |  | 
|---|
|  | 178 | private: | 
|---|
| [f35f7e] | 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); | 
|---|
| [494478] | 188 |  | 
|---|
| [1b6415a] | 189 | //!> list of channels when atom count needs to update | 
|---|
|  | 190 | static const Observable::channels_t AtomCountChannels; | 
|---|
| [5a9e34] | 191 | //!> list of channels when bond count needs to update | 
|---|
|  | 192 | static const Observable::channels_t BondCountChannels; | 
|---|
| [494478] | 193 | //!> list of channels when bounding box needs to update | 
|---|
|  | 194 | static const Observable::channels_t BoundingBoxChannels; | 
|---|
| [1b6415a] | 195 | //!> list of channels when formula needs to update | 
|---|
|  | 196 | static const Observable::channels_t FormulaStringChannels; | 
|---|
| [5a9e34] | 197 | //!> list of channels when the center needs to update | 
|---|
|  | 198 | static const Observable::channels_t CenterChannels; | 
|---|
| [494478] | 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; | 
|---|
| [5a9e34] | 203 | //!> list of channels when the name needs to update | 
|---|
|  | 204 | static const Observable::channels_t NonHydrogenCountChannels; | 
|---|
| [9e9100] | 205 | //!> list of channels when the name needs to update | 
|---|
|  | 206 | static const Observable::channels_t SelectedChannels; | 
|---|
| [494478] | 207 |  | 
|---|
|  | 208 | private: | 
|---|
|  | 209 | /** Observed Values **/ | 
|---|
|  | 210 |  | 
|---|
|  | 211 | //!> enumeration of observed values to match with entries in ObservedValues | 
|---|
|  | 212 | enum ObservedTypes { | 
|---|
| [04c3a3] | 213 | //!> contains the current molecule index | 
|---|
|  | 214 | MolIndex, | 
|---|
| [1b6415a] | 215 | //!> contains the current molecule's atom count | 
|---|
|  | 216 | AtomCount, | 
|---|
| [5a9e34] | 217 | //!> contains the current molecule's number of bonds | 
|---|
|  | 218 | BondCount, | 
|---|
|  | 219 | //!> contains newest version of the bounding box on request | 
|---|
|  | 220 | BoundingBox, | 
|---|
| [1b6415a] | 221 | //!> contains the current molecule's formula | 
|---|
|  | 222 | FormulaString, | 
|---|
| [5a9e34] | 223 | //!> contains the current molecule's center | 
|---|
|  | 224 | MolCenter, | 
|---|
| [494478] | 225 | //!> contains the current molecule name | 
|---|
|  | 226 | MolName, | 
|---|
| [5a9e34] | 227 | //!> contains the current molecule's non-hydrogen atom count | 
|---|
|  | 228 | NonHydrogenCount, | 
|---|
| [9e9100] | 229 | //!> contains the current molecule's selection status | 
|---|
|  | 230 | MolSelected, | 
|---|
| [494478] | 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 | */ | 
|---|
| [62a127] | 242 | void initObservedValues( | 
|---|
| [494478] | 243 | ObservedValues_t &_ObservedValues, | 
|---|
|  | 244 | const moleculeId_t _molid, | 
|---|
|  | 245 | const molecule * const _molref, | 
|---|
| [d48a16] | 246 | const boost::function<void()> &_subjectKilled); | 
|---|
| [0070aa] | 247 |  | 
|---|
| [494478] | 248 | /** Destroys all \a ObservedValues entries. | 
|---|
|  | 249 | * | 
|---|
|  | 250 | * \param _ObservedValues vector of ObservedValue to be destroyed | 
|---|
|  | 251 | */ | 
|---|
| [c44763] | 252 | void destroyObservedValues( | 
|---|
| [494478] | 253 | std::vector<boost::any> &_ObservedValues); | 
|---|
| [0070aa] | 254 |  | 
|---|
| [04c3a3] | 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 |  | 
|---|
| [62a0ee] | 262 |  | 
|---|
|  | 263 | /** Counts the number of subject killed received from the observed values. | 
|---|
|  | 264 | * | 
|---|
|  | 265 | * \param _id id to check against ours | 
|---|
|  | 266 | */ | 
|---|
| [bf6245] | 267 | void countValuesSubjectKilled(ObservedValue_Index_t _id); | 
|---|
| [62a0ee] | 268 |  | 
|---|
| [494478] | 269 | //!> counts how many ObservedValues have already been subjectKilled() | 
|---|
|  | 270 | mutable size_t subjectKilledCount; | 
|---|
| [0070aa] | 271 |  | 
|---|
| [62a0ee] | 272 | /** Helper function to check that all subjectKilled have been received for both | 
|---|
|  | 273 | * this instance and all its internal observed values. | 
|---|
|  | 274 | * | 
|---|
| [bf6245] | 275 | * \param _id id to check against ours | 
|---|
| [62a0ee] | 276 | */ | 
|---|
| [bf6245] | 277 | void checkForRemoval(ObservedValue_Index_t _id); | 
|---|
| [62a0ee] | 278 |  | 
|---|
| [0070aa] | 279 | private: | 
|---|
| [494478] | 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 |  | 
|---|
| [bfd5d5f] | 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 |  | 
|---|
| [494478] | 312 | private: | 
|---|
| [2560f1] | 313 | //!> unique index among all observed instances | 
|---|
|  | 314 | const ObservedValue_Index_t index; | 
|---|
|  | 315 |  | 
|---|
| [494478] | 316 | //!> reference to InstanceBoard for callbacks on subjectKilled() | 
|---|
|  | 317 | QtObservedInstanceBoard & board; | 
|---|
|  | 318 |  | 
|---|
| [04c3a3] | 319 | //!> is board still alive or not, impacts callbacks | 
|---|
|  | 320 | bool BoardIsGone; | 
|---|
|  | 321 |  | 
|---|
| [98c35c] | 322 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard | 
|---|
| [494478] | 323 | ObservedValues_t ObservedValues; | 
|---|
| [0070aa] | 324 | }; | 
|---|
|  | 325 |  | 
|---|
|  | 326 |  | 
|---|
|  | 327 | #endif /* QTOBSERVEDMOLECULE_HPP_ */ | 
|---|