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