[b4bd0e] | 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"
|
---|
[4112691] | 30 | #include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp"
|
---|
[b4bd0e] | 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
|
---|
[4112691] | 74 | * \param _leftatom bond's observed left atom for position, element, ...
|
---|
| 75 | * \param _rightatom bond's observed right atom for position, element, ...
|
---|
[b4bd0e] | 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,
|
---|
[4112691] | 82 | const QtObservedAtom::ptr &_leftatom,
|
---|
| 83 | const QtObservedAtom::ptr &_rightatom,
|
---|
[b4bd0e] | 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 bond's left atom index contained in \a ObservedValues.
|
---|
| 126 | *
|
---|
| 127 | * \return bond's left atom index
|
---|
| 128 | */
|
---|
| 129 | const atomId_t& getLeftAtomIndex() const;
|
---|
| 130 |
|
---|
| 131 | /** Getter to bond's left atom element contained in \a ObservedValues.
|
---|
| 132 | *
|
---|
| 133 | * \return bond's left atom element
|
---|
| 134 | */
|
---|
| 135 | const atomicNumber_t& getLeftAtomElement() const;
|
---|
| 136 |
|
---|
| 137 | /** Getter to bond's right atom position contained in \a ObservedValues.
|
---|
| 138 | *
|
---|
| 139 | * \return bond's left atom position
|
---|
| 140 | */
|
---|
| 141 | const Vector& getLeftAtomPosition() const;
|
---|
| 142 |
|
---|
| 143 | /** Getter to bond's right atom index contained in \a ObservedValues.
|
---|
| 144 | *
|
---|
| 145 | * \return bond's right atom index
|
---|
| 146 | */
|
---|
| 147 | const atomId_t& getRightAtomIndex() const;
|
---|
| 148 |
|
---|
| 149 | /** Getter to bond's right atom element contained in \a ObservedValues.
|
---|
| 150 | *
|
---|
| 151 | * \return bond's right atom element
|
---|
| 152 | */
|
---|
| 153 | const atomicNumber_t& getRightAtomElement() const;
|
---|
| 154 |
|
---|
| 155 | /** Getter to bond's right atom position contained in \a ObservedValues.
|
---|
| 156 | *
|
---|
| 157 | * \return bond's right atom position
|
---|
| 158 | */
|
---|
| 159 | const Vector& getRightAtomPosition() const;
|
---|
| 160 |
|
---|
| 161 | /** Getter to bond's molecule index contained in \a ObservedValues.
|
---|
| 162 | *
|
---|
| 163 | * \return bond's molecule index
|
---|
| 164 | */
|
---|
[785d89] | 165 | const moleculeId_t getMoleculeIndex() const;
|
---|
[b4bd0e] | 166 |
|
---|
| 167 | //!> typedef for internal observable counter maps
|
---|
| 168 | typedef std::map<Observable * const, unsigned int> ObservableCount_t;
|
---|
| 169 |
|
---|
| 170 | signals:
|
---|
| 171 |
|
---|
| 172 | void indexChanged(bondId_t, bondId_t);
|
---|
| 173 | void degreeChanged();
|
---|
| 174 | void leftAtomIndexChanged(atomId_t, atomId_t);
|
---|
| 175 | void leftAtomElementChanged();
|
---|
| 176 | void leftAtomPositionChanged();
|
---|
| 177 | void rightAtomIndexChanged(atomId_t, atomId_t);
|
---|
| 178 | void rightAtomElementChanged();
|
---|
| 179 | void rightAtomPositionChanged();
|
---|
| 180 | void moleculeIndexChanged(moleculeId_t, moleculeId_t);
|
---|
| 181 | void bondRemoved();
|
---|
| 182 |
|
---|
[4112691] | 183 | private slots:
|
---|
| 184 |
|
---|
| 185 | void leftAtomIndexChanges(atomId_t, atomId_t);
|
---|
| 186 | void rightAtomIndexChanges(atomId_t, atomId_t);
|
---|
| 187 | void moleculeIndexChanges(moleculeId_t, moleculeId_t);
|
---|
[b4bd0e] | 188 |
|
---|
| 189 | private:
|
---|
| 190 | void activateObserver();
|
---|
| 191 | void deactivateObserver();
|
---|
| 192 |
|
---|
| 193 | static const atom * const getAtomConst(const atomId_t _id);
|
---|
| 194 | static atom * const getAtom(const atomId_t _id);
|
---|
| 195 |
|
---|
| 196 | private:
|
---|
| 197 | static int updateDegree(const bond &_bond);
|
---|
| 198 |
|
---|
| 199 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
| 200 | enum ObservedTypes {
|
---|
| 201 | //!> contains the current bond degree
|
---|
| 202 | BondDegree,
|
---|
| 203 | //!> gives the size of the enumeration
|
---|
| 204 | MAX_ObservedTypes
|
---|
| 205 | };
|
---|
| 206 |
|
---|
| 207 | /** Initializes all \a ObservedValues entries.
|
---|
| 208 | *
|
---|
| 209 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
| 210 | * \param _id bond id
|
---|
| 211 | * \param _bondref reference to bond
|
---|
| 212 | * \param _bondsubjectKilled ref to function to call on subjectKilled() from bond
|
---|
| 213 | */
|
---|
| 214 | void initObservedValues(
|
---|
| 215 | ObservedValues_t &_ObservedValues,
|
---|
| 216 | const bondId_t _id,
|
---|
| 217 | const bond::ptr _bondref,
|
---|
[4112691] | 218 | const boost::function<void()> &_bondsubjectKilled);
|
---|
[b4bd0e] | 219 |
|
---|
| 220 | /** Destroys all \a ObservedValues entries.
|
---|
| 221 | *
|
---|
| 222 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
| 223 | */
|
---|
| 224 | static void destroyObservedValues(
|
---|
| 225 | std::vector<boost::any> &_ObservedValues);
|
---|
| 226 |
|
---|
| 227 | /** Function is called by InstanceBoard to inform about its destruction.
|
---|
| 228 | *
|
---|
| 229 | * \note callbacks must not be used after this
|
---|
| 230 | */
|
---|
| 231 | void noteBoardIsGone()
|
---|
| 232 | { BoardIsGone = true; }
|
---|
| 233 |
|
---|
| 234 | /** Counts the number of subject killed received from the observed values.
|
---|
| 235 | *
|
---|
| 236 | * \param _id id to check against ours
|
---|
| 237 | * \param _counter counter to decrease
|
---|
| 238 | */
|
---|
| 239 | void countValuesSubjectKilled(ObservedValue_Index_t _id, unsigned int &_counter);
|
---|
| 240 |
|
---|
| 241 | //!> counts how many ObservedValues have already been subjectKilled() for a given observable
|
---|
| 242 | mutable ObservableCount_t subjectKilledCount;
|
---|
| 243 |
|
---|
| 244 | /** Helper function to check that all subjectKilled have been received for both
|
---|
| 245 | * this instance and all its internal observed values.
|
---|
| 246 | *
|
---|
| 247 | * \param _id id to check against ours
|
---|
| 248 | */
|
---|
| 249 | void checkForRemoval(ObservedValue_Index_t _id);
|
---|
| 250 |
|
---|
| 251 | private:
|
---|
| 252 |
|
---|
| 253 | //!> list of channels when index needs to update
|
---|
| 254 | static const Observable::channels_t BondDegreeChannels;
|
---|
| 255 |
|
---|
| 256 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
| 257 | const ObservableCount_t AllsignedOnChannels;
|
---|
| 258 | ObservableCount_t signedOffChannels;
|
---|
| 259 |
|
---|
| 260 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
| 261 | const Observable *bondowner;
|
---|
| 262 |
|
---|
| 263 | private:
|
---|
| 264 |
|
---|
| 265 | /** Internal setter for the weak shared_ptr instance that we sometimes
|
---|
| 266 | * need to convert the ref to this instance into an shared ptr instance that
|
---|
| 267 | * is safe to hand around.
|
---|
| 268 | *
|
---|
| 269 | * \param _selfref ref to shared ptr instance that is internally stored
|
---|
| 270 | */
|
---|
| 271 | void setSelfRef(const weak_ptr &_selfref)
|
---|
| 272 | { const_cast<weak_ptr &>(selfref) = _selfref; }
|
---|
| 273 |
|
---|
| 274 | //!> reference to this instance wrapped in a shared ptr for handing around
|
---|
| 275 | const weak_ptr selfref;
|
---|
| 276 |
|
---|
| 277 | public:
|
---|
| 278 |
|
---|
| 279 | /** Getter for this instance safely wrapped in a shared ptr instance for
|
---|
| 280 | * handing arount.
|
---|
| 281 | *
|
---|
| 282 | * \return shared ptr of this instance
|
---|
| 283 | */
|
---|
| 284 | ptr getRef() const
|
---|
| 285 | { return ptr(selfref); }
|
---|
| 286 |
|
---|
| 287 | private:
|
---|
| 288 | //!> contains still the old id after the index of the bond changed
|
---|
| 289 | bondId_t oldbondId;
|
---|
| 290 |
|
---|
[4112691] | 291 | //!> observed left atom of bond for position, element, ...
|
---|
| 292 | const QtObservedAtom::ptr leftatom;
|
---|
| 293 |
|
---|
| 294 | //!> observed right atom of bond for position, element, ...
|
---|
| 295 | const QtObservedAtom::ptr rightatom;
|
---|
| 296 |
|
---|
| 297 | //!> contains still the old leftatom id after the index of the bond changed
|
---|
[b4bd0e] | 298 | atomId_t oldleftatomId;
|
---|
| 299 |
|
---|
[4112691] | 300 | //!> contains still the old rightatom id after the index of the bond changed
|
---|
[b4bd0e] | 301 | atomId_t oldrightatomId;
|
---|
| 302 |
|
---|
[4112691] | 303 | //!> contains still the old molecule id after the index of the bond changed
|
---|
[b4bd0e] | 304 | moleculeId_t oldmoleculeId;
|
---|
| 305 |
|
---|
| 306 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
| 307 | QtObservedInstanceBoard & board;
|
---|
| 308 |
|
---|
| 309 | //!> is board still alive or not, impacts callbacks
|
---|
| 310 | bool BoardIsGone;
|
---|
| 311 |
|
---|
| 312 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
| 313 | ObservedValues_t ObservedValues;
|
---|
| 314 | };
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 | #endif /* QTOBSERVEDBOND_HPP_ */
|
---|