1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 // File : SMESHGUI_SelectionOp.cxx
23 // Author : Alexander SOLOVYOV
26 #include <SMESHGUI_SelectionOp.h>
27 #include <SMESHGUI_VTKUtils.h>
28 #include <SMESHGUI_MeshUtils.h>
29 #include <SMESHGUI_Selection.h>
31 #include <SUIT_SelectionFilter.h>
32 #include <LightApp_SelectionMgr.h>
33 #include <SalomeApp_Study.h>
34 #include <LightApp_VTKSelector.h>
35 #include <SVTK_ViewWindow.h>
36 #include <SVTK_ViewModel.h>
37 #include <SVTK_Selector.h>
38 #include <SMESH_Actor.h>
40 #include <SMDS_Mesh.hxx>
41 #include <SMDS_MeshNode.hxx>
43 #include CORBA_SERVER_HEADER(GEOM_Gen)
44 #include <SALOMEDS_SObject.hxx>
46 #include <SALOME_ListIteratorOfListIO.hxx>
49 Class : SMESHGUI_SelectionOp
50 Description : Base operation for all operations using object selection in viewer or objectbrowser
51 through common widgets created by LightApp_Dialog::createObject
54 //=================================================================================
55 // name : SMESHGUI_SelectionOp
57 //=================================================================================
58 SMESHGUI_SelectionOp::SMESHGUI_SelectionOp( const Selection_Mode mode )
59 : SMESHGUI_Operation(),
60 myDefSelectionMode( mode )
64 //=================================================================================
65 // name : ~SMESHGUI_SelectionOp
67 //=================================================================================
68 SMESHGUI_SelectionOp::~SMESHGUI_SelectionOp()
70 Filters::const_iterator anIt = myFilters.begin(),
71 aLast = myFilters.end();
72 for( ; anIt!=aLast; anIt++ )
77 //=================================================================================
78 // name : startOperation
80 //=================================================================================
81 void SMESHGUI_SelectionOp::startOperation()
83 myOldSelectionMode = selectionMode();
84 setSelectionMode( myDefSelectionMode );
86 SMESHGUI_Operation::startOperation();
89 disconnect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
90 disconnect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
91 disconnect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
92 connect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
93 connect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
94 connect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
98 //=================================================================================
99 // name : removeCustomFilters
101 //=================================================================================
102 void SMESHGUI_SelectionOp::removeCustomFilters() const
104 LightApp_SelectionMgr* mgr = selectionMgr();
108 Filters::const_iterator anIt = myFilters.begin(),
109 aLast = myFilters.end();
110 for( ; anIt!=aLast; anIt++ )
112 mgr->removeFilter( anIt.data() );
115 //=================================================================================
116 // name : commitOperation
118 //=================================================================================
119 void SMESHGUI_SelectionOp::commitOperation()
121 SMESHGUI_Operation::commitOperation();
122 removeCustomFilters();
123 setSelectionMode( myOldSelectionMode );
126 //=================================================================================
127 // name : abortOperation
129 //=================================================================================
130 void SMESHGUI_SelectionOp::abortOperation()
132 SMESHGUI_Operation::abortOperation();
133 removeCustomFilters();
134 setSelectionMode( myOldSelectionMode );
137 //=================================================================================
138 // name : selectionDone
140 //=================================================================================
141 void SMESHGUI_SelectionOp::selectionDone()
146 if( selectionMode()!=ActorSelection )
149 selectionMgr()->selectedObjects( aList, SVTK_Viewer::Type() );
151 if( aList.Extent() != 1 ) //we can select nodes or elements only within one mesh
153 dlg()->clearSelection();
158 QStringList names, ids;
159 LightApp_Dialog::TypesList types;
160 selected( names, types, ids );
161 dlg()->selectObject( names, types, ids );
164 //=================================================================================
165 // name : createFilter
167 //=================================================================================
168 SUIT_SelectionFilter* SMESHGUI_SelectionOp::createFilter( const int ) const
173 //=================================================================================
174 // name : onActivateObject
176 //=================================================================================
177 void SMESHGUI_SelectionOp::onActivateObject( int id )
179 LightApp_SelectionMgr* mgr = selectionMgr();
183 if( !myFilters.contains( id ) )
184 myFilters[ id ] = createFilter( id );
186 if( myFilters[ id ] )
187 mgr->installFilter( myFilters[ id ] );
192 //=================================================================================
193 // name : onDeactivateObject
195 //=================================================================================
196 void SMESHGUI_SelectionOp::onDeactivateObject( int id )
198 LightApp_SelectionMgr* mgr = selectionMgr();
199 if( mgr && myFilters.contains( id ) && myFilters[ id ] )
200 mgr->removeFilter( myFilters[ id ] );
203 //=================================================================================
206 //=================================================================================
207 void SMESHGUI_SelectionOp::initDialog()
211 dlg()->clearSelection();
212 dlg()->deactivateAll();
216 //=================================================================================
219 //=================================================================================
220 void SMESHGUI_SelectionOp::onSelectionChanged( int )
224 //=======================================================================
225 // name : selectionMode
226 // Purpose : Returns selection mode
227 //=======================================================================
228 Selection_Mode SMESHGUI_SelectionOp::selectionMode() const
230 SVTK_ViewWindow* wnd = viewWindow();
232 return wnd->SelectionMode();
234 return ActorSelection;
237 //=======================================================================
238 // name : setSelectionMode
239 // Purpose : Set selection mode
240 //=======================================================================
241 void SMESHGUI_SelectionOp::setSelectionMode( const Selection_Mode mode )
243 SVTK_ViewWindow* wnd = viewWindow();
245 wnd->SetSelectionMode( mode );
248 //=======================================================================
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 )
255 SVTK_ViewWindow* wnd = viewWindow();
257 wnd->highlight( obj, hilight, immediately );
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 )
268 SVTK_Selector* sel = selector();
270 sel->AddOrRemoveIndex( obj, indices, isModeShift );
273 //=======================================================================
275 // Purpose : Get active view window
276 //=======================================================================
277 SVTK_ViewWindow* SMESHGUI_SelectionOp::viewWindow() const
279 return SMESH::GetViewWindow( getSMESHGUI() );
282 //=======================================================================
284 // Purpose : Get selector
285 //=======================================================================
286 SVTK_Selector* SMESHGUI_SelectionOp::selector() const
288 SVTK_ViewWindow* wnd = viewWindow();
289 return wnd ? wnd->GetSelector() : 0;
292 //=======================================================================
294 // Purpose : Find type by id
295 //=======================================================================
296 int SMESHGUI_SelectionOp::typeById( const QString& str, const EntityType objtype ) const
298 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
302 _PTR( Study ) st = _study->studyDS();
305 if( objtype == Object )
307 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
310 int t = SMESHGUI_Selection::type( str, _study->studyDS() );
313 //try to get GEOM type
314 _PTR( SObject ) sobj = st->FindObjectID( str.latin1() );
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();
324 res = SMESHGUI_Dialog::prefix( "SMESH" ) + t;
329 int pos = str.find( idChar() );
330 QString entry = str.left( pos ),
331 _id = str.mid( pos+1 );
333 int id = _id.toInt( &ok );
336 _PTR( SObject ) sobj = st->FindObjectID( entry.latin1() );
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 );
345 else if( !CORBA::is_nil( submesh ) )
346 res = SMESHGUI_Dialog::prefix( "SMESH element" ) +
347 submesh->GetElementType( id, objtype==MeshElement );
354 //=======================================================================
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
362 /* SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
363 SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
365 for( ; anIt!=aLast; anIt++ )
367 LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( (*anIt).operator->() );
368 LightApp_SVTKDataOwner* vtkowner = dynamic_cast<LightApp_SVTKDataOwner*>( (*anIt).operator->() );
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();
377 for( int i=1, n=ownerids.Extent(); i<=n; i++ )
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 ) );
389 QString id = owner->entry();
391 types.append( typeById( id, Object ) );
392 names.append( owner->IO()->getName() );
396 SALOME_ListIO selObjs;
397 TColStd_IndexedMapOfInteger selIndices;
398 selectionMgr()->selectedObjects( selObjs );
399 Selection_Mode mode = selectionMode();
400 EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
402 for( SALOME_ListIteratorOfListIO anIt( selObjs ); anIt.More(); anIt.Next() )
405 selectionMgr()->GetIndexes( anIt.Value(), selIndices );
406 if( selIndices.Extent() > 0 )
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++ )
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 ) );
420 QString id = anIt.Value()->getEntry();
422 types.append( typeById( id, Object ) );
423 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
426 _PTR(SObject) obj = _study->studyDS()->FindObjectID( anIt.Value()->getEntry() );
428 names.append( obj->GetName().c_str() );
434 //=======================================================================
436 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
437 //=======================================================================
438 QChar SMESHGUI_SelectionOp::idChar() const
443 //=================================================================================
446 //=================================================================================
447 SMESH::SMESH_Mesh_var SMESHGUI_SelectionOp::mesh() const
449 if( selectionMode()==ActorSelection )
450 return SMESH::SMESH_Mesh::_nil();
452 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel, SVTK_Viewer::Type() );
453 if( sel.Extent()==1 )
454 return SMESH::GetMeshByIO( sel.First() );
456 return SMESH::SMESH_Mesh::_nil();
459 //=================================================================================
462 //=================================================================================
463 SMESH_Actor* SMESHGUI_SelectionOp::actor() const
465 SMESH::SMESH_Mesh_var m = mesh();
467 return SMESH::FindActorByObject( m.in() );
472 //=================================================================================
473 // name : onTextChanged
475 //=================================================================================
476 void SMESHGUI_SelectionOp::onTextChanged( int, const QStringList& list )
481 TColStd_MapOfInteger newIndices;
483 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel );
484 SMESH_Actor* anActor = actor();
485 if( sel.Extent()==0 || !anActor )
488 SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
490 IdList ids; extractIds( list, ids, '\0' );
491 IdList::const_iterator anIt = ids.begin(),
493 for( ; anIt!=aLast; anIt++ )
494 if( const SMDS_MeshNode * n = aMesh->FindNode( *anIt ) )
495 newIndices.Add( n->GetID() );
497 selector()->AddOrRemoveIndex( sel.First(), newIndices, false );
498 highlight( sel.First(), true, true );
500 QStringList names, _ids; LightApp_Dialog::TypesList types;
501 selected( names, types, _ids );
502 dlg()->selectObject( names, types, _ids, false );
505 //=================================================================================
506 // name : selectedIds
508 //=================================================================================
509 void SMESHGUI_SelectionOp::selectedIds( const int id, IdList& list ) const
514 QStringList ids; dlg()->selectedObject( id, ids );
515 extractIds( ids, list );
518 //=================================================================================
521 //=================================================================================
522 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list, const QChar idchar )
524 QStringList::const_iterator anIt = ids.begin(),
527 for( ; anIt!=aLast; anIt++ )
530 int pos = idchar=='\0' ? -1 : id_str.find( idchar );
532 if( idchar=='\0' || pos>=0 )
534 id = id_str.mid( pos+1 ).toInt();
540 //=================================================================================
543 //=================================================================================
544 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list ) const
546 extractIds( ids, list, idChar() );