Salome HOME
*** empty log message ***
[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     
140     if( dlg()->testButtonFlags( QtxDialog::OK ) )
141       connect( dlg(), SIGNAL( dlgOk() ), this, SLOT( onOk() ) );
142       
143     if( dlg()->testButtonFlags( QtxDialog::Apply ) )
144       connect( dlg(), SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
145       
146     if( dlg()->testButtonFlags( QtxDialog::Cancel ) )
147       connect( dlg(), SIGNAL( dlgCancel() ), this, SLOT( onCancel() ) );
148
149     initDialog();
150   }
151
152   SalomeApp_Operation::startOperation();
153 }
154
155 //=======================================================================
156 // name    : isReadyToStart
157 // Purpose : Verify whether operation is ready to start
158 //=======================================================================
159 bool SMESHGUI_Operation::isReadyToStart()
160 {
161   if ( !SalomeApp_Operation::isReadyToStart() )
162     return false;
163     
164   if ( getSMESHGUI() == 0 )
165   {
166     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr( "SMESH_WRN_WARNING" ),
167       tr( "NO_MODULE" ), tr( "SMESH_BUT_OK" ) );
168     return false;
169   }
170
171   return true;
172 }
173
174 //=======================================================================
175 // name    : typeById
176 // Purpose : Find type by id
177 //=======================================================================
178 int SMESHGUI_Operation::typeById( const QString& str, const SelectedObjectType objtype ) const
179 {
180   SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
181   if( !_study )
182     return -1;
183
184   _PTR( Study ) st = _study->studyDS();
185
186   int res = -1;
187   if( objtype == Object )
188   {
189     SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
190     if( _study )
191     {
192       int t = SMESHGUI_Selection::type( str, _study->studyDS() );
193       if( t<0 )
194       {
195         //try to get GEOM type
196         _PTR( SObject ) sobj = st->FindObjectID( str.latin1() );
197         if( sobj )
198         {
199           GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
200           if( !CORBA::is_nil( obj ) )
201             res = prefix( "GEOM" ) + obj->GetType();
202         }
203       }
204       else
205         res = prefix( "SMESH" ) + t;
206     }
207   }
208   else  
209   {
210     int pos = str.find( idChar() );
211     QString entry = str.left( pos ),
212             _id = str.mid( pos+1 );
213     bool ok;
214     int id = _id.toInt( &ok );
215     if( ok )
216     {
217       _PTR( SObject ) sobj = st->FindObjectID( entry.latin1() );
218       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
219       SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
220       if( !CORBA::is_nil( mesh ) )
221         res = prefix( "SMESH element" ) + mesh->GetElementType( id, objtype==MeshElement );
222         
223       else if( !CORBA::is_nil( submesh ) )
224         res = prefix( "SMESH element" ) + submesh->GetElementType( id, objtype==MeshElement );
225     }
226   }
227
228   return res;
229 }
230
231 //=======================================================================
232 // name    : prefix
233 // Purpose : Get prefix for module types
234 //=======================================================================
235 int SMESHGUI_Operation::prefix( const QString& name )
236 {
237   if( name == "GEOM" )
238     return 100;
239   else if( name == "SMESH" )
240     return 200;
241   else if( name == "SMESH element" )
242     return 300;
243   else
244     return 0;
245 }
246
247 //=======================================================================
248 // name    : selected
249 // Purpose : Get names, types and ids of selected objects
250 //=======================================================================
251 void SMESHGUI_Operation::selected( QStringList& names, SalomeApp_Dialog::TypesList& types, QStringList& ids ) const
252 {
253   SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
254   SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
255                                         aLast = list.end();
256   for( ; anIt!=aLast; anIt++ )
257   {
258     SalomeApp_DataOwner* owner = dynamic_cast<SalomeApp_DataOwner*>( (*anIt).operator->() );
259     SalomeApp_SVTKDataOwner* vtkowner = dynamic_cast<SalomeApp_SVTKDataOwner*>( (*anIt).operator->() );
260
261     if( vtkowner )
262     {
263       QString id_str = QString( "%1%2%3" ).arg( vtkowner->entry() ).arg( idChar() ), current_id_str;
264       Selection_Mode mode = vtkowner->GetMode();
265       SelectedObjectType objtype = mode == NodeSelection ? MeshNode : MeshElement;
266       const TColStd_IndexedMapOfInteger& ownerids = vtkowner->GetIds();
267
268       for( int i=1, n=ownerids.Extent(); i<=n; i++ )
269       {
270         int curid = ownerids( i );
271         current_id_str = id_str.arg( curid );
272         ids.append( current_id_str );
273         types.append( typeById( current_id_str, objtype ) );
274         names.append( QString( "%1" ).arg( curid ) );
275       }
276     }
277
278     else if( owner )
279     {
280       QString id = owner->entry();
281       ids.append( id );
282       types.append( typeById( id, Object ) );
283       names.append( owner->IO()->getName() );
284     }
285   }
286 }
287
288 //=======================================================================
289 // name    : idChar
290 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
291 //=======================================================================
292 QChar SMESHGUI_Operation::idChar() const
293 {
294   return '#';
295 }
296
297 //=======================================================================
298 // name    : setDialogActive
299 // Purpose : 
300 //=======================================================================
301 void SMESHGUI_Operation::setDialogActive( const bool active )
302 {
303   SalomeApp_Operation::setDialogActive( active );
304
305   SMESHGUI_Dialog* d = dynamic_cast<SMESHGUI_Dialog*>( dlg() );
306   if( d )
307     d->setContentActive( active );
308
309 }
310
311 //=======================================================================
312 // name    : studyDS
313 // Purpose :
314 //=======================================================================
315 _PTR(Study) SMESHGUI_Operation::studyDS() const
316 {
317   SalomeApp_Study* s = dynamic_cast<SalomeApp_Study*>( study() );
318   if( s )
319     return s->studyDS();
320 }
321
322 //=======================================================================
323 // name    : onOk
324 // Purpose :
325 //=======================================================================
326 void SMESHGUI_Operation::onOk()
327 {
328   if( onApply() )
329     commit();
330   else
331     abort();
332 }
333
334 //=======================================================================
335 // name    : onApply
336 // Purpose :
337 //=======================================================================
338 bool SMESHGUI_Operation::onApply()
339 {
340   return false;
341 }
342
343 //=======================================================================
344 // name    : onClose
345 // Purpose :
346 //=======================================================================
347 void SMESHGUI_Operation::onCancel()
348 {
349   abort();
350 }
351
352 //=======================================================================
353 // name    : initDialog
354 // Purpose :
355 //=======================================================================
356 void SMESHGUI_Operation::initDialog()
357 {
358 }