Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
View.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
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 
44 /**
45  @ingroup Plugins
46 
47  @brief View plugins provide a way to dynamically add to a Tulip plateform various ways to visualize a graph.
48 
49  A view takes the following elements as inputs:
50  @li A graph which contains the data to be displayed.
51  @li A state (@c tlp::DataSet) which contains initialization parameters.
52 
53  As an output, a View must provide a @c QGraphicsView instance where all drawing is done.
54  User interaction on a View is handled by the tlp::Interactor class. Several interactors can be installed on the same view but there can be only one active interactor at the same time.
55  In the end, the view can provide several configuration widgets used to set up additional parameters.
56 
57  When a View gets created, the following methods will always be called in the following order:
58  @li The constructor. Basically, you don't want to do anything in this method as View instance may be created at Tulip startup when the plugin system gets initialized. Subsequent methods will be called in order for the view to build UI elements
59  @li View::setupUi(). Notifies the view it can now build GUI components since every part of its initial state should be valid by now. Once this method is called, any call to View::graphicsView() is expected to return a valid pointer object.
60  @li View::setGraph. Sets the graph that is meant to be visualized in the View's panel.
61  @li View::setState(). Sets initial data. This method may be used to restore a previously backed-up state retrieved from the View::state() method.
62  @li View::interactorsInstalled(). Notifies the view of the available interactors. Interactors objects taken from the list have already been initialized.
63 
64  Once the View is initialized, none of the previously mentioned methods, except View::setGraph(), can be called again.
65  View::setGraph method may be called again to notify the view that another graph should be displayed (this may be a sub/parent graph of the previously displayed graph or a graph coming from a totally different hierarchy)
66 
67  Views are meant to be managed by an overleying system. As a consequence, a view may not decide directly when to redraw.
68  Thus, you should never call the View::draw() method. To notify the overleying system that your view needs to be redrawn, emit the View::drawNeeded() signal instead.
69 
70  A tlp::View subclass automatically inherits from the tlp::Observable interface. The tlp::View interface also automatically listn to its active graph to trigger handling trigger when this graph gets deleted.
71  When the graph associated to a View gets deleted, the View::graphDeleted() callback is triggered. @see graphDeleted() for more informations.
72  */
73 class TLP_QT_SCOPE View: public QObject, public tlp::Plugin, public tlp::Observable {
74  Q_OBJECT
75 
76  QList<tlp::Interactor*> _interactors;
77  tlp::Interactor* _currentInteractor;
78  tlp::Graph* _graph;
79 
80  QSet<tlp::Observable*> _triggers;
81 
82 public:
83  /**
84  @brief Default constructor
85  @warning Code of this method should almost be a no-op. Subsequent calls on other methods should allow you to setup your view.
86  */
87  View();
88 
89  /**
90  @brief Destructor
91  View's GUI components (graphics view, configuration widgets) responisbility belongs to the overleying system. Thus, the View is not in charge of deleting its graphcis view.
92  View's interactors are already deleted in the top class.
93  */
94  virtual ~View();
95 
96  virtual std::string category() const {
97  return VIEW_CATEGORY;
98  }
99  std::string icon() const {
100  return ":/tulip/gui/icons/32/plugin_view.png";
101  }
102 
103  /**
104  @return the View's panel as a @c QGraphicsView instance.
105  @note This method MUST ALWAYS return the same instance of a QGraphicsView.
106  */
107  virtual QGraphicsView* graphicsView() const=0;
108 
109  /**
110  @return The list of interactors installed on this view.
111  The list is always the same as the one given when View::setInteractors() was called.
112  @see setInteractors();
113  */
114  QList<tlp::Interactor*> interactors() const;
115 
116  /**
117  @return The currently active interactor.
118  The active interactor is the one that currently recieve user inputs.
119  @see setCurrentInteractor();
120  @warning This method may return a NULL pointer if no interactor is currently active.
121  */
122  tlp::Interactor* currentInteractor() const;
123 
124  /**
125  @return a list of widgets that can be used to set up the view.
126  Since several widgets can be retrived, user will be able to select them from a combo box where each widget will be identified by its windowsTitle.
127  @see View::applySettings()
128  @warning This method must not instantiate configuration widgets on the fly.
129  */
130  virtual QList<QWidget*> configurationWidgets() const;
131 
132  /**
133  * @brief This method can be used to change the configuration widgets' style
134  * sheet. From Qt documentation: The style sheet contains a textual description of customizations to the
135  * widget's style, as described in the Qt Style Sheets document. see http://qt-project.org/doc/qt-4.7/stylesheet.html.
136  * @return The stylesheet to use
137  */
138  virtual QString configurationWidgetsStyleSheet() const;
139 
140  /**
141  @brief Backup the state of the view.
142  This method is used to restore the View's parameters when it's re-opened.
143  */
144  virtual tlp::DataSet state() const=0;
145 
146  /**
147  @return the graph displayed by the view.
148  @note This method MUST return the same graph pointer that was previously passed down to setGraph.
149  */
150  tlp::Graph* graph() const;
151 
152  /**
153  @return The list of currently registered triggers.
154  @see View::addRedrawTrigger()
155  */
156  QSet<tlp::Observable*> triggers() const;
157 
158  /**
159  @brief reimplemented from tlp::Observable to provide the triggers mechanism.
160  @see View::addRedrawTrigger()
161  */
162  void treatEvents(const std::vector<Event> &events);
163 
164  /**
165  @brief defines which item is considered as the central item in the view.
166  The central item is considered to be a background item that will be set as parent of every graphics item added by the workspace into the view.
167  By default, this method returns NULL, which means that no central item is defined.
168  */
169  virtual QGraphicsItem* centralItem() const;
170 
171  /**
172  @brief Takes a snapshot of the view's screen and saves it into the given pixmap.
173  The snapshot is scaled to outputSize. If a null size is given, the snapshot is to be on a 1:1 ratio
174  @return A non-null pixmap of the snapshot was correctly taken.
175  */
176  virtual QPixmap snapshot(const QSize& outputSize=QSize()) const=0;
177 
178 public slots:
179  /**
180  * @brief This method is called whenever the context menu is required on the view.
181  * @param point The screen coordinates where the context menu should be displayed.
182  */
183  void showContextMenu(const QPoint& point, const QPointF &scenePoint);
184 
185  /**
186  * @brief This method is a callback to notify the panel that the pop() method (undo) has just been called on the graph.
187  * By default, this method will make a call to centerView()
188  **/
189  virtual void undoCallback();
190 
191  /**
192  @brief This method applies settings changed in the configuration widgets
193  This method may be called from the overleying system in various situations. The View is expected to apply settings in an optimized way to prevent extra redraws.
194  By default, this method does nothing.
195  */
196  virtual void applySettings();
197 
198  /**
199  @brief Reset the visualization to the center.
200  This method is called after major changes into the data structure. At this point, the user point of view should be reset and brought back to a point where all the data can be seen.
201  @note It is expected for the view to be redrawn when calling centerView
202  For a 3D visualization, this method could be implemented by centering the camera. For a table, this could be done by setting the scroll bar to the top position etc...
203  By default, this method calls draw().
204  */
205  virtual void centerView(bool graphChanged = false);
206 
207 
208  /**
209  @brief defines the list of interactors available on this View
210  @note Calling this will trigger the View::interactorsInstalled() callback for custom handling.
211  */
212  void setInteractors(const QList<tlp::Interactor*>&);
213 
214  /**
215  @brief defines the active interactor that will receive user inputs.
216  @note This method will first remove the previously active interactor (if any) using Interactor::uninstall()
217  @note Calling this will trigger the View::currentInteractorChanged() callback for custom handling.
218  @note Calling View::setCurrentInteractor(NULL) will only remove the previous current interactor.
219  */
220  void setCurrentInteractor(tlp::Interactor* currentInteractor);
221 
222  /**
223  @brief Restores the state of the view.
224  DataSet passed down to this method can come from a previous backup or be generated by the overleying system. It's up to the view to use this data or not.
225  */
226  virtual void setState(const tlp::DataSet&)=0;
227 
228  /**
229  @brief Defines the graph that should be displayed by the View
230  @note Calling setGraph triggers the View::graphChanged() callback for custom handling.
231  @warning This method and its subsequent callback might be called several times.
232  */
233  void setGraph(tlp::Graph* graph);
234 
235  /**
236  @brief Asks the view to draw.
237  A call to draw() means that internal data has most probably been modified and that the View should take that into account when drawing.
238  */
239  virtual void draw()=0;
240 
241  /**
242  @brief Refresh the View's panel.
243  Calling refresh() means that no internal data has been modified. This can happen when the view's panel gets resized, restored etc
244  */
245  inline virtual void refresh() {
246  draw();
247  }
248 
249  /**
250  @brief Sets up GUI elements belonging to the View.
251  This method is called once the initial state as been set (using setGraph and setState) and is called only once.
252  */
253  virtual void setupUi()=0;
254 
255  /**
256  @brief This method is inherited from tlp::Observable and allows the view to trigger custom callback when its associated graph gets deleted.
257  @warning When overriding this method. You MUST always make a call to View::treatEvent before doing anything in order to keep this callback working.
258  */
259  virtual void treatEvent(const Event&);
260 
261  /**
262  @brief Registers a new trigger for automatic view drawing.
263  Triggers are tlp::Observable subclasses. Once registered, the view will listen to the trigger's events and emit the drawNeeded signal each time the Observable::treatEvents() callback is run.
264  For more information about the Observable system, @see tlp::Observable
265 
266  @note This is a convenience function. However, using triggers prevent from performign extra checks on the data structure to know if a redraw must me made or not. For more control over event handling, you will have to implement your own treatEvent/treatEvents callback.
267  @warning If your tlp::View subclass overloads the treatEvents method. You must make sure to call the View::treatEvents method in order to keep the triggers system working.
268  */
269  void addRedrawTrigger(tlp::Observable*);
270 
271  /**
272  @brief Removes a trigger from the list of registered triggers. Event coming from this trigger will no longer trigger the drawNeeded signal.
273  @see View::addRedrawTrigger()
274  */
275  void removeRedrawTrigger(tlp::Observable*);
276 
277  /**
278  @brief Clears the list of attached triggers
279  This method removes all triggers associated to the View.
280  @note From the moment this method is called, no update on previous triggers will be considered. Even if this is called during an Observable::holdObservers()
281  */
282  void clearRedrawTriggers();
283 
284  /**
285  @brief This function emit the signal drawNeeded
286  */
287  void emitDrawNeededSignal();
288 
289 signals:
290  /**
291  @brief Inform the overleying subsystem that this view needs to be drawn.
292  @note Dependeing on the overlying implementation, a subsequent call to draw might not be immediate.
293  */
294  void drawNeeded();
295 
296  /**
297  @brief Emitted after the setGraph method has been called.
298  @note This signal is emitted from the non-virtual View::setGraph() method thus cannot be prevented.
299  */
300  void graphSet(tlp::Graph*);
301 
302 protected slots:
303  /**
304  @brief Callback method after setInteractors() was called.
305  At this point, a call to View::interactors() is considered valid.
306  */
307  virtual void interactorsInstalled(const QList<tlp::Interactor*>& interactors);
308 
309  /**
310  @brief Callback method after setCurrentInteractor() was called.
311  At this point, a call to View::currentInteractor() is considered valid and return the newly active interactor.
312  @warning The interactor passed down to this method MAY BE a NULL pointer ! This means that no current interactor should be set.
313  */
314  virtual void currentInteractorChanged(tlp::Interactor*);
315 
316  /**
317  @brief Callback method after setGraph() was called.
318  At this point, a call to View::graph() is considered valid and return the lastly set graph.
319  */
320  virtual void graphChanged(tlp::Graph*)=0;
321 
322  /**
323  @brief Called when the graph associated to the view gets deleted.
324  This method should call setGraph to input a new graph pointer (NULL or valid)
325  @param parentGraph The parent of the graph that was just deleted. If there is no parent available (eg. the graph was root), parentGraph is NULL
326  */
327  virtual void graphDeleted(tlp::Graph* parentGraph)=0;
328 
329  /**
330  * @brief fills the context menu with entries related to the view.
331  * This method is called whenever the context menu is displayed on the panel.
332  * @param QMenu The popup menu that will be displayed. This menu should be populated with context action related to the panel.
333  */
334  virtual void fillContextMenu(QMenu*,const QPointF &) {}
335 };
336 
337 }
338 
339 #endif /* VIEW_H_ */