| 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 "types.hpp"
|
|---|
| 31 |
|
|---|
| 32 | class QtObservedInstanceBoard;
|
|---|
| 33 |
|
|---|
| 34 | /** This instance is the ObservedValue representation of a World's molecule.
|
|---|
| 35 | *
|
|---|
| 36 | * Due to the signal/slot mechanism and its delays, lifetime of objects in the
|
|---|
| 37 | * World and their QtGui representation cannot directly be related (without
|
|---|
| 38 | * slowing down Actions just for having the representation up to speed).
|
|---|
| 39 | * Hence, the required information for displaying and representing these
|
|---|
| 40 | * objects must be contained in an extra instance.
|
|---|
| 41 | *
|
|---|
| 42 | * This is the instance for information about a particular molecule.
|
|---|
| 43 | */
|
|---|
| 44 | class QtObservedMolecule : public QWidget, public Observer
|
|---|
| 45 | {
|
|---|
| 46 | Q_OBJECT
|
|---|
| 47 |
|
|---|
| 48 | public:
|
|---|
| 49 |
|
|---|
| 50 | //!> typedef for instance wrapped in shared ptr
|
|---|
| 51 | typedef boost::shared_ptr<QtObservedMolecule> ptr;
|
|---|
| 52 |
|
|---|
| 53 | private:
|
|---|
| 54 | //!> ObservedValuesContainer needs to access private cstor and dstor
|
|---|
| 55 | friend class ObservedValuesContainer<QtObservedMolecule, moleculeId_t>;
|
|---|
| 56 | //!> QtObservedInstanceBoard needs to access private cstor and dstor
|
|---|
| 57 | friend class QtObservedInstanceBoard;
|
|---|
| 58 |
|
|---|
| 59 | /** Cstor of QtObservedMolecule.
|
|---|
| 60 | *
|
|---|
| 61 | * \param _ObservedValues ref to set of observed values for this instance
|
|---|
| 62 | * \param _board ref to InstanceBoard for callbacks on occasion of subjectKilled()
|
|---|
| 63 | * \param _parent Qt parent to automatically destroy when parent is destroyed
|
|---|
| 64 | */
|
|---|
| 65 | QtObservedMolecule(
|
|---|
| 66 | const ObservedValues_t &_ObservedValues,
|
|---|
| 67 | QtObservedInstanceBoard &_board,
|
|---|
| 68 | QWidget * _parent=0);
|
|---|
| 69 |
|
|---|
| 70 | public:
|
|---|
| 71 |
|
|---|
| 72 | /** Dstor of QtObservedMolecule.
|
|---|
| 73 | *
|
|---|
| 74 | */
|
|---|
| 75 | ~QtObservedMolecule();
|
|---|
| 76 |
|
|---|
| 77 | // Observer functions
|
|---|
| 78 | void update(Observable *publisher);
|
|---|
| 79 | void subjectKilled(Observable *publisher);
|
|---|
| 80 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
|---|
| 81 |
|
|---|
| 82 | /** Getter to molecule atom count contained in \a ObservedValues.
|
|---|
| 83 | *
|
|---|
| 84 | * \return molecule's atom count
|
|---|
| 85 | */
|
|---|
| 86 | const int& getAtomCount() const;
|
|---|
| 87 |
|
|---|
| 88 | /** Getter to molecule bond count contained in \a ObservedValues.
|
|---|
| 89 | *
|
|---|
| 90 | * \return molecule's bond count
|
|---|
| 91 | */
|
|---|
| 92 | const int& getBondCount() const;
|
|---|
| 93 |
|
|---|
| 94 | /** Getter to molecule center contained in \a ObservedValues.
|
|---|
| 95 | *
|
|---|
| 96 | * \return molecule's center
|
|---|
| 97 | */
|
|---|
| 98 | const Vector& getMolCenter() const;
|
|---|
| 99 |
|
|---|
| 100 | /** Getter to molecule index contained in \a ObservedValues.
|
|---|
| 101 | *
|
|---|
| 102 | * \return molecule's index
|
|---|
| 103 | */
|
|---|
| 104 | const moleculeId_t& getMolIndex() const;
|
|---|
| 105 |
|
|---|
| 106 | /** Getter to molecule name contained in \a ObservedValues.
|
|---|
| 107 | *
|
|---|
| 108 | * \return molecule's name
|
|---|
| 109 | */
|
|---|
| 110 | const std::string& getMolName() const;
|
|---|
| 111 |
|
|---|
| 112 | /** Getter to molecule formula contained in \a ObservedValues.
|
|---|
| 113 | *
|
|---|
| 114 | * \return molecule's formula
|
|---|
| 115 | */
|
|---|
| 116 | const std::string& getMolFormula() const;
|
|---|
| 117 |
|
|---|
| 118 | /** Getter to molecule non-hydrogen atom count contained in \a ObservedValues.
|
|---|
| 119 | *
|
|---|
| 120 | * \return molecule's non-hydrogen atom count
|
|---|
| 121 | */
|
|---|
| 122 | const int& getNonHydrogenCount() const;
|
|---|
| 123 |
|
|---|
| 124 | /** Getter to molecule's bounding box contained in \a ObservedValues.
|
|---|
| 125 | *
|
|---|
| 126 | * \return molecule's bounding box
|
|---|
| 127 | */
|
|---|
| 128 | const molecule::BoundingBoxInfo& getBoundingBox() const;
|
|---|
| 129 |
|
|---|
| 130 | static const molecule * const getMolecule(const moleculeId_t _id);
|
|---|
| 131 |
|
|---|
| 132 | signals:
|
|---|
| 133 | void atomcountChanged();
|
|---|
| 134 | void bondcountChanged();
|
|---|
| 135 | void formulaChanged();
|
|---|
| 136 | void indexChanged();
|
|---|
| 137 | void nameChanged();
|
|---|
| 138 | void nononhydrogenChanged();
|
|---|
| 139 | void centerChanged();
|
|---|
| 140 | void tesselationhullChanged();
|
|---|
| 141 | void boundingboxChanged();
|
|---|
| 142 | void atomInserted(const atomId_t);
|
|---|
| 143 | void atomRemoved(const atomId_t);
|
|---|
| 144 | void moleculeRemoved();
|
|---|
| 145 |
|
|---|
| 146 | private:
|
|---|
| 147 |
|
|---|
| 148 | void activateObserver();
|
|---|
| 149 | void deactivateObserver();
|
|---|
| 150 |
|
|---|
| 151 | private:
|
|---|
| 152 | static int updateAtomCount(
|
|---|
| 153 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 154 | static int updateBondCount(
|
|---|
| 155 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 156 | static molecule::BoundingBoxInfo updateBoundingBox(
|
|---|
| 157 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 158 | static std::string updateFormulaString(
|
|---|
| 159 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 160 | static Vector updateCenter(
|
|---|
| 161 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 162 | static moleculeId_t updateIndex();
|
|---|
| 163 | static std::string updateName(
|
|---|
| 164 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 165 | static int updateNonHydrogenCount(
|
|---|
| 166 | const boost::function<const moleculeId_t ()> &_getMolIndex);
|
|---|
| 167 |
|
|---|
| 168 | //!> list of channels when atom count needs to update
|
|---|
| 169 | static const Observable::channels_t AtomCountChannels;
|
|---|
| 170 | //!> list of channels when bond count needs to update
|
|---|
| 171 | static const Observable::channels_t BondCountChannels;
|
|---|
| 172 | //!> list of channels when bounding box needs to update
|
|---|
| 173 | static const Observable::channels_t BoundingBoxChannels;
|
|---|
| 174 | //!> list of channels when formula needs to update
|
|---|
| 175 | static const Observable::channels_t FormulaStringChannels;
|
|---|
| 176 | //!> list of channels when the center needs to update
|
|---|
| 177 | static const Observable::channels_t CenterChannels;
|
|---|
| 178 | //!> list of channels when the index needs to update
|
|---|
| 179 | static const Observable::channels_t IndexChannels;
|
|---|
| 180 | //!> list of channels when the name needs to update
|
|---|
| 181 | static const Observable::channels_t NameChannels;
|
|---|
| 182 | //!> list of channels when the name needs to update
|
|---|
| 183 | static const Observable::channels_t NonHydrogenCountChannels;
|
|---|
| 184 |
|
|---|
| 185 | private:
|
|---|
| 186 | /** Observed Values **/
|
|---|
| 187 |
|
|---|
| 188 | //!> enumeration of observed values to match with entries in ObservedValues
|
|---|
| 189 | enum ObservedTypes {
|
|---|
| 190 | //!> contains the current molecule index
|
|---|
| 191 | MolIndex,
|
|---|
| 192 | //!> contains the current molecule's atom count
|
|---|
| 193 | AtomCount,
|
|---|
| 194 | //!> contains the current molecule's number of bonds
|
|---|
| 195 | BondCount,
|
|---|
| 196 | //!> contains newest version of the bounding box on request
|
|---|
| 197 | BoundingBox,
|
|---|
| 198 | //!> contains the current molecule's formula
|
|---|
| 199 | FormulaString,
|
|---|
| 200 | //!> contains the current molecule's center
|
|---|
| 201 | MolCenter,
|
|---|
| 202 | //!> contains the current molecule name
|
|---|
| 203 | MolName,
|
|---|
| 204 | //!> contains the current molecule's non-hydrogen atom count
|
|---|
| 205 | NonHydrogenCount,
|
|---|
| 206 | //!> gives the size of the enumeration
|
|---|
| 207 | MAX_ObservedTypes
|
|---|
| 208 | };
|
|---|
| 209 |
|
|---|
| 210 | /** Initializes all \a _ObservedValues entries.
|
|---|
| 211 | *
|
|---|
| 212 | * \param _ObservedValues vector of ObservedValue to be filled
|
|---|
| 213 | * \param _moid molecule id
|
|---|
| 214 | * \param _molref reference to molecule
|
|---|
| 215 | * \param _subjectKilled ref to function to call on subjectKilled()
|
|---|
| 216 | */
|
|---|
| 217 | static void initObservedValues(
|
|---|
| 218 | ObservedValues_t &_ObservedValues,
|
|---|
| 219 | const moleculeId_t _molid,
|
|---|
| 220 | const molecule * const _molref,
|
|---|
| 221 | const boost::function<void(const moleculeId_t)> &_subjectKilled);
|
|---|
| 222 |
|
|---|
| 223 | /** Destroys all \a ObservedValues entries.
|
|---|
| 224 | *
|
|---|
| 225 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
|---|
| 226 | */
|
|---|
| 227 | static void destroyObservedValues(
|
|---|
| 228 | std::vector<boost::any> &_ObservedValues);
|
|---|
| 229 |
|
|---|
| 230 | /** Function is called by InstanceBoard to inform about its destruction.
|
|---|
| 231 | *
|
|---|
| 232 | * \note callbacks must not be used after this
|
|---|
| 233 | */
|
|---|
| 234 | void noteBoardIsGone()
|
|---|
| 235 | { BoardIsGone = true; }
|
|---|
| 236 |
|
|---|
| 237 | //!> counts how many ObservedValues have already been subjectKilled()
|
|---|
| 238 | mutable size_t subjectKilledCount;
|
|---|
| 239 |
|
|---|
| 240 | private:
|
|---|
| 241 |
|
|---|
| 242 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
|---|
| 243 | const unsigned int AllsignedOnChannels;
|
|---|
| 244 | unsigned int signedOffChannels;
|
|---|
| 245 |
|
|---|
| 246 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
|---|
| 247 | const Observable *owner;
|
|---|
| 248 |
|
|---|
| 249 | private:
|
|---|
| 250 |
|
|---|
| 251 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
|---|
| 252 | QtObservedInstanceBoard & board;
|
|---|
| 253 |
|
|---|
| 254 | //!> is board still alive or not, impacts callbacks
|
|---|
| 255 | bool BoardIsGone;
|
|---|
| 256 |
|
|---|
| 257 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
|---|
| 258 | ObservedValues_t ObservedValues;
|
|---|
| 259 | };
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 | #endif /* QTOBSERVEDMOLECULE_HPP_ */
|
|---|