[0070aa] | 1 | /*
|
---|
| 2 | * QtObservedAtom.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Oct 28, 2015
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | #ifndef QTOBSERVEDATOM_HPP_
|
---|
| 10 | #define QTOBSERVEDATOM_HPP_
|
---|
| 11 |
|
---|
| 12 | // include config.h
|
---|
| 13 | #ifdef HAVE_CONFIG_H
|
---|
| 14 | #include <config.h>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | #include <QtGui/QWidget>
|
---|
| 18 |
|
---|
[65c323] | 19 | #include <boost/function.hpp>
|
---|
[98c35c] | 20 | #include <boost/shared_ptr.hpp>
|
---|
[0070aa] | 21 |
|
---|
[65c323] | 22 | #include "CodePatterns/Observer/Observable.hpp"
|
---|
[0070aa] | 23 | #include "CodePatterns/Observer/Observer.hpp"
|
---|
| 24 |
|
---|
[65c323] | 25 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 26 |
|
---|
[98c35c] | 27 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp"
|
---|
[41e287] | 28 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp"
|
---|
[0070aa] | 29 | #include "types.hpp"
|
---|
| 30 |
|
---|
[65c323] | 31 | class atom;
|
---|
[98c35c] | 32 | class QtObservedInstanceBoard;
|
---|
[1187c5] | 33 | class QtObservedMolecule;
|
---|
[98c35c] | 34 |
|
---|
[0070aa] | 35 | /** This instance is the ObservedValue representation of a World's atom.
|
---|
| 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 atom.
|
---|
[65c323] | 44 | *
|
---|
| 45 | * Essentially, this is the interface between molecuilder's World (and a
|
---|
| 46 | * particular atom) and the QtGui part of the code.
|
---|
[0070aa] | 47 | */
|
---|
| 48 | class QtObservedAtom : public QWidget, public Observer
|
---|
| 49 | {
|
---|
| 50 | Q_OBJECT
|
---|
| 51 |
|
---|
[65c323] | 52 | public:
|
---|
| 53 |
|
---|
| 54 | //!> typedef for instance wrapped in shared ptr
|
---|
| 55 | typedef boost::shared_ptr<QtObservedAtom> ptr;
|
---|
| 56 |
|
---|
| 57 | private:
|
---|
[41e287] | 58 | //!> ObservedValuesContainer needs to access private cstor and dstor
|
---|
| 59 | friend class ObservedValuesContainer<QtObservedAtom, atomId_t>;
|
---|
[98c35c] | 60 | //!> QtObservedInstanceBoard needs to access private cstor and dstor
|
---|
| 61 | friend class QtObservedInstanceBoard;
|
---|
| 62 |
|
---|
[0070aa] | 63 | /** Cstor of QtObservedAtom.
|
---|
| 64 | *
|
---|
[62a0ee] | 65 | * \param _id id of observed atom
|
---|
| 66 | * \param _atom ref to observed atom
|
---|
[65c323] | 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 | QtObservedAtom(
|
---|
[62a0ee] | 71 | const atomId_t _id,
|
---|
| 72 | const atom * const _atom,
|
---|
[65c323] | 73 | QtObservedInstanceBoard &_board,
|
---|
[98c35c] | 74 | QWidget * _parent=0);
|
---|
| 75 |
|
---|
| 76 | public:
|
---|
[0070aa] | 77 |
|
---|
| 78 | /** Dstor of QtObservedAtom.
|
---|
| 79 | *
|
---|
| 80 | */
|
---|
| 81 | ~QtObservedAtom();
|
---|
| 82 |
|
---|
[98c35c] | 83 | // Observer functions
|
---|
| 84 | void update(Observable *publisher);
|
---|
| 85 | void subjectKilled(Observable *publisher);
|
---|
| 86 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
---|
| 87 |
|
---|
[65c323] | 88 | /** Getter to atom index contained in \a ObservedValues.
|
---|
| 89 | *
|
---|
| 90 | * \return atom's index
|
---|
| 91 | */
|
---|
[3b9aa1] | 92 | const atomId_t& getAtomIndex() const;
|
---|
[65c323] | 93 |
|
---|
[fe493f] | 94 | //!> typedef for list of bonds, defined by pairs of atom ids
|
---|
| 95 | typedef std::vector< std::pair<atomId_t, atomId_t> > ListOfBonds_t;
|
---|
| 96 |
|
---|
| 97 | /** Getter to atom bonds contained in \a ObservedValues.
|
---|
[65c323] | 98 | *
|
---|
[fe493f] | 99 | * \return atom's bonds
|
---|
[65c323] | 100 | */
|
---|
[3b9aa1] | 101 | const ListOfBonds_t& getAtomBonds() const;
|
---|
[65c323] | 102 |
|
---|
| 103 | /** Getter to atom element contained in \a ObservedValues.
|
---|
| 104 | *
|
---|
[fe493f] | 105 | * \return atom's element
|
---|
[65c323] | 106 | */
|
---|
[3b9aa1] | 107 | const atomicNumber_t& getAtomElement() const;
|
---|
[65c323] | 108 |
|
---|
[fe493f] | 109 | /** Getter to atom name contained in \a ObservedValues.
|
---|
| 110 | *
|
---|
| 111 | * \return atom's name
|
---|
| 112 | */
|
---|
[3b9aa1] | 113 | const std::string& getAtomName() const;
|
---|
[65c323] | 114 |
|
---|
[fe493f] | 115 | /** Getter to atom position contained in \a ObservedValues.
|
---|
[65c323] | 116 | *
|
---|
[fe493f] | 117 | * \return atom's position
|
---|
[65c323] | 118 | */
|
---|
[3b9aa1] | 119 | const Vector& getAtomPosition() const;
|
---|
[fe493f] | 120 |
|
---|
| 121 | /** Getter to atom's molecule index contained in \a ObservedValues.
|
---|
| 122 | *
|
---|
| 123 | * \return atom's molecule index
|
---|
| 124 | */
|
---|
[1187c5] | 125 | QtObservedMolecule* const getAtomMolecule() const;
|
---|
[65c323] | 126 |
|
---|
[9e9100] | 127 | /** Getter to atom's selected status.
|
---|
| 128 | *
|
---|
| 129 | * \return true - atom selected, false - else
|
---|
| 130 | */
|
---|
| 131 | const bool getAtomSelected() const;
|
---|
| 132 |
|
---|
[0070aa] | 133 | signals:
|
---|
[1c0961] | 134 | void indexChanged(const atomId_t, const atomId_t);
|
---|
[65c323] | 135 | void bondsChanged();
|
---|
[fe493f] | 136 | void elementChanged();
|
---|
[1187c5] | 137 | void moleculeChanged();
|
---|
[fe493f] | 138 | void nameChanged();
|
---|
| 139 | void positionChanged();
|
---|
| 140 | void atomRemoved();
|
---|
[9e9100] | 141 | void selectedChanged();
|
---|
[65c323] | 142 |
|
---|
| 143 | //private slots:
|
---|
[0070aa] | 144 |
|
---|
[65c323] | 145 | private:
|
---|
| 146 | void activateObserver();
|
---|
| 147 | void deactivateObserver();
|
---|
[0070aa] | 148 |
|
---|
[65c323] | 149 | static const atom * const getAtomConst(const atomId_t _id);
|
---|
| 150 | static atom * const getAtom(const atomId_t _id);
|
---|
[0070aa] | 151 |
|
---|
| 152 | private:
|
---|
[65c323] | 153 | static atomId_t updateIndex();
|
---|
[fe493f] | 154 | static ListOfBonds_t updateBonds(
|
---|
[65c323] | 155 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
| 156 | static atomicNumber_t updateElement(
|
---|
| 157 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
[1187c5] | 158 | QtObservedMolecule* updateMoleculeIndex(
|
---|
[fe493f] | 159 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
| 160 | static std::string updateName(
|
---|
| 161 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
| 162 | static Vector updatePosition(
|
---|
[65c323] | 163 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
[9e9100] | 164 | static bool updateSelected(
|
---|
| 165 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
[65c323] | 166 |
|
---|
| 167 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
| 168 | enum ObservedTypes {
|
---|
| 169 | //!> contains the current atom index
|
---|
| 170 | AtomIndex,
|
---|
[fe493f] | 171 | //!> contains the current set of bonds atoms for the atom
|
---|
| 172 | AtomBonds,
|
---|
[65c323] | 173 | //!> contains the current atom element
|
---|
| 174 | AtomElement,
|
---|
[fe493f] | 175 | //!> contains the current atom's molecule index
|
---|
[1187c5] | 176 | AtomMolecule,
|
---|
[fe493f] | 177 | //!> contains the current atom position
|
---|
| 178 | AtomName,
|
---|
| 179 | //!> contains the current atom position
|
---|
| 180 | AtomPosition,
|
---|
[9e9100] | 181 | //!> contains the current atom's selection status
|
---|
| 182 | AtomSelected,
|
---|
[65c323] | 183 | //!> gives the size of the enumeration
|
---|
| 184 | MAX_ObservedTypes
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | /** Initializes all \a ObservedValues entries.
|
---|
| 188 | *
|
---|
| 189 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
| 190 | * \param _id atom id
|
---|
| 191 | * \param _atomref reference to atom
|
---|
| 192 | * \param _subjectKilled ref to function to call on subjectKilled()
|
---|
| 193 | */
|
---|
[62a0ee] | 194 | void initObservedValues(
|
---|
[65c323] | 195 | ObservedValues_t &_ObservedValues,
|
---|
| 196 | const atomId_t _id,
|
---|
| 197 | const atom * const _atomref,
|
---|
| 198 | const boost::function<void(const atomId_t)> &_subjectKilled);
|
---|
| 199 |
|
---|
| 200 | /** Destroys all \a ObservedValues entries.
|
---|
| 201 | *
|
---|
| 202 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
| 203 | */
|
---|
| 204 | static void destroyObservedValues(
|
---|
| 205 | std::vector<boost::any> &_ObservedValues);
|
---|
| 206 |
|
---|
[04c3a3] | 207 | /** Function is called by InstanceBoard to inform about its destruction.
|
---|
| 208 | *
|
---|
| 209 | * \note callbacks must not be used after this
|
---|
| 210 | */
|
---|
| 211 | void noteBoardIsGone()
|
---|
| 212 | { BoardIsGone = true; }
|
---|
| 213 |
|
---|
[62a0ee] | 214 | /** Counts the number of subject killed received from the observed values.
|
---|
| 215 | *
|
---|
| 216 | * \param _id id to check against ours
|
---|
| 217 | */
|
---|
| 218 | void countValuesSubjectKilled(const atomId_t _id);
|
---|
| 219 |
|
---|
[65c323] | 220 | //!> counts how many ObservedValues have already been subjectKilled()
|
---|
| 221 | mutable size_t subjectKilledCount;
|
---|
| 222 |
|
---|
[62a0ee] | 223 | /** Helper function to check that all subjectKilled have been received for both
|
---|
| 224 | * this instance and all its internal observed values.
|
---|
| 225 | *
|
---|
| 226 | */
|
---|
| 227 | void checkForRemoval();
|
---|
| 228 |
|
---|
[65c323] | 229 | private:
|
---|
| 230 |
|
---|
| 231 | //!> list of channels when index needs to update
|
---|
| 232 | static const Observable::channels_t AtomIndexChannels;
|
---|
| 233 | //!> list of channels when bonds needs to update
|
---|
| 234 | static const Observable::channels_t AtomBondsChannels;
|
---|
[fe493f] | 235 | //!> list of channels when element needs to update
|
---|
| 236 | static const Observable::channels_t AtomElementChannels;
|
---|
| 237 | //!> list of channels when molecule index needs to update
|
---|
[1187c5] | 238 | static const Observable::channels_t AtomMoleculeChannels;
|
---|
[fe493f] | 239 | //!> list of channels when name needs to update
|
---|
| 240 | static const Observable::channels_t AtomNameChannels;
|
---|
| 241 | //!> list of channels when position needs to update
|
---|
| 242 | static const Observable::channels_t AtomPositionChannels;
|
---|
[9e9100] | 243 | //!> list of channels when selection needs to update
|
---|
| 244 | static const Observable::channels_t AtomSelectedChannels;
|
---|
[65c323] | 245 |
|
---|
| 246 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
| 247 | const unsigned int AllsignedOnChannels;
|
---|
| 248 | unsigned int signedOffChannels;
|
---|
| 249 |
|
---|
| 250 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
| 251 | const Observable *owner;
|
---|
| 252 |
|
---|
| 253 | private:
|
---|
[1c0961] | 254 | //!> contains still the old id after the index of the atom changed
|
---|
| 255 | atomId_t oldId;
|
---|
[65c323] | 256 |
|
---|
| 257 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
| 258 | QtObservedInstanceBoard & board;
|
---|
| 259 |
|
---|
[04c3a3] | 260 | //!> is board still alive or not, impacts callbacks
|
---|
| 261 | bool BoardIsGone;
|
---|
| 262 |
|
---|
[98c35c] | 263 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
[65c323] | 264 | ObservedValues_t ObservedValues;
|
---|
[0070aa] | 265 | };
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 | #endif /* QTOBSERVEDATOM_HPP_ */
|
---|