Tulip  5.4.0
Large graphs analysis and drawing
GlMainWidget.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 Tulip_GLMAINWIDGET_H
21 #define Tulip_GLMAINWIDGET_H
22 
23 #include <QOpenGLWidget>
24 
25 #include <tulip/tulipconf.h>
26 #include <tulip/GlScene.h>
27 #include <tulip/Graph.h>
28 
29 class QOpenGLFramebufferObject;
30 
31 namespace tlp {
32 
33 class View;
34 class GlCompositeHierarchyManager;
35 
36 /** @ingroup OpenGL
37  * @brief This widget provide a simple system to visualize data/graph with OpenGL 3D engine
38  *
39  * This widget is an interface between Qt Widget system and tulip OpenGL engine
40  * The central object of GlMainWidget is the GlScene member
41  * @see GlScene
42  *
43  * To use this class you have to :
44  * - create a GlMainWidget
45  * - get the GlScene with getScene() function
46  * - add GlLayer and GlEntity to this scene
47  * - call centerScene() to compute a good GlCamera
48  * - see the result
49  *
50  * @see GlLayer
51  * @see GlSimpleEntity
52  *
53  *
54  * If you only want to visualize a graph, you can call the setGraph function
55  *
56  *
57  * After scene construction you can perform some operation on GlMainWidget :
58  * - Selection with pickNodesEdges() and pickGlEntities()
59  * - Image output with getImage(), createPicture()
60  * - Texture output with createTexture()
61  * - others operation on GlScene and QGlWidget
62  */
63 class TLP_QT_SCOPE GlMainWidget : public QOpenGLWidget {
64  Q_OBJECT
65 
66 public:
67  /**
68  * @brief Configure the rendering process ( see render function)
69  * @see render
70  **/
72  RenderScene = 0x1, /** Force to render the graph even if there is a previous buffered render.
73  You need to call this option if the graph is updated to regenerate the
74  buffer. If not set try to use the last buffered graph render, if there is
75  no valid buffer this flag is forced. **/
76  SwapBuffers = 0x2 /** Call the swapBuffer functions at the end of the rendering process. If the
77  disabled it's up to you to call the swapBuffer function. **/
78  };
79  Q_DECLARE_FLAGS(RenderingOptions, RenderingOption)
80 
81  /**
82  * @brief Constructor of GlMainWidget
83  *
84  * Create a GlMainWidget with the GlScene associated to it
85  * @param parent Qt Widget parent system
86  * @param view if you want to link this GlMainWidget to a view : use this parameter
87  */
88  GlMainWidget(QWidget *parent = nullptr, View *view = nullptr);
89  ~GlMainWidget() override;
90 
91  /**
92  * @brief Get the GlScene of this GlMainWidget
93  * You have to add yours GlLayer and GlEntity to this GlScene
94  * At the construction this GlScene is empty
95  * @see GlScene
96  * @see GlScene::createLayer(const std::string &name)
97  * @see GlLayer::addGlEntity(GlSimpleEntity *entity,const std::string& name)
98  */
100  return &scene;
101  }
102 
103  /** @brief Select nodes and edges in a region of the screen
104  *
105  * Select all nodes and edges lying in the area of the screen of given width and height,
106  * and with its upper-left corner at (x,y)
107  * @param selectedNodes filled by the method with the nodes found in the region
108  * @param selectedEdges filled by the method with the edges found in the region
109  * @param layer specify the layer in which to perform the picking
110  * @param pickNodes enable or disable the picking of nodes
111  * @param pickEdges enable or disable the picking of edges
112  */
113  void pickNodesEdges(const int x, const int y, const int width, const int height,
114  std::vector<SelectedEntity> &selectedNodes,
115  std::vector<SelectedEntity> &selectedEdges, tlp::GlLayer *layer = nullptr,
116  bool pickNodes = true, bool pickEdges = true);
117 
118  /** @brief Select a node or edge at a screen point
119  * Try to select at point (x,y) a node in the first place then if no result try to select an edge
120  * @param type tells what has been found: NODE, EDGE
121  * @param layer specify the layer in which to perform the picking
122  * @param pickNodes enable or disable the picking of nodes
123  * @param pickEdges enable or disable the picking of edges
124  * @return true if something has been found, false otherwise
125  */
126  bool pickNodesEdges(const int x, const int y, SelectedEntity &selectedEntity,
127  tlp::GlLayer *layer = nullptr, bool pickNodes = true, bool pickEdges = true);
128 
129  /**
130  * @brief convert a screen measure into a viewport measure
131  * @param a measure in screen coordinates specified as an integer
132  * @return the converted measure in viewport coordinates as an integer
133  */
134  inline int screenToViewport(int l) const {
135  return l * devicePixelRatio();
136  }
137 
138  /**
139  * @brief convert a screen measure into a viewport measure
140  * @param a measure in screen coordinates specified as a double
141  * @return the converted measure in viewport coordinates as a double
142  */
143  inline double screenToViewport(double l) const {
144  return l * devicePixelRatio();
145  }
146 
147  /**
148  * @brief convert a screen point into a viewport point
149  * @param a point in screen coordinates
150  * @return the converted point in viewport coordinates
151  */
152  inline Coord screenToViewport(const Coord &point) const {
153  qreal dpr = devicePixelRatio();
154  return Coord(point.x() * dpr, point.y() * dpr);
155  }
156 
157  /**
158  * @brief convert a viewport measure into a screen measure
159  * @param a measure in viewport coordinates specified as a double
160  * @return the converted measure in screen coordinates as a double
161  */
162  inline double viewportToScreen(double l) const {
163  return l / devicePixelRatio();
164  }
165 
166  /**
167  * @brief convert a viewport point into a screen point
168  * @param a point in viewport coordinates
169  * @return the converted point in screen coordinates
170  */
171  inline Coord viewportToScreen(const Coord &point) const {
172  qreal dpr = devicePixelRatio();
173  return Coord(point.x() / dpr, point.y() / dpr);
174  }
175 
176  /**
177  * @brief Compute texture size in power of two with given height and width
178  * For example if you set width to 94 and height to 256, this function set textureRealWidth to 128
179  * and textureRealHeight to 256
180  */
181  static void getTextureRealSize(int width, int height, int &textureRealWidth,
182  int &textureRealHeight);
183 
184  /**
185  * @brief Take a snapshot of the Widget and put it in a picture
186  * @param width size
187  * @param height size
188  * @param center if true this function calls a centerScene() before picture output
189  */
190  void createPicture(const std::string &pictureName, int width, int height, bool center = true);
191 
192  /**
193  * Take a snapshot of the Widget and return it
194  * @param width size
195  * @param height size
196  * @param center if true this function calls a centerScene() before picture output
197  * @param format indicates the format of the created image
198  */
199  QImage createPicture(int width, int height, bool center = true,
200  QImage::Format format = QImage::Format_RGB32);
201 
202  /**
203  * @brief Function to do picking on entities in a screen region
204  * It just calls selectEntities on the GlScene instance.
205  * @param x screen coordinates
206  * @param y screen coordinates
207  * @param width screen size
208  * @param height screen size
209  * @param pickedEntities filled with entity under the selection screen rectangle
210  * @param layer if you want to do the selection only on one GlLayer
211  */
212  bool pickGlEntities(const int x, const int y, const int width, const int height,
213  std::vector<SelectedEntity> &pickedEntities, tlp::GlLayer *layer = nullptr);
214  /**
215  * @brief Function to do picking on entities.
216  * It just calls selectEntities on the GlScene instance with a small window of twelve pixels.
217  * @param x screen coordinates
218  * @param y screen coordinates
219  * @param pickedEntities filled with entity under the selection screen rectangle
220  * @param layer if you want to do the selection only on one GlLayer
221  */
222  bool pickGlEntities(const int x, const int y, std::vector<SelectedEntity> &pickedEntities,
223  tlp::GlLayer *layer = nullptr);
224 
225  /**
226  * Override default makeCurrent/doneCurrent behavior to activate deactivate
227  * adequate OpenGL context based on the QOpenGLWidget visibility
228  */
229  void makeCurrent();
230  void doneCurrent();
231 
232  /**
233  * Resize openGL view
234  */
235  void resizeGL(int w, int h) override;
236 
237  /**
238  * Compute interactors before drawing
239  */
240  void computeInteractors();
241 
242  /**
243  * Draw interactors
244  */
245  void drawInteractors();
246 
247  /**
248  * @brief This function performs all the rendering process of the graph.
249  * Use this function only for advanced purpose, if you want to perform simple rendering use the
250  *draw or redraw functions instead.
251  * @param options Configure the rendering process, see the RenderingOption documentation for more
252  *information on each rendering option effect.
253  * @see RenderingOption
254  * @param checkVisibility If check visibility is true : the engine check if GlMainWidget
255  *QWidget is visible. If false: the engine renders the scene in all cases
256  **/
257  void render(RenderingOptions options = RenderingOptions(RenderScene | SwapBuffers),
258  bool checkVisibility = true);
259 
260  /**
261  * @brief Specify if the scene point of view must be kept
262  * when changing between graphs belonging to the same hierarchy
263  */
264  void setKeepScenePointOfViewOnSubgraphChanging(bool);
265  /**
266  * @brief Returns if the scene point of view must be kept
267  * when changing between graphs belonging to the same hierarchy
268  */
269  bool keepScenePointOfViewOnSubgraphChanging() const;
270 
271 private:
272  void createFramebuffers(int width, int height);
273  void deleteFramebuffers();
274 
275  tlp::GlScene scene;
276  QRegion _visibleArea;
277  View *view;
278  int widthStored;
279  int heightStored;
280  bool frameBufferStored;
281  QOpenGLFramebufferObject *glFrameBuf, *glFrameBuf2;
282  static bool inRendering;
283  bool keepPointOfViewOnSubgraphChanging;
284  std::string sceneTextureId;
285 
286 public slots:
287  /**
288  * Draw the GlScene and the interactors
289  */
290  void draw(bool graphChanged = true);
291  /**
292  * This function is given for optimisation purpose only. If the hardware allows it,
293  * it enables to redraw only the Augmented display and the interactors and not the graph
294  * it is really useful for interactors such as zoom box etc..
295  * Warning, if you change the graph or the properties of element (Colors, size, etc...)
296  * applying that function will not display the change, in that case, use the draw function.
297  */
298  void redraw();
299 
300  void closeEvent(QCloseEvent *e) override;
301 
302  /**
303  * @brief Convenience function that calls center function on the current scene, applies a zoom (if
304  *needed) and draws the view.
305  * Same thing than
306  * @code
307  * getScene()->centerScene();
308  * getScene()->zoomFactor();
309  * draw();
310  * @endcode
311  **/
312  void centerScene(bool graphChanged = false, float zoomFactor = 1.0);
313 
314  void emitGraphChanged();
315 
316 protected slots:
317  void paintEvent(QPaintEvent *) override;
318 
319 signals:
320  /**
321  * This signal is emitted when the GlMainWidget will be deleted
322  */
323  void closing(GlMainWidget *, QCloseEvent *);
324 
325  /**
326  * This signal is emitted when GlMainWidget::redraw() is call
327  */
328  void viewRedrawn(GlMainWidget *glWidget);
329  /**
330  * This signal is emitted when GlMainWidget::draw() is call
331  */
332  void viewDrawn(GlMainWidget *glWidget, bool graphChanged);
333 
334  void glResized(int w, int h);
335 
336  void graphChanged();
337 };
338 } // namespace tlp
339 
340 #endif
tlp::GlScene * getScene()
Get the GlScene of this GlMainWidget You have to add yours GlLayer and GlEntity to this GlScene At th...
Definition: GlMainWidget.h:99
double viewportToScreen(double l) const
convert a viewport measure into a screen measure
Definition: GlMainWidget.h:162
Coord viewportToScreen(const Coord &point) const
convert a viewport point into a screen point
Definition: GlMainWidget.h:171
Coord screenToViewport(const Coord &point) const
convert a screen point into a viewport point
Definition: GlMainWidget.h:152
Tulip scene class.
Definition: GlScene.h:160
int screenToViewport(int l) const
convert a screen measure into a viewport measure
Definition: GlMainWidget.h:134
double screenToViewport(double l) const
convert a screen measure into a viewport measure
Definition: GlMainWidget.h:143
View plugins provide a way to dynamically add to a Tulip plateform various ways to visualize a graph...
Definition: View.h:95
Structure to store selected entities.
Definition: GlScene.h:48
RenderingOption
Configure the rendering process ( see render function)
Definition: GlMainWidget.h:71
A GlLayer is like an 2D drawing software layer system.
Definition: GlLayer.h:54
This widget provide a simple system to visualize data/graph with OpenGL 3D engine.
Definition: GlMainWidget.h:63