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