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 removeCustomFilters();
73 //=================================================================================
74 // name : startOperation
76 //=================================================================================
77 void SMESHGUI_SelectionOp::startOperation()
79 myOldSelectionMode = selectionMode();
80 setSelectionMode( myDefSelectionMode );
82 SMESHGUI_Operation::startOperation();
85 disconnect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
86 disconnect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
87 disconnect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
88 connect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
89 connect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
90 connect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
94 //=================================================================================
95 // name : removeCustomFilters
97 //=================================================================================
98 void SMESHGUI_SelectionOp::removeCustomFilters()
100 if (myFilters.count() > 0) {
101 LightApp_SelectionMgr* mgr = selectionMgr();
102 Filters::const_iterator anIt = myFilters.begin(),
103 aLast = myFilters.end();
104 for (; anIt != aLast; anIt++) {
106 if (mgr) 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 removeCustomFilters();
201 //=================================================================================
204 //=================================================================================
205 void SMESHGUI_SelectionOp::initDialog()
209 dlg()->clearSelection();
210 dlg()->deactivateAll();
214 //=================================================================================
217 //=================================================================================
218 void SMESHGUI_SelectionOp::onSelectionChanged( int )
222 //=======================================================================
223 // name : selectionMode
224 // Purpose : Returns selection mode
225 //=======================================================================
226 Selection_Mode SMESHGUI_SelectionOp::selectionMode() const
228 SVTK_ViewWindow* wnd = viewWindow();
230 return wnd->SelectionMode();
232 return ActorSelection;
235 //=======================================================================
236 // name : setSelectionMode
237 // Purpose : Set selection mode
238 //=======================================================================
239 void SMESHGUI_SelectionOp::setSelectionMode( const Selection_Mode mode )
241 SVTK_ViewWindow* wnd = viewWindow();
243 wnd->SetSelectionMode( mode );
246 //=======================================================================
248 // Purpose : Highlight object in 3d viewer
249 //=======================================================================
250 void SMESHGUI_SelectionOp::highlight( const Handle( SALOME_InteractiveObject )& obj,
251 const bool hilight, const bool immediately )
253 SVTK_ViewWindow* wnd = viewWindow();
255 wnd->highlight( obj, hilight, immediately );
258 //=======================================================================
259 // name : addOrRemoveIndex
260 // Purpose : Select/deselect cells of mesh
261 //=======================================================================
262 void SMESHGUI_SelectionOp::addOrRemoveIndex( const Handle( SALOME_InteractiveObject )& obj,
263 const TColStd_MapOfInteger& indices,
264 const bool isModeShift )
266 SVTK_Selector* sel = selector();
268 sel->AddOrRemoveIndex( obj, indices, isModeShift );
271 //=======================================================================
273 // Purpose : Get active view window
274 //=======================================================================
275 SVTK_ViewWindow* SMESHGUI_SelectionOp::viewWindow() const
277 return SMESH::GetViewWindow( getSMESHGUI() );
280 //=======================================================================
282 // Purpose : Get selector
283 //=======================================================================
284 SVTK_Selector* SMESHGUI_SelectionOp::selector() const
286 SVTK_ViewWindow* wnd = viewWindow();
287 return wnd ? wnd->GetSelector() : 0;
290 //=======================================================================
292 // Purpose : Find type by id
293 //=======================================================================
294 int SMESHGUI_SelectionOp::typeById( const QString& str, const EntityType objtype ) const
296 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
300 _PTR( Study ) st = _study->studyDS();
303 if( objtype == Object )
305 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
308 int t = SMESHGUI_Selection::type( str, _study->studyDS() );
311 //try to get GEOM type
312 _PTR( SObject ) sobj = st->FindObjectID( str.latin1() );
315 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow(
316 dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
317 if( !CORBA::is_nil( obj ) )
318 res = SMESHGUI_Dialog::prefix( "GEOM" ) + obj->GetType();
322 res = SMESHGUI_Dialog::prefix( "SMESH" ) + t;
327 int pos = str.find( idChar() );
328 QString entry = str.left( pos ),
329 _id = str.mid( pos+1 );
331 int id = _id.toInt( &ok );
334 _PTR( SObject ) sobj = st->FindObjectID( entry.latin1() );
335 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow(
336 dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
337 SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow(
338 dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
339 if( !CORBA::is_nil( mesh ) )
340 res = SMESHGUI_Dialog::prefix( "SMESH element" ) +
341 mesh->GetElementType( id, objtype==MeshElement );
343 else if( !CORBA::is_nil( submesh ) )
344 res = SMESHGUI_Dialog::prefix( "SMESH element" ) +
345 submesh->GetElementType( id, objtype==MeshElement );
352 //=======================================================================
354 // Purpose : Get names, types and ids of selected objects
355 //=======================================================================
356 void SMESHGUI_SelectionOp::selected( QStringList& names,
357 LightApp_Dialog::TypesList& types,
358 QStringList& ids ) const
360 /* SUIT_DataOwnerPtrList list; selectionMgr()->selected( list );
361 SUIT_DataOwnerPtrList::const_iterator anIt = list.begin(),
363 for( ; anIt!=aLast; anIt++ )
365 LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( (*anIt).operator->() );
366 LightApp_SVTKDataOwner* vtkowner = dynamic_cast<LightApp_SVTKDataOwner*>( (*anIt).operator->() );
370 QString id_str = QString( "%1%2%3" ).arg( vtkowner->entry() ).arg( idChar() ), current_id_str;
371 Selection_Mode mode = vtkowner->GetMode();
372 EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
373 const TColStd_IndexedMapOfInteger& ownerids = vtkowner->GetIds();
375 for( int i=1, n=ownerids.Extent(); i<=n; i++ )
377 int curid = ownerids( i );
378 current_id_str = id_str.arg( curid );
379 ids.append( current_id_str );
380 types.append( typeById( current_id_str, objtype ) );
381 names.append( QString( "%1" ).arg( curid ) );
387 QString id = owner->entry();
389 types.append( typeById( id, Object ) );
390 names.append( owner->IO()->getName() );
394 SALOME_ListIO selObjs;
395 TColStd_IndexedMapOfInteger selIndices;
396 selectionMgr()->selectedObjects( selObjs );
397 Selection_Mode mode = selectionMode();
398 EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
400 for( SALOME_ListIteratorOfListIO anIt( selObjs ); anIt.More(); anIt.Next() )
403 selectionMgr()->GetIndexes( anIt.Value(), selIndices );
404 if( selIndices.Extent() > 0 )
406 QString id_str = QString( "%1%2%3" ).arg( anIt.Value()->getEntry() ).arg( idChar() ), current_id_str;
407 for( int i=1, n=selIndices.Extent(); i<=n; i++ )
409 int curid = selIndices( i );
410 current_id_str = id_str.arg( curid );
411 ids.append( current_id_str );
412 types.append( typeById( current_id_str, objtype ) );
413 names.append( QString( "%1" ).arg( curid ) );
418 QString id = anIt.Value()->getEntry();
420 types.append( typeById( id, Object ) );
421 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
424 _PTR(SObject) obj = _study->studyDS()->FindObjectID( anIt.Value()->getEntry() );
426 names.append( obj->GetName().c_str() );
432 //=======================================================================
434 // Purpose : Char using to divide <entry> and <id> in string id representation. By default, '#'
435 //=======================================================================
436 QChar SMESHGUI_SelectionOp::idChar() const
441 //=================================================================================
444 //=================================================================================
445 SMESH::SMESH_Mesh_var SMESHGUI_SelectionOp::mesh() const
447 if( selectionMode()==ActorSelection )
448 return SMESH::SMESH_Mesh::_nil();
450 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel, SVTK_Viewer::Type() );
451 if( sel.Extent()==1 )
452 return SMESH::GetMeshByIO( sel.First() );
454 return SMESH::SMESH_Mesh::_nil();
457 //=================================================================================
460 //=================================================================================
461 SMESH_Actor* SMESHGUI_SelectionOp::actor() const
463 SMESH::SMESH_Mesh_var m = mesh();
465 return SMESH::FindActorByObject( m.in() );
470 //=================================================================================
471 // name : onTextChanged
473 //=================================================================================
474 void SMESHGUI_SelectionOp::onTextChanged( int, const QStringList& list )
479 TColStd_MapOfInteger newIndices;
481 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel );
482 SMESH_Actor* anActor = actor();
483 if( sel.Extent()==0 || !anActor )
486 SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
488 IdList ids; extractIds( list, ids, '\0' );
489 IdList::const_iterator anIt = ids.begin(),
491 for( ; anIt!=aLast; anIt++ )
492 if( const SMDS_MeshNode * n = aMesh->FindNode( *anIt ) )
493 newIndices.Add( n->GetID() );
495 selector()->AddOrRemoveIndex( sel.First(), newIndices, false );
496 highlight( sel.First(), true, true );
498 QStringList names, _ids; LightApp_Dialog::TypesList types;
499 selected( names, types, _ids );
500 dlg()->selectObject( names, types, _ids, false );
503 //=================================================================================
504 // name : selectedIds
506 //=================================================================================
507 void SMESHGUI_SelectionOp::selectedIds( const int id, IdList& list ) const
512 QStringList ids; dlg()->selectedObject( id, ids );
513 extractIds( ids, list );
516 //=================================================================================
519 //=================================================================================
520 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list, const QChar idchar )
522 QStringList::const_iterator anIt = ids.begin(),
525 for( ; anIt!=aLast; anIt++ )
528 int pos = idchar=='\0' ? -1 : id_str.find( idchar );
530 if( idchar=='\0' || pos>=0 )
532 id = id_str.mid( pos+1 ).toInt();
538 //=================================================================================
541 //=================================================================================
542 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list ) const
544 extractIds( ids, list, idChar() );