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