| 1 | /*
|
|---|
| 2 | * QtInstanceInformationBoard.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Oct 17, 2015
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | #ifndef QTINSTANCEINFORMATIONBOARD_HPP_
|
|---|
| 10 | #define QTINSTANCEINFORMATIONBOARD_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 <map>
|
|---|
| 20 | #include <vector>
|
|---|
| 21 |
|
|---|
| 22 | #include <boost/any.hpp>
|
|---|
| 23 |
|
|---|
| 24 | #include "CodePatterns/Observer/Observer.hpp"
|
|---|
| 25 | #include "CodePatterns/ObservedValue.hpp"
|
|---|
| 26 |
|
|---|
| 27 | #include "types.hpp"
|
|---|
| 28 |
|
|---|
| 29 | class GLWorldScene;
|
|---|
| 30 | class GLMoleculeObject_atom;
|
|---|
| 31 | class GLMoleculeObject_bond;
|
|---|
| 32 | class GLMoleculeObject_molecule;
|
|---|
| 33 |
|
|---|
| 34 | /** The QtInstanceInformationBoard is the central class for communicating instance
|
|---|
| 35 | * creation and destruction from MoleCuilder into the QtGui realm. It should be
|
|---|
| 36 | * thought of as an interface to allow for safe multi-threaded computing.
|
|---|
| 37 | *
|
|---|
| 38 | * The idea is as follows: As soon as a molecule is instantiated, all the QtGui
|
|---|
| 39 | * needs to access the instance (for displaying it) is wrapped up in a
|
|---|
| 40 | * ObservedValue. This ObservedValue separates the lifetime of the molecule object
|
|---|
| 41 | * from the information contained therein and thus makes the visual representation
|
|---|
| 42 | * independent of the life time. The Observer/Observable signal from the World,
|
|---|
| 43 | * i.e. World::MoleculeInserted, is caught (directly within the same thread) by
|
|---|
| 44 | * the QtInstanceInformationBoard. Here, we instantiate all ObservedValue's needed
|
|---|
| 45 | * for this specific molecule and store them in an internal map. Next, we emit
|
|---|
| 46 | * a Qt signal informing the QtGui part about the new molecule.
|
|---|
| 47 | * At some later stage, the QtGui will (probably in a different thread)
|
|---|
| 48 | * instantiate a GLMoleculeObject_molecule as a visual representation of the
|
|---|
| 49 | * molecule. It requests the ObservedValues from the QtInstanceInformationBoard
|
|---|
| 50 | * and uses these directly.
|
|---|
| 51 | * The QtInstanceInformationBoard also records all subjectKilled() signals from
|
|---|
| 52 | * each ObservedValue. Additionally, each class using the ObservedValues
|
|---|
| 53 | * additionally informs the QtInstanceInformationBoard when subjectKilled() was
|
|---|
| 54 | * called. If subjectKilled() for each ObservedValue of a molecule and from the
|
|---|
| 55 | * visual representation have been received, a removal Qt signal is emitted.
|
|---|
| 56 | *
|
|---|
| 57 | * The same holds for the atom
|
|---|
| 58 | */
|
|---|
| 59 | class QtInstanceInformationBoard : public QWidget, public Observer
|
|---|
| 60 | {
|
|---|
| 61 | Q_OBJECT
|
|---|
| 62 |
|
|---|
| 63 | public:
|
|---|
| 64 | /** typedef for a vector of ObservedValue's (each is due to templating its
|
|---|
| 65 | * own type. Therefore, we need to use boost::any).
|
|---|
| 66 | */
|
|---|
| 67 | typedef std::vector<boost::any> ObservedValues_t;
|
|---|
| 68 |
|
|---|
| 69 | /** Cstor of QtInstanceInformationBoard.
|
|---|
| 70 | *
|
|---|
| 71 | */
|
|---|
| 72 | QtInstanceInformationBoard(QWidget * _parent=0);
|
|---|
| 73 |
|
|---|
| 74 | /** Dstor of QtInstanceInformationBoard.
|
|---|
| 75 | *
|
|---|
| 76 | */
|
|---|
| 77 | ~QtInstanceInformationBoard();
|
|---|
| 78 |
|
|---|
| 79 | // Observer functions
|
|---|
| 80 | void update(Observable *publisher);
|
|---|
| 81 | void subjectKilled(Observable *publisher);
|
|---|
| 82 | void recieveNotification(Observable *publisher, Notification_ptr notification);
|
|---|
| 83 |
|
|---|
| 84 | ObservedValues_t getAtomObservedValues(const atomId_t _id);
|
|---|
| 85 | ObservedValues_t getMoleculeObservedValues(const moleculeId_t _id);
|
|---|
| 86 |
|
|---|
| 87 | void returnAtomObservedValues(const atomId_t _id, ObservedValues_t &_observedvalues);
|
|---|
| 88 | void returnMoleculeObservedValues(const moleculeId_t _id, ObservedValues_t &_observedvalues);
|
|---|
| 89 |
|
|---|
| 90 | signals:
|
|---|
| 91 | void atomInserted(const moleculeId_t _molid, const atomId_t _atomid);
|
|---|
| 92 | void atomRemoved(const moleculeId_t _molid, const atomId_t _atomid);
|
|---|
| 93 | void atomIndexChanged(const atomId_t _oldid, const atomId_t _newid);
|
|---|
| 94 | void moleculeInserted(const moleculeId_t _molid);
|
|---|
| 95 | void moleculeRemoved(const moleculeId_t _molid);
|
|---|
| 96 | void moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid);
|
|---|
| 97 |
|
|---|
| 98 | private:
|
|---|
| 99 | friend class GLWorldScene;
|
|---|
| 100 | friend class GLMoleculeObject_atom;
|
|---|
| 101 | friend class GLMoleculeObject_bond;
|
|---|
| 102 | friend class GLMoleculeObject_molecule;
|
|---|
| 103 |
|
|---|
| 104 | //!> indicating whether we are still signedOn to World or not
|
|---|
| 105 | bool WorldSignedOn;
|
|---|
| 106 |
|
|---|
| 107 | typedef std::multiset<Observable *> SignedOn_t;
|
|---|
| 108 | //!> map indicating to which atom we are currently signed on
|
|---|
| 109 | SignedOn_t AtomSignedOn;
|
|---|
| 110 | //!> map indicating to which molecule we are currently signed on
|
|---|
| 111 | SignedOn_t MoleculeSignedOn;
|
|---|
| 112 |
|
|---|
| 113 | /** Counts how many atom's ObservedValues got subjectKilled.
|
|---|
| 114 | *
|
|---|
| 115 | * This is used to give removal signal only when each and every
|
|---|
| 116 | * ObservedValue (and the instance itself) has been subjectKilled by the
|
|---|
| 117 | * monitored Observable. Only then can we safely remove the instance.
|
|---|
| 118 | *
|
|---|
| 119 | * \param _atomid id of the atom
|
|---|
| 120 | */
|
|---|
| 121 | void atomcountsubjectKilled(const atomId_t _atomid);
|
|---|
| 122 |
|
|---|
| 123 | /** Counts how many molecule's ObservedValues got subjectKilled.
|
|---|
| 124 | *
|
|---|
| 125 | * This is used to give removal signal only when each and every
|
|---|
| 126 | * ObservedValue (and the instance itself) has been subjectKilled by the
|
|---|
| 127 | * monitored Observable. Only then can we safely remove the instance.
|
|---|
| 128 | *
|
|---|
| 129 | * \param _molid id of the molecule
|
|---|
| 130 | */
|
|---|
| 131 | void moleculecountsubjectKilled(const moleculeId_t _molid);
|
|---|
| 132 |
|
|---|
| 133 | //!> typedef for map with subjectKilledCounts for each atom
|
|---|
| 134 | typedef typename std::map<atomId_t, size_t> atomsubjectKilledCount_t;
|
|---|
| 135 |
|
|---|
| 136 | //!> typedef for map with subjectKilledCounts for each molecule
|
|---|
| 137 | typedef typename std::map<moleculeId_t, size_t> moleculesubjectKilledCount_t;
|
|---|
| 138 |
|
|---|
| 139 | //!> counts how many ObservedValues have already been subjectKilled()
|
|---|
| 140 | atomsubjectKilledCount_t atomsubjectKilledCount;
|
|---|
| 141 |
|
|---|
| 142 | //!> counts how many ObservedValues have already been subjectKilled()
|
|---|
| 143 | moleculesubjectKilledCount_t moleculesubjectKilledCount;
|
|---|
| 144 |
|
|---|
| 145 | //!> typedef for the map of id to each one's ObservedValues
|
|---|
| 146 | typedef typename std::map<atomId_t, ObservedValues_t> atomObservedValues_t;
|
|---|
| 147 | //!> map containing all ObservedValues for each atom, associated by id
|
|---|
| 148 | atomObservedValues_t atomObservedValues;
|
|---|
| 149 |
|
|---|
| 150 | //!> typedef for the map of id to each one's ObservedValues
|
|---|
| 151 | typedef typename std::map<moleculeId_t, ObservedValues_t> moleculeObservedValues_t;
|
|---|
| 152 | //!> map containing all ObservedValues for each molecule, associated by id
|
|---|
| 153 | moleculeObservedValues_t moleculeObservedValues;
|
|---|
| 154 |
|
|---|
| 155 | //!> stored callback function for notifying QtInstanceInformationBoard about subjectKilled in atom
|
|---|
| 156 | boost::function<void (atomId_t)> atomSubjectKilled;
|
|---|
| 157 | //!> stored callback function for notifying QtInstanceInformationBoard about subjectKilled in molecule
|
|---|
| 158 | boost::function<void (moleculeId_t)> moleculeSubjectKilled;
|
|---|
| 159 |
|
|---|
| 160 | //!> note down atom id of last removed atom to drop its ObservedValues
|
|---|
| 161 | atomId_t lastremovedatom;
|
|---|
| 162 |
|
|---|
| 163 | //!> note down atom id of last removed atom to drop its ObservedValues
|
|---|
| 164 | std::pair<moleculeId_t, atomId_t> lastremovedatomsmolecule;
|
|---|
| 165 |
|
|---|
| 166 | //!> note down molecule id of last removed molecule to drop its ObservedValues
|
|---|
| 167 | moleculeId_t lastremovedmolecule;
|
|---|
| 168 |
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | #endif /* QTINSTANCEINFORMATIONBOARD_HPP_ */
|
|---|