Tulip  5.6.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  * Indicates if this is associated to the current OpenGL context
234  */
235  bool isCurrent();
236 
237  /**
238  * Resize openGL view
239  */
240  void resizeGL(int w, int h) override;
241 
242  /**
243  * Compute interactors before drawing
244  */
245  void computeInteractors();
246 
247  /**
248  * Draw interactors
249  */
250  void drawInteractors();
251 
252  /**
253  * @brief This function performs all the rendering process of the graph.
254  * Use this function only for advanced purpose, if you want to perform simple rendering use the
255  *draw or redraw functions instead.
256  * @param options Configure the rendering process, see the RenderingOption documentation for more
257  *information on each rendering option effect.
258  * @see RenderingOption
259  * @param checkVisibility If check visibility is true : the engine check if GlMainWidget
260  *QWidget is visible. If false: the engine renders the scene in all cases
261  **/
262  void render(RenderingOptions options = RenderingOptions(RenderScene | SwapBuffers),
263  bool checkVisibility = true);
264 
265  /**
266  * @brief Specify if the scene point of view must be kept
267  * when changing between graphs belonging to the same hierarchy
268  */
269  void setKeepScenePointOfViewOnSubgraphChanging(bool);
270  /**
271  * @brief Returns if the scene point of view must be kept
272  * when changing between graphs belonging to the same hierarchy
273  */
274  bool keepScenePointOfViewOnSubgraphChanging() const;
275 
276 private:
277  void createFramebuffers(int width, int height);
278  void deleteFramebuffers();
279 
280  tlp::GlScene scene;
281  QRegion _visibleArea;
282  View *view;
283  int widthStored;
284  int heightStored;
285  bool frameBufferStored;
286  QOpenGLFramebufferObject *glFrameBuf, *glFrameBuf2;
287  static bool inRendering;
288  bool keepPointOfViewOnSubgraphChanging;
289  std::string sceneTextureId;
290 
291 public slots:
292  /**
293  * Draw the GlScene and the interactors
294  */
295  void draw(bool graphChanged = true);
296  /**
297  * This function is given for optimisation purpose only. If the hardware allows it,
298  * it enables to redraw only the Augmented display and the interactors and not the graph
299  * it is really useful for interactors such as zoom box etc..
300  * Warning, if you change the graph or the properties of element (Colors, size, etc...)
301  * applying that function will not display the change, in that case, use the draw function.
302  */
303  void redraw();
304 
305  void closeEvent(QCloseEvent *e) override;
306 
307  /**
308  * @brief Convenience function that calls center function on the current scene, applies a zoom (if
309  *needed) and draws the view.
310  * Same thing than
311  * @code
312  * getScene()->centerScene();
313  * getScene()->zoomFactor();
314  * draw();
315  * @endcode
316  **/
317  void centerScene(bool graphChanged = false, float zoomFactor = 1.0);
318 
319  void emitGraphChanged();
320 
321 protected slots:
322  void paintEvent(QPaintEvent *) override;
323 
324 signals:
325  /**
326  * This signal is emitted when the GlMainWidget will be deleted
327  */
328  void closing(GlMainWidget *, QCloseEvent *);
329 
330  /**
331  * This signal is emitted when GlMainWidget::redraw() is call
332  */
333  void viewRedrawn(GlMainWidget *glWidget);
334  /**
335  * This signal is emitted when GlMainWidget::draw() is call
336  */
337  void viewDrawn(GlMainWidget *glWidget, bool graphChanged);
338 
339  void glResized(int w, int h);
340 
341  void graphChanged();
342 };
343 } // namespace tlp
344 
345 #endif
tlp::GlLayer
A GlLayer is like an 2D drawing software layer system.
Definition: GlLayer.h:54
tlp::GlMainWidget::screenToViewport
double screenToViewport(double l) const
convert a screen measure into a viewport measure
Definition: GlMainWidget.h:143
tlp::GlMainWidget::RenderingOption
RenderingOption
Configure the rendering process ( see render function)
Definition: GlMainWidget.h:71
tlp::GlMainWidget::getScene
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
tlp::GlMainWidget::viewportToScreen
double viewportToScreen(double l) const
convert a viewport measure into a screen measure
Definition: GlMainWidget.h:162
tlp::SelectedEntity
Structure to store selected entities.
Definition: GlScene.h:50
tlp::GlMainWidget::viewportToScreen
Coord viewportToScreen(const Coord &point) const
convert a viewport point into a screen point
Definition: GlMainWidget.h:171
tlp
Definition: AbstractProperty.h:34
tlp::GlScene
Tulip scene class.
Definition: GlScene.h:144
tlp::GlMainWidget::screenToViewport
int screenToViewport(int l) const
convert a screen measure into a viewport measure
Definition: GlMainWidget.h:134
tlp::View
View plugins provide a way to dynamically add to a Tulip plateform various ways to visualize a graph.
Definition: View.h:95
tlp::GlMainWidget
This widget provide a simple system to visualize data/graph with OpenGL 3D engine.
Definition: GlMainWidget.h:63
tlp::GlMainWidget::screenToViewport
Coord screenToViewport(const Coord &point) const
convert a screen point into a viewport point
Definition: GlMainWidget.h:152