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