| 1 | /*
 | 
|---|
| 2 |  * GLWorldScene.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  *  Created on: Aug 17, 2011
 | 
|---|
| 7 |  *      Author: heber
 | 
|---|
| 8 |  */
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #ifndef GLWORLDSCENE_HPP_
 | 
|---|
| 11 | #define GLWORLDSCENE_HPP_
 | 
|---|
| 12 | 
 | 
|---|
| 13 | // include config.h
 | 
|---|
| 14 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 15 | #include <config.h>
 | 
|---|
| 16 | #endif
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <Qt/qobject.h>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <iosfwd>
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <boost/bimap.hpp>
 | 
|---|
| 23 | #include <boost/thread/recursive_mutex.hpp>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "Bond/bond.hpp"
 | 
|---|
| 26 | #include "GLMoleculeObject_bond.hpp"
 | 
|---|
| 27 | #include "GLMoleculeObject_molecule.hpp"
 | 
|---|
| 28 | #include "types.hpp"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp"
 | 
|---|
| 31 | #include "UIElements/Qt4/InstanceBoard/QtObservedBond.hpp"
 | 
|---|
| 32 | #include "UIElements/Qt4/InstanceBoard/QtObservedMolecule.hpp"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | class Shape;
 | 
|---|
| 35 | 
 | 
|---|
| 36 | class QGLPainter;
 | 
|---|
| 37 | class QGLSceneNode;
 | 
|---|
| 38 | class QGLView;
 | 
|---|
| 39 | 
 | 
|---|
| 40 | class GLMoleculeObject;
 | 
|---|
| 41 | class GLMoleculeObject_atom;
 | 
|---|
| 42 | class GLMoleculeObject_molecule;
 | 
|---|
| 43 | class GLMoleculeObject_shape;
 | 
|---|
| 44 | 
 | 
|---|
| 45 | class QtObservedInstanceBoard;
 | 
|---|
| 46 | 
 | 
|---|
| 47 | /** This class contains a list of all molecules in the world.
 | 
|---|
| 48 |  *
 | 
|---|
| 49 |  */
 | 
|---|
| 50 | class GLWorldScene : public QObject
 | 
|---|
| 51 | {
 | 
|---|
| 52 |   Q_OBJECT
 | 
|---|
| 53 | public:
 | 
|---|
| 54 |   GLWorldScene(
 | 
|---|
| 55 |       QtObservedInstanceBoard * _board,
 | 
|---|
| 56 |       QObject *parent=0);
 | 
|---|
| 57 |   virtual ~GLWorldScene();
 | 
|---|
| 58 | 
 | 
|---|
| 59 | //#if !defined(QT_OPENGL_ES_1)
 | 
|---|
| 60 | //  PerPixelEffect *lighting;
 | 
|---|
| 61 | //#endif
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   void changeMaterials(bool perPixel);
 | 
|---|
| 64 |   QGLSceneNode* getAtom(size_t);
 | 
|---|
| 65 |   QGLSceneNode* getMolecule(size_t);
 | 
|---|
| 66 |   QGLSceneNode* getBond(size_t, size_t);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   void initialize(QGLView *view, QGLPainter *painter) const;
 | 
|---|
| 69 |   void draw(QGLPainter *painter, const QVector4D &cameraPlane) const;
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   enum SelectionModeType{
 | 
|---|
| 72 |     SelectAtom,
 | 
|---|
| 73 |     SelectMolecule
 | 
|---|
| 74 |   };
 | 
|---|
| 75 |   void setSelectionMode(SelectionModeType mode);
 | 
|---|
| 76 | 
 | 
|---|
| 77 | signals:
 | 
|---|
| 78 |   void changed();
 | 
|---|
| 79 | //  void updated();
 | 
|---|
| 80 |   void changeOccured();
 | 
|---|
| 81 |   void pressed();
 | 
|---|
| 82 |   void released();
 | 
|---|
| 83 |   void clicked();
 | 
|---|
| 84 |   void clicked(atomId_t);
 | 
|---|
| 85 |   void doubleClicked();
 | 
|---|
| 86 |   void hoverChanged(const atomId_t);
 | 
|---|
| 87 |   void hoverChanged(const moleculeId_t, int);
 | 
|---|
| 88 |   void atomConnected(QtObservedAtom::ptr);
 | 
|---|
| 89 |   void bondConnected(QtObservedBond::ptr);
 | 
|---|
| 90 | 
 | 
|---|
| 91 | private slots:
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   void clickAtom(atomId_t no);
 | 
|---|
| 94 |   void connectAtom(QtObservedAtom::ptr);
 | 
|---|
| 95 |   void insertAtom(QtObservedAtom::ptr);
 | 
|---|
| 96 |   void removeAtom(ObservedValue_Index_t _atomid);
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   void connectBond(QtObservedBond::ptr);
 | 
|---|
| 99 |   void insertBond(QtObservedBond::ptr);
 | 
|---|
| 100 |   void removeBond(ObservedValue_Index_t _bondid);
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   void clickMolecule(moleculeId_t no);
 | 
|---|
| 103 |   void insertMolecule(QtObservedMolecule::ptr);
 | 
|---|
| 104 |   void removeMolecule(ObservedValue_Index_t _molid);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   void setSelectionModeAtom();
 | 
|---|
| 107 |   void setSelectionModeMolecule();
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   void addShape(const std::string &_name);
 | 
|---|
| 110 |   void removeShape(const std::string &_name);
 | 
|---|
| 111 | //  void update();
 | 
|---|
| 112 |   void moleculesVisibilityChanged(ObservedValue_Index_t _id, bool _visible);
 | 
|---|
| 113 |   void hoverChangedSignalled(GLMoleculeObject *ob);
 | 
|---|
| 114 | 
 | 
|---|
| 115 |   void reparentAtom();
 | 
|---|
| 116 |   void reparentBondLeft();
 | 
|---|
| 117 |   void reparentBondRight();
 | 
|---|
| 118 | 
 | 
|---|
| 119 | private:
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   GLMoleculeObject_molecule *getMoleculeObject(
 | 
|---|
| 122 |       const ObservedValue_Index_t _molid) const;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   void resetParent(
 | 
|---|
| 125 |       GLMoleculeObject *_obj,
 | 
|---|
| 126 |       GLMoleculeObject_molecule *_molobj);
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   void reparentBond(
 | 
|---|
| 129 |       const QtObservedBond::ptr _bond,
 | 
|---|
| 130 |       const QtObservedAtom::ptr _atom,
 | 
|---|
| 131 |       const GLMoleculeObject_bond::SideOfBond _side);
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   GLMoleculeObject_bond *getBondInScene(
 | 
|---|
| 134 |       const ObservedValue_Index_t _bondid,
 | 
|---|
| 135 |       GLMoleculeObject_bond::SideOfBond _side) const;
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   void checkAndRemoveBond(const ObservedValue_Index_t &_bondid);
 | 
|---|
| 138 | 
 | 
|---|
| 139 |   void checkAndRemoveAtom(const ObservedValue_Index_t &_atomid);
 | 
|---|
| 140 | 
 | 
|---|
| 141 | public:
 | 
|---|
| 142 |   void updateSelectedShapes();
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   typedef std::map< QObject*, QtObservedAtom::ptr > ObservedAtoms_t;
 | 
|---|
| 145 |   typedef std::map< QObject*, QtObservedBond::ptr > ObservedBonds_t;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   typedef std::map< ObservedValue_Index_t, QObject* > ObservedValueIndexLookup_t;
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   typedef std::map< ObservedValue_Index_t, GLMoleculeObject_molecule* > MoleculeNodeMap;
 | 
|---|
| 150 |   typedef std::map< ObservedValue_Index_t, GLMoleculeObject_atom* > AtomNodeMap;
 | 
|---|
| 151 |   typedef std::multimap< ObservedValue_Index_t, GLMoleculeObject_bond* > BondNodeMap;
 | 
|---|
| 152 |   typedef std::map< std::string , GLMoleculeObject_shape* > ShapeNodeMap;
 | 
|---|
| 153 | 
 | 
|---|
| 154 |   //!> typedef for storing the current parent to each atom node in scene, NULL indicates GLWorldScene is parent
 | 
|---|
| 155 |   typedef boost::bimap<
 | 
|---|
| 156 |       boost::bimaps::set_of< ObservedValue_Index_t >,
 | 
|---|
| 157 |       boost::bimaps::multiset_of< ObservedValue_Index_t >
 | 
|---|
| 158 |   > AtomNodeParentMap_t;
 | 
|---|
| 159 |   //!> typedef for storing the current parent to each bond node in scene, NULL indicates GLWorldScene is parent
 | 
|---|
| 160 |   typedef boost::bimap<
 | 
|---|
| 161 |       boost::bimaps::set_of< ObservedValue_Index_t >,
 | 
|---|
| 162 |       boost::bimaps::multiset_of< ObservedValue_Index_t >
 | 
|---|
| 163 |   > BondNodeParentMap_t;
 | 
|---|
| 164 | 
 | 
|---|
| 165 |   typedef std::set< ObservedValue_Index_t > ToBeRemovedNodes_t;
 | 
|---|
| 166 | 
 | 
|---|
| 167 | private:
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   //!> id of atom hovered over
 | 
|---|
| 170 |   atomId_t hoverAtomId;
 | 
|---|
| 171 | 
 | 
|---|
| 172 |   //!> mutex to ascertain atomic access to atom-related internal maps
 | 
|---|
| 173 |   boost::recursive_mutex Atom_mutex;
 | 
|---|
| 174 |   //!> mutex to ascertain atomic access to bond-related internal maps
 | 
|---|
| 175 |   boost::recursive_mutex Bond_mutex;
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   AtomNodeMap AtomsinSceneMap;
 | 
|---|
| 178 |   BondNodeMap BondsinSceneMap;
 | 
|---|
| 179 |   MoleculeNodeMap MoleculesinSceneMap;
 | 
|---|
| 180 |   ShapeNodeMap ShapesinSceneMap;
 | 
|---|
| 181 |   AtomNodeParentMap_t AtomNodeParentMap;
 | 
|---|
| 182 |   std::vector<BondNodeParentMap_t> BondNodeParentMaps;
 | 
|---|
| 183 | 
 | 
|---|
| 184 |   //!> stored observed atom such that its signals may always be safely evaluated
 | 
|---|
| 185 |   ObservedAtoms_t ObservedAtoms;
 | 
|---|
| 186 |   //!> stored observed bond such that its signals may always be safely evaluated
 | 
|---|
| 187 |   ObservedBonds_t ObservedBonds;
 | 
|---|
| 188 |   //!> in moleculeChanged() we only have QObject*, in reparentAtom() we have ObservedValue_Index_t, need lookup
 | 
|---|
| 189 |   ObservedValueIndexLookup_t ObservedAtomIndexLookup;
 | 
|---|
| 190 |   //!> in moleculeChanged() we only have QObject*, in reparentBond..() we have ObservedValue_Index_t, need lookup
 | 
|---|
| 191 |   ObservedValueIndexLookup_t ObservedBondIndexLookup;
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   //!> all atoms and bond id where we got the ...Removed signals for
 | 
|---|
| 194 |   ToBeRemovedNodes_t ToBeRemovedNodes;
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   SelectionModeType selectionMode;
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   QtObservedInstanceBoard * board;
 | 
|---|
| 199 | };
 | 
|---|
| 200 | 
 | 
|---|
| 201 | #endif /* GLWORLDSCENE_HPP_ */
 | 
|---|