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