| 1 | /* | 
|---|
| 2 | * QtObservedBond.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Mar 03, 2016 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | #ifndef QTOBSERVEDBOND_HPP_ | 
|---|
| 10 | #define QTOBSERVEDBOND_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 "Bond/bond.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 atom; | 
|---|
| 34 | class molecule; | 
|---|
| 35 | class QtObservedInstanceBoard; | 
|---|
| 36 | class QtObservedMolecule; | 
|---|
| 37 |  | 
|---|
| 38 | /** This instance is the ObservedValue representation of a World's bond. | 
|---|
| 39 | * | 
|---|
| 40 | * Due to the signal/slot mechanism and its delays, lifetime of objects in the | 
|---|
| 41 | * World and their QtGui representation cannot directly be related (without | 
|---|
| 42 | * slowing down Actions just for having the representation up to speed). | 
|---|
| 43 | * Hence, the required information for displaying and representing these | 
|---|
| 44 | * objects must be contained in an extra instance. | 
|---|
| 45 | * | 
|---|
| 46 | * This is the instance for information about a particular bond. | 
|---|
| 47 | * | 
|---|
| 48 | * Essentially, this is the interface between molecuilder's World (and a | 
|---|
| 49 | * particular bond) and the QtGui part of the code. | 
|---|
| 50 | */ | 
|---|
| 51 | class QtObservedBond : public QWidget, public Observer | 
|---|
| 52 | { | 
|---|
| 53 | Q_OBJECT | 
|---|
| 54 |  | 
|---|
| 55 | public: | 
|---|
| 56 | typedef std::pair<atomId_t, atomId_t> bondId_t; | 
|---|
| 57 |  | 
|---|
| 58 | //!> typedef for instance wrapped in shared ptr | 
|---|
| 59 | typedef boost::shared_ptr<QtObservedBond> ptr; | 
|---|
| 60 |  | 
|---|
| 61 | //!> typedef for instance wrapped in weak shared ptr | 
|---|
| 62 | typedef boost::weak_ptr<QtObservedBond> weak_ptr; | 
|---|
| 63 |  | 
|---|
| 64 | private: | 
|---|
| 65 | //!> ObservedValuesContainer needs to access private cstor and dstor | 
|---|
| 66 | friend class ObservedValuesContainer<QtObservedBond, ObservedValue_Index_t>; | 
|---|
| 67 | //!> QtObservedInstanceBoard needs to access private cstor and dstor | 
|---|
| 68 | friend class QtObservedInstanceBoard; | 
|---|
| 69 |  | 
|---|
| 70 | /** Cstor of QtObservedBond. | 
|---|
| 71 | * | 
|---|
| 72 | * \param _id id of observed bond | 
|---|
| 73 | * \param _bond ref to observed bond | 
|---|
| 74 | * \param _leftatom bond's observed left atom for position, element, ... | 
|---|
| 75 | * \param _rightatom bond's observed right atom for position, element, ... | 
|---|
| 76 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled() | 
|---|
| 77 | * \param _parent Qt parent to automatically destroy when parent is destroyed | 
|---|
| 78 | */ | 
|---|
| 79 | QtObservedBond( | 
|---|
| 80 | const bondId_t _id, | 
|---|
| 81 | const bond::ptr _bond, | 
|---|
| 82 | const QtObservedAtom::ptr &_leftatom, | 
|---|
| 83 | const QtObservedAtom::ptr &_rightatom, | 
|---|
| 84 | QtObservedInstanceBoard &_board, | 
|---|
| 85 | QWidget * _parent=0); | 
|---|
| 86 |  | 
|---|
| 87 | public: | 
|---|
| 88 |  | 
|---|
| 89 | /** Dstor of QtObservedBond. | 
|---|
| 90 | * | 
|---|
| 91 | */ | 
|---|
| 92 | ~QtObservedBond(); | 
|---|
| 93 |  | 
|---|
| 94 | // Observer functions | 
|---|
| 95 | void update(Observable *publisher); | 
|---|
| 96 | void subjectKilled(Observable *publisher); | 
|---|
| 97 | void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
| 98 |  | 
|---|
| 99 | /** Getter for a permanent and unique index of this instance. | 
|---|
| 100 | * | 
|---|
| 101 | * \note ALWAYS use this index if you need to store and identifier to this | 
|---|
| 102 | * instance which you might need to retrieve at some later date. | 
|---|
| 103 | * | 
|---|
| 104 | * \warning DO NOT STORE the QtObserved...:ptr directly. This will possibly | 
|---|
| 105 | * prevent their eventual destruction. Only store the ObservedValue_Index_t | 
|---|
| 106 | * as means to obtain the ptr from the QtObservedInstanceBoard. | 
|---|
| 107 | * | 
|---|
| 108 | * \return returns a unique and permanent index that can be used to retrieve this | 
|---|
| 109 | * instance from the QtObservedInstanceBoard as it must not be stored. | 
|---|
| 110 | */ | 
|---|
| 111 | ObservedValue_Index_t getIndex() const; | 
|---|
| 112 |  | 
|---|
| 113 | /** Getter to bond's index pair contained in \a ObservedValues. | 
|---|
| 114 | * | 
|---|
| 115 | * \return bond's index pair | 
|---|
| 116 | */ | 
|---|
| 117 | const bondId_t getBondIndex() const; | 
|---|
| 118 |  | 
|---|
| 119 | /** Getter to bond's degree contained in \a ObservedValues. | 
|---|
| 120 | * | 
|---|
| 121 | * \return bond's degree | 
|---|
| 122 | */ | 
|---|
| 123 | const int& getBondDegree() const; | 
|---|
| 124 |  | 
|---|
| 125 | /** Getter to the observed state of the left atom of this bond. | 
|---|
| 126 | * | 
|---|
| 127 | * \return const ref to left atom's observed state. | 
|---|
| 128 | */ | 
|---|
| 129 | const QtObservedAtom::ptr getLeftAtom() const | 
|---|
| 130 | { return leftatom; } | 
|---|
| 131 |  | 
|---|
| 132 | /** Getter to the observed state of the right atom of this bond. | 
|---|
| 133 | * | 
|---|
| 134 | * \return const ref to right atom's observed state. | 
|---|
| 135 | */ | 
|---|
| 136 | const QtObservedAtom::ptr getRightAtom() const | 
|---|
| 137 | { return rightatom; } | 
|---|
| 138 |  | 
|---|
| 139 | /** Getter to bond's left atom index contained in \a ObservedValues. | 
|---|
| 140 | * | 
|---|
| 141 | * \return bond's left atom index | 
|---|
| 142 | */ | 
|---|
| 143 | const atomId_t& getLeftAtomIndex() const; | 
|---|
| 144 |  | 
|---|
| 145 | /** Getter to bond's left atom element contained in \a ObservedValues. | 
|---|
| 146 | * | 
|---|
| 147 | * \return bond's left atom element | 
|---|
| 148 | */ | 
|---|
| 149 | const atomicNumber_t& getLeftAtomElement() const; | 
|---|
| 150 |  | 
|---|
| 151 | /** Getter to left atoms's molecule index contained in \a ObservedValues. | 
|---|
| 152 | * | 
|---|
| 153 | * \return left atom's molecule index | 
|---|
| 154 | */ | 
|---|
| 155 | const moleculeId_t getLeftMoleculeIndex() const; | 
|---|
| 156 |  | 
|---|
| 157 | /** Getter to bond's right atom position contained in \a ObservedValues. | 
|---|
| 158 | * | 
|---|
| 159 | * \return bond's left atom position | 
|---|
| 160 | */ | 
|---|
| 161 | const Vector& getLeftAtomPosition() const; | 
|---|
| 162 |  | 
|---|
| 163 | /** Getter to bond's right atom index contained in \a ObservedValues. | 
|---|
| 164 | * | 
|---|
| 165 | * \return bond's right atom index | 
|---|
| 166 | */ | 
|---|
| 167 | const atomId_t& getRightAtomIndex() const; | 
|---|
| 168 |  | 
|---|
| 169 | /** Getter to bond's right atom element contained in \a ObservedValues. | 
|---|
| 170 | * | 
|---|
| 171 | * \return bond's right atom element | 
|---|
| 172 | */ | 
|---|
| 173 | const atomicNumber_t& getRightAtomElement() const; | 
|---|
| 174 |  | 
|---|
| 175 | /** Getter to bond's right atom position contained in \a ObservedValues. | 
|---|
| 176 | * | 
|---|
| 177 | * \return bond's right atom position | 
|---|
| 178 | */ | 
|---|
| 179 | const Vector& getRightAtomPosition() const; | 
|---|
| 180 |  | 
|---|
| 181 | /** Getter to right atom's molecule index contained in \a ObservedValues. | 
|---|
| 182 | * | 
|---|
| 183 | * \return right atom's molecule index | 
|---|
| 184 | */ | 
|---|
| 185 | const moleculeId_t getRightMoleculeIndex() const; | 
|---|
| 186 |  | 
|---|
| 187 | //!> typedef for internal observable counter maps | 
|---|
| 188 | typedef std::map<Observable * const, unsigned int> ObservableCount_t; | 
|---|
| 189 |  | 
|---|
| 190 | signals: | 
|---|
| 191 |  | 
|---|
| 192 | void indexChanged(); | 
|---|
| 193 | void degreeChanged(); | 
|---|
| 194 | void leftAtomIndexChanged(); | 
|---|
| 195 | void leftAtomElementChanged(); | 
|---|
| 196 | void leftAtomPositionChanged(); | 
|---|
| 197 | void leftmoleculeChanged(); | 
|---|
| 198 | void rightAtomIndexChanged(); | 
|---|
| 199 | void rightAtomElementChanged(); | 
|---|
| 200 | void rightAtomPositionChanged(); | 
|---|
| 201 | void rightmoleculeChanged(); | 
|---|
| 202 | void bondRemoved(); | 
|---|
| 203 |  | 
|---|
| 204 | //private slots: | 
|---|
| 205 |  | 
|---|
| 206 | private: | 
|---|
| 207 | void activateObserver(); | 
|---|
| 208 | void deactivateObserver(); | 
|---|
| 209 |  | 
|---|
| 210 | static const atom * const getAtomConst(const atomId_t _id); | 
|---|
| 211 | static atom * const getAtom(const atomId_t _id); | 
|---|
| 212 |  | 
|---|
| 213 | private: | 
|---|
| 214 | static int updateDegree(const bond &_bond); | 
|---|
| 215 |  | 
|---|
| 216 | //!> enumeration of observed values to match with entries in ObservedValues | 
|---|
| 217 | enum ObservedTypes { | 
|---|
| 218 | //!> contains the current bond degree | 
|---|
| 219 | BondDegree, | 
|---|
| 220 | //!> gives the size of the enumeration | 
|---|
| 221 | MAX_ObservedTypes | 
|---|
| 222 | }; | 
|---|
| 223 |  | 
|---|
| 224 | /** Initializes all \a ObservedValues entries. | 
|---|
| 225 | * | 
|---|
| 226 | * \param _ObservedValues vector of ObservedValue to be filled | 
|---|
| 227 | * \param _id bond id | 
|---|
| 228 | * \param _bondref reference to bond | 
|---|
| 229 | * \param _bondsubjectKilled ref to function to call on subjectKilled() from bond | 
|---|
| 230 | */ | 
|---|
| 231 | void initObservedValues( | 
|---|
| 232 | ObservedValues_t &_ObservedValues, | 
|---|
| 233 | const bondId_t _id, | 
|---|
| 234 | const bond::ptr _bondref, | 
|---|
| 235 | const boost::function<void()> &_bondsubjectKilled); | 
|---|
| 236 |  | 
|---|
| 237 | /** Destroys all \a ObservedValues entries. | 
|---|
| 238 | * | 
|---|
| 239 | * \param _ObservedValues vector of ObservedValue to be destroyed | 
|---|
| 240 | */ | 
|---|
| 241 | static void destroyObservedValues( | 
|---|
| 242 | std::vector<boost::any> &_ObservedValues); | 
|---|
| 243 |  | 
|---|
| 244 | /** Function is called by InstanceBoard to inform about its destruction. | 
|---|
| 245 | * | 
|---|
| 246 | * \note callbacks must not be used after this | 
|---|
| 247 | */ | 
|---|
| 248 | void noteBoardIsGone() | 
|---|
| 249 | { BoardIsGone = true; } | 
|---|
| 250 |  | 
|---|
| 251 | /** Counts the number of subject killed received from the observed values. | 
|---|
| 252 | * | 
|---|
| 253 | * \param _id id to check against ours | 
|---|
| 254 | * \param _counter counter to decrease | 
|---|
| 255 | */ | 
|---|
| 256 | void countValuesSubjectKilled(ObservedValue_Index_t _id, unsigned int &_counter); | 
|---|
| 257 |  | 
|---|
| 258 | //!> counts how many ObservedValues have already been subjectKilled() for a given observable | 
|---|
| 259 | mutable ObservableCount_t subjectKilledCount; | 
|---|
| 260 |  | 
|---|
| 261 | /** Helper function to check that all subjectKilled have been received for both | 
|---|
| 262 | * this instance and all its internal observed values. | 
|---|
| 263 | * | 
|---|
| 264 | * \param _id id to check against ours | 
|---|
| 265 | */ | 
|---|
| 266 | void checkForRemoval(ObservedValue_Index_t _id); | 
|---|
| 267 |  | 
|---|
| 268 | private: | 
|---|
| 269 |  | 
|---|
| 270 | //!> list of channels when index needs to update | 
|---|
| 271 | static const Observable::channels_t BondDegreeChannels; | 
|---|
| 272 |  | 
|---|
| 273 | //!> we get multiple subjectKilled(), count and call callback() only on last | 
|---|
| 274 | const ObservableCount_t AllsignedOnChannels; | 
|---|
| 275 | ObservableCount_t signedOffChannels; | 
|---|
| 276 |  | 
|---|
| 277 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL) | 
|---|
| 278 | const Observable *bondowner; | 
|---|
| 279 |  | 
|---|
| 280 | private: | 
|---|
| 281 |  | 
|---|
| 282 | /** Internal setter for the weak shared_ptr instance that we sometimes | 
|---|
| 283 | * need to convert the ref to this instance into an shared ptr instance that | 
|---|
| 284 | * is safe to hand around. | 
|---|
| 285 | * | 
|---|
| 286 | * \param _selfref ref to shared ptr instance that is internally stored | 
|---|
| 287 | */ | 
|---|
| 288 | void setSelfRef(const weak_ptr &_selfref) | 
|---|
| 289 | { const_cast<weak_ptr &>(selfref) = _selfref; } | 
|---|
| 290 |  | 
|---|
| 291 | //!> reference to this instance wrapped in a shared ptr for handing around | 
|---|
| 292 | const weak_ptr selfref; | 
|---|
| 293 |  | 
|---|
| 294 | public: | 
|---|
| 295 |  | 
|---|
| 296 | /** Getter for this instance safely wrapped in a shared ptr instance for | 
|---|
| 297 | * handing arount. | 
|---|
| 298 | * | 
|---|
| 299 | * \return shared ptr of this instance | 
|---|
| 300 | */ | 
|---|
| 301 | ptr getRef() const | 
|---|
| 302 | { return ptr(selfref); } | 
|---|
| 303 |  | 
|---|
| 304 | private: | 
|---|
| 305 | //!> contains still the old id after the index of the bond changed | 
|---|
| 306 | bondId_t oldbondId; | 
|---|
| 307 |  | 
|---|
| 308 | //!> observed left atom of bond for position, element, ... | 
|---|
| 309 | const QtObservedAtom::ptr leftatom; | 
|---|
| 310 |  | 
|---|
| 311 | //!> observed right atom of bond for position, element, ... | 
|---|
| 312 | const QtObservedAtom::ptr rightatom; | 
|---|
| 313 |  | 
|---|
| 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 /* QTOBSERVEDBOND_HPP_ */ | 
|---|