Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / LightApp / LightApp_SwitchOp.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "LightApp_SwitchOp.h"
24 #include "LightApp_Module.h"
25 #include "LightApp_Operation.h"
26 #include "LightApp_Dialog.h"
27 #include <CAM_Application.h>
28 #include <SUIT_Operation.h>
29 #include <SUIT_Study.h>
30
31 #include <QEvent>
32 #include <QWidget>
33 #include <QList>
34 #include <QApplication>
35
36 /*!
37  * \brief Constructor
38   * \param theParent - parent of object
39 *
40 * Creates instance of the object. Connects signals and slots. Install eveny filter
41 * on application
42 */
43 LightApp_SwitchOp::LightApp_SwitchOp( LightApp_Module* theModule )
44 : QObject( 0 ),
45   myModule( theModule )
46 {
47   qApp->installEventFilter( this );
48 }
49
50 /*!
51  * \brief Destructor
52 */
53 LightApp_SwitchOp::~LightApp_SwitchOp()
54 {
55   
56 }
57
58 /*!
59  * \brief Get module
60 *
61 * Get module. Module is a parent of this class
62 */
63 LightApp_Module* LightApp_SwitchOp::module() const
64 {
65   return myModule;
66 }
67
68 /*!
69  * \brief Get study
70  * \return Active study of application (in current realisation)
71 *
72 * Get study
73 */
74 SUIT_Study* LightApp_SwitchOp::study() const
75 {
76   return module()->application()->activeStudy();
77 }
78
79 /*!
80  * \brief Get operation by widget
81   * \param theWg - key widget to find operation
82   * \return Pointer to the operations if it is found or zero 
83 *
84 * Find operation containing dialog with given widget
85 */
86 LightApp_Operation* LightApp_SwitchOp::operation( QWidget* theWg ) const
87 {
88   // get dialog from widget
89   LightApp_Dialog* aDlg = 0;
90   QWidget* aParent = theWg;
91   while( aParent && !aParent->inherits( "LightApp_Dialog" ) )
92     aParent = aParent->parentWidget();
93
94   if ( aParent && aParent->inherits( "LightApp_Dialog" ) )
95     aDlg = (LightApp_Dialog*)aParent;
96
97   // try to find operation corresponding to the dialog
98   if ( aDlg != 0 && study() != 0 )
99   {
100     QListIterator<SUIT_Operation*> anIter( study()->operations() );
101     while( anIter.hasNext() )
102     {
103       SUIT_Operation* anOp = anIter.next();
104           
105       if ( anOp &&
106            anOp->inherits( "LightApp_Operation" ) &&
107            ((LightApp_Operation*)anOp)->dlg() == aDlg )
108         return ((LightApp_Operation*)anOp);
109     }
110   }
111
112   return 0;
113 }
114
115 /*!
116  * \brief Event filter
117   * \param theObj - object
118   * \param theEv - event
119 *
120 * Event filter. Catched signals off application. If event concerns to dialog then
121 * corresponding operation is found and activated.
122 */
123 bool LightApp_SwitchOp::eventFilter( QObject* theObj, QEvent* theEv )
124 {
125   if ( theObj->inherits( "QWidget" ) && ( theEv->type() == QEvent::Enter ) )
126   {
127     QEvent::Type aType = theEv->type();
128     LightApp_Operation* anOp = operation( (QWidget*)theObj );
129     if ( anOp )
130     {
131       switch ( aType )
132       {
133         case QEvent::Enter:
134         {
135           if ( !anOp->isActive() && anOp->isAutoResumed() &&
136                study() && !study()->blockingOperation( anOp ) )
137             study()->resume( anOp );
138         }
139         break;
140         
141         case QEvent::MouseButtonRelease:
142         case QEvent::MouseButtonPress:
143         case QEvent::MouseButtonDblClick:
144         case QEvent::MouseMove:
145         case QEvent::KeyPress:
146         case QEvent::KeyRelease:
147         {
148           if ( !anOp->isActive() )
149             return true;
150         }
151         break;
152         
153       }
154     }
155   }
156
157   return QObject::eventFilter( theObj, theEv );
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177