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