00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "SMESHGUI_SelectionOp.h"
00028
00029 #include "SMESHGUI.h"
00030 #include "SMESHGUI_VTKUtils.h"
00031 #include "SMESHGUI_MeshUtils.h"
00032 #include "SMESHGUI_Selection.h"
00033
00034 #include <SMESH_Actor.h>
00035 #include <SMDS_Mesh.hxx>
00036 #include <SMDS_MeshNode.hxx>
00037
00038
00039 #include <SUIT_SelectionFilter.h>
00040 #include <LightApp_SelectionMgr.h>
00041 #include <SalomeApp_Study.h>
00042 #include <SVTK_ViewWindow.h>
00043 #include <SVTK_ViewModel.h>
00044
00045 #include <SALOME_ListIO.hxx>
00046 #include <SALOME_ListIteratorOfListIO.hxx>
00047
00048
00049 #include <SALOMEDS_SObject.hxx>
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 SMESHGUI_SelectionOp::SMESHGUI_SelectionOp( const Selection_Mode mode )
00062 : SMESHGUI_Operation(),
00063 myDefSelectionMode( mode )
00064 {
00065 }
00066
00067
00068
00069
00070
00071 SMESHGUI_SelectionOp::~SMESHGUI_SelectionOp()
00072 {
00073 removeCustomFilters();
00074 }
00075
00076
00077
00078
00079
00080 void SMESHGUI_SelectionOp::startOperation()
00081 {
00082 myOldSelectionMode = selectionMode();
00083 setSelectionMode( myDefSelectionMode );
00084
00085 SMESHGUI_Operation::startOperation();
00086 if( dlg() )
00087 {
00088 disconnect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
00089 disconnect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
00090 disconnect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
00091 connect( dlg(), SIGNAL( objectActivated( int ) ), this, SLOT( onActivateObject( int ) ) );
00092 connect( dlg(), SIGNAL( objectDeactivated( int ) ), this, SLOT( onDeactivateObject( int ) ) );
00093 connect( dlg(), SIGNAL( selectionChanged( int ) ), this, SLOT( onSelectionChanged( int ) ) );
00094 }
00095 }
00096
00097
00098
00099
00100
00101 void SMESHGUI_SelectionOp::removeCustomFilters()
00102 {
00103 if (myFilters.count() > 0) {
00104 LightApp_SelectionMgr* mgr = selectionMgr();
00105 Filters::const_iterator anIt = myFilters.begin(),
00106 aLast = myFilters.end();
00107 for ( ; anIt != aLast; anIt++) {
00108 if (anIt.value()) {
00109 if (mgr) mgr->removeFilter(anIt.value());
00110 delete anIt.value();
00111 }
00112 }
00113
00114 myFilters.clear();
00115 }
00116 }
00117
00118
00119
00120
00121
00122 void SMESHGUI_SelectionOp::commitOperation()
00123 {
00124 SMESHGUI_Operation::commitOperation();
00125 removeCustomFilters();
00126 setSelectionMode( myOldSelectionMode );
00127 }
00128
00129
00130
00131
00132
00133 void SMESHGUI_SelectionOp::abortOperation()
00134 {
00135 SMESHGUI_Operation::abortOperation();
00136 removeCustomFilters();
00137 setSelectionMode( myOldSelectionMode );
00138 }
00139
00140
00141
00142
00143
00144 void SMESHGUI_SelectionOp::selectionDone()
00145 {
00146 if( !dlg() )
00147 return;
00148
00149 if( selectionMode()!=ActorSelection )
00150 {
00151 SALOME_ListIO aList;
00152 selectionMgr()->selectedObjects( aList, SVTK_Viewer::Type() );
00153
00154 if( aList.Extent() != 1 )
00155 {
00156 dlg()->clearSelection();
00157 return;
00158 }
00159 }
00160
00161 QStringList names, ids;
00162 LightApp_Dialog::TypesList types;
00163 selected( names, types, ids );
00164 dlg()->selectObject( names, types, ids );
00165 }
00166
00167
00168
00169
00170
00171 SUIT_SelectionFilter* SMESHGUI_SelectionOp::createFilter( const int ) const
00172 {
00173 return 0;
00174 }
00175
00176
00177
00178
00179
00180 void SMESHGUI_SelectionOp::onActivateObject( int id )
00181 {
00182 LightApp_SelectionMgr* mgr = selectionMgr();
00183 if( !mgr )
00184 return;
00185
00186 if( !myFilters.contains( id ) )
00187 myFilters[ id ] = createFilter( id );
00188
00189 if( myFilters[ id ] )
00190 mgr->installFilter( myFilters[ id ] );
00191
00192 selectionDone();
00193 }
00194
00195
00196
00197
00198
00199 void SMESHGUI_SelectionOp::onDeactivateObject( int id )
00200 {
00201 removeCustomFilters();
00202 }
00203
00204
00205
00206
00207
00208 void SMESHGUI_SelectionOp::initDialog()
00209 {
00210 if( dlg() )
00211 {
00212 dlg()->clearSelection();
00213 dlg()->deactivateAll();
00214 }
00215 }
00216
00217
00218
00219
00220
00221 void SMESHGUI_SelectionOp::onSelectionChanged( int )
00222 {
00223 }
00224
00225
00226
00227
00228
00229 Selection_Mode SMESHGUI_SelectionOp::selectionMode() const
00230 {
00231 SVTK_ViewWindow* wnd = viewWindow();
00232 if( wnd )
00233 return wnd->SelectionMode();
00234 else
00235 return ActorSelection;
00236 }
00237
00238
00239
00240
00241
00242 void SMESHGUI_SelectionOp::setSelectionMode( const Selection_Mode mode )
00243 {
00244 SVTK_ViewWindow* wnd = viewWindow();
00245 if( wnd )
00246 wnd->SetSelectionMode( mode );
00247 }
00248
00249
00250
00251
00252
00253 void SMESHGUI_SelectionOp::highlight( const Handle( SALOME_InteractiveObject )& obj,
00254 const bool hilight, const bool immediately )
00255 {
00256 SVTK_ViewWindow* wnd = viewWindow();
00257 if( wnd )
00258 wnd->highlight( obj, hilight, immediately );
00259 }
00260
00261
00262
00263
00264
00265 void SMESHGUI_SelectionOp::addOrRemoveIndex( const Handle( SALOME_InteractiveObject )& obj,
00266 const TColStd_MapOfInteger& indices,
00267 const bool isModeShift )
00268 {
00269 SVTK_Selector* sel = selector();
00270 if( sel )
00271 sel->AddOrRemoveIndex( obj, indices, isModeShift );
00272 }
00273
00274
00275
00276
00277
00278 SVTK_ViewWindow* SMESHGUI_SelectionOp::viewWindow() const
00279 {
00280 return SMESH::GetViewWindow( getSMESHGUI() );
00281 }
00282
00283
00284
00285
00286
00287 SVTK_Selector* SMESHGUI_SelectionOp::selector() const
00288 {
00289 SVTK_ViewWindow* wnd = viewWindow();
00290 return wnd ? wnd->GetSelector() : 0;
00291 }
00292
00293
00294
00295
00296
00297 int SMESHGUI_SelectionOp::typeById( const QString& str, const EntityType objtype ) const
00298 {
00299 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
00300 if( !_study )
00301 return -1;
00302
00303 _PTR( Study ) st = _study->studyDS();
00304
00305 int res = -1;
00306 if( objtype == Object )
00307 {
00308 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
00309 if( _study )
00310 {
00311 int t = SMESHGUI_Selection::type( str, _study->studyDS() );
00312 if( t<0 )
00313 {
00314
00315 _PTR( SObject ) sobj = st->FindObjectID( str.toLatin1().data() );
00316 if( sobj )
00317 {
00318 GEOM::GEOM_Object_var obj = GEOM::GEOM_Object::_narrow(
00319 dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
00320 if( !CORBA::is_nil( obj ) )
00321 res = SMESHGUI_Dialog::prefix( "GEOM" ) + obj->GetType();
00322 }
00323 }
00324 else
00325 res = SMESHGUI_Dialog::prefix( "SMESH" ) + t;
00326 }
00327 }
00328 else
00329 {
00330 int pos = str.indexOf( idChar() );
00331 QString entry = str.left( pos ),
00332 _id = str.mid( pos+1 );
00333 bool ok;
00334 int id = _id.toInt( &ok );
00335 if( ok )
00336 {
00337 _PTR( SObject ) sobj = st->FindObjectID( entry.toLatin1().data() );
00338 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow(
00339 dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
00340 SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow(
00341 dynamic_cast<SALOMEDS_SObject*>( sobj.get() )->GetObject() );
00342 if( !CORBA::is_nil( mesh ) )
00343 res = SMESHGUI_Dialog::prefix( "SMESH element" ) +
00344 mesh->GetElementType( id, objtype==MeshElement );
00345
00346 else if( !CORBA::is_nil( submesh ) )
00347 res = SMESHGUI_Dialog::prefix( "SMESH element" ) +
00348 submesh->GetElementType( id, objtype==MeshElement );
00349 }
00350 }
00351
00352 return res;
00353 }
00354
00355
00356
00357
00358
00359 void SMESHGUI_SelectionOp::selected( QStringList& names,
00360 LightApp_Dialog::TypesList& types,
00361 QStringList& ids ) const
00362 {
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 SALOME_ListIO selObjs;
00398 TColStd_IndexedMapOfInteger selIndices;
00399 selectionMgr()->selectedObjects( selObjs );
00400 Selection_Mode mode = selectionMode();
00401 EntityType objtype = mode == NodeSelection ? MeshNode : MeshElement;
00402
00403 for( SALOME_ListIteratorOfListIO anIt( selObjs ); anIt.More(); anIt.Next() )
00404 {
00405 selIndices.Clear();
00406 selectionMgr()->GetIndexes( anIt.Value(), selIndices );
00407 if( selIndices.Extent() > 0 )
00408 {
00409 QString id_str = QString( "%1%2%3" ).arg( anIt.Value()->getEntry() ).arg( idChar() ), current_id_str;
00410 for( int i=1, n=selIndices.Extent(); i<=n; i++ )
00411 {
00412 int curid = selIndices( i );
00413 current_id_str = id_str.arg( curid );
00414 ids.append( current_id_str );
00415 types.append( typeById( current_id_str, objtype ) );
00416 names.append( QString( "%1" ).arg( curid ) );
00417 }
00418 }
00419 else
00420 {
00421 QString id = anIt.Value()->getEntry();
00422 ids.append( id );
00423 types.append( typeById( id, Object ) );
00424 SalomeApp_Study* _study = dynamic_cast<SalomeApp_Study*>( study() );
00425 if( _study )
00426 {
00427 _PTR(SObject) obj = _study->studyDS()->FindObjectID( anIt.Value()->getEntry() );
00428 if( obj )
00429 names.append( obj->GetName().c_str() );
00430 }
00431 }
00432 }
00433 }
00434
00435
00436
00437
00438
00439 QChar SMESHGUI_SelectionOp::idChar() const
00440 {
00441 return '#';
00442 }
00443
00444
00445
00446
00447
00448 SMESH::SMESH_Mesh_var SMESHGUI_SelectionOp::mesh() const
00449 {
00450 if( selectionMode()==ActorSelection )
00451 return SMESH::SMESH_Mesh::_nil();
00452
00453 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel, SVTK_Viewer::Type() );
00454 if( sel.Extent()==1 )
00455 return SMESH::GetMeshByIO( sel.First() );
00456 else
00457 return SMESH::SMESH_Mesh::_nil();
00458 }
00459
00460
00461
00462
00463
00464 SMESH_Actor* SMESHGUI_SelectionOp::actor() const
00465 {
00466 SMESH::SMESH_Mesh_var m = mesh();
00467 if( !m->_is_nil() )
00468 return SMESH::FindActorByObject( m.in() );
00469 else
00470 return 0;
00471 }
00472
00473
00474
00475
00476
00477 void SMESHGUI_SelectionOp::onTextChanged( int, const QStringList& list )
00478 {
00479 if( !dlg() )
00480 return;
00481
00482 TColStd_MapOfInteger newIndices;
00483
00484 SALOME_ListIO sel; selectionMgr()->selectedObjects( sel );
00485 SMESH_Actor* anActor = actor();
00486 if( sel.Extent()==0 || !anActor )
00487 return;
00488
00489 SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
00490
00491 IdList ids; extractIds( list, ids, '\0' );
00492 IdList::const_iterator anIt = ids.begin(),
00493 aLast = ids.end();
00494 for( ; anIt!=aLast; anIt++ )
00495 if( const SMDS_MeshNode * n = aMesh->FindNode( *anIt ) )
00496 newIndices.Add( n->GetID() );
00497
00498 selector()->AddOrRemoveIndex( sel.First(), newIndices, false );
00499 highlight( sel.First(), true, true );
00500
00501 QStringList names, _ids; LightApp_Dialog::TypesList types;
00502 selected( names, types, _ids );
00503 dlg()->selectObject( names, types, _ids, false );
00504 }
00505
00506
00507
00508
00509
00510 void SMESHGUI_SelectionOp::selectedIds( const int id, IdList& list ) const
00511 {
00512 if( !dlg() )
00513 return;
00514
00515 QStringList ids; dlg()->selectedObject( id, ids );
00516 extractIds( ids, list );
00517 }
00518
00519
00520
00521
00522
00523 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list, const QChar idchar )
00524 {
00525 QStringList::const_iterator anIt = ids.begin(),
00526 aLast = ids.end();
00527 QString id_str;
00528 for( ; anIt!=aLast; anIt++ )
00529 {
00530 id_str = *anIt;
00531 int pos = idchar=='\0' ? -1 : id_str.indexOf( idchar );
00532 int id = -1;
00533 if( idchar=='\0' || pos>=0 )
00534 {
00535 id = id_str.mid( pos+1 ).toInt();
00536 list.append( id );
00537 }
00538 }
00539 }
00540
00541
00542
00543
00544
00545 void SMESHGUI_SelectionOp::extractIds( const QStringList& ids, IdList& list ) const
00546 {
00547 extractIds( ids, list, idChar() );
00548 }