Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
Interactor.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 
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 
33 namespace tlp {
34 
35 static const std::string INTERACTOR_CATEGORY = "Interactor";
36 
37 class View;
38 
39 /**
40  @ingroup Plugins
41  @brief Interactor provides a way to handle user inputs over a view.
42  Basically, The interactor class is an overlay to the Qt's event filtering mechanism. It adds several features like the ability to define priorities, custom cursors, etc
43 
44  When an interactor is constructed, the following methods are called in this order:
45  @li The constructor. This method should almost be a no-op. Interactors may be instantiated by the plugin manager system and doing unneeded operations in the constructor may lead to poor performances when the application loads up.
46  @li isCompatible. When creating a View, the application will check for all interactors to see if they could be installed on it. This is done using the isCompatible method.
47  @li construct. Initialize the interactor. Since the constructor should be a no-op, initialization code should be put here.
48  @li setView. Defines the view this interactor is bound to. The Interactor may keep a reference to the view object to perform specific operations on user inputs.
49 
50  Methods listed above are only called once. Once the interactor is initialized, it may be installed/removed several times on different QObjects. It will then repond to user inputs via the eventFilter method
51  @see QObject::eventFilter()
52  */
53 class TLP_QT_SCOPE Interactor: public QObject, public Plugin {
54  Q_OBJECT
55  Q_PROPERTY(unsigned int priority READ priority)
56  Q_PROPERTY(QAction* action READ action)
57  Q_PROPERTY(tlp::View* view READ view WRITE setView)
58  Q_PROPERTY(QCursor cursor READ cursor)
59 
60 public:
61  virtual std::string category() const {
62  return INTERACTOR_CATEGORY;
63  }
64  std::string icon() const {
65  return ":/tulip/gui/icons/32/plugin_interactor.png";
66  }
67  /**
68  @brief Checks the compatibility between the interactor and the given view (identified by its name).
69  If this method returns true, it's very likely that the interactor will be installed on the associated view.
70  */
71  virtual bool isCompatible(const std::string& viewName) const=0;
72 
73  /**
74  @return the configuration widget used to set up the interactor.
75  @warning This method MUST ALWAYS return the same pointer. Doing otherwise may lead to memory leaks.
76  @note The configuration widget has to be instantiated from the construct method.
77  @note It is up to the interactor developper to delete the configuration widget
78  */
79  virtual QWidget* configurationWidget() const=0;
80 
81  /**
82  @return the interactor's priority.
83  Priority defines how interactors gets ordered when displayed in the View's toolbar.
84  Interactors with the top-most priority value will be displayed at the beginning of the list while lowest priority will be position at the end.
85  */
86  virtual unsigned int priority() const=0;
87 
88  /**
89  @return a QAction associated to this interactor.
90  This is used by the overleying system to associate an icon/text to the interactor.
91  @warning The parent (QObject::parent()) object of this QAction MUST BE the Interactor.
92  */
93  virtual QAction* action() const=0;
94 
95  /**
96  @return the View object associated to this Interactor.
97  @warning The returned object MUST be the same as the one passed down to the setView method.
98  */
99  virtual tlp::View* view() const=0;
100 
101  /**
102  @return The cursor associated to this interactor.
103  When the interactor gets active on a view, the View's cursor is changed to what this method returns.
104  */
105  virtual QCursor cursor() const=0;
106 
107  /**
108  @brief Builds up the interactor's internal state.
109  This method should be used instead of the constructor to initialize the interactor.
110  */
111  virtual void construct()=0;
112 
113 public slots:
114  /**
115  @brief Defines the view object associated to this interactor.
116  @warning The view() method MUST ALWAYS return the same pointer as the one passed down to this method.
117  */
118  virtual void setView(tlp::View*)=0;
119 
120  /**
121  @brief Install the interactor on the given target
122  A call to this method means thatr the interactor should start listening to the target's events and handle them.
123  Returning true prevents further handling of the event. Doing otherwise means that the interactor will let following filters to hand over this kind of event.
124  */
125  virtual void install(QObject* target)=0;
126 
127  /**
128  @brief Removes the interactor from the previously set target.
129  Interactors can be installed on only one target at once.
130  */
131  virtual void uninstall()=0;
132 
133  /**
134  @brief Informs the interactor when the undo command (Ctrl+Z) has been triggered
135  */
136  virtual void undoIsDone()=0;
137 
138 protected:
139  /**
140  @brief Provides input filtering for the interactor
141  @see QObject::eventFilter()
142  */
143  inline virtual bool eventFilter(QObject* obj, QEvent* ev) {
144  return QObject::eventFilter(obj,ev);
145  }
146 };
147 
148 /**
149  * @ingroup Plugins
150  * @brief The InteractorLister class lists compatible interactors for a given tlp::View
151  */
152 class TLP_QT_SCOPE InteractorLister {
153  static QMap<std::string,QList<std::string> > _compatibilityMap;
154 public:
155  static void initInteractorsDependencies();
156  static QList<std::string> compatibleInteractors(const std::string& viewName);
157 };
158 
159 /**
160  * @ingroup Plugins
161  * @def INTERACTORPLUGINVIEWEXTENSION(CLASS_NAME,STRING_CLASS_NAME,BASE_INTERACTOR_STRING_NAME,VIEW_STRING_NAME,AUTHOR,DATE,DESCRIPTION,VERSION)
162  *
163  * @brief Copy an existing Tulip interactor and sets it compatible with a given View.
164  *
165  * This macro is used when you're making your own View and want to use an existing interactor with it. Interactors are declared to be compatible with a list of View. This macro extends the compatibility of an existing interactor by subclassing it.
166  *
167  * @note: This macro used the same interactor priority as the base interactor. To define your own priority, see INTERACTORPLUGINVIEWEXTENSIONWITHPRIORITY
168  *
169  * @param CLASS_NAME The name of the interactor class to generate.
170  * @param STRING_CLASS_NAME The name of the interactor plugin to generate (see tlp::Plugin::name())
171  * @param BASE_INTERACTOR_STRING_NAME The name of the interactor to extend
172  * @param VIEW_STRING_NAME The name of the View to set the interactor compatible with
173  * @param AUTHOR see tlp::Plugin::author()
174  * @param DATE see tlp::Plugin::date()
175  * @param DESCRIPTION see tlp::Plugin::info()
176  * @param VERSION see tlp::Plugin::version()
177  */
178 #define INTERACTORPLUGINVIEWEXTENSION(CLASS_NAME,STRING_CLASS_NAME,BASE_INTERACTOR_STRING_NAME,VIEW_STRING_NAME,AUTHOR,DATE,DESCRIPTION,VERSION)\
179 class CLASS_NAME : public tlp::Interactor {\
180  mutable tlp::Interactor* _component;\
181 public:\
182  std::string name() const { return std::string(STRING_CLASS_NAME); } \
183  std::string author() const { return std::string(AUTHOR); }\
184  std::string date() const { return std::string(DATE); } \
185  std::string info() const { return std::string(DESCRIPTION); } \
186  std::string release() const { return std::string(VERSION); }\
187  std::string tulipRelease() const { return std::string(TULIP_RELEASE); }\
188  std::string group() const { return getComponent()->group(); }\
189  CLASS_NAME(const PluginContext *):_component(NULL) {}\
190  bool isCompatible(const std::string& viewName) const { return viewName == VIEW_STRING_NAME; }\
191  QWidget* configurationWidget() const { return getComponent()->configurationWidget(); }\
192  unsigned int priority() const { return getComponent()->priority(); }\
193  QAction* action() const { return getComponent()->action(); }\
194  tlp::View* view() const { return getComponent()->view(); }\
195  QCursor cursor() const { return getComponent()->cursor(); }\
196  void construct() { getComponent()->construct(); }\
197  void setView(tlp::View* v) { getComponent()->setView(v); }\
198  void install(QObject* target) { getComponent()->install(target); }\
199  void uninstall() { getComponent()->uninstall(); }\
200  void undoIsDone() { getComponent()->undoIsDone(); }\
201  tlp::Interactor* getComponent() const {\
202  if(!_component) {\
203  _component = tlp::PluginLister::instance()->getPluginObject<Interactor>(BASE_INTERACTOR_STRING_NAME,NULL); assert(_component != NULL);\
204  }\
205  return _component;\
206  }\
207 };\
208 PLUGIN(CLASS_NAME)
209 
210 /**
211  * @ingroup Plugins
212  * @def INTERACTORPLUGINVIEWEXTENSIONWITHPRIORITY(CLASS_NAME,STRING_CLASS_NAME,BASE_INTERACTOR_STRING_NAME,VIEW_STRING_NAME,AUTHOR,DATE,DESCRIPTION,VERSION,PRIORITY)
213  * @brief Similar to INTERACTORPLUGINVIEWEXTENSION but allows to define the generated interactor's priority.
214  * @see tlp::Interactor::priority()
215  * @see INTERACTORPLUGINVIEWEXTENSION
216  */
217 #define INTERACTORPLUGINVIEWEXTENSIONWITHPRIORITY(CLASS_NAME,STRING_CLASS_NAME,BASE_INTERACTOR_STRING_NAME,VIEW_STRING_NAME,AUTHOR,DATE,DESCRIPTION,VERSION,PRIORITY) \
218 class CLASS_NAME : public tlp::Interactor {\
219  mutable tlp::Interactor* _component;\
220 public:\
221  std::string name() const { return std::string(STRING_CLASS_NAME); } \
222  std::string author() const { return std::string(AUTHOR); }\
223  std::string date() const { return std::string(DATE); } \
224  std::string info() const { return std::string(DESCRIPTION); } \
225  std::string release() const { return std::string(VERSION); }\
226  std::string tulipRelease() const { return std::string(TULIP_RELEASE); }\
227  std::string group() const { return getComponent()->group(); }\
228  CLASS_NAME(const PluginContext *):_component(NULL) {}\
229  bool isCompatible(const std::string& viewName) const { return viewName == VIEW_STRING_NAME; }\
230  QWidget* configurationWidget() const { return getComponent()->configurationWidget(); }\
231  unsigned int priority() const { return PRIORITY; }\
232  QAction* action() const { return getComponent()->action(); }\
233  tlp::View* view() const { return getComponent()->view(); }\
234  QCursor cursor() const { return getComponent()->cursor(); }\
235  void construct() { getComponent()->construct(); }\
236  void setView(tlp::View* v) { getComponent()->setView(v); }\
237  void install(QObject* target) { getComponent()->install(target); }\
238  void uninstall() { getComponent()->uninstall(); }\
239  void undoIsDone() { getComponent()->undoIsDone(); }\
240  tlp::Interactor* getComponent() const {\
241  if(!_component) {\
242  _component = tlp::PluginLister::instance()->getPluginObject<Interactor>(BASE_INTERACTOR_STRING_NAME,NULL); assert(_component != NULL);\
243  }\
244  return _component;\
245  }\
246 };\
247 PLUGIN(CLASS_NAME)
248 
249 }
250 
251 #endif