| 1 | /* | 
|---|
| 2 | * QtObservedInstanceBoard.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Oct 17, 2015 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | #ifndef QTOBSERVEDINSTANCEBOARD_HPP_ | 
|---|
| 10 | #define QTOBSERVEDINSTANCEBOARD_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 "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp" | 
|---|
| 20 | #include "UIElements/Qt4/InstanceBoard/QtObservedBond.hpp" | 
|---|
| 21 | #include "UIElements/Qt4/InstanceBoard/QtObservedMolecule.hpp" | 
|---|
| 22 |  | 
|---|
| 23 | #include <map> | 
|---|
| 24 | #include <boost/bimap.hpp> | 
|---|
| 25 | #include <boost/function.hpp> | 
|---|
| 26 |  | 
|---|
| 27 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp" | 
|---|
| 30 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp" | 
|---|
| 31 | #include "types.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | class GLWorldScene; | 
|---|
| 34 | class GLMoleculeObject_bond; | 
|---|
| 35 | class GLMoleculeObject_molecule; | 
|---|
| 36 |  | 
|---|
| 37 | /** The QtObservedInstanceBoard is the central class for communicating instance | 
|---|
| 38 | * creation and destruction from MoleCuilder into the QtGui realm. It should be | 
|---|
| 39 | * thought of as an interface to allow for safe multi-threaded computing. | 
|---|
| 40 | * | 
|---|
| 41 | * The idea is as follows: As soon as a molecule is instantiated, all the QtGui | 
|---|
| 42 | * needs to access the instance (for displaying it) is wrapped up in a | 
|---|
| 43 | * ObservedValue. This ObservedValue separates the lifetime of the molecule object | 
|---|
| 44 | * from the information contained therein and thus makes the visual representation | 
|---|
| 45 | * independent of the life time. The Observer/Observable signal from the World, | 
|---|
| 46 | * i.e. World::MoleculeInserted, is caught (directly within the same thread) by | 
|---|
| 47 | * the QtObservedInstanceBoard. Here, we instantiate all ObservedValue's needed | 
|---|
| 48 | * for this specific molecule and store them in an internal map. Next, we emit | 
|---|
| 49 | * a Qt signal informing the QtGui part about the new molecule. | 
|---|
| 50 | * At some later stage, the QtGui will (probably in a different thread) | 
|---|
| 51 | * instantiate a GLMoleculeObject_molecule as a visual representation of the | 
|---|
| 52 | * molecule. It requests the ObservedValues from the QtObservedInstanceBoard | 
|---|
| 53 | * and uses these directly. | 
|---|
| 54 | * The QtObservedInstanceBoard also records all subjectKilled() signals from | 
|---|
| 55 | * each ObservedValue. Additionally, each class using the ObservedValues | 
|---|
| 56 | * additionally informs the QtObservedInstanceBoard when subjectKilled() was | 
|---|
| 57 | * called. If subjectKilled() for each ObservedValue of a molecule and from the | 
|---|
| 58 | * visual representation have been received, a removal Qt signal is emitted. | 
|---|
| 59 | * | 
|---|
| 60 | * The same holds for the atom | 
|---|
| 61 | */ | 
|---|
| 62 | class QtObservedInstanceBoard : public QWidget, public Observer | 
|---|
| 63 | { | 
|---|
| 64 | Q_OBJECT | 
|---|
| 65 |  | 
|---|
| 66 | public: | 
|---|
| 67 | //!> copy bond id type from QtObservedBond | 
|---|
| 68 | typedef QtObservedBond::bondId_t bondId_t; | 
|---|
| 69 |  | 
|---|
| 70 | /** Cstor of QtObservedInstanceBoard. | 
|---|
| 71 | * | 
|---|
| 72 | * \param _parent Qt parent to automatically destroy when parent is destroyed | 
|---|
| 73 | */ | 
|---|
| 74 | QtObservedInstanceBoard(QWidget * _parent=0); | 
|---|
| 75 |  | 
|---|
| 76 | /** Dstor of QtObservedInstanceBoard. | 
|---|
| 77 | * | 
|---|
| 78 | */ | 
|---|
| 79 | ~QtObservedInstanceBoard(); | 
|---|
| 80 |  | 
|---|
| 81 | // Observer functions | 
|---|
| 82 | void update(Observable *publisher); | 
|---|
| 83 | void subjectKilled(Observable *publisher); | 
|---|
| 84 | void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
| 85 |  | 
|---|
| 86 | const atomId_t getAtomIdToIndex(ObservedValue_Index_t _id) const; | 
|---|
| 87 | const bondId_t getBondIdToIndex(ObservedValue_Index_t _id) const; | 
|---|
| 88 | const moleculeId_t getMoleculeIdToIndex(ObservedValue_Index_t _id) const; | 
|---|
| 89 |  | 
|---|
| 90 | QtObservedAtom::ptr getObservedAtom(const atomId_t _id); | 
|---|
| 91 | QtObservedAtom::ptr getObservedAtom(ObservedValue_Index_t _id); | 
|---|
| 92 | QtObservedBond::ptr getObservedBond(const bondId_t _id); | 
|---|
| 93 | QtObservedBond::ptr getObservedBond(ObservedValue_Index_t _id); | 
|---|
| 94 | QtObservedMolecule::ptr getObservedMolecule(const moleculeId_t _id); | 
|---|
| 95 | QtObservedMolecule::ptr getObservedMolecule(ObservedValue_Index_t _id); | 
|---|
| 96 | void markObservedAtomAsConnected(ObservedValue_Index_t _id); | 
|---|
| 97 | void markObservedAtomAsDisconnected(ObservedValue_Index_t _id); | 
|---|
| 98 | void markObservedAtomForErase(ObservedValue_Index_t _id); | 
|---|
| 99 | void markObservedBondAsConnected(ObservedValue_Index_t _id); | 
|---|
| 100 | void markObservedBondAsDisconnected(ObservedValue_Index_t _id); | 
|---|
| 101 | void markObservedBondForErase(ObservedValue_Index_t _id); | 
|---|
| 102 | void markObservedMoleculeAsConnected(ObservedValue_Index_t _id); | 
|---|
| 103 | void markObservedMoleculeAsDisconnected(ObservedValue_Index_t _id); | 
|---|
| 104 | void markObservedMoleculeForErase(ObservedValue_Index_t _id); | 
|---|
| 105 |  | 
|---|
| 106 | signals: | 
|---|
| 107 | void atomInserted(QtObservedAtom::ptr _atom); | 
|---|
| 108 | void atomRemoved(ObservedValue_Index_t _atomid); | 
|---|
| 109 | void atomIndexChanged(const atomId_t _oldid, const atomId_t _newid); | 
|---|
| 110 | void bondInserted(QtObservedBond::ptr _bond); | 
|---|
| 111 | void bondRemoved(ObservedValue_Index_t _bondid); | 
|---|
| 112 | void bondIndexChanged(const bondId_t _oldid, const bondId_t _newid); | 
|---|
| 113 | void moleculeInserted(QtObservedMolecule::ptr _mol); | 
|---|
| 114 | void moleculeRemoved(ObservedValue_Index_t _molid); | 
|---|
| 115 | void moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid); | 
|---|
| 116 |  | 
|---|
| 117 | private: | 
|---|
| 118 | friend class GLWorldScene; | 
|---|
| 119 | friend class GLMoleculeObject_bond; | 
|---|
| 120 | friend class QtObservedAtom; | 
|---|
| 121 | friend class QtObservedMolecule; | 
|---|
| 122 |  | 
|---|
| 123 | //!> indicating whether we are still signedOn to World or not | 
|---|
| 124 | bool WorldSignedOn; | 
|---|
| 125 |  | 
|---|
| 126 | typedef std::multiset<Observable *> SignedOn_t; | 
|---|
| 127 | //!> map indicating to which atom we are currently signed on | 
|---|
| 128 | SignedOn_t AtomSignedOn; | 
|---|
| 129 | //!> map indicating to which molecule we are currently signed on | 
|---|
| 130 | SignedOn_t MoleculeSignedOn; | 
|---|
| 131 |  | 
|---|
| 132 | //!> "templated typedef" for an id to index map. | 
|---|
| 133 | template <class id> | 
|---|
| 134 | struct IdtoIndex_t : boost::bimap<id, ObservedValue_Index_t> {}; | 
|---|
| 135 | IdtoIndex_t<atomId_t> atomids_lookup; | 
|---|
| 136 | IdtoIndex_t<bondId_t> bondids_lookup; | 
|---|
| 137 | IdtoIndex_t<moleculeId_t> moleculeids_lookup; | 
|---|
| 138 |  | 
|---|
| 139 | /** Counts how many atom's ObservedValues got subjectKilled. | 
|---|
| 140 | * | 
|---|
| 141 | * This is used to give removal signal only when each and every | 
|---|
| 142 | * ObservedValue (and the instance itself) has been subjectKilled by the | 
|---|
| 143 | * monitored Observable. Only then can we safely remove the instance. | 
|---|
| 144 | * | 
|---|
| 145 | * \param _id observable that received the subjectKilled() | 
|---|
| 146 | */ | 
|---|
| 147 | void atomcountsubjectKilled(ObservedValue_Index_t _id); | 
|---|
| 148 |  | 
|---|
| 149 | /** Counts how many bond's ObservedValues got subjectKilled. | 
|---|
| 150 | * | 
|---|
| 151 | * This is used to give removal signal only when each and every | 
|---|
| 152 | * ObservedValue (and the instance itself) has been subjectKilled by the | 
|---|
| 153 | * monitored Observable. Only then can we safely remove the instance. | 
|---|
| 154 | * | 
|---|
| 155 | * \param _bondid id of the bond | 
|---|
| 156 | */ | 
|---|
| 157 | void bondcountsubjectKilled(ObservedValue_Index_t _bondid); | 
|---|
| 158 |  | 
|---|
| 159 | /** Counts how many molecule's ObservedValues got subjectKilled. | 
|---|
| 160 | * | 
|---|
| 161 | * This is used to give removal signal only when each and every | 
|---|
| 162 | * ObservedValue (and the instance itself) has been subjectKilled by the | 
|---|
| 163 | * monitored Observable. Only then can we safely remove the instance. | 
|---|
| 164 | * | 
|---|
| 165 | * \param _id observable that received the subjectKilled() | 
|---|
| 166 | */ | 
|---|
| 167 | void moleculecountsubjectKilled(ObservedValue_Index_t _id); | 
|---|
| 168 |  | 
|---|
| 169 | //!> container with all ObservedValues for each atom, associated by id | 
|---|
| 170 | ObservedValuesContainer<QtObservedAtom, ObservedValue_Index_t> atomObservedValues; | 
|---|
| 171 | //!> container with all ObservedValues for each bond, associated by id pairs | 
|---|
| 172 | ObservedValuesContainer<QtObservedBond, ObservedValue_Index_t> bondObservedValues; | 
|---|
| 173 | //!> container with all ObservedValues for each molecule, associated by id | 
|---|
| 174 | ObservedValuesContainer<QtObservedMolecule, ObservedValue_Index_t> moleculeObservedValues; | 
|---|
| 175 | }; | 
|---|
| 176 |  | 
|---|
| 177 | #endif /* QTOBSERVEDINSTANCEBOARD_HPP_ */ | 
|---|