Back to index

salome-smesh  6.5.0
SMESHGUI_CreatePatternDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : SMESHGUI_CreatePatternDlg.cxx
00024 // Author : Sergey LITONIN, Open CASCADE S.A.S.
00025 // SMESH includes
00026 //
00027 #include "SMESHGUI_CreatePatternDlg.h"
00028 
00029 #include "SMESHGUI.h"
00030 #include "SMESHGUI_PatternWidget.h"
00031 #include "SMESHGUI_Utils.h"
00032 #include "SMESHGUI_VTKUtils.h"
00033 #include "SMESHGUI_PatternUtils.h"
00034 #include "SMESHGUI_GEOMGenUtils.h"
00035 
00036 #include <SMESH_NumberFilter.hxx>
00037 
00038 // SALOME GUI includes
00039 #include <SUIT_ResourceMgr.h>
00040 #include <SUIT_Desktop.h>
00041 #include <SUIT_FileDlg.h>
00042 #include <SUIT_Session.h>
00043 #include <SUIT_MessageBox.h>
00044 
00045 #include <LightApp_Application.h>
00046 #include <LightApp_SelectionMgr.h>
00047 #include <SalomeApp_Tools.h>
00048 
00049 #include <SALOME_ListIO.hxx>
00050 #include <SVTK_ViewModel.h>
00051 #include <SVTK_ViewWindow.h>
00052 
00053 // SALOME KERNEL includes
00054 #include <SALOMEDS_SObject.hxx>
00055 
00056 // OCCT includes
00057 #include <TColStd_MapOfInteger.hxx>
00058 
00059 // Qt includes
00060 #include <QFrame>
00061 #include <QVBoxLayout>
00062 #include <QHBoxLayout>
00063 #include <QLineEdit>
00064 #include <QPushButton>
00065 #include <QGroupBox>
00066 #include <QLabel>
00067 #include <QRadioButton>
00068 #include <QCheckBox>
00069 #include <QButtonGroup>
00070 #include <QApplication>
00071 #include <QKeyEvent>
00072 #include <QFile>
00073 #include <QDir>
00074 
00075 #define SPACING 6
00076 #define MARGIN  11
00077 
00083 //=======================================================================
00084 // function : SMESHGUI_CreatePatternDlg()
00085 // purpose  : Constructor
00086 //=======================================================================
00087 SMESHGUI_CreatePatternDlg::SMESHGUI_CreatePatternDlg( SMESHGUI*   theModule,
00088                                                       const int   theType )
00089   : QDialog( SMESH::GetDesktop( theModule ) ),
00090     mySMESHGUI( theModule ),
00091     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
00092 {
00093   setModal( false );
00094   setWindowTitle( tr( "CAPTION" ) );
00095 
00096   QVBoxLayout* aDlgLay = new QVBoxLayout( this );
00097   aDlgLay->setMargin( MARGIN );
00098   aDlgLay->setSpacing( SPACING );
00099 
00100   QWidget* aMainFrame = createMainFrame( this );
00101   QWidget* aBtnFrame  = createButtonFrame( this );
00102 
00103   aDlgLay->addWidget( aMainFrame );
00104   aDlgLay->addWidget( aBtnFrame );
00105 
00106   aDlgLay->setStretchFactor( aMainFrame, 1 );
00107 
00108   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
00109     mySelector = aViewWindow->GetSelector();
00110 
00111   myHelpFileName = "pattern_mapping_page.html";
00112 
00113   Init( theType );
00114 }
00115 
00116 //=======================================================================
00117 // function : createMainFrame()
00118 // purpose  : Create frame containing dialog's input fields
00119 //=======================================================================
00120 QWidget* SMESHGUI_CreatePatternDlg::createMainFrame( QWidget* theParent )
00121 {
00122   QPixmap iconSlct    ( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
00123   QPixmap icon2d      ( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_PATTERN_2d" ) ) );
00124   QPixmap icon3d      ( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_PATTERN_3d" ) ) );
00125   QPixmap iconSample2d( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_PATTERN_SAMPLE_2D" ) ) );
00126 
00127   QWidget* aMainGrp = new QWidget( theParent );
00128   QVBoxLayout* aMainGrpLayout = new QVBoxLayout( aMainGrp );
00129   aMainGrpLayout->setMargin( 0 );
00130   aMainGrpLayout->setSpacing( SPACING );
00131 
00132   // Pattern type group
00133 
00134   QGroupBox* aTypeGrp = new QGroupBox( tr( "PATTERN_TYPE" ), aMainGrp );
00135   QHBoxLayout* aTypeGrpLayout = new QHBoxLayout( aTypeGrp );
00136   aTypeGrpLayout->setMargin( MARGIN );
00137   aTypeGrpLayout->setSpacing( SPACING );
00138 
00139   mySwitch2d = new QRadioButton( aTypeGrp );
00140   mySwitch3d = new QRadioButton( aTypeGrp );
00141   mySwitch2d->setIcon( icon2d );
00142   mySwitch3d->setIcon( icon3d );
00143 
00144   myTypeGrp = new QButtonGroup( aMainGrp );
00145   myTypeGrp->addButton( mySwitch2d, Type_2d );
00146   myTypeGrp->addButton( mySwitch3d, Type_3d );
00147 
00148   // ... layout widgets
00149 
00150   aTypeGrpLayout->addWidget( mySwitch2d );
00151   aTypeGrpLayout->addWidget( mySwitch3d );
00152 
00153   // Mesh and pattern name group
00154 
00155   QGroupBox* aPatternGrp = new QGroupBox( tr( "PATTERN" ), aMainGrp );
00156   QGridLayout* aPatternGrpLayout = new QGridLayout( aPatternGrp );
00157   aPatternGrpLayout->setMargin( MARGIN );
00158   aPatternGrpLayout->setSpacing( SPACING );
00159 
00160   QLabel* aMeshLab = new QLabel( tr( "MESH_OR_SUBMESH" ), aPatternGrp );
00161 
00162   QPushButton* aSelBtn = new QPushButton( aPatternGrp );
00163   aSelBtn->setIcon( iconSlct );
00164   myMeshEdit = new QLineEdit( aPatternGrp );
00165   myMeshEdit->setReadOnly( true );
00166 
00167   QLabel* aNameLab = new QLabel( tr( "PATTERN_NAME" ), aPatternGrp );
00168   myName = new QLineEdit( aPatternGrp );
00169 
00170   // Picture 2d
00171 
00172   myPicture2d = new SMESHGUI_PatternWidget( aPatternGrp ),
00173   myPicture2d->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00174 
00175   // Project check box
00176 
00177   myProjectChk = new QCheckBox( tr( "PROJECT" ), aPatternGrp );
00178 
00179   // ... layout widgets
00180 
00181   aPatternGrpLayout->addWidget( aMeshLab,     0, 0 );
00182   aPatternGrpLayout->addWidget( aSelBtn,      0, 1 );
00183   aPatternGrpLayout->addWidget( myMeshEdit,   0, 2 );
00184   aPatternGrpLayout->addWidget( aNameLab,     1, 0 );
00185   aPatternGrpLayout->addWidget( myName,       1, 2 );
00186   aPatternGrpLayout->addWidget( myPicture2d,  2, 0, 1, 3 );
00187   aPatternGrpLayout->addWidget( myProjectChk, 3, 0, 1, 3 );
00188 
00189   // main layout
00190 
00191   aMainGrpLayout->addWidget( aTypeGrp );
00192   aMainGrpLayout->addWidget( aPatternGrp );
00193 
00194   // Connect signals and slots
00195 
00196   connect( myTypeGrp,    SIGNAL( buttonClicked( int )  ),
00197            this,         SLOT( onTypeChanged( int ) ) );
00198   connect( myProjectChk, SIGNAL( toggled( bool ) ),
00199            this,         SLOT( onProject( bool ) ) );
00200   connect( aSelBtn,      SIGNAL( clicked() ),
00201            this,         SLOT( onSelBtnClicked() ) );
00202 
00203   return aMainGrp;
00204 }
00205 
00206 //=======================================================================
00207 // function : createButtonFrame()
00208 // purpose  : Create frame containing buttons
00209 //=======================================================================
00210 QWidget* SMESHGUI_CreatePatternDlg::createButtonFrame( QWidget* theParent )
00211 {
00212   QFrame* aFrame = new QFrame( theParent );
00213   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );
00214 
00215   QPushButton* myOkBtn    = new QPushButton( tr( "SMESH_BUT_OK"    ), aFrame );
00216   QPushButton* mySaveBtn  = new QPushButton( tr( "SAVE"            ), aFrame );
00217   QPushButton* myCloseBtn = new QPushButton( tr( "SMESH_BUT_CANCEL"), aFrame );
00218   QPushButton* myHelpBtn  = new QPushButton( tr( "SMESH_BUT_HELP"),   aFrame );
00219 
00220   QHBoxLayout* aLay = new QHBoxLayout( aFrame );
00221   aLay->setMargin( MARGIN );
00222   aLay->setSpacing( SPACING );
00223 
00224   aLay->addWidget( myOkBtn );
00225   aLay->addSpacing( 10 );
00226   aLay->addWidget( mySaveBtn );
00227   aLay->addSpacing( 10 );
00228   aLay->addStretch();
00229   aLay->addWidget( myCloseBtn );
00230   aLay->addWidget( myHelpBtn );
00231 
00232   connect( myOkBtn,    SIGNAL( clicked() ), this, SLOT( onOk() ) );
00233   connect( myCloseBtn, SIGNAL( clicked() ), this, SLOT( onClose() ) );
00234   connect( mySaveBtn,  SIGNAL( clicked() ), this, SLOT( onSave() ) );
00235   connect( myHelpBtn,  SIGNAL( clicked() ), this, SLOT( onHelp() ) );
00236 
00237   return aFrame;
00238 }
00239 
00240 //=======================================================================
00241 // function : ~SMESHGUI_CreatePatternDlg()
00242 // purpose  : Destructor
00243 //=======================================================================
00244 SMESHGUI_CreatePatternDlg::~SMESHGUI_CreatePatternDlg()
00245 {
00246 }
00247 
00248 //=======================================================================
00249 // function : onProject()
00250 // purpose  : SLOT. Called when state of "Project nodes on ther face"
00251 //            checkbox is changed
00252 //=======================================================================
00253 void SMESHGUI_CreatePatternDlg::onProject( bool )
00254 {
00255   loadFromObject( false );
00256   displayPreview();
00257 }
00258 
00259 //=======================================================================
00260 // function : Init()
00261 // purpose  : Init dialog fields, connect signals and slots, show dialog
00262 //=======================================================================
00263 void SMESHGUI_CreatePatternDlg::Init( const int theType )
00264 {
00265   myIsLoaded     = false;
00266   myType         = -1;
00267   mySubMesh      = SMESH::SMESH_subMesh::_nil();
00268   myMesh         = SMESH::SMESH_Mesh::_nil();
00269   myGeomObj      = GEOM::GEOM_Object::_nil();
00270   myPattern      = SMESH::SMESH_Pattern::_nil();
00271 
00272   erasePreview();
00273 
00274   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
00275 
00276   // selection and SMESHGUI
00277   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ),
00278            this,           SLOT( onSelectionDone() ) );
00279   connect( mySMESHGUI,     SIGNAL( SignalDeactivateActiveDialog() ),
00280            this,           SLOT( onDeactivate() ) );
00281   connect( mySMESHGUI,     SIGNAL( SignalCloseAllDialogs() ),
00282            this,           SLOT( onClose() ) );
00283 
00284   mySwitch2d->setEnabled( theType == Type_2d );
00285   mySwitch3d->setEnabled( theType == Type_3d );
00286 
00287   myTypeGrp->button( theType )->setChecked( true );
00288   onTypeChanged( theType );
00289 
00290   myName->setText( getDefaultName() );
00291   myMeshEdit->setText( "" );
00292 
00293   QApplication::instance()->processEvents();
00294   updateGeometry();
00295   resize(100,100);
00296 
00297   activateSelection();
00298   onSelectionDone();
00299 
00300   //show();
00301 }
00302 
00303 //=======================================================================
00304 // function : SetMesh()
00305 // purpose  : Set mesh to dialog
00306 //=======================================================================
00307 void SMESHGUI_CreatePatternDlg::SetMesh( SMESH::SMESH_Mesh_ptr thePtr )
00308 {
00309   myMesh = SMESH::SMESH_Mesh::_duplicate( thePtr );
00310   mySubMesh = SMESH::SMESH_subMesh::_nil();
00311 
00312   bool isValidMesh = false;
00313   if ( !myMesh->_is_nil() ) {
00314     _PTR(SObject) aSobj = SMESH::FindSObject( myMesh.in() );
00315     //Handle(SALOME_InteractiveObject) anIObj =
00316     //  new SALOME_InteractiveObject(aSobj->GetID().c_str(), "SMESH");
00317     SUIT_DataOwnerPtr anIObj( new LightApp_DataOwner( aSobj->GetID().c_str() ) );
00318 
00319     isValidMesh = mySelectionMgr->isOk( anIObj );
00320   }
00321 
00322   if ( isValidMesh ) {
00323     _PTR(SObject) aSO = SMESH::FindSObject( myMesh.in() );
00324     myMeshEdit->setText( aSO->GetName().c_str() );
00325     myGeomObj = SMESH::GetGeom( aSO );
00326   } 
00327   else {
00328     myMeshEdit->setText( "" );
00329     myGeomObj = GEOM::GEOM_Object::_nil();
00330   }
00331 
00332   if ( myType == Type_2d ) {
00333     loadFromObject( false );
00334     displayPreview();
00335   }
00336 }
00337 
00338 //=======================================================================
00339 // function : isValid()
00340 // purpose  : Verify validity of entry data
00341 //=======================================================================
00342 bool SMESHGUI_CreatePatternDlg::isValid()
00343 {
00344   if ( myGeomObj->_is_nil() ) {
00345     SUIT_MessageBox::information( this,
00346                                   tr( "SMESH_INSUFFICIENT_DATA" ),
00347                                   tr( "SMESHGUI_INVALID_PARAMETERS" ) );
00348     return false;
00349   }
00350   return true;
00351 }
00352 
00353 //=======================================================================
00354 // function : getDefaultName()
00355 // purpose  : Get default pattern name
00356 //=======================================================================
00357 QString SMESHGUI_CreatePatternDlg::getDefaultName() const
00358 {
00359   return myType == Type_2d ? tr( "DEFAULT_2D" ) : tr( "DEFAULT_3D" );
00360 }
00361 
00362 //=======================================================================
00363 // function : onSave()
00364 // purpose  : SLOT called when "Save" button pressed. Build pattern and
00365 //           save it to disk
00366 //=======================================================================
00367 void SMESHGUI_CreatePatternDlg::onSave()
00368 {
00369   try {
00370     if ( !isValid() )
00371       return;
00372 
00373     if ( !myIsLoaded )
00374       loadFromObject( true );
00375 
00376     // Load pattern from object
00377     if ( !myIsLoaded )
00378       return;
00379 
00381     SUIT_FileDlg* aDlg = new SUIT_FileDlg( this, false );
00382     aDlg->setWindowTitle( tr( "SAVE_PATTERN" ) );
00383     aDlg->setFileMode( QFileDialog::AnyFile );
00384     aDlg->setFilter( tr( "PATTERN_FILT" ) );
00385     if ( myName->text() != "" )
00386       aDlg->selectFile( myName->text() );
00387 
00388     if ( aDlg->exec() != Accepted )
00389       return;
00390 
00391     QString fName = aDlg->selectedFile();
00392     if ( fName.isEmpty() )
00393       return;
00394 
00395     if ( QFileInfo( fName ).suffix().isEmpty() )
00396       fName = autoExtension( fName );
00397 
00398     fName = QDir::convertSeparators( fName );
00399 
00400     QString aData( myPattern->GetString() );
00401     long aLen = aData.length();
00402 
00403     QFile aFile( fName );
00404     aFile.open( QIODevice::WriteOnly );
00405     long aWritten = aFile.write( aData.toLatin1(), aLen );
00406     aFile.close();
00407 
00408     if ( aWritten != aLen ) {
00409       SUIT_MessageBox::information( this,
00410                                     tr( "SMESH_ERROR" ),
00411                                     tr( "ERROR_OF_SAVING" ) );
00412     } 
00413     else {
00414       //SUIT_Application::getDesktop()->setSelectionModes(ActorSelection);
00415       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
00416         aViewWindow->SetSelectionMode( ActorSelection );
00417       disconnect( mySelectionMgr, 0, this, 0 );
00418       disconnect( mySMESHGUI, 0, this, 0 );
00419       mySMESHGUI->ResetState();
00420       accept();
00421       emit NewPattern();
00422     }
00423   } 
00424   catch ( const SALOME::SALOME_Exception& S_ex ) {
00425     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00426   } 
00427   catch (...) {
00428   }
00429 }
00430 
00431 //=======================================================================
00432 // function : GetPatternName()
00433 // purpose  : Get name of pattern
00434 //=======================================================================
00435 QString SMESHGUI_CreatePatternDlg::GetPatternName() const
00436 {
00437   return myName->text();
00438 }
00439 
00440 //=======================================================================
00441 // function : GetPattern()
00442 // purpose  : Get result pattern
00443 //=======================================================================
00444 SMESH::SMESH_Pattern_ptr SMESHGUI_CreatePatternDlg::GetPattern()
00445 {
00446   return myPattern.in();
00447 }
00448 
00449 //=======================================================================
00450 // function : onOk()
00451 // purpose  : SLOT called when "Ok" button pressed.
00452 //=======================================================================
00453 void SMESHGUI_CreatePatternDlg::onOk()
00454 {
00455   try {
00456     if ( !isValid() )
00457       return;
00458 
00459     if ( !myIsLoaded )
00460       loadFromObject( true );
00461 
00462     // Load pattern from object
00463     if ( !myIsLoaded ) {
00464       return;
00465     }
00466     else {
00467       //SUIT_Application::getDesktop()->setSelectionModes(ActorSelection);
00468       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
00469         aViewWindow->SetSelectionMode( ActorSelection );
00470       disconnect( mySelectionMgr, 0, this, 0 );
00471       disconnect( mySMESHGUI, 0, this, 0 );
00472       mySMESHGUI->ResetState();
00473       accept();
00474       emit NewPattern();
00475     }
00476   } 
00477   catch ( const SALOME::SALOME_Exception& S_ex ) {
00478     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00479   } 
00480   catch (...) {
00481   }
00482 }
00483 
00484 //=======================================================================
00485 // function : onClose()
00486 // purpose  : SLOT called when "Close" button pressed. Close dialog
00487 //=======================================================================
00488 void SMESHGUI_CreatePatternDlg::onClose()
00489 {
00490   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
00491     aViewWindow->SetSelectionMode( ActorSelection );
00492   disconnect( mySelectionMgr, 0, this, 0 );
00493   disconnect( mySMESHGUI, 0, this, 0 );
00494   mySMESHGUI->ResetState();
00495   reject();
00496   emit Close();
00497 }
00498 
00499 //=================================================================================
00500 // function : onHelp()
00501 // purpose  :
00502 //=================================================================================
00503 void SMESHGUI_CreatePatternDlg::onHelp()
00504 {
00505   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
00506   if ( app ) 
00507     app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : QString( "" ), myHelpFileName );
00508   else {
00509     QString platform;
00510 #ifdef WIN32
00511     platform = "winapplication";
00512 #else
00513     platform = "application";
00514 #endif
00515     SUIT_MessageBox::warning( this, 
00516                               tr( "WRN_WARNING" ),
00517                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
00518                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", 
00519                                                                     platform ) ).
00520                               arg( myHelpFileName ) );
00521   }
00522 }
00523 
00524 //=======================================================================
00525 // function : loadFromObject()
00526 // purpose  : Load pattern from geom object corresponding to the mesh/submesh
00527 //=======================================================================
00528 bool SMESHGUI_CreatePatternDlg::loadFromObject( const bool theMess )
00529 {
00530   try {
00531     myIsLoaded = false;
00532 
00533     if ( myPattern->_is_nil() )
00534       myPattern = SMESH::GetPattern();
00535 
00536     if ( (myMesh->_is_nil() && mySubMesh->_is_nil()) || myGeomObj->_is_nil() )
00537       return false;
00538 
00539     SMESH::SMESH_Mesh_ptr aMesh = mySubMesh->_is_nil() ? myMesh.in() : mySubMesh->GetFather();
00540 
00541     myIsLoaded = myType == Type_2d
00542       ? myPattern->LoadFromFace( aMesh, myGeomObj, myProjectChk->isChecked() )
00543       : myPattern->LoadFrom3DBlock( aMesh, myGeomObj );
00544 
00545     if ( !myIsLoaded && theMess ) {
00546       QString aMess;
00547       SMESH::SMESH_Pattern::ErrorCode aCode = myPattern->GetErrorCode();
00548 
00549       if      ( aCode == SMESH::SMESH_Pattern::ERR_LOAD_EMPTY_SUBMESH )   aMess = tr( "ERR_LOAD_EMPTY_SUBMESH" );
00550       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADF_NARROW_FACE )    aMess = tr( "ERR_LOADF_NARROW_FACE" );
00551       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADF_CLOSED_FACE )    aMess = tr( "ERR_LOADF_CLOSED_FACE" );
00552       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADF_CANT_PROJECT )   aMess = tr( "ERR_LOADF_CANT_PROJECT" );
00553       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADV_BAD_SHAPE )      aMess = tr( "ERR_LOADV_BAD_SHAPE" );
00554       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADV_COMPUTE_PARAMS ) aMess = tr( "ERR_LOADV_COMPUTE_PARAMS" );
00555       else                                                                aMess = tr( "ERROR_OF_CREATION" );
00556 
00557       SUIT_MessageBox::information( this, tr( "SMESH_ERROR" ), aMess );
00558     }
00559   } 
00560   catch ( const SALOME::SALOME_Exception& S_ex ) {
00561     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00562   }
00563 
00564   return myIsLoaded;
00565 }
00566 
00567 //=======================================================================
00568 // function : onSelectionDone()
00569 // purpose  : SLOT called when selection changed
00570 //=======================================================================
00571 void SMESHGUI_CreatePatternDlg::onSelectionDone()
00572 {
00573   try {
00574     SALOME_ListIO aList;
00575     mySelectionMgr->selectedObjects( aList, SVTK_Viewer::Type() );
00576     if ( aList.Extent() != 1 )
00577       return;
00578 
00579     // Get mesh or sub-mesh from selection
00580     Handle(SALOME_InteractiveObject) anIO = aList.First();
00581     SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>( anIO );
00582     SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>( anIO );
00583     if ( aMesh->_is_nil() && aSubMesh->_is_nil() )
00584       return;
00585 
00586     // Get geom object corresponding to the mesh
00587     _PTR(SObject) aSO;
00588     if ( !aMesh->_is_nil() )
00589       aSO = SMESH::FindSObject( aMesh.in() );
00590     else
00591       aSO = SMESH::FindSObject( aSubMesh.in() );
00592 
00593     GEOM::GEOM_Object_var aGeomObj = SMESH::GetGeom( aSO );
00594     if ( aGeomObj->_is_nil() )
00595       return;
00596 
00597     myGeomObj = aGeomObj;
00598 
00599     // init class fields
00600     if ( !aMesh->_is_nil() ) {
00601       myMesh = aMesh;
00602       mySubMesh = SMESH::SMESH_subMesh::_nil();
00603     } 
00604     else {
00605       mySubMesh = aSubMesh;
00606       myMesh = SMESH::SMESH_Mesh::_nil();
00607     }
00608 
00609     QString aName;
00610     SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aName );
00611     myMeshEdit->setText( aName );
00612 
00613     if ( myType == Type_2d ) {
00614       loadFromObject( true );
00615       displayPreview();
00616     }
00617   } 
00618   catch (...) {
00619     myMesh = SMESH::SMESH_Mesh::_nil();
00620     mySubMesh = SMESH::SMESH_subMesh::_nil();
00621     myGeomObj = GEOM::GEOM_Object::_nil();
00622     erasePreview();
00623   }
00624 }
00625 
00626 //=======================================================================
00627 // function : onDeactivate()
00628 // purpose  : SLOT called when dialog must be deativated
00629 //=======================================================================
00630 void SMESHGUI_CreatePatternDlg::onDeactivate()
00631 {
00632   disconnect( mySelectionMgr, 0, this, 0 );
00633   setEnabled( false );
00634 }
00635 
00636 //=======================================================================
00637 // function : enterEvent()
00638 // purpose  : Event filter
00639 //=======================================================================
00640 void SMESHGUI_CreatePatternDlg::enterEvent( QEvent* )
00641 {
00642   // there is a stange problem that enterEvent() comes after onSave()
00643   if ( isVisible () ) {
00644     mySMESHGUI->EmitSignalDeactivateDialog();
00645     setEnabled( true );
00646     activateSelection();
00647     connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );
00648   }
00649 }
00650 
00651 //=================================================================================
00652 // function : closeEvent()
00653 // purpose  : Close dialog box
00654 //=================================================================================
00655 void SMESHGUI_CreatePatternDlg::closeEvent( QCloseEvent* )
00656 {
00657   onClose();
00658 }
00659 
00660 //=======================================================================
00661 // function : onSelBtnClicked()
00662 // purpose  : SLOT. Called when -> button clicked.
00663 //=======================================================================
00664 void SMESHGUI_CreatePatternDlg::onSelBtnClicked()
00665 {
00666   onSelectionDone();
00667 }
00668 
00669 //================================================================
00670 // function : autoExtension()
00671 // purpose  : Append extension to the file name
00672 //================================================================
00673 QString SMESHGUI_CreatePatternDlg::autoExtension( const QString& theFileName ) const
00674 {
00675   QString anExt = theFileName.section('.', -1);
00676   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;
00677 }
00678 
00679 //=======================================================================
00680 // function : displayPreview()
00681 // purpose  : Display preview
00682 //=======================================================================
00683 void SMESHGUI_CreatePatternDlg::displayPreview()
00684 {
00685   // Redisplay preview in dialog
00686   try {
00687     if ( !myIsLoaded ) {
00688       erasePreview();
00689     }
00690     else {
00691       SMESH::point_array_var pnts = myPattern->GetPoints();
00692       SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
00693       SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints( false );
00694 
00695       if ( pnts->length()       == 0 ||
00696            keyPoints->length()  == 0 ||
00697            elemPoints->length() == 0 ) {
00698         myIsLoaded = false;
00699         erasePreview();
00700         return;
00701       }
00702 
00703       PointVector aPoints( pnts->length() );
00704       QVector<int> aKeyPoints( keyPoints->length() );
00705       ConnectivityVector anElemPoints( elemPoints->length() );
00706 
00707       for ( int i = 0, n = pnts->length(); i < n; i++ )
00708         aPoints[ i ] = pnts[ i ];
00709 
00710       for ( int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++ )
00711         aKeyPoints[ i2 ] = keyPoints[ i2 ];
00712 
00713       for (int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++) {
00714         QVector<int> aVec (elemPoints[ i3 ].length());
00715         for (int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++)
00716           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];
00717 
00718         anElemPoints[ i3 ] = aVec;
00719       }
00720 
00721       myPicture2d->SetPoints( aPoints, aKeyPoints, anElemPoints );
00722     }
00723 
00724     return;
00725 
00726   } 
00727   catch ( const SALOME::SALOME_Exception& S_ex ) {
00728     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00729   }
00730   catch (...) {
00731   }
00732   erasePreview();
00733 }
00734 
00735 //=======================================================================
00736 // function : erasePreview()
00737 // purpose  : Erase preview
00738 //=======================================================================
00739 void SMESHGUI_CreatePatternDlg::erasePreview()
00740 {
00741   // Erase preview in 2D viewer
00742   myPicture2d->SetPoints( PointVector(), QVector<int>(), ConnectivityVector() );
00743 }
00744 
00745 //=======================================================================
00746 // function : activateSelection()
00747 // purpose  : Activate selection in accordance with current pattern type
00748 //=======================================================================
00749 void SMESHGUI_CreatePatternDlg::activateSelection()
00750 {
00751   mySelectionMgr->clearFilters();
00752   //SUIT_Application::getDesktop()->setSelectionModes(ActorSelection);
00753   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
00754     aViewWindow->SetSelectionMode( ActorSelection );
00755 
00756   if ( myType == Type_2d ) {
00757     mySelectionMgr->installFilter( new SMESH_NumberFilter( "SMESH",
00758                                                            TopAbs_SHAPE,
00759                                                            -1,
00760                                                            TopAbs_FACE ) );
00761   } 
00762   else {
00763     TColStd_MapOfInteger aTypes;
00764     aTypes.Add( TopAbs_SHELL );
00765     aTypes.Add( TopAbs_SOLID );
00766     mySelectionMgr->installFilter( new SMESH_NumberFilter( "SMESH",
00767                                                            TopAbs_FACE,
00768                                                            6,
00769                                                            aTypes,
00770                                                            GEOM::GEOM_Object::_nil(),
00771                                                            true ) );
00772   }
00773 }
00774 
00775 //=======================================================================
00776 // function : onTypeChanged()
00777 // purpose  : SLOT. Called when pattern type changed.
00778 //            Change dialog's look and feel
00779 //=======================================================================
00780 void SMESHGUI_CreatePatternDlg::onTypeChanged( int theType )
00781 {
00782   if ( myType == theType )
00783     return;
00784 
00785   myType = theType;
00786 
00787   myPicture2d->setVisible( theType == Type_2d );
00788   myProjectChk->setVisible( theType == Type_2d );
00789 }
00790 
00791 //=================================================================================
00792 // function : keyPressEvent()
00793 // purpose  :
00794 //=================================================================================
00795 void SMESHGUI_CreatePatternDlg::keyPressEvent( QKeyEvent* e )
00796 {
00797   QDialog::keyPressEvent( e );
00798   if ( e->isAccepted() )
00799     return;
00800 
00801   if ( e->key() == Qt::Key_F1 ){
00802     e->accept();
00803     onHelp();
00804   }
00805 }