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