Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SelectionOp.cxx
1 //  Copyright (C) 2007-2008  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 // File   : SMESHGUI_SelectionOp.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
24 // SMESH includes
25 //
26 #include "SMESHGUI_SelectionOp.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_MeshUtils.h"
31 #include "SMESHGUI_Selection.h"
32
33 #include <SMESH_Actor.h>
34 #include <SMDS_Mesh.hxx>
35 #include <SMDS_MeshNode.hxx>
36
37 // SALOME GUI includes
38 #include <SUIT_SelectionFilter.h>
39 #include <LightApp_SelectionMgr.h>
40 #include <SalomeApp_Study.h>
41 #include <SVTK_ViewWindow.h>
42 #include <SVTK_ViewModel.h>
43
44 #include <SALOME_ListIO.hxx>
45 #include <SALOME_ListIteratorOfListIO.hxx>
46
47 // SALOME KERNEL includes 
48 #include <SALOMEDS_SObject.hxx>
49
50 /*
51   Class       : SMESHGUI_SelectionOp
52   Description : Base operation for all operations using object selection in viewer or objectbrowser
53                 through common widgets created by LightApp_Dialog::createObject
54 */
55
56 //=================================================================================
57 // name     : SMESHGUI_SelectionOp
58 // purpose  : 
59 //=================================================================================
60 SMESHGUI_SelectionOp::SMESHGUI_SelectionOp( const Selection_Mode mode )
61 : SMESHGUI_Operation(),
62   myDefSelectionMode( mode )
63 {
64 }
65
66 //=================================================================================
67 // name     : ~SMESHGUI_SelectionOp
68 // purpose  :
69 //=================================================================================
70 SMESHGUI_SelectionOp::~SMESHGUI_SelectionOp()
71 {
72   removeCustomFilters();
73 }
74
75 //=================================================================================
76 // name     : startOperation
77 // purpose  :
78 //=================================================================================
79 void SMESHGUI_SelectionOp::startOperation()
80 {
81   myOldSelectionMode = selectionMode();
82   setSelectionMode( myDefSelectionMode );
83
84   SMESHGUI_Operation::startOperation();
85   if( dlg() )
86   {
87     disconnect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
88     disconnect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
89     disconnect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
90     connect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
91     connect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
92     connect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
93   }
94 }
95
96 //=================================================================================
97 // name     : removeCustomFilters
98 // purpose  :
99 //=================================================================================
100 void SMESHGUI_SelectionOp::removeCustomFilters()
101 {
102   if (myFilters.count() > 0) {
103     LightApp_SelectionMgr* mgr = selectionMgr();
104     Filters::const_iterator anIt = myFilters.begin(),
105                             aLast = myFilters.end();
106     for ( ; anIt != aLast; anIt++) {
107       if (anIt.value()) {
108         if (mgr) mgr->removeFilter(anIt.value());
109         delete anIt.value();
110       }
111     }
112
113     myFilters.clear();
114   }
115 }
116
117 //=================================================================================
118 // name     : commitOperation
119 // purpose  :
120 //=================================================================================
121 void SMESHGUI_SelectionOp::commitOperation()
122 {
123   SMESHGUI_Operation::commitOperation();  
124   removeCustomFilters();
125   setSelectionMode( myOldSelectionMode );
126 }
127
128 //=================================================================================
129 // name     : abortOperation
130 // purpose  :
131 //=================================================================================
132 void SMESHGUI_SelectionOp::abortOperation()
133 {
134   SMESHGUI_Operation::abortOperation();
135   removeCustomFilters();
136   setSelectionMode( myOldSelectionMode );  
137 }
138
139 //=================================================================================
140 // name     : selectionDone
141 // purpose  :
142 //=================================================================================
143 void SMESHGUI_SelectionOp::selectionDone()
144 {
145   if( !dlg() )
146     return;
147
148   if( selectionMode()!=ActorSelection )
149   {
150     SALOME_ListIO aList;
151     selectionMgr()->selectedObjects( aList, SVTK_Viewer::Type() );
152
153     if( aList.Extent() != 1 ) //we can select nodes or elements only within one mesh
154     {
155       dlg()->clearSelection();
156       return;
157     }    
158   }
159     
160   QStringList names, ids;
161   LightApp_Dialog::TypesList types;
162   selected( names, types, ids );
163   dlg()->selectObject( names, types, ids );
164 }
165
166 //=================================================================================
167 // name     : createFilter
168 // purpose  :
169 //=================================================================================
170 SUIT_SelectionFilter* SMESHGUI_SelectionOp::createFilter( const int ) const
171 {
172   return 0;
173 }
174
175 //=================================================================================
176 // name     : onActivateObject
177 // purpose  :
178 //=================================================================================
179 void SMESHGUI_SelectionOp::onActivateObject( int id )
180 {
181   LightApp_SelectionMgr* mgr = selectionMgr();
182   if( !mgr )
183     return;
184     
185   if( !myFilters.contains( id ) )
186     myFilters[ id ] = createFilter( id );
187
188   if( myFilters[ id ] )
189     mgr->installFilter( myFilters[ id ] );
190
191   selectionDone();
192 }
193
194 //=================================================================================
195 // name     : onDeactivateObject
196 // purpose  :
197 //=================================================================================
198 void SMESHGUI_SelectionOp::onDeactivateObject( int id )
199 {
200   removeCustomFilters();
201 }
202
203 //=================================================================================
204 // name     : initDialog
205 // purpose  :
206 //=================================================================================
207 void SMESHGUI_SelectionOp::initDialog()
208 {
209   if( dlg() )
210   {
211     dlg()->clearSelection();
212     dlg()->deactivateAll();
213   }
214 }
215
216 //=================================================================================
217 // name     : initDialog
218 // purpose  :
219 //=================================================================================
220 void SMESHGUI_SelectionOp::onSelectionChanged( int )
221 {
222 }
223
224 //=======================================================================
225 // name    : selectionMode
226 // Purpose : Returns selection mode
227 //=======================================================================
228 Selection_Mode SMESHGUI_SelectionOp::selectionMode() const
229 {
230   SVTK_ViewWindow* wnd = viewWindow();
231   if( wnd )
232     return wnd->SelectionMode();
233   else
234     return ActorSelection;
235 }
236
237 //=======================================================================
238 // name    : setSelectionMode
239 // Purpose : Set selection mode
240 //=======================================================================
241 void SMESHGUI_SelectionOp::setSelectionMode( const Selection_Mode mode )
242 {
243   SVTK_ViewWindow* wnd = viewWindow();
244   if( wnd )
245     wnd->SetSelectionMode( mode );
246 }
247
248 //=======================================================================
249 // name    : highlight
250 // Purpose : Highlight object in 3d viewer
251 //=======================================================================
252 void SMESHGUI_SelectionOp::highlight( const Handle( SALOME_InteractiveObject )& obj,
253                                       const bool hilight, const bool immediately )
254 {
255   SVTK_ViewWindow* wnd = viewWindow();
256   if( wnd )
257     wnd->highlight( obj, hilight, immediately );
258 }
259
260 //=======================================================================
261 // name    : addOrRemoveIndex
262 // Purpose : Select/deselect cells of mesh
263 //=======================================================================
264 void SMESHGUI_SelectionOp::addOrRemoveIndex( const Handle( SALOME_InteractiveObject )& obj,
265                                              const TColStd_MapOfInteger& indices,
266                                              const bool isModeShift )
267 {
268   SVTK_Selector* sel = selector();
269   if( sel )
270     sel->AddOrRemoveIndex( obj, indices, isModeShift );
271 }
272
273 //=======================================================================
274 // name    : viewWindow
275 // Purpose : Get active view window
276 //=======================================================================
277 SVTK_ViewWindow* SMESHGUI_SelectionOp::viewWindow() const
278 {
279   return SMESH::GetViewWindow( getSMESHGUI() );
280 }
281
282 //=======================================================================
283 // name    : selector
284 // Purpose : Get selector
285 //=======================================================================
286 SVTK_Selector* SMESHGUI_SelectionOp::selector() const
287 {
288   SVTK_ViewWindow* wnd = viewWindow();
289   return wnd ? wnd->GetSelector() : 0;
290 }
291
292 //=======================================================================
293 // name    : typeById
294 // Purpose : Find type by id
295 //=======================================================================
296 int SMESHGUI_SelectionOp::typeById( const QString& str, const EntityType objtype ) const
297 {
298   SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
299   if( !_study )
300     return -1;
301
302   _PTR( Study ) st = _study->studyDS();
303
304   int res = -1;
305   if( objtype == Object )
306   {
307     SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
308     if( _study )
309     {
310       int t = SMESHGUI_Selection::type( str, _study->studyDS() );
311       if( t<0 )
312       {
313         //try to get GEOM type
314         _PTR( SObject ) sobj = st->FindObjectID( str.toLatin1().data() );
315         if( sobj )
316         {
317           GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow(
318             dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
319           if( !CORBA::is_nil( obj ) )
320             res = SMESHGUI_Dialog::prefix( "GEOM" ) + obj->GetType();
321         }
322       }
323       else
324         res = SMESHGUI_Dialog::prefix( "SMESH" ) + t;
325     }
326   }
327   else
328   {
329     int pos = str.indexOf( idChar() );
330     QString entry = str.left( pos ),
331             _id = str.mid( pos+1 );
332     bool ok;
333     int id = _id.toInt( &ok );
334     if( ok )
335     {
336       _PTR( SObject ) sobj = st->FindObjectID( entry.toLatin1().data() );
337       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( 
338         dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
339       SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( 
340         dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
341       if( !CORBA::is_nil( mesh ) )
342         res = SMESHGUI_Dialog::prefix( "SMESH element" ) + 
343           mesh->GetElementType( id, objtype==MeshElement );
344
345       else if( !CORBA::is_nil( submesh ) )
346         res = SMESHGUI_Dialog::prefix( "SMESH element" ) + 
347           submesh->GetElementType( id, objtype==MeshElement );
348     }
349   }
350
351   return res;
352 }
353
354 //=======================================================================
355 // name    : selected
356 // Purpose : Get names, types and ids of selected objects
357 //=======================================================================
358 void SMESHGUI_SelectionOp::selected( QStringList& names,
359                                      LightApp_Dialog::TypesList& types,
360                                      QStringList& ids ) const
361 {
362 /*  SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
363   SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
364                                         aLast = list.end();
365   for( ; anIt!=aLast; anIt++ )
366   {
367     LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( (*anIt).operator->() );
368     LightApp_SVTKDataOwner* vtkowner = dynamic_cast<LightApp_SVTKDataOwner*>( (*anIt).operator->() );
369
370     if( vtkowner )
371     {
372       QString id_str = QString( "%1%2%3" ).arg( vtkowner->entry() ).arg( idChar() ), current_id_str;
373       Selection_Mode mode = vtkowner->GetMode();
374       EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
375       const TColStd_IndexedMapOfInteger& ownerids = vtkowner->GetIds();
376
377       for( int i=1, n=ownerids.Extent(); i<=n; i++ )
378       {
379         int curid = ownerids( i );
380         current_id_str = id_str.arg( curid );
381         ids.append( current_id_str );
382         types.append( typeById( current_id_str, objtype ) );
383         names.append( QString( "%1" ).arg( curid ) );
384       }
385     }
386
387     else if( owner )
388     {
389       QString id = owner->entry();
390       ids.append( id );
391       types.append( typeById( id, Object ) );
392       names.append( owner->IO()->getName() );
393     }
394   }*/
395
396   SALOME_ListIO selObjs;
397   TColStd_IndexedMapOfInteger selIndices;
398   selectionMgr()->selectedObjects( selObjs );
399   Selection_Mode mode = selectionMode();
400   EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
401
402   for( SALOME_ListIteratorOfListIO anIt( selObjs ); anIt.More(); anIt.Next() )
403   {
404     selIndices.Clear();
405     selectionMgr()->GetIndexes( anIt.Value(), selIndices );
406     if( selIndices.Extent() > 0 )
407     {
408       QString id_str = QString( "%1%2%3" ).arg( anIt.Value()->getEntry() ).arg( idChar() ), current_id_str;
409       for( int i=1, n=selIndices.Extent(); i<=n; i++ )
410       {
411         int curid = selIndices( i );
412         current_id_str = id_str.arg( curid );
413         ids.append( current_id_str );
414         types.append( typeById( current_id_str, objtype ) );
415         names.append( QString( "%1" ).arg( curid ) );
416       }
417     }
418     else
419     {
420       QString id = anIt.Value()->getEntry();
421       ids.append( id );
422       types.append( typeById( id, Object ) );
423       SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
424       if( _study )
425       {
426         _PTR(SObject) obj = _study->studyDS()->FindObjectID( anIt.Value()->getEntry() );
427         if( obj )
428           names.append( obj->GetName().c_str() );
429       }
430     }
431   }
432 }
433
434 //=======================================================================
435 // name    : idChar
436 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
437 //=======================================================================
438 QChar SMESHGUI_SelectionOp::idChar() const
439 {
440   return '#';
441 }
442
443 //=================================================================================
444 // name     : mesh
445 // purpose  :
446 //=================================================================================
447 SMESH::SMESH_Mesh_var SMESHGUI_SelectionOp::mesh() const
448 {
449   if( selectionMode()==ActorSelection )
450     return SMESH::SMESH_Mesh::_nil();
451     
452   SALOME_ListIO sel; selectionMgr()->selectedObjects( sel, SVTK_Viewer::Type() );
453   if( sel.Extent()==1 )
454     return SMESH::GetMeshByIO( sel.First() );
455   else
456     return SMESH::SMESH_Mesh::_nil();
457 }
458
459 //=================================================================================
460 // name     : actor
461 // purpose  :
462 //=================================================================================
463 SMESH_Actor* SMESHGUI_SelectionOp::actor() const
464 {
465   SMESH::SMESH_Mesh_var m = mesh();
466   if( !m->_is_nil() )
467     return SMESH::FindActorByObject( m.in() );
468   else
469     return 0;
470 }
471
472 //=================================================================================
473 // name     : onTextChanged
474 // purpose  :
475 //=================================================================================
476 void SMESHGUI_SelectionOp::onTextChanged( int, const QStringList& list )
477 {
478     if( !dlg() )
479       return;
480
481     TColStd_MapOfInteger newIndices;
482
483     SALOME_ListIO sel; selectionMgr()->selectedObjects( sel );
484     SMESH_Actor* anActor = actor();
485     if( sel.Extent()==0 || !anActor )
486       return;
487
488     SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
489
490     IdList ids; extractIds( list, ids, '\0' );
491     IdList::const_iterator anIt = ids.begin(),
492                            aLast = ids.end();
493     for( ; anIt!=aLast; anIt++ )
494       if( const SMDS_MeshNode * n = aMesh->FindNode( *anIt ) )
495         newIndices.Add( n->GetID() );
496
497     selector()->AddOrRemoveIndex( sel.First(), newIndices, false );
498     highlight( sel.First(), true, true );
499
500     QStringList names, _ids; LightApp_Dialog::TypesList types;
501     selected( names, types, _ids );
502     dlg()->selectObject( names, types, _ids, false );
503 }
504
505 //=================================================================================
506 // name     : selectedIds
507 // purpose  :
508 //=================================================================================
509 void SMESHGUI_SelectionOp::selectedIds( const int id, IdList& list ) const
510 {
511   if( !dlg() )
512     return;
513
514   QStringList ids; dlg()->selectedObject( id, ids );
515   extractIds( ids, list );
516 }
517
518 //=================================================================================
519 // name     : extractIds
520 // purpose  :
521 //=================================================================================
522 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list, const QChar idchar )
523 {
524   QStringList::const_iterator anIt = ids.begin(),
525                               aLast = ids.end();
526   QString id_str;
527   for( ; anIt!=aLast; anIt++ )
528   {
529     id_str = *anIt;
530     int pos = idchar=='\0' ? -1 : id_str.indexOf( idchar );
531     int id = -1;
532     if( idchar=='\0' || pos>=0 )
533     {
534       id = id_str.mid( pos+1 ).toInt();
535       list.append( id );
536     }
537   }
538 }
539
540 //=================================================================================
541 // name     : extractIds
542 // purpose  :
543 //=================================================================================
544 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list ) const
545 {
546   extractIds( ids, list, idChar() );
547 }