Version: 6.3.1

src/StdMeshersGUI/StdMeshersGUI_SubShapeSelectorWdg.cxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File   : StdMeshersGUI_SubShapeSelectorWdg.cxx
00021 // Author : Open CASCADE S.A.S. (dmv)
00022 // SMESH includes
00023 //
00024 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
00025 
00026 // SMESH Includes
00027 #include "SMESH_Type.h"
00028 #include "SMESHGUI_MeshUtils.h"
00029 #include "SMESH_Actor.h"
00030 #include "SMESH_PreviewActorsCollection.h"
00031 #include "SMESH_ActorUtils.h"
00032 #include "SMESHGUI_GroupUtils.h"
00033 #include "SMESH_Gen_i.hxx"
00034 #include "SMESHGUI_GEOMGenUtils.h"
00035 #include "SMESH_LogicalFilter.hxx"
00036 
00037 // SVTK Includes
00038 #include <SVTK_ViewWindow.h>
00039 #include <SVTK_ViewModel.h>
00040 #include <SVTK_ViewWindow.h>
00041 #include <SVTK_Selector.h>
00042 
00043 // SALOME GUI includes
00044 #include <SALOME_ListIO.hxx>
00045 #include <LightApp_SelectionMgr.h>
00046 #include <SALOME_ListIteratorOfListIO.hxx>
00047 
00048 // SUIT Includes
00049 #include <SUIT_ResourceMgr.h>
00050 
00051 // GEOM Includes
00052 #include <GEOMBase.h>
00053 #include <GEOM_TypeFilter.h>
00054 #include <GEOM_CompoundFilter.h>
00055 
00056 // Qt includes
00057 #include <QPushButton>
00058 #include <QGridLayout>
00059 #include <QListWidget>
00060 #include <QCheckBox>
00061 #include <QLineEdit>
00062 
00063 // OCCT includes
00064 #include <TColStd_MapOfInteger.hxx>
00065 #include <TColStd_IndexedMapOfInteger.hxx>
00066 #include <TopoDS_Shape.hxx>
00067 #include <TopExp.hxx>
00068 #include <TopExp_Explorer.hxx>
00069 #include <StdSelect_TypeOfEdge.hxx>
00070 
00071 // SALOME KERNEL includes
00072 #include <SALOMEDS_SObject.hxx>
00073 
00074 
00075 #define SPACING 6
00076 #define MARGIN 0
00077 
00078 //================================================================================
00082 //================================================================================
00083 
00084 StdMeshersGUI_SubShapeSelectorWdg
00085 ::StdMeshersGUI_SubShapeSelectorWdg( QWidget * parent, TopAbs_ShapeEnum aSubShType ): 
00086   QWidget( parent ),
00087   myPreviewActor( 0 ),
00088   myMaxSize( -1 )
00089 {
00090   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
00091 
00092   QGridLayout* edgesLayout = new QGridLayout( this );
00093   edgesLayout->setMargin( MARGIN );
00094   edgesLayout->setSpacing( SPACING );
00095   
00096   myListWidget    = new QListWidget( this );
00097   myAddButton    = new QPushButton( tr( "SMESH_BUT_ADD" ),    this );
00098   myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );      
00099   myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
00100 
00101   edgesLayout->addWidget(myListWidget,   0, 0, 3, 3);
00102   edgesLayout->addWidget(myAddButton,    0, 4);
00103   edgesLayout->addWidget(myRemoveButton, 1, 4);
00104 
00105   //edgesLayout->setRowStretch(2, 5);
00106   edgesLayout->setColumnStretch(2, 5);
00107 
00108   setLayout( edgesLayout );
00109   setMinimumWidth( 300 );
00110 
00111   mySubShType = aSubShType;
00112 
00113   init();
00114 }
00115 
00116 //================================================================================
00120 //================================================================================
00121 
00122 StdMeshersGUI_SubShapeSelectorWdg::~StdMeshersGUI_SubShapeSelectorWdg()
00123 {
00124   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
00125     if ( myPreviewActor ) {
00126       myPreviewActor->RemoveFromRender( myRenderer );
00127       aViewWindow->Repaint();
00128 
00129       delete myPreviewActor;
00130       myPreviewActor = 0;
00131     }
00132   }
00133   myEntry = "";
00134   myParamValue = "";
00135   myMainShape.Nullify();
00136 
00137   if ( mySelectionMgr && myFilter )
00138     mySelectionMgr->removeFilter( myFilter );
00139   delete myFilter; myFilter=0;
00140 
00141   SUIT_SelectionFilter* filter;
00142   foreach( filter, myGeomFilters )
00143     delete filter;
00144 }
00145 
00146 //================================================================================
00150 //================================================================================
00151 
00152 void StdMeshersGUI_SubShapeSelectorWdg::init()
00153 {
00154   myParamValue = "";
00155   myIsNotCorrected = true; // to dont call the GetCorrectedValue method twice
00156   myListOfIDs.clear();
00157   mySelectedIDs.clear();
00158 
00159   myAddButton->setEnabled( false );
00160   myRemoveButton->setEnabled( false );
00161 
00162   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
00163   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
00164   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
00165 
00166   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00167     aViewWindow->SetSelectionMode( ActorSelection );
00168 
00169   myFilter=0;
00170   //setFilter();
00171 
00172   connect( myAddButton,    SIGNAL(clicked()), SLOT(onAdd()));
00173   connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
00174   
00175   connect( mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
00176   connect( myListWidget,   SIGNAL(itemSelectionChanged()),    this, SLOT(onListSelectionChanged()));
00177 
00178   updateState();
00179 }
00180 
00181 //================================================================================
00185 //================================================================================
00186 
00187 void StdMeshersGUI_SubShapeSelectorWdg::setFilter()
00188 {
00189   SalomeApp_Study* study = mySMESHGUI->activeStudy();
00190   GEOM_TypeFilter* typeFilter = new GEOM_TypeFilter(study, mySubShType, /*isShapeType=*/true );
00191   GEOM_CompoundFilter* gpoupFilter = new GEOM_CompoundFilter(study);
00192   gpoupFilter->addSubType( mySubShType );
00193   myGeomFilters.append( typeFilter );
00194   myGeomFilters.append( gpoupFilter );
00195   myFilter = new SMESH_LogicalFilter( myGeomFilters, SMESH_LogicalFilter::LO_OR );
00196   mySelectionMgr->installFilter( myFilter );
00197 }
00198 
00199 //================================================================================
00203 //================================================================================
00204 
00205 void StdMeshersGUI_SubShapeSelectorWdg::showPreview( bool visible)
00206 {
00207   if ( !myPreviewActor )
00208     return;
00209 
00210   if ( myIsShown != visible ) {
00211     myPreviewActor->SetShown( visible );
00212     
00213     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
00214       aViewWindow->Repaint();
00215 
00216     myIsShown = visible;
00217   }
00218 }
00219 
00220 //=================================================================================
00221 // function : SelectionIntoArgument()
00222 // purpose  : Called when selection as changed or other case
00223 //=================================================================================
00224 void StdMeshersGUI_SubShapeSelectorWdg::SelectionIntoArgument()
00225 {
00226   if ( !myPreviewActor )
00227     return;
00228 
00229   mySelectedIDs.clear();
00230 
00231   // get selected mesh
00232   SALOME_ListIO aList;
00233   mySelectionMgr->selectedObjects( aList );
00234   int nbSel = aList.Extent();
00235 
00236   if (nbSel > 0) {
00237     SALOME_ListIteratorOfListIO anIt (aList);
00238     
00239     for ( ; anIt.More(); anIt.Next()) { // Loop on selected objects
00240       Handle(SALOME_InteractiveObject) IO = anIt.Value();
00241       
00242       GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( IO->getEntry() );  
00243       if ( !CORBA::is_nil( aGeomObj ) ) { // Selected Object From Study
00244         GEOM::GEOM_Object_ptr aGeomFatherObj = aGeomObj->GetMainShape();
00245         QString aFatherEntry = "";
00246         QString aMainFatherEntry = "";
00247         TopoDS_Shape shape;
00248         if ( !CORBA::is_nil( aGeomFatherObj ) ) {
00249           // Get Main Shape
00250           GEOM::GEOM_Object_var aGeomMain = GetGeomObjectByEntry( myEntry );
00251           if ( !CORBA::is_nil( aGeomMain ) && aGeomMain->GetType() == 37 ) {  // Main Shape is a Group
00252             GEOM::GEOM_Object_ptr aMainFatherObj = aGeomMain->GetMainShape();
00253             if ( !CORBA::is_nil( aMainFatherObj ) )
00254               aMainFatherEntry = aMainFatherObj->GetStudyEntry();
00255           }
00256           aFatherEntry = aGeomFatherObj->GetStudyEntry();
00257         }
00258         
00259         if ( aFatherEntry != "" && ( aFatherEntry == myEntry || aFatherEntry == aMainFatherEntry ) )
00260         {
00261           if ( aGeomObj->GetType() == 37 /*GEOM_GROUP*/ ) { // Selected Group that belongs the main object
00262             GEOMBase::GetShape(aGeomObj, shape); 
00263             if ( !shape.IsNull() ) {
00264               TopExp_Explorer exp( shape, mySubShType );
00265               for ( ; exp.More(); exp.Next() ) {
00266                 int index = myPreviewActor->GetIndexByShape( exp.Current() );
00267                 if ( index ) {
00268                   mySelectedIDs.append( index );
00269                   myPreviewActor->HighlightID( index );
00270                 }
00271               }
00272             }
00273           } else if ( aGeomObj->GetType() == 28 /*GEOM_SUBSHAPE*/  ) {
00274             GEOMBase::GetShape(aGeomObj, shape); 
00275             if ( !shape.IsNull() && shape.ShapeType() == mySubShType ) {
00276               int index = myPreviewActor->GetIndexByShape( shape );
00277               if ( index ) {
00278                 mySelectedIDs.append( index );
00279                 myPreviewActor->HighlightID( index );
00280               }
00281             }
00282           }
00283         }
00284       } else { // Selected Actor from Actor Collection
00285         QString anEntry = IO->getEntry();
00286         QString str = "_";
00287         int index = anEntry.lastIndexOf( str );
00288         anEntry.remove(0, index+1);
00289         int ind = anEntry.toInt();
00290         if ( ind )
00291           mySelectedIDs.append( ind );
00292       }
00293     }
00294   }
00295   // update add button
00296   myAddButton->setEnabled( ( myListWidget->count() < myMaxSize || myMaxSize == -1 ) && mySelectedIDs.size() > 0 && ( mySelectedIDs.size() <= myMaxSize || myMaxSize == -1 ) );
00297 
00298   //Connect Selected Ids in viewer and dialog's Ids list
00299   myListWidget->clearSelection();
00300   if ( mySelectedIDs.size() > 0 ) {
00301     for (int i = 0; i < mySelectedIDs.size(); i++) {
00302       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
00303       QList<QListWidgetItem*> anItems = myListWidget->findItems ( anID, Qt::MatchExactly );
00304       QListWidgetItem* item;
00305       foreach(item, anItems)
00306         item->setSelected(true);
00307     }
00308   }
00309 }
00310 
00311 //=================================================================================
00312 // function : onAdd()
00313 // purpose  : Called when Add Button Clicked
00314 //=================================================================================
00315 void StdMeshersGUI_SubShapeSelectorWdg::onAdd()
00316 {
00317   if ( mySelectedIDs.size() < 1 )
00318     return;
00319 
00320   myListWidget->blockSignals( true );
00321   for (int i = 0; i < mySelectedIDs.size() && (myMaxSize == -1 || myListOfIDs.size() < myMaxSize); i++) {
00322     if ( myListOfIDs.indexOf( mySelectedIDs.at(i) ) == -1 ) {
00323       QString anID = QString(" %1").arg( mySelectedIDs.at(i) );
00324 
00325       QListWidgetItem* anItem = new QListWidgetItem( anID, myListWidget );
00326       anItem->setSelected(true);
00327       
00328       myListOfIDs.append( mySelectedIDs.at(i) );
00329     }
00330   }
00331   onListSelectionChanged();
00332   myListWidget->blockSignals( false );
00333   myAddButton->setEnabled( myMaxSize == -1 || myListOfIDs.size() < myMaxSize );
00334 }
00335          
00336 //=================================================================================
00337 // function : onRemove()
00338 // purpose  : Called when Remove Button Clicked
00339 //=================================================================================
00340 void StdMeshersGUI_SubShapeSelectorWdg::onRemove()
00341 {
00342   if ( myListWidget->count() < 1 )
00343     return;
00344 
00345   myListWidget->blockSignals( true );
00346   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
00347   QListWidgetItem* item;
00348   foreach(item, selItems) {
00349     QString idStr = item->text();
00350     int id = idStr.toInt();
00351 
00352     int index = myListOfIDs.indexOf( id );
00353     myListOfIDs.removeAt( index );
00354     delete item;
00355   }
00356 
00357   onListSelectionChanged();
00358   myListWidget->blockSignals( false );
00359   
00360   myAddButton->setEnabled( true );
00361 }
00362 
00363 //=================================================================================
00364 // function : onListSelectionChanged()
00365 // purpose  : Called when selection in element list is changed
00366 //=================================================================================
00367 void StdMeshersGUI_SubShapeSelectorWdg::onListSelectionChanged()
00368 {
00369   if ( !myPreviewActor )
00370     return;
00371 
00372   mySelectionMgr->clearSelected();
00373   TColStd_MapOfInteger aIndexes;
00374   QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
00375   QListWidgetItem* anItem;
00376   foreach(anItem, selItems)
00377     myPreviewActor->HighlightID( anItem->text().toInt() );
00378 
00379   // update remove button
00380   myRemoveButton->setEnabled( selItems.size() > 0 );
00381 }
00382 
00383 //=================================================================================
00384 // function : setGeomShape
00385 // purpose  : Called to set geometry whose sub-shapes are selected
00386 //================================================================================
00387 void StdMeshersGUI_SubShapeSelectorWdg::SetGeomShapeEntry( const QString& theEntry )
00388 {
00389   if ( theEntry != "") {
00390     myParamValue = theEntry;
00391     myEntry = theEntry;
00392     myGeomShape = GetTopoDSByEntry( theEntry );
00393     updateState();
00394     myIsNotCorrected = true;
00395   }
00396 }
00397 
00398 //=================================================================================
00399 // function : updateState
00400 // purpose  : update Widget state
00401 //=================================================================================
00402 void StdMeshersGUI_SubShapeSelectorWdg::updateState()
00403 {
00404   bool state = false;
00405   if ( !myGeomShape.IsNull() )
00406     state = true;
00407   
00408   myListWidget->setEnabled( state );
00409   myAddButton->setEnabled( mySelectedIDs.size() > 0 );
00410   
00411   if (state) {
00412     myPreviewActor = new SMESH_PreviewActorsCollection();
00413     myPreviewActor->SetSelector( mySelector );
00414     myPreviewActor->Init( myGeomShape, mySubShType, myEntry );
00415     myPreviewActor->SetShown( false );
00416     myIsShown = false;
00417     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI )) {
00418       myRenderer = aViewWindow->getRenderer();
00419       myPreviewActor->AddToRender( myRenderer );
00420       aViewWindow->Repaint();
00421     }
00422   }
00423 }
00424 
00425 //=================================================================================
00426 // function : GetGeomObjectByEntry
00427 // purpose  : Called to get GeomObject
00428 //=================================================================================
00429 GEOM::GEOM_Object_var StdMeshersGUI_SubShapeSelectorWdg::GetGeomObjectByEntry( const QString& theEntry )
00430 {
00431   GEOM::GEOM_Object_var aGeomObj;
00432   SALOMEDS::Study_var aStudy = SMESHGUI::GetSMESHGen()->GetCurrentStudy();
00433   if (aStudy != 0) {
00434     SALOMEDS::SObject_var aSObj = aStudy->FindObjectID( theEntry.toLatin1().data() );
00435     SALOMEDS::GenericAttribute_var anAttr;
00436 
00437     if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
00438       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
00439       CORBA::String_var aVal = anIOR->Value();
00440       CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
00441       aGeomObj = GEOM::GEOM_Object::_narrow(obj);
00442     }
00443   }
00444   return aGeomObj;
00445 }
00446 
00447 //=================================================================================
00448 // function : setObjectByEntry
00449 // purpose  : Called to get GeomObject
00450 //=================================================================================
00451 TopoDS_Shape StdMeshersGUI_SubShapeSelectorWdg::GetTopoDSByEntry( const QString& theEntry )
00452 {
00453   TopoDS_Shape shape;
00454   GEOM::GEOM_Object_var aGeomObj = GetGeomObjectByEntry( theEntry );
00455   GEOMBase::GetShape(aGeomObj, shape);
00456   return shape;
00457 }
00458 
00459 //=================================================================================
00460 // function : GetListOfIds
00461 // purpose  : Called to get the list of SubShapes IDs
00462 //=================================================================================
00463 SMESH::long_array_var StdMeshersGUI_SubShapeSelectorWdg::GetListOfIDs()
00464 {
00465   SMESH::long_array_var anArray = new SMESH::long_array;
00466 
00467   if ( myMainEntry != "" && myIsNotCorrected )
00468     myListOfIDs = GetCorrectedListOfIDs( true );
00469 
00470   int size = myListOfIDs.size();
00471   anArray->length( size );
00472   if ( size ) {
00473     for (int i = 0; i < size; i++) {
00474         anArray[i] = myListOfIDs.at(i);
00475     }
00476   }
00477   return anArray;
00478 }
00479 
00480 //=================================================================================
00481 // function : SetListOfIds
00482 // purpose  : Called to set the list of SubShapes IDs
00483 //=================================================================================
00484 void StdMeshersGUI_SubShapeSelectorWdg::SetListOfIDs( SMESH::long_array_var theIds)
00485 {
00486   mySelectedIDs.clear();
00487   myListOfIDs.clear();
00488   int size = theIds->length();
00489   for ( int i = 0; i < size; i++ )
00490     mySelectedIDs.append( theIds[ i ] );
00491 
00492   mySelectedIDs = GetCorrectedListOfIDs( false );
00493   onAdd();
00494 }
00495 
00496 //=================================================================================
00497 // function : SetMainShapeEntry
00498 // purpose  : Called to set the Entry of main shape of the mesh
00499 //=================================================================================
00500 void StdMeshersGUI_SubShapeSelectorWdg::SetMainShapeEntry( const QString& theEntry )
00501 {
00502   myMainEntry = theEntry;
00503   myMainShape = GetTopoDSByEntry( theEntry );
00504   myIsNotCorrected = true;
00505 }
00506 
00507 //=================================================================================
00508 // function : GetMainShapeEntry
00509 // purpose  : Called to get the Main Object Entry
00510 //=================================================================================
00511 const char* StdMeshersGUI_SubShapeSelectorWdg::GetMainShapeEntry()
00512 {
00513   if ( myMainEntry == "")
00514     return myEntry.toLatin1().data();
00515 
00516   return myMainEntry.toLatin1().data();
00517 }
00518 
00519 //=================================================================================
00520 // function : GetCorrectedListOfIds
00521 // purpose  : Called to convert the list of IDs from subshape IDs to main shape IDs
00522 //=================================================================================
00523 QList<int> StdMeshersGUI_SubShapeSelectorWdg::GetCorrectedListOfIDs( bool fromSubshapeToMainshape )
00524 {
00525   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  fromSubshapeToMainshape )
00526     return myListOfIDs;
00527   else   if ( ( myMainShape.IsNull() || myGeomShape.IsNull() ) &&  !fromSubshapeToMainshape )
00528     return mySelectedIDs;
00529 
00530   QList<int> aList;
00531   TopTools_IndexedMapOfShape   aGeomMap;
00532   TopTools_IndexedMapOfShape   aMainMap;
00533   TopExp::MapShapes(myGeomShape, aGeomMap);
00534   TopExp::MapShapes(myMainShape, aMainMap);
00535 
00536   if ( fromSubshapeToMainshape ) { // convert indexes from subshape to mainshape
00537     int size = myListOfIDs.size();
00538     for (int i = 0; i < size; i++) {
00539       TopoDS_Shape aSubShape = aGeomMap.FindKey( myListOfIDs.at(i) );
00540       int index = aMainMap.FindIndex( aSubShape );
00541       aList.append( index );
00542     }
00543     myIsNotCorrected = false;
00544   } else { // convert indexes from main shape to subshape
00545     int size = mySelectedIDs.size();
00546     for (int i = 0; i < size; i++) {
00547       TopoDS_Shape aSubShape = aMainMap.FindKey( mySelectedIDs.at(i) );
00548       int index = aGeomMap.FindIndex( aSubShape );
00549       aList.append( index );
00550     }
00551   }
00552 
00553   return aList;
00554 }
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS