]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHGUI/SMESHGUI_Operation.cxx
Salome HOME
new methods:
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Operation.cxx
1 //  SALOME SMESHGUI
2 //
3 //  Copyright (C) 2005  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SMESHGUI_Operation.h
8 //  Author : Sergey LITONIN
9 //  Module : SALOME
10
11 #include "SMESHGUI_Operation.h"
12 #include "SMESHGUI.h"
13 #include "SMESHGUI_VTKUtils.h"
14 #include "SMESHGUI_Selection.h"
15
16 #include <SVTK_ViewWindow.h>
17 #include <SVTK_Selector.h>
18 #include <SUIT_MessageBox.h>
19 #include <SUIT_Desktop.h>
20
21 #include <SalomeApp_Study.h>
22 #include <SalomeApp_VTKSelector.h>
23 #include <SalomeApp_SelectionMgr.h>
24
25 #include <SALOMEDS_SObject.hxx>
26
27 #include <SMESHDS_Mesh.hxx>
28
29 /*
30   Class       : SMESHGUI_Operation
31   Description : Base class for all SMESH operations
32 */
33
34 //=======================================================================
35 // name    : SMESHGUI_Operation
36 // Purpose : Constructor
37 //=======================================================================
38 SMESHGUI_Operation::SMESHGUI_Operation()
39 : SalomeApp_Operation()
40 {
41 }
42
43 SMESHGUI_Operation::~SMESHGUI_Operation()
44 {
45   
46 }
47
48 //=======================================================================
49 // name    : setSelectionMode
50 // Purpose : Set selection mode
51 //=======================================================================
52 void SMESHGUI_Operation::setSelectionMode( const Selection_Mode mode )
53 {
54   SVTK_ViewWindow* wnd = viewWindow();
55   if( wnd )
56     wnd->SetSelectionMode( mode );
57 }
58
59 //=======================================================================
60 // name    : highlight
61 // Purpose : Highlight object in 3d viewer
62 //=======================================================================
63 void SMESHGUI_Operation::highlight( const Handle( SALOME_InteractiveObject )& obj,
64                                     const bool hilight, const bool immediately )
65 {
66   SVTK_ViewWindow* wnd = viewWindow();
67   if( wnd )
68     wnd->highlight( obj, hilight, immediately );
69 }
70
71 //=======================================================================
72 // name    : addOrRemoveIndex
73 // Purpose : Select/deselect cells of mesh
74 //=======================================================================
75 void SMESHGUI_Operation::addOrRemoveIndex( const Handle( SALOME_InteractiveObject )& obj,
76                                            const TColStd_MapOfInteger& indices,
77                                            const bool isModeShift )
78 {
79   SVTK_Selector* sel = selector();
80   if( sel )
81     sel->AddOrRemoveIndex( obj, indices, isModeShift );
82 }
83
84 //=======================================================================
85 // name    : getSMESHGUI
86 // Purpose : Get SMESH module
87 //=======================================================================
88 SMESHGUI* SMESHGUI_Operation::getSMESHGUI() const
89 {
90   return dynamic_cast<SMESHGUI*>( module() );
91 }
92
93 //=======================================================================
94 // name    : viewWindow
95 // Purpose : Get active view window
96 //=======================================================================
97 SVTK_ViewWindow* SMESHGUI_Operation::viewWindow() const
98 {
99   return SMESH::GetViewWindow( getSMESHGUI() );
100 }
101
102 //=======================================================================
103 // name    : selector
104 // Purpose : Get selector
105 //=======================================================================
106 SVTK_Selector* SMESHGUI_Operation::selector() const
107 {
108   SVTK_ViewWindow* wnd = viewWindow();
109   return wnd ? wnd->GetSelector() : 0;
110 }
111
112 //=======================================================================
113 // name    : startOperation
114 // Purpose : Start opeartion
115 //=======================================================================
116 void SMESHGUI_Operation::startOperation()
117 {
118   if( dlg() )
119   {
120     disconnect( dlg(), SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
121     disconnect( dlg(), SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
122     disconnect( dlg(), SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
123     
124     if( dlg()->testButtonFlags( QtxDialog::OK ) )
125       connect( dlg(), SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
126       
127     if( dlg()->testButtonFlags( QtxDialog::Apply ) )
128       connect( dlg(), SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
129       
130     if( dlg()->testButtonFlags( QtxDialog::Cancel ) )
131       connect( dlg(), SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
132   }    
133
134   SalomeApp_Operation::startOperation();
135 }
136
137 //=======================================================================
138 // name    : isReadyToStart
139 // Purpose : Verify whether operation is ready to start
140 //=======================================================================
141 bool SMESHGUI_Operation::isReadyToStart()
142 {
143   if ( !SalomeApp_Operation::isReadyToStart() )
144     return false;
145     
146   if ( getSMESHGUI() == 0 )
147   {
148     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr( "SMESH_WRN_WARNING" ),
149       tr( "NO_MODULE" ), tr( "SMESH_BUT_OK" ) );
150     return false;
151   }
152
153   return true;
154 }
155
156 //=======================================================================
157 // name    : typeById
158 // Purpose : Find type by id
159 //=======================================================================
160 int SMESHGUI_Operation::typeById( const QString& str, const SelectedObjectType objtype ) const
161 {
162   SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
163   if( !_study )
164     return -1;
165
166   _PTR( Study ) st = _study->studyDS();
167
168   int res = -1;
169   if( objtype == Object )
170   {
171     SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
172     if( _study )
173     {
174       int t = SMESHGUI_Selection::type( str, _study->studyDS() );
175       if( t<0 )
176       {
177         //try to get GEOM type
178         _PTR( SObject ) sobj = st->FindObjectID( str.latin1() );
179         if( sobj )
180         {
181           GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
182           if( !CORBA::is_nil( obj ) )
183             res = prefix( "GEOM" ) + obj->GetType();
184         }
185       }
186       else
187         res = prefix( "SMESH" ) + t;
188     }
189   }
190   else  
191   {
192     int pos = str.find( idChar() );
193     QString entry = str.left( pos ),
194             _id = str.mid( pos+1 );
195     bool ok;
196     int id = _id.toInt( &ok );
197     if( ok )
198     {
199       _PTR( SObject ) sobj = st->FindObjectID( entry.latin1() );
200       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
201       SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
202       if( !CORBA::is_nil( mesh ) )
203         res = prefix( "SMESH element" ) + mesh->GetElementType( id, objtype==MeshElement );
204         
205       else if( !CORBA::is_nil( submesh ) )
206         res = prefix( "SMESH element" ) + submesh->GetElementType( id, objtype==MeshElement );
207     }
208   }
209
210   return res;
211 }
212
213 //=======================================================================
214 // name    : prefix
215 // Purpose : Get prefix for module types
216 //=======================================================================
217 int SMESHGUI_Operation::prefix( const QString& name )
218 {
219   if( name == "GEOM" )
220     return 100;
221   else if( name == "SMESH" )
222     return 200;
223   else if( name == "SMESH element" )
224     return 300;
225   else
226     return 0;
227 }
228
229 //=======================================================================
230 // name    : selected
231 // Purpose : Get names, types and ids of selected objects
232 //=======================================================================
233 void SMESHGUI_Operation::selected( QStringList& names, SalomeApp_Dialog::TypesList& types, QStringList& ids ) const
234 {
235   SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
236   SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
237                                         aLast = list.end();
238   for( ; anIt!=aLast; anIt++ )
239   {
240     SalomeApp_DataOwner* owner = dynamic_cast<SalomeApp_DataOwner*>( (*anIt).operator->() );
241     SalomeApp_SVTKDataOwner* vtkowner = dynamic_cast<SalomeApp_SVTKDataOwner*>( (*anIt).operator->() );
242
243     if( vtkowner )
244     {
245       QString id_str = QString( "%1%2%3" ).arg( vtkowner->entry() ).arg( idChar() ), current_id_str;
246       Selection_Mode mode = vtkowner->GetMode();
247       SelectedObjectType objtype = mode == NodeSelection ? MeshNode : MeshElement;
248       const TColStd_IndexedMapOfInteger& ownerids = vtkowner->GetIds();
249
250       for( int i=1, n=ownerids.Extent(); i<=n; i++ )
251       {
252         int curid = ownerids( i );
253         current_id_str = id_str.arg( curid );
254         ids.append( current_id_str );
255         types.append( typeById( current_id_str, objtype ) );
256         names.append( QString( "%1" ).arg( curid ) );
257       }
258     }
259
260     else if( owner )
261     {
262       QString id = owner->entry();
263       ids.append( id );
264       types.append( typeById( id, Object ) );
265       names.append( owner->IO()->getName() );
266     }
267   }
268 }
269
270 //=======================================================================
271 // name    : idChar
272 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
273 //=======================================================================
274 QChar SMESHGUI_Operation::idChar() const
275 {
276   return '#';
277 }
278
279 //=======================================================================
280 // name    : setDialogActive
281 // Purpose : 
282 //=======================================================================
283 void SMESHGUI_Operation::setDialogActive( const bool active )
284 {
285   SalomeApp_Operation::setDialogActive( active );
286
287   SMESHGUI_Dialog* d = dynamic_cast<SMESHGUI_Dialog*>( dlg() );
288   if( d )
289     d->setContentActive( active );
290
291 }
292
293 //=======================================================================
294 // name    : studyDS
295 // Purpose :
296 //=======================================================================
297 _PTR(Study) SMESHGUI_Operation::studyDS() const
298 {
299   SalomeApp_Study* s = dynamic_cast<SalomeApp_Study*>( study() );
300   if( s )
301     return s->studyDS();
302 }
303
304 //=======================================================================
305 // name    : onOk
306 // Purpose :
307 //=======================================================================
308 void SMESHGUI_Operation::onOk()
309 {
310   if( onApply() )
311     commit();
312   else
313     abort();
314 }
315
316 //=======================================================================
317 // name    : onApply
318 // Purpose :
319 //=======================================================================
320 bool SMESHGUI_Operation::onApply()
321 {
322   return false;
323 }
324
325 //=======================================================================
326 // name    : onClose
327 // Purpose :
328 //=======================================================================
329 void SMESHGUI_Operation::onCancel()
330 {
331   abort();
332 }
333
334 //=======================================================================
335 // name    : commitOperation
336 // Purpose :
337 //=======================================================================
338 void SMESHGUI_Operation::commitOperation()
339 {
340   selectionMgr()->clearFilters();
341   SalomeApp_Operation::commitOperation();
342 }
343
344 //=======================================================================
345 // name    : abortOperation
346 // Purpose :
347 //=======================================================================
348 void SMESHGUI_Operation::abortOperation()
349 {
350   selectionMgr()->clearFilters();
351   SalomeApp_Operation::abortOperation();
352 }