Tulip  6.0.0
Large graphs analysis and drawing
GlMainWidget.h
1 /*
2  *
3  * This file is part of Tulip (https://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  */
99  tlp::GlScene *getScene() {
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  // devicePixelRatio() must be redefined because the inherited
130  // method returns 1 when the widget is no attached to a window
131  qreal devicePixelRatio() const {
132  return wdpr->devicePixelRatio();
133  }
134 
135  /**
136  * @brief convert a screen measure into a viewport measure
137  * @param a measure in screen coordinates specified as an integer
138  * @return the converted measure in viewport coordinates as an integer
139  */
140  inline int screenToViewport(int l) const {
141  return l * devicePixelRatio();
142  }
143 
144  /**
145  * @brief convert a screen measure into a viewport measure
146  * @param a measure in screen coordinates specified as a double
147  * @return the converted measure in viewport coordinates as a double
148  */
149  inline double screenToViewport(double l) const {
150  return l * devicePixelRatio();
151  }
152 
153  /**
154  * @brief convert a screen point into a viewport point
155  * @param a point in screen coordinates
156  * @return the converted point in viewport coordinates
157  */
158  inline Coord screenToViewport(const Coord &point) const {
159  qreal dpr = devicePixelRatio();
160  return Coord(point.x() * dpr, point.y() * dpr);
161  }
162 
163  /**
164  * @brief convert a viewport measure into a screen measure
165  * @param a measure in viewport coordinates specified as a double
166  * @return the converted measure in screen coordinates as a double
167  */
168  inline double viewportToScreen(double l) const {
169  return l / devicePixelRatio();
170  }
171 
172  /**
173  * @brief convert a viewport point into a screen point
174  * @param a point in viewport coordinates
175  * @return the converted point in screen coordinates
176  */
177  inline Coord viewportToScreen(const Coord &point) const {
178  qreal dpr = devicePixelRatio();
179  return Coord(point.x() / dpr, point.y() / dpr);
180  }
181 
182  /**
183  * @brief Compute texture size in power of two with given height and width
184  * For example if you set width to 94 and height to 256, this function set textureRealWidth to 128
185  * and textureRealHeight to 256
186  */
187  static void getTextureRealSize(int width, int height, int &textureRealWidth,
188  int &textureRealHeight);
189 
190  /**
191  * @brief Take a snapshot of the Widget and put it in a picture
192  * @param width size
193  * @param height size
194  * @param center if true this function calls a centerScene() before picture output
195  */
196  void createPicture(const std::string &pictureName, int width, int height, bool center = true);
197 
198  /**
199  * Take a snapshot of the Widget and return it
200  * @param width size
201  * @param height size
202  * @param center if true this function calls a centerScene() before picture output
203  * @param format indicates the format of the created image
204  */
205  QImage createPicture(int width, int height, bool center = true,
206  QImage::Format format = QImage::Format_RGB32);
207 
208  /**
209  * @brief Function to do picking on entities in a screen region
210  * It just calls selectEntities on the GlScene instance.
211  * @param x screen coordinates
212  * @param y screen coordinates
213  * @param width screen size
214  * @param height screen size
215  * @param pickedEntities filled with entity under the selection screen rectangle
216  * @param layer if you want to do the selection only on one GlLayer
217  */
218  bool pickGlEntities(const int x, const int y, const int width, const int height,
219  std::vector<SelectedEntity> &pickedEntities, tlp::GlLayer *layer = nullptr);
220  /**
221  * @brief Function to do picking on entities.
222  * It just calls selectEntities on the GlScene instance with a small window of twelve pixels.
223  * @param x screen coordinates
224  * @param y screen coordinates
225  * @param pickedEntities filled with entity under the selection screen rectangle
226  * @param layer if you want to do the selection only on one GlLayer
227  */
228  bool pickGlEntities(const int x, const int y, std::vector<SelectedEntity> &pickedEntities,
229  tlp::GlLayer *layer = nullptr);
230 
231  /**
232  * Override default makeCurrent/doneCurrent behavior to activate deactivate
233  * adequate OpenGL context based on the QOpenGLWidget visibility
234  */
235  void makeCurrent();
236  void doneCurrent();
237 
238  /**
239  * Indicates if this is associated to the current OpenGL context
240  */
241  bool isCurrent();
242 
243  /**
244  * Resize openGL view
245  */
246  void resizeGL(int w, int h) override;
247 
248  /**
249  * Compute interactors before drawing
250  */
252 
253  /**
254  * Draw interactors
255  */
257 
258  /**
259  * @brief This function performs all the rendering process of the graph.
260  * Use this function only for advanced purpose, if you want to perform simple rendering use the
261  *draw or redraw functions instead.
262  * @param options Configure the rendering process, see the RenderingOption documentation for more
263  *information on each rendering option effect.
264  * @see RenderingOption
265  * @param checkVisibility If check visibility is true : the engine check if GlMainWidget
266  *QWidget is visible. If false: the engine renders the scene in all cases
267  **/
268  void render(RenderingOptions options = RenderingOptions(RenderScene | SwapBuffers),
269  bool checkVisibility = true);
270 
271  /**
272  * @brief Specify if the scene point of view must be kept
273  * when changing between graphs belonging to the same hierarchy
274  */
276  /**
277  * @brief Returns if the scene point of view must be kept
278  * when changing between graphs belonging to the same hierarchy
279  */
281 
282 private:
283  void createFramebuffers(int width, int height);
284  void deleteFramebuffers();
285 
286  tlp::GlScene scene;
287  QRegion _visibleArea;
288  View *view;
289  QWidget *wdpr;
290  int widthStored;
291  int heightStored;
292  QOpenGLFramebufferObject *glFrameBuf, *glFrameBuf2;
293  static bool inRendering;
294  bool keepPointOfViewOnSubgraphChanging;
295  std::string sceneTextureId;
296 
297 public slots:
298  /**
299  * Draw the GlScene and the interactors
300  */
301  void draw(bool graphChanged = true);
302  /**
303  * This function is given for optimisation purpose only. If the hardware allows it,
304  * it enables to redraw only the Augmented display and the interactors and not the graph
305  * it is really useful for interactors such as zoom box etc..
306  * Warning, if you change the graph or the properties of element (Colors, size, etc...)
307  * applying that function will not display the change, in that case, use the draw function.
308  */
309  void redraw();
310 
311  void closeEvent(QCloseEvent *e) override;
312 
313  /**
314  * @brief Convenience function that calls center function on the current scene, applies a zoom (if
315  *needed) and draws the view.
316  * Same thing than
317  * @code
318  * getScene()->centerScene();
319  * getScene()->zoomFactor();
320  * draw();
321  * @endcode
322  **/
323  void centerScene(bool graphChanged = false, float zoomFactor = 1.0);
324 
325  void emitGraphChanged();
326 
327 protected slots:
328  void paintEvent(QPaintEvent *) override;
329 
330 signals:
331  /**
332  * This signal is emitted when the GlMainWidget will be deleted
333  */
334  void closing(GlMainWidget *, QCloseEvent *);
335 
336  /**
337  * This signal is emitted when GlMainWidget::redraw() is call
338  */
339  void viewRedrawn(GlMainWidget *glWidget);
340  /**
341  * This signal is emitted when GlMainWidget::draw() is call
342  */
343  void viewDrawn(GlMainWidget *glWidget, bool graphChanged);
344 
345  void glResized(int w, int h);
346 
347  void graphChanged();
348 };
349 } // namespace tlp
350 
351 #endif
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
QImage createPicture(int width, int height, bool center=true, QImage::Format format=QImage::Format_RGB32)
void render(RenderingOptions options=RenderingOptions(RenderScene|SwapBuffers), bool checkVisibility=true)
This function performs all the rendering process of the graph. Use this function only for advanced pu...
void computeInteractors()
void setKeepScenePointOfViewOnSubgraphChanging(bool)
Specify if the scene point of view must be kept when changing between graphs belonging to the same hi...
double screenToViewport(double l) const
convert a screen measure into a viewport measure
Definition: GlMainWidget.h:149
void draw(bool graphChanged=true)
Coord viewportToScreen(const Coord &point) const
convert a viewport point into a screen point
Definition: GlMainWidget.h:177
void viewDrawn(GlMainWidget *glWidget, bool graphChanged)
void createPicture(const std::string &pictureName, int width, int height, bool center=true)
Take a snapshot of the Widget and put it in a picture.
Coord screenToViewport(const Coord &point) const
convert a screen point into a viewport point
Definition: GlMainWidget.h:158
void pickNodesEdges(const int x, const int y, const int width, const int height, std::vector< SelectedEntity > &selectedNodes, std::vector< SelectedEntity > &selectedEdges, tlp::GlLayer *layer=nullptr, bool pickNodes=true, bool pickEdges=true)
Select nodes and edges in a region of the screen.
void resizeGL(int w, int h) override
static void getTextureRealSize(int width, int height, int &textureRealWidth, int &textureRealHeight)
Compute texture size in power of two with given height and width For example if you set width to 94 a...
bool keepScenePointOfViewOnSubgraphChanging() const
Returns if the scene point of view must be kept when changing between graphs belonging to the same hi...
int screenToViewport(int l) const
convert a screen measure into a viewport measure
Definition: GlMainWidget.h:140
void centerScene(bool graphChanged=false, float zoomFactor=1.0)
Convenience function that calls center function on the current scene, applies a zoom (if needed) and ...
RenderingOption
Configure the rendering process ( see render function)
Definition: GlMainWidget.h:71
void viewRedrawn(GlMainWidget *glWidget)
double viewportToScreen(double l) const
convert a viewport measure into a screen measure
Definition: GlMainWidget.h:168
bool pickNodesEdges(const int x, const int y, SelectedEntity &selectedEntity, tlp::GlLayer *layer=nullptr, bool pickNodes=true, bool pickEdges=true)
Select a node or edge at a screen point Try to select at point (x,y) a node in the first place then i...
void closing(GlMainWidget *, QCloseEvent *)
bool pickGlEntities(const int x, const int y, std::vector< SelectedEntity > &pickedEntities, tlp::GlLayer *layer=nullptr)
Function to do picking on entities. It just calls selectEntities on the GlScene instance with a small...
bool pickGlEntities(const int x, const int y, const int width, const int height, std::vector< SelectedEntity > &pickedEntities, tlp::GlLayer *layer=nullptr)
Function to do picking on entities in a screen region It just calls selectEntities on the GlScene ins...
Tulip scene class.
Definition: GlScene.h:144
View plugins provide a way to dynamically add to a Tulip plateform various ways to visualize a graph.
Definition: View.h:94
Structure to store selected entities.
Definition: GlScene.h:50