Salome HOME
Update of CheckDone
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SelectionOp.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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
23 // File   : SMESHGUI_SelectionOp.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_SelectionOp.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_MeshUtils.h"
32 #include "SMESHGUI_Selection.h"
33
34 #include <SMESH_Actor.h>
35 #include <SMDS_Mesh.hxx>
36 #include <SMDS_MeshNode.hxx>
37
38 // SALOME GUI includes
39 #include <SUIT_SelectionFilter.h>
40 #include <LightApp_SelectionMgr.h>
41 #include <SalomeApp_Study.h>
42 #include <SVTK_ViewWindow.h>
43 #include <SVTK_ViewModel.h>
44
45 #include <SALOME_ListIO.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 highlight, const bool immediately )
254 {
255   SVTK_ViewWindow* wnd = viewWindow();
256   if( wnd )
257     wnd->highlight( obj, highlight, 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 SVTK_TVtkIDsMap& 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   _PTR(Study) aStudy = SMESH::getStudy();
299
300   int res = -1;
301   if( objtype == Object )
302   {
303     int t = SMESHGUI_Selection::type( str );
304     if( t<0 )
305     {
306       //try to get GEOM type
307       _PTR( SObject ) sobj = aStudy->FindObjectID( str.toUtf8().data() );
308       if( sobj )
309       {
310         GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow(
311           dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
312         if( !CORBA::is_nil( obj ) )
313           // as decoding of type id is not realized in LightApp_Dialog,
314           //make all GEOM objects have same type id
315           res = SMESHGUI_Dialog::prefix( "GEOM" );// + obj->GetType();
316       }
317     }
318     else
319       res = SMESHGUI_Dialog::prefix( "SMESH" ) + t;
320   }
321   else
322   {
323     int pos = str.indexOf( idChar() );
324     QString entry = str.left( pos ),
325             _id = str.mid( pos+1 );
326     bool ok;
327     int id = _id.toInt( &ok );
328     if( ok )
329     {
330       _PTR( SObject ) sobj = aStudy->FindObjectID( entry.toUtf8().data() );
331       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( 
332         dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
333       SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( 
334         dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
335       if( !CORBA::is_nil( mesh ) )
336         res = SMESHGUI_Dialog::prefix( "SMESH element" ) + 
337           mesh->GetElementType( id, objtype==MeshElement );
338
339       else if( !CORBA::is_nil( submesh ) )
340         res = SMESHGUI_Dialog::prefix( "SMESH element" ) + 
341           submesh->GetElementType( id, objtype==MeshElement );
342     }
343   }
344
345   return res;
346 }
347
348 //=======================================================================
349 // name    : selected
350 // Purpose : Get names, types and ids of selected objects
351 //=======================================================================
352 void SMESHGUI_SelectionOp::selected( QStringList& names,
353                                      LightApp_Dialog::TypesList& types,
354                                      QStringList& ids ) const
355 {
356 /*  SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
357   SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
358                                         aLast = list.end();
359   for( ; anIt!=aLast; anIt++ )
360   {
361     LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( (*anIt).operator->() );
362     LightApp_SVTKDataOwner* vtkowner = dynamic_cast<LightApp_SVTKDataOwner*>( (*anIt).operator->() );
363
364     if( vtkowner )
365     {
366       QString id_str = QString( "%1%2%3" ).arg( vtkowner->entry() ).arg( idChar() ), current_id_str;
367       Selection_Mode mode = vtkowner->GetMode();
368       EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
369       const TColStd_IndexedMapOfInteger& ownerids = vtkowner->GetIds();
370
371       for( int i=1, n=ownerids.Extent(); i<=n; i++ )
372       {
373         int curid = ownerids( i );
374         current_id_str = id_str.arg( curid );
375         ids.append( current_id_str );
376         types.append( typeById( current_id_str, objtype ) );
377         names.append( QString( "%1" ).arg( curid ) );
378       }
379     }
380
381     else if( owner )
382     {
383       QString id = owner->entry();
384       ids.append( id );
385       types.append( typeById( id, Object ) );
386       names.append( owner->IO()->getName() );
387     }
388   }*/
389
390   SALOME_ListIO selObjs;
391   TColStd_IndexedMapOfInteger selIndices;
392   selectionMgr()->selectedObjects( selObjs );
393   Selection_Mode mode = selectionMode();
394   EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
395
396   for( SALOME_ListIteratorOfListIO anIt( selObjs ); anIt.More(); anIt.Next() )
397   {
398     selIndices.Clear();
399     selectionMgr()->GetIndexes( anIt.Value(), selIndices );
400     if( selIndices.Extent() > 0 )
401     {
402       QString id_str = QString( "%1%2%3" ).arg( anIt.Value()->getEntry() ).arg( idChar() ), current_id_str;
403       for( int i=1, n=selIndices.Extent(); i<=n; i++ )
404       {
405         int curid = selIndices( i );
406         current_id_str = id_str.arg( curid );
407         ids.append( current_id_str );
408         types.append( typeById( current_id_str, objtype ) );
409         names.append( QString( "%1" ).arg( curid ) );
410       }
411     }
412     else
413     {
414       QString id = anIt.Value()->getEntry();
415       ids.append( id );
416       types.append( typeById( id, Object ) );
417       _PTR(SObject) obj = SMESH::getStudy()->FindObjectID( anIt.Value()->getEntry() );
418       if( obj )
419         names.append( QString( obj->GetName().c_str() ).trimmed() );
420     }
421   }
422 }
423
424 //=======================================================================
425 // name    : idChar
426 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
427 //=======================================================================
428 QChar SMESHGUI_SelectionOp::idChar() const
429 {
430   return '#';
431 }
432
433 //=================================================================================
434 // name     : mesh
435 // purpose  :
436 //=================================================================================
437 SMESH::SMESH_Mesh_var SMESHGUI_SelectionOp::mesh() const
438 {
439   if( selectionMode()==ActorSelection )
440     return SMESH::SMESH_Mesh::_nil();
441     
442   SALOME_ListIO sel; selectionMgr()->selectedObjects( sel, SVTK_Viewer::Type() );
443   if( sel.Extent()==1 )
444     return SMESH::GetMeshByIO( sel.First() );
445   else
446     return SMESH::SMESH_Mesh::_nil();
447 }
448
449 //=================================================================================
450 // name     : actor
451 // purpose  :
452 //=================================================================================
453 SMESH_Actor* SMESHGUI_SelectionOp::actor() const
454 {
455   SMESH::SMESH_Mesh_var m = mesh();
456   if( !m->_is_nil() )
457     return SMESH::FindActorByObject( m.in() );
458   else
459     return 0;
460 }
461
462 //=================================================================================
463 // name     : onTextChanged
464 // purpose  :
465 //=================================================================================
466 void SMESHGUI_SelectionOp::onTextChanged( int, const QStringList& list )
467 {
468     if( !dlg() )
469       return;
470
471     SVTK_TVtkIDsMap newIndices;
472
473     SALOME_ListIO sel; selectionMgr()->selectedObjects( sel );
474     SMESH_Actor* anActor = actor();
475     if( sel.Extent()==0 || !anActor )
476       return;
477
478     SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
479
480     IdList ids; extractIds( list, ids, '\0' );
481     IdList::const_iterator anIt = ids.begin(),
482                            aLast = ids.end();
483     if ( selectionMode() == NodeSelection )
484       for( ; anIt!=aLast; anIt++ ) {
485         if( const SMDS_MeshNode * n = aMesh->FindNode( *anIt ) )
486           newIndices.Add( n->GetID() );
487       }
488     else 
489       for( ; anIt!=aLast; anIt++ ) {
490         if( const SMDS_MeshElement* e = aMesh->FindElement( *anIt ) )
491           newIndices.Add( e->GetID() );
492       }
493
494     selector()->AddOrRemoveIndex( sel.First(), newIndices, false );
495     highlight( sel.First(), true, true );
496
497     QStringList names, _ids; LightApp_Dialog::TypesList types;
498     selected( names, types, _ids );
499     dlg()->selectObject( names, types, _ids, false );
500 }
501
502 //=================================================================================
503 // name     : selectedIds
504 // purpose  :
505 //=================================================================================
506 void SMESHGUI_SelectionOp::selectedIds( const int id, IdList& list ) const
507 {
508   if( !dlg() )
509     return;
510
511   QStringList ids; dlg()->selectedObject( id, ids );
512   extractIds( ids, list );
513 }
514
515 //=================================================================================
516 // name     : extractIds
517 // purpose  :
518 //=================================================================================
519 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list, const QChar idchar )
520 {
521   QStringList::const_iterator anIt = ids.begin(),
522                               aLast = ids.end();
523   QString id_str;
524   for( ; anIt!=aLast; anIt++ )
525   {
526     id_str = *anIt;
527     int pos = idchar=='\0' ? -1 : id_str.indexOf( idchar );
528     int id = -1;
529     if( idchar=='\0' || pos>=0 )
530     {
531       id = id_str.mid( pos+1 ).toInt();
532       list.append( id );
533     }
534   }
535 }
536
537 //=================================================================================
538 // name     : extractIds
539 // purpose  :
540 //=================================================================================
541 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list ) const
542 {
543   extractIds( ids, list, idChar() );
544 }