Tulip  4.2.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
InteractorComposite.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 INTERACTORCOMPOSITE_H
21 #define INTERACTORCOMPOSITE_H
22 
23 #include <tulip/Interactor.h>
24 #include <QtCore/QList>
25 
26 namespace tlp {
27 
28 /**
29  @class InteractorComposite represent an event handler stored inside an InteractorComposite
30 
31  This is meant to be focused on event handling only. An InteractorComponent should respond to user inputs in its eventFilter method and return true to avoid further propagation of the event to other components.
32  This system is inherited from Qt event handling, see QObject::eventFilter() for details.
33  */
34 class TLP_QT_SCOPE InteractorComponent: public QObject {
35  Q_OBJECT
36 
37  View* _view;
38 public:
39 
40  /**
41  * @brief The init() method is called after the component gets installed on a target. Note that this method is called before any event from could have been retrieved.
42  */
43  virtual void init();
44 
45  /**
46  * @brief The main event handling method.
47  * See QObject::eventFilter for details.
48  * @note The target object is the one on which the InteractorComponent has been installed.
49  */
50  virtual bool eventFilter(QObject*, QEvent*);
51 
52  /**
53  * @brief This method is called after the component is uninstalled from its target.
54  */
55  virtual void clear() {}
56 
57  /**
58  * @brief setView is called when the InteractorComposite is installed on a new view.
59  * @see InteractorComposite::setView
60  */
61  void setView(View* view);
62 
63  /**
64  * @return The view on which the interactor is installed (NULL if none)
65  */
66  View* view() const;
67 
68  /**
69  * @brief A callback method after setView was called.
70  */
71  virtual void viewChanged(View *) {}
72 };
73 
74 /**
75  @class InteractorComposite is a subclass of Interactor that allows building Interactors using component classes focused specifically on input handling.
76 
77  The composite behaves like a regular interactor. Event handling is made using InteractorComponent subclasses.
78  Basically, an InteractorComposite is a list of InteractorComponent. Each of them are meant to be pushed in the list using the push_back and push_front methods.
79  When an event is caught by the InteractorComposite, it will iterate over all components (in th order provided by the list) and run eventFilter on each one of them until one of the component returns true.
80 
81  When subclassing InteractorComposite, you should push your components in the construct() method (take care of the order they are pushed in).
82  Once the setView() method is called (after construct()), the init() method will be run on every components.
83  */
84 class TLP_QT_SCOPE InteractorComposite : public tlp::Interactor {
85  Q_OBJECT
86  QAction* _action;
87  tlp::View* _view;
88  QObject* _lastTarget;
89 
90 protected:
91  QList<tlp::InteractorComponent*> _components;
92  /**
93  @brief backup a QObject into the InteractorComposite.
94  This can be used to store the last target the interactor was installed on. This can be used when uninstall is called.
95  */
96  void setLastTarget(QObject*);
97 
98  /**
99  @return The last target the interactor was installed on.
100  @note If the lastTarget is destroyed before uninstall was called, the lastTarget() method will return NULL.
101  */
102  QObject* lastTarget() const;
103 
104 protected slots:
105  void lastTargetDestroyed();
106 
107 public:
108  typedef QList<InteractorComponent*>::iterator iterator;
109  typedef QList<InteractorComponent*>::const_iterator const_iterator;
110 
111  /**
112  @brief Default constructor
113  @param icon The icon set on the interactor's action
114  @param text The text set on the interactor's action
115  */
116  InteractorComposite(const QIcon& icon, const QString& text="");
117  virtual ~InteractorComposite();
118 
119  virtual tlp::View* view() const;
120  virtual QAction* action() const;
121  virtual QCursor cursor() const;
122 
123  /**
124  @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
125  */
126  iterator begin();
127  /**
128  @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
129  */
130  iterator end();
131  /**
132  @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
133  */
134  const_iterator begin() const;
135  /**
136  @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
137  */
138  const_iterator end() const;
139 
140  /**
141  @brief Pushs an InteractorComponent at the end of the list
142  */
143  void push_back(InteractorComponent* component);
144 
145  /**
146  @brief Pushs an InteractorComponent at the beggining of the list
147  */
148  void push_front(InteractorComponent* component);
149 
150 public slots:
151  virtual void undoIsDone();
152  virtual void setView(tlp::View* view);
153  virtual void install(QObject* target);
154  virtual void uninstall();
155 };
156 
157 }
158 #endif // INTERACTORCOMPOSITE_H