[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 | */
|
---|
| 89 | atomId_t getAtomIndex() const;
|
---|
| 90 |
|
---|
| 91 | /** Getter to atom position contained in \a ObservedValues.
|
---|
| 92 | *
|
---|
| 93 | * \return atom's position
|
---|
| 94 | */
|
---|
| 95 | Vector getAtomPosition() const;
|
---|
| 96 |
|
---|
| 97 | /** Getter to atom element contained in \a ObservedValues.
|
---|
| 98 | *
|
---|
| 99 | * \return atom's elemnt
|
---|
| 100 | */
|
---|
| 101 | atomicNumber_t getAtomElement() const;
|
---|
| 102 |
|
---|
| 103 | //!> typedef for list of bonds, defined by pairs of atom ids
|
---|
| 104 | typedef std::vector< std::pair<atomId_t, atomId_t> > ListOfBonds_t;
|
---|
| 105 |
|
---|
| 106 | /** Getter to atom bonds contained in \a ObservedValues.
|
---|
| 107 | *
|
---|
| 108 | * \return atom's bonds
|
---|
| 109 | */
|
---|
| 110 | ListOfBonds_t getAtomBonds() const;
|
---|
| 111 |
|
---|
[0070aa] | 112 | signals:
|
---|
[65c323] | 113 | void indexChanged();
|
---|
| 114 | void positionChanged();
|
---|
| 115 | void elementChanged();
|
---|
| 116 | void bondsChanged();
|
---|
| 117 |
|
---|
| 118 | //private slots:
|
---|
[0070aa] | 119 |
|
---|
[65c323] | 120 | private:
|
---|
| 121 | void activateObserver();
|
---|
| 122 | void deactivateObserver();
|
---|
[0070aa] | 123 |
|
---|
[65c323] | 124 | static const atom * const getAtomConst(const atomId_t _id);
|
---|
| 125 | static atom * const getAtom(const atomId_t _id);
|
---|
[0070aa] | 126 |
|
---|
| 127 | private:
|
---|
[65c323] | 128 | static atomId_t updateIndex();
|
---|
| 129 | static Vector updatePosition(
|
---|
| 130 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
| 131 | static atomicNumber_t updateElement(
|
---|
| 132 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
| 133 | static ListOfBonds_t updateBonds(
|
---|
| 134 | const boost::function<const atomId_t ()> &_getAtomIndex);
|
---|
| 135 |
|
---|
| 136 | //!> enumeration of observed values to match with entries in ObservedValues
|
---|
| 137 | enum ObservedTypes {
|
---|
| 138 | //!> contains the current atom index
|
---|
| 139 | AtomIndex,
|
---|
| 140 | //!> contains the current atom position
|
---|
| 141 | AtomPosition,
|
---|
| 142 | //!> contains the current atom element
|
---|
| 143 | AtomElement,
|
---|
| 144 | //!> contains the current set of bonds atoms fort the atom
|
---|
| 145 | AtomBonds,
|
---|
| 146 | //!> gives the size of the enumeration
|
---|
| 147 | MAX_ObservedTypes
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 | /** Initializes all \a ObservedValues entries.
|
---|
| 151 | *
|
---|
| 152 | * \param _ObservedValues vector of ObservedValue to be filled
|
---|
| 153 | * \param _id atom id
|
---|
| 154 | * \param _atomref reference to atom
|
---|
| 155 | * \param _subjectKilled ref to function to call on subjectKilled()
|
---|
| 156 | */
|
---|
| 157 | static void initObservedValues(
|
---|
| 158 | ObservedValues_t &_ObservedValues,
|
---|
| 159 | const atomId_t _id,
|
---|
| 160 | const atom * const _atomref,
|
---|
| 161 | const boost::function<void(const atomId_t)> &_subjectKilled);
|
---|
| 162 |
|
---|
| 163 | /** Destroys all \a ObservedValues entries.
|
---|
| 164 | *
|
---|
| 165 | * \param _ObservedValues vector of ObservedValue to be destroyed
|
---|
| 166 | */
|
---|
| 167 | static void destroyObservedValues(
|
---|
| 168 | std::vector<boost::any> &_ObservedValues);
|
---|
| 169 |
|
---|
| 170 | //!> counts how many ObservedValues have already been subjectKilled()
|
---|
| 171 | mutable size_t subjectKilledCount;
|
---|
| 172 |
|
---|
| 173 | private:
|
---|
| 174 |
|
---|
| 175 | //!> list of channels when index needs to update
|
---|
| 176 | static const Observable::channels_t AtomIndexChannels;
|
---|
| 177 | //!> list of channels when position needs to update
|
---|
| 178 | static const Observable::channels_t AtomPositionChannels;
|
---|
| 179 | //!> list of channels when element needs to update
|
---|
| 180 | static const Observable::channels_t AtomElementChannels;
|
---|
| 181 | //!> list of channels when bonds needs to update
|
---|
| 182 | static const Observable::channels_t AtomBondsChannels;
|
---|
| 183 |
|
---|
| 184 | //!> we get multiple subjectKilled(), count and call callback() only on last
|
---|
| 185 | const unsigned int AllsignedOnChannels;
|
---|
| 186 | unsigned int signedOffChannels;
|
---|
| 187 |
|
---|
| 188 | //!> the Observable we are signed on, also indicates whether we are sign on (not NULL)
|
---|
| 189 | const Observable *owner;
|
---|
| 190 |
|
---|
| 191 | private:
|
---|
| 192 |
|
---|
| 193 | //!> reference to InstanceBoard for callbacks on subjectKilled()
|
---|
| 194 | QtObservedInstanceBoard & board;
|
---|
| 195 |
|
---|
[98c35c] | 196 | //!> internal reference to ObservedValues held by QtObservedInstanceBoard
|
---|
[65c323] | 197 | ObservedValues_t ObservedValues;
|
---|
[0070aa] | 198 | };
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | #endif /* QTOBSERVEDATOM_HPP_ */
|
---|