| [15c8a9] | 1 | /* | 
|---|
| [2f7988] | 2 | * QtObservedInstanceBoard.hpp | 
|---|
| [15c8a9] | 3 | * | 
|---|
|  | 4 | *  Created on: Oct 17, 2015 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 |  | 
|---|
| [2f7988] | 9 | #ifndef QTOBSERVEDINSTANCEBOARD_HPP_ | 
|---|
|  | 10 | #define QTOBSERVEDINSTANCEBOARD_HPP_ | 
|---|
| [15c8a9] | 11 |  | 
|---|
|  | 12 | // include config.h | 
|---|
|  | 13 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 14 | #include <config.h> | 
|---|
|  | 15 | #endif | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include <QtGui/QWidget> | 
|---|
|  | 18 |  | 
|---|
| [98c35c] | 19 | #include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp" | 
|---|
| [b4bd0e] | 20 | #include "UIElements/Qt4/InstanceBoard/QtObservedBond.hpp" | 
|---|
| [98c35c] | 21 | #include "UIElements/Qt4/InstanceBoard/QtObservedMolecule.hpp" | 
|---|
| [15c8a9] | 22 |  | 
|---|
| [98c35c] | 23 | #include <map> | 
|---|
| [1a58ce] | 24 | #include <boost/bimap.hpp> | 
|---|
| [98c35c] | 25 | #include <boost/function.hpp> | 
|---|
| [15c8a9] | 26 |  | 
|---|
|  | 27 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
|  | 28 |  | 
|---|
| [98c35c] | 29 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_types.hpp" | 
|---|
| [41e287] | 30 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer.hpp" | 
|---|
| [15c8a9] | 31 | #include "types.hpp" | 
|---|
|  | 32 |  | 
|---|
|  | 33 | class GLWorldScene; | 
|---|
|  | 34 | class GLMoleculeObject_bond; | 
|---|
|  | 35 | class GLMoleculeObject_molecule; | 
|---|
|  | 36 |  | 
|---|
| [2f7988] | 37 | /** The QtObservedInstanceBoard is the central class for communicating instance | 
|---|
| [15c8a9] | 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 | 
|---|
| [2f7988] | 47 | * the QtObservedInstanceBoard. Here, we instantiate all ObservedValue's needed | 
|---|
| [15c8a9] | 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 | 
|---|
| [2f7988] | 52 | * molecule. It requests the ObservedValues from the QtObservedInstanceBoard | 
|---|
| [15c8a9] | 53 | * and uses these directly. | 
|---|
| [2f7988] | 54 | * The QtObservedInstanceBoard also records all subjectKilled() signals from | 
|---|
| [15c8a9] | 55 | * each ObservedValue. Additionally, each class using the ObservedValues | 
|---|
| [2f7988] | 56 | * additionally informs the QtObservedInstanceBoard when subjectKilled() was | 
|---|
| [15c8a9] | 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 | */ | 
|---|
| [2f7988] | 62 | class QtObservedInstanceBoard : public QWidget, public Observer | 
|---|
| [15c8a9] | 63 | { | 
|---|
|  | 64 | Q_OBJECT | 
|---|
|  | 65 |  | 
|---|
|  | 66 | public: | 
|---|
| [b4bd0e] | 67 | //!> copy bond id type from QtObservedBond | 
|---|
|  | 68 | typedef QtObservedBond::bondId_t bondId_t; | 
|---|
|  | 69 |  | 
|---|
| [2f7988] | 70 | /** Cstor of QtObservedInstanceBoard. | 
|---|
| [15c8a9] | 71 | * | 
|---|
| [2f7988] | 72 | * \param _parent Qt parent to automatically destroy when parent is destroyed | 
|---|
| [15c8a9] | 73 | */ | 
|---|
| [2f7988] | 74 | QtObservedInstanceBoard(QWidget * _parent=0); | 
|---|
| [15c8a9] | 75 |  | 
|---|
| [2f7988] | 76 | /** Dstor of QtObservedInstanceBoard. | 
|---|
| [15c8a9] | 77 | * | 
|---|
|  | 78 | */ | 
|---|
| [2f7988] | 79 | ~QtObservedInstanceBoard(); | 
|---|
| [15c8a9] | 80 |  | 
|---|
|  | 81 | // Observer functions | 
|---|
|  | 82 | void update(Observable *publisher); | 
|---|
|  | 83 | void subjectKilled(Observable *publisher); | 
|---|
|  | 84 | void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
|  | 85 |  | 
|---|
| [1a58ce] | 86 | const atomId_t getAtomIdToIndex(ObservedValue_Index_t _id) const; | 
|---|
| [b4bd0e] | 87 | const bondId_t getBondIdToIndex(ObservedValue_Index_t _id) const; | 
|---|
| [1a58ce] | 88 | const moleculeId_t getMoleculeIdToIndex(ObservedValue_Index_t _id) const; | 
|---|
|  | 89 |  | 
|---|
| [98c35c] | 90 | QtObservedAtom::ptr getObservedAtom(const atomId_t _id); | 
|---|
| [3054f4a] | 91 | QtObservedAtom::ptr getObservedAtom(ObservedValue_Index_t _id); | 
|---|
| [b4bd0e] | 92 | QtObservedBond::ptr getObservedBond(const bondId_t _id); | 
|---|
|  | 93 | QtObservedBond::ptr getObservedBond(ObservedValue_Index_t _id); | 
|---|
| [98c35c] | 94 | QtObservedMolecule::ptr getObservedMolecule(const moleculeId_t _id); | 
|---|
| [3054f4a] | 95 | QtObservedMolecule::ptr getObservedMolecule(ObservedValue_Index_t _id); | 
|---|
| [59eabc] | 96 | void markObservedAtomAsConnected(ObservedValue_Index_t _id); | 
|---|
|  | 97 | void markObservedAtomAsDisconnected(ObservedValue_Index_t _id); | 
|---|
|  | 98 | void markObservedAtomForErase(ObservedValue_Index_t _id); | 
|---|
| [b4bd0e] | 99 | void markObservedBondAsConnected(ObservedValue_Index_t _id); | 
|---|
|  | 100 | void markObservedBondAsDisconnected(ObservedValue_Index_t _id); | 
|---|
|  | 101 | void markObservedBondForErase(ObservedValue_Index_t _id); | 
|---|
| [59eabc] | 102 | void markObservedMoleculeAsConnected(ObservedValue_Index_t _id); | 
|---|
|  | 103 | void markObservedMoleculeAsDisconnected(ObservedValue_Index_t _id); | 
|---|
|  | 104 | void markObservedMoleculeForErase(ObservedValue_Index_t _id); | 
|---|
| [15c8a9] | 105 |  | 
|---|
|  | 106 | signals: | 
|---|
| [1b07b1] | 107 | void atomInserted(QtObservedAtom::ptr _atom); | 
|---|
| [59eabc] | 108 | void atomRemoved(ObservedValue_Index_t _atomid); | 
|---|
| [15c8a9] | 109 | void atomIndexChanged(const atomId_t _oldid, const atomId_t _newid); | 
|---|
| [b4bd0e] | 110 | void bondInserted(QtObservedBond::ptr _bond); | 
|---|
|  | 111 | void bondRemoved(ObservedValue_Index_t _bondid); | 
|---|
|  | 112 | void bondIndexChanged(const bondId_t _oldid, const bondId_t _newid); | 
|---|
| [1b07b1] | 113 | void moleculeInserted(QtObservedMolecule::ptr _mol); | 
|---|
| [59eabc] | 114 | void moleculeRemoved(ObservedValue_Index_t _molid); | 
|---|
| [15c8a9] | 115 | void moleculeIndexChanged(const moleculeId_t _oldid, const moleculeId_t _newid); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | private: | 
|---|
|  | 118 | friend class GLWorldScene; | 
|---|
|  | 119 | friend class GLMoleculeObject_bond; | 
|---|
| [65c323] | 120 | friend class QtObservedAtom; | 
|---|
| [494478] | 121 | friend class QtObservedMolecule; | 
|---|
| [15c8a9] | 122 |  | 
|---|
|  | 123 | //!> indicating whether we are still signedOn to World or not | 
|---|
|  | 124 | bool WorldSignedOn; | 
|---|
|  | 125 |  | 
|---|
| [b4bd0e] | 126 | typedef std::multiset<Observable *> SignedOn_t; | 
|---|
| [15c8a9] | 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 |  | 
|---|
| [59eabc] | 132 | //!> "templated typedef" for an id to index map. | 
|---|
|  | 133 | template <class id> | 
|---|
| [1a58ce] | 134 | struct IdtoIndex_t : boost::bimap<id, ObservedValue_Index_t> {}; | 
|---|
| [59eabc] | 135 | IdtoIndex_t<atomId_t> atomids_lookup; | 
|---|
| [b4bd0e] | 136 | IdtoIndex_t<bondId_t> bondids_lookup; | 
|---|
| [59eabc] | 137 | IdtoIndex_t<moleculeId_t> moleculeids_lookup; | 
|---|
|  | 138 |  | 
|---|
| [15c8a9] | 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 | * | 
|---|
| [59eabc] | 145 | * \param _id observable that received the subjectKilled() | 
|---|
| [15c8a9] | 146 | */ | 
|---|
| [59eabc] | 147 | void atomcountsubjectKilled(ObservedValue_Index_t _id); | 
|---|
| [15c8a9] | 148 |  | 
|---|
| [b4bd0e] | 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 |  | 
|---|
| [15c8a9] | 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 | * | 
|---|
| [59eabc] | 165 | * \param _id observable that received the subjectKilled() | 
|---|
| [15c8a9] | 166 | */ | 
|---|
| [59eabc] | 167 | void moleculecountsubjectKilled(ObservedValue_Index_t _id); | 
|---|
| [15c8a9] | 168 |  | 
|---|
| [41e287] | 169 | //!> container with all ObservedValues for each atom, associated by id | 
|---|
| [59eabc] | 170 | ObservedValuesContainer<QtObservedAtom, ObservedValue_Index_t> atomObservedValues; | 
|---|
| [b4bd0e] | 171 | //!> container with all ObservedValues for each bond, associated by id pairs | 
|---|
|  | 172 | ObservedValuesContainer<QtObservedBond, ObservedValue_Index_t> bondObservedValues; | 
|---|
| [41e287] | 173 | //!> container with all ObservedValues for each molecule, associated by id | 
|---|
| [59eabc] | 174 | ObservedValuesContainer<QtObservedMolecule, ObservedValue_Index_t> moleculeObservedValues; | 
|---|
| [15c8a9] | 175 | }; | 
|---|
|  | 176 |  | 
|---|
| [2f7988] | 177 | #endif /* QTOBSERVEDINSTANCEBOARD_HPP_ */ | 
|---|