Tulip  5.3.1
Large graphs analysis and drawing
Interactor.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 _INTERACTOR_H
21 #define _INTERACTOR_H
22 
23 #include <tulip/Plugin.h>
24 
25 #include <QObject>
26 #include <QCursor>
27 #include <QMap>
28 
29 #include <string>
30 
31 class QAction;
32 class QLabel;
33 
34 namespace tlp {
35 
36 static const std::string INTERACTOR_CATEGORY = "Interactor";
37 
38 class View;
39 
40 /**
41  @ingroup Plugins
42  @brief Interactor provides a way to handle user inputs over a view.
43  Basically, The interactor class is an overlay to the Qt's event filtering mechanism. It adds
44  several features like the ability to define priorities, custom cursors, etc
45 
46  When an interactor is constructed, the following methods are called in this order:
47  @li The constructor. This method should almost be a no-op. Interactors may be instantiated by the
48  plugin manager system and doing unneeded operations in the constructor may lead to poor
49  performances when the application loads up.
50  @li isCompatible. When creating a View, the application will check for all interactors to see if
51  they could be installed on it. This is done using the isCompatible method.
52  @li construct. Initialize the interactor. Since the constructor should be a no-op, initialization
53  code should be put here.
54  @li setView. Defines the view this interactor is bound to. The Interactor may keep a reference to
55  the view object to perform specific operations on user inputs.
56 
57  Methods listed above are only called once. Once the interactor is initialized, it may be
58  installed/removed several times on different QObjects. It will then repond to user inputs via the
59  eventFilter method
60  @see QObject::eventFilter()
61  */
62 class TLP_QT_SCOPE Interactor : public QObject, public Plugin {
63  Q_OBJECT
64  Q_PROPERTY(unsigned int priority READ priority)
65  Q_PROPERTY(QAction *action READ action)
66  Q_PROPERTY(tlp::View *view READ view WRITE setView)
67  Q_PROPERTY(QCursor cursor READ cursor)
68 
69 public:
70  std::string category() const override {
71  return INTERACTOR_CATEGORY;
72  }
73  std::string icon() const override {
74  return ":/tulip/gui/icons/32/plugin_interactor.png";
75  }
76  /**
77  @brief Checks the compatibility between the interactor and the given view (identified by its
78  name).
79  If this method returns true, it's very likely that the interactor will be installed on the
80  associated view.
81  */
82  virtual bool isCompatible(const std::string &viewName) const = 0;
83 
84  /**
85  @deprecated Use QWidget *configurationDocWidget() and/or QWidget *configurationOptionsWidget()
86  instead
87  @return the configuration widget used to set up the interactor.
88  @warning This method MUST ALWAYS return the same pointer. Doing otherwise may lead to memory
89  leaks.
90  @note The configuration widget has to be instantiated from the construct method.
91  @note It is up to the interactor developper to delete the configuration widget
92  */
93  virtual QWidget *configurationWidget() const {
94  return nullptr;
95  }
96  virtual QLabel *configurationDocWidget() const {
97  return nullptr;
98  }
99  virtual QWidget *configurationOptionsWidget() const {
100  return nullptr;
101  }
102 
103  /**
104  @return the interactor's priority.
105  Priority defines how interactors gets ordered when displayed in the View's toolbar.
106  Interactors with the top-most priority value will be displayed at the beginning of the list
107  while lowest priority will be position at the end.
108  */
109  virtual unsigned int priority() const = 0;
110 
111  /**
112  @return a QAction associated to this interactor.
113  This is used by the overleying system to associate an icon/text to the interactor.
114  @warning The parent (QObject::parent()) object of this QAction MUST BE the Interactor.
115  */
116  virtual QAction *action() const = 0;
117 
118  /**
119  @return the View object associated to this Interactor.
120  @warning The returned object MUST be the same as the one passed down to the setView method.
121  */
122  virtual tlp::View *view() const = 0;
123 
124  /**
125  @return The cursor associated to this interactor.
126  When the interactor gets active on a view, the View's cursor is changed to what this method
127  returns.
128  */
129  virtual QCursor cursor() const = 0;
130 
131  /**
132  @brief Builds up the interactor's internal state.
133  This method should be used instead of the constructor to initialize the interactor.
134  */
135  virtual void construct() = 0;
136 
137 public slots:
138  /**
139  @brief Defines the view object associated to this interactor.
140  @warning The view() method MUST ALWAYS return the same pointer as the one passed down to this
141  method.
142  */
143  virtual void setView(tlp::View *) = 0;
144 
145  /**
146  @brief Install the interactor on the given target
147  A call to this method means that the interactor should start listening to the target's events
148  and handle them.
149  Returning true prevents further handling of the event. Doing otherwise means that the interactor
150  will let following filters to hand over this kind of event.
151  */
152  virtual void install(QObject *target) = 0;
153 
154  /**
155  @brief Removes the interactor from the previously set target.
156  Interactors can be installed on only one target at once.
157  */
158  virtual void uninstall() = 0;
159 
160  /**
161  @brief Informs the interactor when the undo command (Ctrl+Z) has been triggered
162  */
163  virtual void undoIsDone() = 0;
164 
165 protected:
166  /**
167  @brief Provides input filtering for the interactor
168  @see QObject::eventFilter()
169  */
170  inline bool eventFilter(QObject *obj, QEvent *ev) override {
171  return QObject::eventFilter(obj, ev);
172  }
173 };
174 
175 ///@cond DOXYGEN_HIDDEN
176 /**
177  * @ingroup Plugins
178  * @brief The InteractorLister class lists compatible interactors for a given tlp::View
179  */
180 class TLP_QT_SCOPE InteractorLister {
181  static QMap<std::string, QList<std::string>> _compatibilityMap;
182 
183 public:
184  static void initInteractorsDependencies();
185  static QList<std::string> compatibleInteractors(const std::string &viewName);
186 };
187 ///@endcond
188 
189 /**
190  * @ingroup Plugins
191  * @def
192  * INTERACTORPLUGINVIEWEXTENSION(CLASS_NAME,STRING_CLASS_NAME,BASE_INTERACTOR_STRING_NAME,VIEW_STRING_NAME,AUTHOR,DATE,DESCRIPTION,VERSION)
193  *
194  * @brief Copy an existing Tulip interactor and sets it compatible with a given View.
195  *
196  * This macro is used when you're making your own View and want to use an existing interactor with
197  * it. Interactors are declared to be compatible with a list of View. This macro extends the
198  * compatibility of an existing interactor by subclassing it.
199  *
200  * @note: This macro used the same interactor priority as the base interactor. To define your own
201  * priority, see INTERACTORPLUGINVIEWEXTENSIONWITHPRIORITY
202  *
203  * @param CLASS_NAME The name of the interactor class to generate.
204  * @param STRING_CLASS_NAME The name of the interactor plugin to generate (see tlp::Plugin::name())
205  * @param BASE_INTERACTOR_STRING_NAME The name of the interactor to extend
206  * @param VIEW_STRING_NAME The name of the View to set the interactor compatible with
207  * @param AUTHOR see tlp::Plugin::author()
208  * @param DATE see tlp::Plugin::date()
209  * @param DESCRIPTION see tlp::Plugin::info()
210  * @param VERSION see tlp::Plugin::version()
211  */
212 #define INTERACTORPLUGINVIEWEXTENSION(CLASS_NAME, STRING_CLASS_NAME, BASE_INTERACTOR_STRING_NAME, \
213  VIEW_STRING_NAME, AUTHOR, DATE, DESCRIPTION, VERSION) \
214  class CLASS_NAME : public tlp::Interactor { \
215  mutable tlp::Interactor *_component; \
216  \
217  public: \
218  std::string name() const { \
219  return std::string(STRING_CLASS_NAME); \
220  } \
221  std::string author() const { \
222  return std::string(AUTHOR); \
223  } \
224  std::string date() const { \
225  return std::string(DATE); \
226  } \
227  std::string info() const { \
228  return std::string(DESCRIPTION); \
229  } \
230  std::string release() const { \
231  return std::string(VERSION); \
232  } \
233  std::string tulipRelease() const { \
234  return std::string(TULIP_VERSION); \
235  } \
236  std::string group() const { \
237  return getComponent()->group(); \
238  } \
239  CLASS_NAME(const PluginContext *) : _component(nullptr) {} \
240  bool isCompatible(const std::string &viewName) const { \
241  return viewName == VIEW_STRING_NAME; \
242  } \
243  QWidget *configurationWidget() const { \
244  return getComponent()->configurationWidget(); \
245  } \
246  QLabel *configurationDocWidget() const { \
247  return getComponent()->configurationDocWidget(); \
248  } \
249  QWidget *configurationActionsWidget() const { \
250  return getComponent()->configurationOptionsWidget(); \
251  } \
252  unsigned int priority() const { \
253  return getComponent()->priority(); \
254  } \
255  QAction *action() const { \
256  return getComponent()->action(); \
257  } \
258  tlp::View *view() const { \
259  return getComponent()->view(); \
260  } \
261  QCursor cursor() const { \
262  return getComponent()->cursor(); \
263  } \
264  void construct() { \
265  getComponent()->construct(); \
266  } \
267  void setView(tlp::View *v) { \
268  getComponent()->setView(v); \
269  } \
270  void install(QObject *target) { \
271  getComponent()->install(target); \
272  } \
273  void uninstall() { \
274  getComponent()->uninstall(); \
275  } \
276  void undoIsDone() { \
277  getComponent()->undoIsDone(); \
278  } \
279  tlp::Interactor *getComponent() const { \
280  if (!_component) { \
281  _component = \
282  tlp::PluginLister::getPluginObject<Interactor>(BASE_INTERACTOR_STRING_NAME, nullptr); \
283  assert(_component != nullptr); \
284  } \
285  return _component; \
286  } \
287  }; \
288  PLUGIN(CLASS_NAME)
289 
290 /**
291  * @ingroup Plugins
292  * @def
293  * INTERACTORPLUGINVIEWEXTENSIONWITHPRIORITY(CLASS_NAME,STRING_CLASS_NAME,BASE_INTERACTOR_STRING_NAME,VIEW_STRING_NAME,AUTHOR,DATE,DESCRIPTION,VERSION,PRIORITY)
294  * @brief Similar to INTERACTORPLUGINVIEWEXTENSION but allows to define the generated interactor's
295  * priority.
296  * @see tlp::Interactor::priority()
297  * @see INTERACTORPLUGINVIEWEXTENSION
298  */
299 #define INTERACTORPLUGINVIEWEXTENSIONWITHPRIORITY(CLASS_NAME, STRING_CLASS_NAME, \
300  BASE_INTERACTOR_STRING_NAME, VIEW_STRING_NAME, \
301  AUTHOR, DATE, DESCRIPTION, VERSION, PRIORITY) \
302  class CLASS_NAME : public tlp::Interactor { \
303  mutable tlp::Interactor *_component; \
304  \
305  public: \
306  std::string name() const { \
307  return std::string(STRING_CLASS_NAME); \
308  } \
309  std::string author() const { \
310  return std::string(AUTHOR); \
311  } \
312  std::string date() const { \
313  return std::string(DATE); \
314  } \
315  std::string info() const { \
316  return std::string(DESCRIPTION); \
317  } \
318  std::string release() const { \
319  return std::string(VERSION); \
320  } \
321  std::string tulipRelease() const { \
322  return std::string(TULIP_VERSION); \
323  } \
324  std::string group() const { \
325  return getComponent()->group(); \
326  } \
327  CLASS_NAME(const PluginContext *) : _component(nullptr) {} \
328  bool isCompatible(const std::string &viewName) const { \
329  return viewName == VIEW_STRING_NAME; \
330  } \
331  QWidget *configurationWidget() const { \
332  return getComponent()->configurationWidget(); \
333  } \
334  QLabel *configurationDocWidget() const { \
335  return getComponent()->configurationDocWidget(); \
336  } \
337  QWidget *configurationActionsWidget() const { \
338  return getComponent()->configurationOptionsWidget(); \
339  } \
340  unsigned int priority() const { \
341  return PRIORITY; \
342  } \
343  QAction *action() const { \
344  return getComponent()->action(); \
345  } \
346  tlp::View *view() const { \
347  return getComponent()->view(); \
348  } \
349  QCursor cursor() const { \
350  return getComponent()->cursor(); \
351  } \
352  void construct() { \
353  getComponent()->construct(); \
354  } \
355  void setView(tlp::View *v) { \
356  getComponent()->setView(v); \
357  } \
358  void install(QObject *target) { \
359  getComponent()->install(target); \
360  } \
361  void uninstall() { \
362  getComponent()->uninstall(); \
363  } \
364  void undoIsDone() { \
365  getComponent()->undoIsDone(); \
366  } \
367  tlp::Interactor *getComponent() const { \
368  if (!_component) { \
369  _component = \
370  tlp::PluginLister::getPluginObject<Interactor>(BASE_INTERACTOR_STRING_NAME, nullptr); \
371  assert(_component != nullptr); \
372  } \
373  return _component; \
374  } \
375  }; \
376  PLUGIN(CLASS_NAME)
377 } // namespace tlp
378 
379 #endif
virtual QWidget * configurationWidget() const
Definition: Interactor.h:93
bool eventFilter(QObject *obj, QEvent *ev) override
Provides input filtering for the interactor.
Definition: Interactor.h:170
Interactor provides a way to handle user inputs over a view. Basically, The interactor class is an ov...
Definition: Interactor.h:62
Top-level interface for plug-ins.
Definition: Plugin.h:85
std::string category() const override
A string identifier for a plugin used for categorization purposes.
Definition: Interactor.h:70
View plugins provide a way to dynamically add to a Tulip plateform various ways to visualize a graph...
Definition: View.h:95
std::string icon() const override
The icon (preferably a thumbnail) of the plugin.
Definition: Interactor.h:73