Salome HOME
Copyrights update
[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 *  LIGHT LightApp
21 *
22 *  Copyright (C) 2005  CEA/DEN, EDF R&D
23 *
24 *
25 *
26 *  File   : LightApp_SwitchOp.h
27 *  Author : Sergey LITONIN
28 *  Module : LIGHT
29 */
30
31 #include "LightApp_SwitchOp.h"
32 #include "LightApp_Module.h"
33 #include "LightApp_Operation.h"
34 #include "LightApp_Dialog.h"
35 #include <CAM_Application.h>
36 #include <SUIT_Operation.h>
37 #include <SUIT_Study.h>
38 #include <qevent.h>
39 #include <qwidget.h>
40 #include <qptrlist.h>
41 #include <qapplication.h>
42
43 /*!
44  * \brief Constructor
45   * \param theParent - parent of object
46 *
47 * Creates instance of the object. Connects signals and slots. Install eveny filter
48 * on application
49 */
50 LightApp_SwitchOp::LightApp_SwitchOp( LightApp_Module* theModule )
51 : QObject( 0 ),
52   myModule( theModule )
53 {
54   qApp->installEventFilter( this );
55 }
56
57 /*!
58  * \brief Destructor
59 */
60 LightApp_SwitchOp::~LightApp_SwitchOp()
61 {
62   
63 }
64
65 /*!
66  * \brief Get module
67 *
68 * Get module. Module is a parent of this class
69 */
70 LightApp_Module* LightApp_SwitchOp::module() const
71 {
72   return myModule;
73 }
74
75 /*!
76  * \brief Get study
77  * \return Active study of application (in current realisation)
78 *
79 * Get study
80 */
81 SUIT_Study* LightApp_SwitchOp::study() const
82 {
83   return module()->application()->activeStudy();
84 }
85
86 /*!
87  * \brief Get operation by widget
88   * \param theWg - key widget to find operation
89   * \return Pointer to the operations if it is found or zero 
90 *
91 * Find operation containing dialog with given widget
92 */
93 LightApp_Operation* LightApp_SwitchOp::operation( QWidget* theWg ) const
94 {
95   // get dialog from widget
96   LightApp_Dialog* aDlg = 0;
97   QWidget* aParent = theWg;
98   while( aParent && !aParent->inherits( "LightApp_Dialog" ) )
99     aParent = aParent->parentWidget();
100
101   if ( aParent && aParent->inherits( "LightApp_Dialog" ) )
102     aDlg = (LightApp_Dialog*)aParent;
103
104   // try to find operation corresponding to the dialog
105   if ( aDlg != 0 && study() != 0 )
106   {
107     QPtrListIterator<SUIT_Operation> anIter( study()->operations() );
108     while( SUIT_Operation* anOp = anIter.current() )
109     {
110       if ( anOp->inherits( "LightApp_Operation" ) &&
111            ((LightApp_Operation*)anOp)->dlg() == aDlg )
112         return ((LightApp_Operation*)anOp);
113       ++anIter;
114    }
115   }
116
117   return 0;
118 }
119
120 /*!
121  * \brief Event filter
122   * \param theObj - object
123   * \param theEv - event
124 *
125 * Event filter. Catched signals off application. If event concerns to dialog then
126 * corresponding operation is found and activated.
127 */
128 bool LightApp_SwitchOp::eventFilter( QObject* theObj, QEvent* theEv )
129 {
130   if ( theObj->inherits( "QWidget" ) && ( theEv->type() == QEvent::Enter ) )
131   {
132     QEvent::Type aType = theEv->type();
133     LightApp_Operation* anOp = operation( (QWidget*)theObj );
134     if ( anOp )
135     {
136       switch ( aType )
137       {
138         case QEvent::Enter:
139         {
140           if ( !anOp->isActive() && anOp->isAutoResumed() &&
141                study() && !study()->blockingOperation( anOp ) )
142             study()->resume( anOp );
143         }
144         break;
145         
146         case QEvent::MouseButtonRelease:
147         case QEvent::MouseButtonPress:
148         case QEvent::MouseButtonDblClick:
149         case QEvent::MouseMove:
150         case QEvent::KeyPress:
151         case QEvent::KeyRelease:
152         {
153           if ( !anOp->isActive() )
154             return true;
155         }
156         break;
157         
158       }
159     }
160   }
161
162   return QObject::eventFilter( theObj, theEv );
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182