Tulip  4.6.0
Better Visualization Through Research
library/tulip-gui/include/tulip/InteractorComposite.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 
00020 #ifndef INTERACTORCOMPOSITE_H
00021 #define INTERACTORCOMPOSITE_H
00022 
00023 #include <tulip/Interactor.h>
00024 
00025 #include <QObject>
00026 
00027 class QIcon;
00028 
00029 namespace tlp {
00030 
00031 /**
00032   @class InteractorComposite represent an event handler stored inside an InteractorComposite
00033 
00034   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.
00035   This system is inherited from Qt event handling, see QObject::eventFilter() for details.
00036   */
00037 class TLP_QT_SCOPE InteractorComponent: public QObject {
00038   Q_OBJECT
00039 
00040   View* _view;
00041 public:
00042 
00043   /**
00044    * @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.
00045    */
00046   virtual void init();
00047 
00048   /**
00049    * @brief The main event handling method.
00050    * See QObject::eventFilter for details.
00051    * @note The target object is the one on which the InteractorComponent has been installed.
00052    */
00053   virtual bool eventFilter(QObject*, QEvent*);
00054 
00055   /**
00056    * @brief This method is called after the component is uninstalled from its target.
00057    */
00058   virtual void clear() {}
00059 
00060   /**
00061    * @brief setView is called when the InteractorComposite is installed on a new view.
00062    * @see InteractorComposite::setView
00063    */
00064   void setView(View* view);
00065 
00066   /**
00067    * @return The view on which the interactor is installed (NULL if none)
00068    */
00069   View* view() const;
00070 
00071   /**
00072    * @brief A callback method after setView was called.
00073    */
00074   virtual void viewChanged(View *) {}
00075 };
00076 
00077 /**
00078   @class InteractorComposite is a subclass of Interactor that allows building Interactors using component classes focused specifically on input handling.
00079 
00080   The composite behaves like a regular interactor. Event handling is made using InteractorComponent subclasses.
00081   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.
00082   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.
00083 
00084   When subclassing InteractorComposite, you should push your components in the construct() method (take care of the order they are pushed in).
00085   Once the setView() method is called (after construct()), the init() method will be run on every components.
00086   */
00087 class TLP_QT_SCOPE InteractorComposite : public tlp::Interactor {
00088   Q_OBJECT
00089   QAction* _action;
00090   tlp::View* _view;
00091   QObject* _lastTarget;
00092 
00093 protected:
00094   QList<tlp::InteractorComponent*> _components;
00095   /**
00096     @brief backup a QObject into the InteractorComposite.
00097     This can be used to store the last target the interactor was installed on. This can be used when uninstall is called.
00098     */
00099   void setLastTarget(QObject*);
00100 
00101   /**
00102     @return The last target the interactor was installed on.
00103     @note If the lastTarget is destroyed before uninstall was called, the lastTarget() method will return NULL.
00104     */
00105   QObject* lastTarget() const;
00106 
00107 protected slots:
00108   void lastTargetDestroyed();
00109 
00110 public:
00111   typedef QList<InteractorComponent*>::iterator iterator;
00112   typedef QList<InteractorComponent*>::const_iterator const_iterator;
00113 
00114   /**
00115     @brief Default constructor
00116     @param icon The icon set on the interactor's action
00117     @param text The text set on the interactor's action
00118     */
00119   InteractorComposite(const QIcon& icon, const QString& text="");
00120   virtual ~InteractorComposite();
00121 
00122   virtual tlp::View* view() const;
00123   virtual QAction* action() const;
00124   virtual QCursor cursor() const;
00125 
00126   /**
00127     @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
00128     */
00129   iterator begin();
00130   /**
00131     @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
00132     */
00133   iterator end();
00134   /**
00135     @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
00136     */
00137   const_iterator begin() const;
00138   /**
00139     @brief Since InteractorComposte behaves like a list of InteractorComponent, this method is here to allow this class to be iterable
00140     */
00141   const_iterator end() const;
00142 
00143   /**
00144     @brief Pushs an InteractorComponent at the end of the list
00145     */
00146   void push_back(InteractorComponent* component);
00147 
00148   /**
00149     @brief Pushs an InteractorComponent at the beggining of the list
00150     */
00151   void push_front(InteractorComponent* component);
00152 
00153 public slots:
00154   virtual void undoIsDone();
00155   virtual void setView(tlp::View* view);
00156   virtual void install(QObject* target);
00157   virtual void uninstall();
00158 };
00159 
00160 }
00161 #endif // INTERACTORCOMPOSITE_H
 All Classes Files Functions Variables Enumerations Enumerator Properties