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