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