Tulip  5.6.0
Large graphs analysis and drawing
GlComposite.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
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 <map>
24 #include <list>
25 #include <string>
26 
27 #include <tulip/GlSimpleEntity.h>
28 #include <tulip/tulipconf.h>
29 
30 namespace tlp {
31 
32 /**
33  * @ingroup OpenGL
34  * \brief GlSimpleEntity used to aggregate other GlEntity
35  *
36  * This class provide basic container to manage other GlEntity
37  * @see GlSimpleEntity
38  */
39 class TLP_GL_SCOPE GlComposite : public GlSimpleEntity {
40 
41 public:
42  /**
43  * @brief Constructor
44  * @param deleteComponentsInDestructor if true : call delete on components when the GlComposite is
45  * delete
46  */
47  GlComposite(bool deleteComponentsInDestructor = true);
48 
49  /**
50  * @brief Destructor
51  */
52  ~GlComposite() override;
53 
54  /**
55  * @brief Clear the composite
56  *
57  * If deleteElems is true, composite's entities are delete
58  */
59  void reset(bool deleteElems);
60  /**
61  * @brief Add new entity with name : key.
62  *
63  * The composite does not takes the entity's ownership, i.e. it is not its responsibility to
64  * delete it.
65  */
66  void addGlEntity(GlSimpleEntity *entity, const std::string &key);
67  /**
68  * @brief Remove entity with name : key
69  *
70  * The entity is not deleted
71  */
72  void deleteGlEntity(const std::string &key, bool informTheEntity = true);
73  /**
74  * @brief Remove given entity
75  *
76  * The entity is not deleted
77  */
78  void deleteGlEntity(GlSimpleEntity *entity, bool informTheEntity = true);
79  /**
80  * @brief Find name of given entity
81  */
82  std::string findKey(GlSimpleEntity *entity);
83  /**
84  * @brief Find entity with name : key
85  */
86  GlSimpleEntity *findGlEntity(const std::string &key);
87  /**
88  * @brief Return map of entities in composite
89  */
90  const std::map<std::string, GlSimpleEntity *> &getGlEntities() const {
91  return elements;
92  }
93 
94  /**
95  * @brief Set stencil number for all composite's children
96  *
97  * For more information on stencil :
98  * @see GlSimpleEntity
99  */
100  void setStencil(int stencil) override {
101  this->stencil = stencil;
102 
103  for (std::list<GlSimpleEntity *>::iterator it = _sortedElements.begin();
104  it != _sortedElements.end(); ++it) {
105  (*it)->setStencil(stencil);
106  }
107  }
108 
109  /**
110  * @brief Set if at the destruction of composite, components well be deleted
111  */
112  void setDeleteComponentsInDestructor(bool deleteComponentsInDestructor) {
113  this->deleteComponentsInDestructor = deleteComponentsInDestructor;
114  }
115 
116  /**
117  * @brief translate the composite with children
118  */
119  void translate(const Coord &mouvement) override;
120 
121  /**
122  * @brief Function to export data in outString (in XML format)
123  */
124  void getXML(std::string &outString) override;
125 
126  /**
127  * @brief Function to set data with inString (in XML format)
128  */
129  void setWithXML(const std::string &inString, unsigned int &currentPosition) override;
130 
131  ///@cond DOXYGEN_HIDDEN
132 
133  /**
134  * Function used to visit composite's children
135  */
136  void acceptVisitor(GlSceneVisitor *visitor) override {
137  // visitor->visit(this);
138  for (auto entity : _sortedElements) {
139  if (entity->isVisible()) {
140 
141 #ifndef NDEBUG
142  GlComposite *composite = dynamic_cast<GlComposite *>(entity);
143 
144  if (!composite && !entity->getBoundingBox().isValid()) {
145  for (auto &itE : elements) {
146  if (itE.second == entity) {
147  tlp::warning() << "Invalid bounding box for entity: " << itE.first << std::endl;
148  assert(false);
149  }
150  }
151  }
152 
153 #endif
154 
155  entity->acceptVisitor(visitor);
156  }
157  }
158  }
159 
160  /**
161  * Add a layer parent from this entity
162  */
163  virtual void addLayerParent(GlLayer *layer);
164 
165  /**
166  * Remove a layer parent from this entity
167  */
168  virtual void removeLayerParent(GlLayer *layer);
169 
170  /**
171  * Call when a child of the composite is modified
172  */
173  void notifyModified(GlSimpleEntity *entity);
174 
175  /**
176  * \warning This function does nothing, GlComposite is a GlSimpleEntity
177  * so the draw member function must be defined
178  */
179  void draw(float, Camera *) override {}
180 
181  ///@endcond
182 
183 protected:
184  std::map<std::string, GlSimpleEntity *> elements;
185  std::list<GlSimpleEntity *>
186  _sortedElements; // necessary to enable ordering of elements (for alpha blending)
187  std::vector<GlLayer *> layerParents;
188  bool deleteComponentsInDestructor;
189 };
190 } // namespace tlp
191 #endif
tlp::GlSimpleEntity::getBoundingBox
virtual BoundingBox getBoundingBox()
Return the entity boundingbox.
Definition: GlSimpleEntity.h:118
tlp::BoundingBox::isValid
bool isValid() const
Checks whether the bounding box's lowest point is less than it's highest point. "Less Than" means axi...
tlp::GlComposite::getGlEntities
const std::map< std::string, GlSimpleEntity * > & getGlEntities() const
Return map of entities in composite.
Definition: GlComposite.h:90
tlp::GlSimpleEntity
Base class for all Tulip OpenGL entities.
Definition: GlSimpleEntity.h:62
tlp::GlComposite::setDeleteComponentsInDestructor
void setDeleteComponentsInDestructor(bool deleteComponentsInDestructor)
Set if at the destruction of composite, components well be deleted.
Definition: GlComposite.h:112
tlp::GlComposite
GlSimpleEntity used to aggregate other GlEntity.
Definition: GlComposite.h:39
tlp
Definition: AbstractProperty.h:34
tlp::GlSimpleEntity::isVisible
bool isVisible() const
Return if entity is visible.
Definition: GlSimpleEntity.h:82
tlp::GlComposite::setStencil
void setStencil(int stencil) override
Set stencil number for all composite's children.
Definition: GlComposite.h:100