Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
GlComposite.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef COMPOSITE_H
21 #define COMPOSITE_H
22 
23 #include <tulip/tulipconf.h>
24 
25 #include <map>
26 #include <list>
27 #include <string>
28 
29 #include <tulip/GlSimpleEntity.h>
30 
31 namespace tlp {
32 
33 /**
34  * @ingroup OpenGL
35  * \brief GlSimpleEntity used to agregate other GlEntity
36  *
37  * This class provide basic container to manage other GlEntity
38  * @see GlSimpleEntity
39  */
40 class TLP_GL_SCOPE GlComposite : public GlSimpleEntity {
41 
42 
43 public:
44 
45  /**
46  * @brief Constructor
47  * @param deleteComponentsInDestructor if true : call delete on components when the GlComposite is delete
48  */
49  GlComposite(bool deleteComponentsInDestructor=true);
50 
51  /**
52  * @brief Destructor
53  */
54  ~GlComposite();
55 
56  /**
57  * @brief Clear the composite
58  *
59  * If deleteElems is true, composite's entities are delete
60  */
61  void reset(bool deleteElems);
62  /**
63  * @brief Add new entity with name : key.
64  *
65  * The composite does not takes the entity's ownership, i.e. it is not its responsibility to delete it.
66  */
67  void addGlEntity(GlSimpleEntity *entity, const std::string &key);
68  /**
69  * @brief Remove entity with name : key
70  *
71  * The entity is not deleted
72  */
73  void deleteGlEntity(const std::string &key,bool informTheEntity=true);
74  /**
75  * @brief Remove given entity
76  *
77  * The entity is not deleted
78  */
79  void deleteGlEntity(GlSimpleEntity *entity,bool informTheEntity=true);
80  /**
81  * @brief Find name of given entity
82  */
83  std::string findKey(GlSimpleEntity *entity);
84  /**
85  * @brief Find entity with name : key
86  */
87  GlSimpleEntity* findGlEntity(const std::string &key);
88  /**
89  * @brief Return map of entities in composite
90  */
91  const std::map<std::string, GlSimpleEntity*> &
92  getGlEntities () const {
93  return elements;
94  }
95 
96  /**
97  * @brief Set stencil number for all composite's children
98  *
99  * For more informations on stencil :
100  * @see GlSimpleEntity
101  */
102  virtual void setStencil(int stencil) {
103  this->stencil=stencil;
104 
105  for(std::list<GlSimpleEntity*>::iterator it=_sortedElements.begin(); it!=_sortedElements.end(); ++it) {
106  (*it)->setStencil(stencil);
107  }
108  }
109 
110  /**
111  * @brief Set if at the destruction of composite, components well be deleted
112  */
113  void setDeleteComponentsInDestructor(bool deleteComponentsInDestructor) {
114  this->deleteComponentsInDestructor=deleteComponentsInDestructor;
115  }
116 
117  /**
118  * @brief translate the composite with children
119  */
120  virtual void translate(const Coord &mouvement);
121 
122  /**
123  * @brief Function to export data in outString (in XML format)
124  */
125  virtual void getXML(std::string &outString);
126 
127  /**
128  * @brief Function to set data with inString (in XML format)
129  */
130  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
131 
132 ///@cond DOXYGEN_HIDDEN
133 
134  /**
135  * Function used to visit composite's children
136  */
137  virtual void acceptVisitor(GlSceneVisitor *visitor) {
138  //visitor->visit(this);
139  for(std::list<GlSimpleEntity*>::iterator it=_sortedElements.begin(); it!=_sortedElements.end(); ++it) {
140  if((*it)->isVisible()) {
141 
142 #ifndef NDEBUG
143  GlComposite *composite=dynamic_cast<GlComposite*>(*it);
144 
145  if(!composite && !(*it)->getBoundingBox().isValid()) {
146  for(std::map<std::string, GlSimpleEntity*>::iterator itE=elements.begin(); itE!=elements.end(); ++itE) {
147  if((*itE).second==(*it)) {
148  qWarning() << "Invalid bounding box for entity : " << (*itE).first;
149  assert(false);
150  }
151  }
152  }
153 
154 #endif
155 
156  (*it)->acceptVisitor(visitor);
157  }
158  }
159  }
160 
161  /**
162  * Add a layer parent to this entity
163  */
164  virtual void addLayerParent(GlLayer *layer);
165 
166  /**
167  * Remove a layer parent to this entity
168  */
169  virtual void removeLayerParent(GlLayer *layer);
170 
171  /**
172  * Call when a child of the composite is modified
173  */
174  void notifyModified(GlSimpleEntity *entity);
175 
176  /**
177  * \attention This function do nothing, GlComposite is a GlSimpleEntity so draw function must be define
178  */
179  virtual void draw(float,Camera *) {}
180 
181 ///@endcond
182 
183 protected:
184 
185  std::map<std::string, GlSimpleEntity*> elements;
186  std::list<GlSimpleEntity *> _sortedElements; //necessary to enable ordering of elements (for alpha blending)
187  std::vector<GlLayer *> layerParents;
188  bool deleteComponentsInDestructor;
189 };
190 
191 }
192 #endif