Tulip  6.0.0
Large graphs analysis and drawing
View.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 #ifndef VIEW_H
20 #define VIEW_H
21 
22 #include <unordered_set>
23 #include <list>
24 
25 #include <QObject>
26 #include <QSize>
27 
28 #include <tulip/tulipconf.h>
29 #include <tulip/Edge.h>
30 #include <tulip/Interactor.h>
31 #include <tulip/Observable.h>
32 #include <tulip/Plugin.h>
33 
34 class QGraphicsView;
35 class QGraphicsItem;
36 class QWidget;
37 class QPixmap;
38 class QMenu;
39 class QPointF;
40 class QPoint;
41 
42 namespace tlp {
43 static const std::string VIEW_CATEGORY = "Panel";
44 
45 class GlMainWidget;
46 class ViewToolTipAndUrlManager;
47 
48 /**
49  @ingroup Plugins
50 
51  @brief View plugins provide a way to dynamically add to a Tulip plateform various ways to
52  visualize a graph.
53 
54  A view takes the following elements as inputs:
55  @li A graph which contains the data to be displayed.
56  @li A state (@c tlp::DataSet) which contains initialization parameters.
57 
58  As an output, a View must provide a @c QGraphicsView instance where all drawing is done.
59  User interaction on a View is handled by the tlp::Interactor class. Several interactors can be
60  installed on the same view but there can be only one active interactor at the same time.
61  In the end, the view can provide several configuration widgets used to set up additional
62  parameters.
63 
64  When a View gets created, the following methods will always be called in the following order:
65  @li The constructor. Basically, you don't want to do anything in this method as View instance may
66  be created at Tulip startup when the plugin system gets initialized. Subsequent methods will be
67  called in order for the view to build UI elements
68  @li View::setupUi(). Notifies the view it can now build GUI components since every part of its
69  initial state should be valid by now. Once this method is called, any call to View::graphicsView()
70  is expected to return a valid pointer object.
71  @li View::setGraph. Sets the graph that is meant to be visualized in the View's panel.
72  @li View::setState(). Sets initial data. This method may be used to restore a previously backed-up
73  state retrieved from the View::state() method.
74  @li View::interactorsInstalled(). Notifies the view of the available interactors. Interactors
75  objects taken from the list have already been initialized.
76 
77  Once the View is initialized, none of the previously mentioned methods, except View::setGraph(),
78  can be called again.
79  View::setGraph method may be called again to notify the view that another graph should be
80  displayed (this may be a sub/parent graph of the previously displayed graph or a graph coming from
81  a totally different hierarchy)
82 
83  Views are meant to be managed by an overleying system. As a consequence, a view may not decide
84  directly when to redraw.
85  Thus, you should never call the View::draw() method. To notify the overleying system that your
86  view needs to be redrawn, emit the View::drawNeeded() signal instead.
87 
88  A tlp::View subclass automatically inherits from the tlp::Observable interface. The tlp::View
89  interface also automatically listn to its active graph to trigger handling trigger when this graph
90  gets deleted.
91  When the graph associated to a View gets deleted, the View::graphDeleted() callback is triggered.
92  @see graphDeleted() for more information.
93  */
94 class TLP_QT_SCOPE View : public QObject, public tlp::Plugin, public tlp::Observable {
95  Q_OBJECT
96 
97  std::list<tlp::Interactor *> _interactors;
98  tlp::Interactor *_currentInteractor;
99  tlp::Graph *_graph;
100  tlp::ViewToolTipAndUrlManager *_tturlManager;
101  bool interactorsActivated;
102 
103  std::list<tlp::Observable *> _triggers;
104  bool _displayContextMenu;
105 
106 public:
107  /**
108  @brief Default constructor
109  @warning Code of this method should almost be a no-op. Subsequent calls on other methods should
110  allow you to setup your view.
111  */
112  View();
113 
114  /**
115  @brief Destructor
116  View's GUI components (graphics view, configuration widgets) responsibility belongs to the
117  overleying system. Thus, the View is not in charge of deleting its graphics view.
118  View's interactors are already deleted in the top class.
119  */
120  ~View() override;
121 
122  std::string category() const override {
123  return VIEW_CATEGORY;
124  }
125  std::string icon() const override {
126  return ":/tulip/gui/icons/32/plugin_view.png";
127  }
128 
129  /**
130  @return the View's panel as a @c QGraphicsView instance.
131  @note This method MUST ALWAYS return the same instance of a QGraphicsView.
132  */
133  virtual QGraphicsView *graphicsView() const = 0;
134  // Following commit #10531
135  // (see void WorkspacePanel::showEvent(QShowEvent *event);)
136  // this method is called when creating a new QGraphicsScene
137  // to restore any specific behaviour in user made graph views
138  virtual void resetGraphicsScene() {}
139 
140  // this method indicates if the view needs a rebuild of the scene
141  // when it is shown (see void WorkspacePanel::showEvent(QShowEvent *event);
142  // defaut is no rebuild
143  virtual bool rebuildSceneOnShowEvent() {
144  return false;
145  }
146 
147  // this method indicates if the view needs to be centered
148  // when added to a Workspace
149  // default is centered
150  virtual bool centeredWhenAddedToWorkspace() {
151  return true;
152  }
153 
154  /**
155  @return The list of interactors installed on this view.
156  The list is always the same as the one given when View::setInteractors() was called.
157  @see setInteractors();
158  */
159  inline const std::list<Interactor *> &interactors() const {
160  return _interactors;
161  }
162 
163  /**
164  @return The currently active interactor.
165  The active interactor is the one that currently receive user inputs.
166  @see setCurrentInteractor();
167  @warning This method may return a nullptr pointer if no interactor is currently active.
168  */
170 
171  /**
172  * @brief interactorsEnabled indicates if interactors are enabled or not
173  * @return true if interactors are enabled, false instead
174  */
175  virtual bool interactorsEnabled() const {
176  return interactorsActivated;
177  }
178 
179  /**
180  @return a list of widgets that can be used to set up the view.
181  Since several widgets can be retrieved, user will be able to select them from a combo box where
182  each widget will be identified by its windowsTitle.
183  @see View::applySettings()
184  @warning This method must not instantiate configuration widgets on the fly.
185  */
186  virtual std::list<QWidget *> configurationWidgets() const;
187 
188  /**
189  @brief Backup the state of the view.
190  This method is used to restore the View's parameters when it's re-opened.
191  */
192  virtual tlp::DataSet state() const;
193 
194  /**
195  @return the graph displayed by the view.
196  @note This method MUST return the same graph pointer that was previously passed down to
197  setGraph.
198  */
199  tlp::Graph *graph() const;
200 
201  /**
202  @return The list of currently registered triggers.
203  @see View::addRedrawTrigger()
204  */
205  std::list<tlp::Observable *> triggers() const;
206 
207  /**
208  @brief reimplemented from tlp::Observable to provide the triggers mechanism.
209  @see View::addRedrawTrigger()
210  */
211  void treatEvents(const std::vector<Event> &events) override;
212 
213  /**
214  @brief defines which item is considered as the central item in the view.
215  The central item is considered to be a background item that will be set as parent of every
216  graphics item added by the workspace into the view.
217  By default, this method returns nullptr, which means that no central item is defined.
218  */
219  virtual QGraphicsItem *centralItem() const;
220 
221  /**
222  @brief Takes a snapshot of the view's screen and saves it into the given pixmap.
223  The snapshot is scaled to outputSize. If a null size is given, the snapshot is to be on a 1:1
224  ratio
225  @return A non-null pixmap of the snapshot was correctly taken.
226  */
227  virtual QPixmap snapshot(const QSize &outputSize = QSize()) const = 0;
228 
229  /**
230  * @brief This method is called whenever the context menu is required on the view.
231  * @param point The screen coordinates where the context menu should be displayed.
232  @return true or false whether the context menu has been shown or not
233  */
234  bool showContextMenu(const QPoint &point, const QPointF &scenePoint);
235 
236  /**
237  * @brief This method allows to control the display of the context menu.
238  * @param show a bool indicating if the context menu must be displayed or not.
239  */
240  void setShowContextMenu(bool show) {
241  _displayContextMenu = show;
242  }
243 
244 public slots:
245  /**
246  * @brief This method is a callback to notify the panel that the pop() method (undo) has just been
247  *called on the graph.
248  * By default, this method will make a call to centerView()
249  **/
250  virtual void undoCallback();
251 
252  /**
253  @brief This method applies settings changed in the configuration widgets
254  This method may be called from the overleying system in various situations. The View is expected
255  to apply settings in an optimized way to prevent extra redraws.
256  By default, this method does nothing.
257  */
258  virtual void applySettings();
259 
260  /**
261  @brief Reset the visualization to the center.
262  This method is called after major changes into the data structure. At this point, the user point
263  of view should be reset and brought back to a point where all the data can be seen.
264  @note It is expected for the view to be redrawn when calling centerView
265  For a 3D visualization, this method could be implemented by centering the camera. For a table,
266  this could be done by setting the scroll bar to the top position etc...
267  By default, this method calls draw().
268  */
269  virtual void centerView(bool graphChanged = false);
270 
271  /**
272  @brief defines the list of interactors available on this View
273  @note Calling this will trigger the View::interactorsInstalled() callback for custom handling.
274  */
275  virtual void setInteractors(const std::list<tlp::Interactor *> &);
276 
277  /**
278  @brief defines the active interactor that will receive user inputs.
279  @note This method will first remove the previously active interactor (if any) using
280  Interactor::uninstall()
281  @note Calling this will trigger the View::currentInteractorChanged() callback for custom
282  handling.
283  @note Calling View::setCurrentInteractor(nullptr) will only remove the previous current
284  interactor.
285  */
286  void setCurrentInteractor(tlp::Interactor *currentInteractor);
287 
288  /**
289  @brief Restores the state of the view.
290  DataSet passed down to this method can come from a previous backup or be generated by the
291  overlaying system. It's up to the view to use this data or not.
292  */
293  virtual void setState(const tlp::DataSet &);
294 
295  /**
296  @brief Defines the graph that should be displayed by the View
297  @note Calling setGraph triggers the View::graphChanged() callback for custom handling.
298  @warning This method and its subsequent callback might be called several times.
299  */
300  void setGraph(tlp::Graph *graph);
301 
302  /**
303  @brief Asks the view to draw.
304  A call to draw() means that internal data has most probably been modified and that the View
305  should take that into account when drawing.
306  */
307  virtual void draw() = 0;
308 
309  /**
310  @brief Refresh the View's panel.
311  Calling refresh() means that no internal data has been modified. This can happen when the view's
312  panel gets resized, restored etc
313  */
314  inline virtual void refresh() {
315  draw();
316  }
317 
318  /**
319  @brief Sets up GUI elements belonging to the View.
320  This method is called once the initial state as been set (using setGraph and setState) and is
321  called only once.
322  */
323  virtual void setupUi() = 0;
324 
325  /**
326  @brief This method is inherited from tlp::Observable and allows the view to trigger custom
327  callback when its associated graph gets deleted.
328  @warning When overriding this method. You MUST always make a call to View::treatEvent before
329  doing anything in order to keep this callback working.
330  */
331  void treatEvent(const Event &) override;
332 
333  /**
334  @brief Registers a new trigger for automatic view drawing.
335  Triggers are tlp::Observable subclasses. Once registered, the view will listen to the trigger's
336  events and emit the drawNeeded signal each time the Observable::treatEvents() callback is run.
337  For more information about the Observable system, @see tlp::Observable
338 
339  @note This is a convenience function. However, using triggers prevent from performign extra
340  checks on the data structure to know if a redraw must me made or not. For more control over
341  event handling, you will have to implement your own treatEvent/treatEvents callback.
342  @warning If your tlp::View subclass overloads the treatEvents method. You must make sure to call
343  the View::treatEvents method in order to keep the triggers system working.
344  */
346 
347  /**
348  @brief Removes a trigger from the list of registered triggers. Event coming from this trigger
349  will no longer trigger the drawNeeded signal.
350  @see View::addRedrawTrigger()
351  */
353 
354  /**
355  @brief Clears the list of attached triggers
356  This method removes all triggers associated to the View.
357  @note From the moment this method is called, no update on previous triggers will be considered.
358  Even if this is called during an Observable::holdObservers()
359  */
361 
362  /**
363  @brief This function emit the signal drawNeeded
364  */
366 
367  /**
368  @brief allow to add some check when a user want to close a view.
369  @return true if the view can be closed, false if not
370  */
371  virtual bool checkOnClose() {
372  return true;
373  }
374 
375  /**
376  * @brief indicate which node or edge is under the (x, y) position in graphicsView()->viewport()
377  * @param x the x axis coordinate
378  * @param y the y axis coordinate
379  * @param n on return will give the found node
380  * @param e on return will give the found edge
381  @return true if a node or edge has been found, false if not
382  */
383  virtual bool getNodeOrEdgeAtViewportPos(int /*x*/, int /*y*/, node & /*n*/, edge & /*e*/) const {
384  return false;
385  }
386 
387 signals:
388  /**
389  @brief Inform the overlying subsystem that this view needs to be drawn.
390  @note Depending on the overlying implementation, a subsequent call to draw might not be
391  immediate.
392  */
393  void drawNeeded();
394 
395  /**
396  @brief Emitted after the setGraph method has been called.
397  @note This signal is emitted from the non-virtual View::setGraph() method thus cannot be
398  prevented.
399  */
401 
402  void interactorsChanged();
403 
404 protected:
405  /**
406  @brief Save view state associated to the current graph
407  */
408  void saveState();
409  /**
410  @brief retrieve the view saved state associated to the graph
411  @note if there is no saved state for the graph,
412  the saved state of the nearest parent graph which have a saved state
413  will be returned or an empty DataSet if not.
414  */
416 
417 protected slots:
418  /**
419  @brief Callback method after setInteractors() was called.
420  At this point, a call to View::interactors() is considered valid.
421  */
422  virtual void interactorsInstalled(const std::list<tlp::Interactor *> &interactors);
423 
424  /**
425  @brief Callback method after setCurrentInteractor() was called.
426  At this point, a call to View::currentInteractor() is considered valid and return the newly
427  active interactor.
428  @warning The interactor passed down to this method MAY BE a nullptr pointer ! This means that no
429  current interactor should be set.
430  */
432 
433  /**
434  * @brief Activate or deactivate interactors in the view
435  * @param activate: set to true (resp. false) to enable (resp. disable) interactors
436  * @param exceptions: a set of interactor names whose bevahior has to be left unchanged
437  */
438  void toggleInteractors(const bool activate, const std::unordered_set<const char *> &exceptions);
439 
440  /**
441  @brief Callback method after setGraph() was called.
442  At this point, a call to View::graph() is considered valid and return the lastly set graph.
443  */
444  virtual void graphChanged(tlp::Graph *) = 0;
445 
446  /**
447  @brief Called when the graph associated to the view gets deleted.
448  This method should call setGraph to input a new graph pointer (nullptr or valid)
449  @param parentGraph The parent of the graph that was just deleted. If there is no parent
450  available (eg. the graph was root), parentGraph is nullptr
451  */
452  virtual void graphDeleted(tlp::Graph *parentGraph) = 0;
453 
454  /**
455  * @brief fills the context menu with entries related to the view.
456  * This method is called whenever the context menu is displayed on the panel.
457  * @param QMenu The popup menu that will be displayed. This menu should be populated with context
458  * action related to the panel.
459  */
460  virtual void fillContextMenu(QMenu *, const QPointF &);
461 
462  /**
463  * @brief fills the context menu with entries related to the node.
464  */
465  void fillContextMenu(QMenu *menu, node n);
466 
467  /**
468  * @brief fills the context menu with entries related to the edge.
469  */
470  void fillContextMenu(QMenu *menu, edge e);
471 
472  /**
473  * @brief activate the management of tooltips and urls
474  * through the context menu
475  * @param Qwidget the widget to manage.
476  */
478 };
479 } // namespace tlp
480 
481 #endif /* VIEW_H_ */
A container that can store data from any type.
Definition: DataSet.h:195
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:52
Interactor provides a way to handle user inputs over a view. Basically, The interactor class is an ov...
Definition: Interactor.h:62
The Observable class is the base of Tulip's observation system.
Definition: Observable.h:127
Top-level interface for plug-ins.
Definition: Plugin.h:86
View plugins provide a way to dynamically add to a Tulip plateform various ways to visualize a graph.
Definition: View.h:94
DataSet getState(Graph *graph)
retrieve the view saved state associated to the graph
virtual QGraphicsView * graphicsView() const =0
void activateTooltipAndUrlManager(QWidget *)
activate the management of tooltips and urls through the context menu
void toggleInteractors(const bool activate, const std::unordered_set< const char * > &exceptions)
Activate or deactivate interactors in the view.
void fillContextMenu(QMenu *menu, node n)
fills the context menu with entries related to the node.
~View() override
Destructor View's GUI components (graphics view, configuration widgets) responsibility belongs to the...
virtual void draw()=0
Asks the view to draw. A call to draw() means that internal data has most probably been modified and ...
virtual QPixmap snapshot(const QSize &outputSize=QSize()) const =0
Takes a snapshot of the view's screen and saves it into the given pixmap. The snapshot is scaled to o...
virtual void fillContextMenu(QMenu *, const QPointF &)
fills the context menu with entries related to the view. This method is called whenever the context m...
virtual void setupUi()=0
Sets up GUI elements belonging to the View. This method is called once the initial state as been set ...
virtual bool getNodeOrEdgeAtViewportPos(int, int, node &, edge &) const
indicate which node or edge is under the (x, y) position in graphicsView()->viewport()
Definition: View.h:383
std::string icon() const override
The icon (preferably a thumbnail) of the plugin.
Definition: View.h:125
virtual void setInteractors(const std::list< tlp::Interactor * > &)
defines the list of interactors available on this View
void setGraph(tlp::Graph *graph)
Defines the graph that should be displayed by the View.
virtual void refresh()
Refresh the View's panel. Calling refresh() means that no internal data has been modified....
Definition: View.h:314
virtual void graphDeleted(tlp::Graph *parentGraph)=0
Called when the graph associated to the view gets deleted. This method should call setGraph to input ...
View()
Default constructor.
void treatEvents(const std::vector< Event > &events) override
reimplemented from tlp::Observable to provide the triggers mechanism.
virtual void centerView(bool graphChanged=false)
Reset the visualization to the center. This method is called after major changes into the data struct...
const std::list< Interactor * > & interactors() const
Definition: View.h:159
virtual void currentInteractorChanged(tlp::Interactor *)
Callback method after setCurrentInteractor() was called. At this point, a call to View::currentIntera...
void setCurrentInteractor(tlp::Interactor *currentInteractor)
defines the active interactor that will receive user inputs.
virtual void interactorsInstalled(const std::list< tlp::Interactor * > &interactors)
Callback method after setInteractors() was called. At this point, a call to View::interactors() is co...
tlp::Graph * graph() const
std::string category() const override
A string identifier for a plugin used for categorization purposes.
Definition: View.h:122
void addRedrawTrigger(tlp::Observable *)
Registers a new trigger for automatic view drawing. Triggers are tlp::Observable subclasses....
void clearRedrawTriggers()
Clears the list of attached triggers This method removes all triggers associated to the View.
virtual bool checkOnClose()
allow to add some check when a user want to close a view.
Definition: View.h:371
virtual std::list< QWidget * > configurationWidgets() const
void setShowContextMenu(bool show)
This method allows to control the display of the context menu.
Definition: View.h:240
void saveState()
Save view state associated to the current graph.
void emitDrawNeededSignal()
This function emit the signal drawNeeded.
virtual tlp::DataSet state() const
Backup the state of the view. This method is used to restore the View's parameters when it's re-opene...
void fillContextMenu(QMenu *menu, edge e)
fills the context menu with entries related to the edge.
void removeRedrawTrigger(tlp::Observable *)
Removes a trigger from the list of registered triggers. Event coming from this trigger will no longer...
std::list< tlp::Observable * > triggers() const
tlp::Interactor * currentInteractor() const
virtual QGraphicsItem * centralItem() const
defines which item is considered as the central item in the view. The central item is considered to b...
bool showContextMenu(const QPoint &point, const QPointF &scenePoint)
This method is called whenever the context menu is required on the view.
virtual void graphChanged(tlp::Graph *)=0
Callback method after setGraph() was called. At this point, a call to View::graph() is considered val...
virtual void undoCallback()
This method is a callback to notify the panel that the pop() method (undo) has just been called on th...
void treatEvent(const Event &) override
This method is inherited from tlp::Observable and allows the view to trigger custom callback when its...
void drawNeeded()
Inform the overlying subsystem that this view needs to be drawn.
virtual bool interactorsEnabled() const
interactorsEnabled indicates if interactors are enabled or not
Definition: View.h:175
virtual void applySettings()
This method applies settings changed in the configuration widgets This method may be called from the ...
void graphSet(tlp::Graph *)
Emitted after the setGraph method has been called.
virtual void setState(const tlp::DataSet &)
Restores the state of the view. DataSet passed down to this method can come from a previous backup or...
The edge struct represents an edge in a Graph object.
Definition: Edge.h:40
The node struct represents a node in a Graph object.
Definition: Node.h:40