Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Operation.cxx
1 //  Copyright (C) 2007-2010  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 // SMESH SMDS : implementaion of Salome mesh data structure
24 // File   : SMESHGUI_Operation.cxx
25 // Author : Sergey LITONIN, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_Operation.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Dialog.h"
32
33 // SALOME GUI includes
34 #include <SalomeApp_Study.h>
35 #include <LightApp_Application.h>
36
37 #include <SUIT_Session.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_ResourceMgr.h>
41
42 // Qt includes
43 #include <QStringList>
44
45 /*
46   Class       : SMESHGUI_Operation
47   Description : Base class for all SMESH operations
48 */
49
50 //=======================================================================
51 // name    : SMESHGUI_Operation
52 // Purpose : Constructor
53 //=======================================================================
54 SMESHGUI_Operation::SMESHGUI_Operation()
55 : LightApp_Operation()
56 {
57   myHelpFileName = "";
58 }
59
60 //=======================================================================
61 // name    : ~SMESHGUI_Operation
62 // Purpose : Destructor
63 //=======================================================================
64 SMESHGUI_Operation::~SMESHGUI_Operation()
65 {
66 }
67
68 //=======================================================================
69 // name    : getSMESHGUI
70 // Purpose : Get SMESH module
71 //=======================================================================
72 SMESHGUI* SMESHGUI_Operation::getSMESHGUI() const
73 {
74   return dynamic_cast<SMESHGUI*>( module() );
75 }
76
77 //=======================================================================
78 // name    : startOperation
79 // Purpose : Start opeartion
80 //=======================================================================
81 void SMESHGUI_Operation::startOperation()
82 {
83   if( dlg() )
84   {
85     disconnect( dlg(), SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
86     disconnect( dlg(), SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
87     disconnect( dlg(), SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
88     disconnect( dlg(), SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
89     disconnect( dlg(), SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
90     
91     if( dlg()->testButtonFlags( QtxDialog::OK ) )
92       connect( dlg(), SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
93       
94     if( dlg()->testButtonFlags( QtxDialog::Apply ) )
95       connect( dlg(), SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
96       
97     if( dlg()->testButtonFlags( QtxDialog::Cancel ) )
98       connect( dlg(), SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
99
100     if( dlg()->testButtonFlags( QtxDialog::Help ) )
101       connect( dlg(), SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
102       
103     //if( dlg()->testButtonFlags( QtxDialog::Close ) )
104     //if dialog hasn't close, cancel, no and etc buttons, dlgClose will be emitted when dialog is closed not by OK
105     connect( dlg(), SIGNAL( dlgClose() ), this, SLOT( onCancel() ) );
106
107     initDialog();
108   }
109
110   LightApp_Operation::startOperation();
111 }
112
113 //=======================================================================
114 // name    : isReadyToStart
115 // Purpose : Verify whether operation is ready to start
116 //=======================================================================
117 bool SMESHGUI_Operation::isReadyToStart() const
118 {
119   if ( !LightApp_Operation::isReadyToStart() )
120     return false;
121   else if ( getSMESHGUI() == 0 )
122   {
123     SUIT_MessageBox::warning( desktop(), tr( "SMESH_WRN_WARNING" ),
124                               tr( "NO_MODULE" ) );
125     return false;
126   }
127   else if ( isStudyLocked() )
128     return false;
129   
130   return true;
131 }
132
133 //=======================================================================
134 // name    : setDialogActive
135 // Purpose : 
136 //=======================================================================
137 void SMESHGUI_Operation::setDialogActive( const bool active )
138 {
139   LightApp_Operation::setDialogActive( active );
140
141   SMESHGUI_Dialog* d = dynamic_cast<SMESHGUI_Dialog*>( dlg() );
142   if( d )
143     d->setContentActive( active );
144
145 }
146
147 //=======================================================================
148 // name    : studyDS
149 // Purpose :
150 //=======================================================================
151 _PTR(Study) SMESHGUI_Operation::studyDS() const
152 {
153   SalomeApp_Study* s = dynamic_cast<SalomeApp_Study*>( study() );
154   return s->studyDS();
155 }
156
157 //=======================================================================
158 // name    : onOk
159 // Purpose :
160 //=======================================================================
161 void SMESHGUI_Operation::onOk()
162 {
163   if( onApply() )
164     commit();
165   //else
166   //  abort();
167 }
168
169 //=======================================================================
170 // name    : onApply
171 // Purpose :
172 //=======================================================================
173 bool SMESHGUI_Operation::onApply()
174 {
175   return false;
176 }
177
178 //=======================================================================
179 // name    : onClose
180 // Purpose :
181 //=======================================================================
182 void SMESHGUI_Operation::onCancel()
183 {
184   abort();
185 }
186
187 //=======================================================================
188 // name    : onHelp
189 // Purpose :
190 //=======================================================================
191 void SMESHGUI_Operation::onHelp()
192 {
193   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
194   if (app) 
195     app->onHelpContextModule(getSMESHGUI() ? app->moduleName(getSMESHGUI()->moduleName()) : QString(""), myHelpFileName);
196   else {
197     QString platform;
198 #ifdef WIN32
199     platform = "winapplication";
200 #else
201     platform = "application";
202 #endif
203     SUIT_MessageBox::warning( desktop(), tr("WRN_WARNING"),
204                               tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
205                               arg(app->resourceMgr()->stringValue("ExternalBrowser", 
206                                                                   platform)).
207                               arg(myHelpFileName) );
208   }
209 }
210
211 //=======================================================================
212 // name    : initDialog
213 // Purpose :
214 //=======================================================================
215 void SMESHGUI_Operation::initDialog()
216 {
217 }
218
219 /*!
220  * \brief Verifies whether study of operation is locked
221   * \param theMess - specifies whether message box must be shown if study is locked
222   * \return State of study.
223 *
224 * Verifies whether study of operation is locked. If second parameter is TRUE and study
225 * is locked when corresponding message box appears
226 */
227 bool SMESHGUI_Operation::isStudyLocked( const bool theMess ) const
228 {
229   if ( studyDS() )
230   {
231     if ( studyDS()->GetProperties()->IsLocked() )
232     {
233       if ( theMess )
234         SUIT_MessageBox::warning( SMESHGUI::desktop(), tr( "WRN_WARNING" ),
235                                   tr( "WRN_STUDY_LOCKED" ) );
236       return true;
237     }
238   }
239   
240   return false;
241 }
242
243 /*!
244  * \brief Verifies whether given operator is valid for this one
245   * \param theOtherOp - other operation
246   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
247 *
248 * Virtual method redefined from base class verifies whether given operator is valid for
249 * this one (i.e. can be started "above" this operator). In current implementation method
250 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
251 * elements.
252 */
253 bool SMESHGUI_Operation::isValid( SUIT_Operation* theOtherOp ) const
254 {
255   static QStringList anOps;
256   if ( anOps.count() == 0 )
257   {
258     anOps.append( "SMESHGUI_DeleteOp" );
259     // to do add other operations here
260   }
261
262   return theOtherOp && theOtherOp->inherits( "SMESHGUI_Operation" ) &&
263          ( !anOps.contains( theOtherOp->metaObject()->className() ) || 
264            anOps.contains( metaObject()->className() ) );
265
266   return true;
267 }