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