1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_AddSubMeshDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_AddSubMeshDlg.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_GEOMGenUtils.h"
34 #include "SMESHGUI_HypothesesUtils.h"
38 #include "QAD_Application.h"
39 #include "QAD_Desktop.h"
40 #include "QAD_MessageBox.h"
41 #include "QAD_WaitCursor.h"
42 #include "QAD_Operation.h"
44 #include "SALOME_ListIteratorOfListIO.hxx"
45 #include "SALOMEGUI_QtCatchCorbaException.hxx"
47 #include "utilities.h"
50 #include <qgroupbox.h>
52 #include <qlineedit.h>
53 #include <qpushbutton.h>
60 SMESH::SMESH_subMesh_var AddSubMesh(SMESH::SMESH_Mesh_ptr theMesh,
61 GEOM::GEOM_Object_ptr theShapeObject,
62 const char* theMeshName )
64 SMESH::SMESH_subMesh_var aSubMesh;
66 if ( !theMesh->_is_nil() && !theShapeObject->_is_nil() )
67 aSubMesh = theMesh->GetSubMesh( theShapeObject, theMeshName );
69 catch( const SALOME::SALOME_Exception& S_ex ) {
70 QtCatchCorbaException( S_ex );
72 return aSubMesh._retn();
76 //=================================================================================
77 // class : SMESHGUI_AddSubMeshDlg()
78 // purpose : Constructs a SMESHGUI_AddSubMeshDlg which is a child of 'parent', with the
79 // name 'name' and widget flags set to 'f'.
80 // The dialog will by default be modeless, unless you set 'modal' to
81 // TRUE to construct a modal dialog.
82 //=================================================================================
83 SMESHGUI_AddSubMeshDlg::SMESHGUI_AddSubMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
84 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
86 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
88 setName( "SMESHGUI_AddSubMeshDlg" );
89 setCaption( tr( "SMESH_ADD_SUBMESH" ) );
90 setSizeGripEnabled( TRUE );
91 QGridLayout* SMESHGUI_AddSubMeshDlgLayout = new QGridLayout( this );
92 SMESHGUI_AddSubMeshDlgLayout->setSpacing( 6 );
93 SMESHGUI_AddSubMeshDlgLayout->setMargin( 11 );
95 /***************************************************************/
96 GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
97 GroupC1->setColumnLayout(0, Qt::Vertical );
98 GroupC1->layout()->setSpacing( 0 );
99 GroupC1->layout()->setMargin( 0 );
100 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
101 GroupC1Layout->setAlignment( Qt::AlignTop );
102 GroupC1Layout->setSpacing( 6 );
103 GroupC1Layout->setMargin( 11 );
105 TextLabel_NameMesh = new QLabel( tr( "SMESH_NAME" ), GroupC1, "TextLabel_NameMesh" );
106 GroupC1Layout->addWidget( TextLabel_NameMesh, 0, 0 );
107 LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
108 GroupC1Layout->addWidget( LineEdit_NameMesh, 0, 2 );
110 TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_MESH" ), GroupC1, "TextLabelC1A1" );
111 GroupC1Layout->addWidget( TextLabelC1A1, 1, 0 );
112 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
113 SelectButtonC1A1->setPixmap( image0 );
114 SelectButtonC1A1->setToggleButton( FALSE );
115 GroupC1Layout->addWidget( SelectButtonC1A1, 1, 1 );
116 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
117 LineEditC1A1->setReadOnly( true );
118 GroupC1Layout->addWidget( LineEditC1A1, 1, 2 );
120 TextLabelC1A2 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A2" );
121 GroupC1Layout->addWidget( TextLabelC1A2, 2, 0 );
122 SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
123 SelectButtonC1A2->setPixmap( image0 );
124 SelectButtonC1A2->setToggleButton( FALSE );
125 GroupC1Layout->addWidget( SelectButtonC1A2, 2, 1 );
126 LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
127 LineEditC1A2->setReadOnly( true );
128 GroupC1Layout->addWidget( LineEditC1A2, 2, 2 );
130 TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
131 GroupC1Layout->addWidget( TextLabelC1A1Hyp, 3, 0 );
132 SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
133 SelectButtonC1A1Hyp->setPixmap( image0 );
134 GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 3, 1 );
135 LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
136 LineEditC1A1Hyp->setReadOnly( true );
137 GroupC1Layout->addWidget( LineEditC1A1Hyp, 3, 2 );
139 TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
140 GroupC1Layout->addWidget( TextLabelC1A1Algo, 4, 0 );
141 SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
142 SelectButtonC1A1Algo->setPixmap( image0 );
143 GroupC1Layout->addWidget( SelectButtonC1A1Algo, 4, 1 );
144 LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
145 LineEditC1A1Algo->setReadOnly( true );
146 GroupC1Layout->addWidget( LineEditC1A1Algo, 4, 2 );
148 SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupC1, 1, 0 );
150 /***************************************************************/
151 GroupButtons = new QGroupBox( this, "GroupButtons" );
152 GroupButtons->setColumnLayout(0, Qt::Vertical );
153 GroupButtons->layout()->setSpacing( 0 );
154 GroupButtons->layout()->setMargin( 0 );
155 QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
156 GroupButtonsLayout->setAlignment( Qt::AlignTop );
157 GroupButtonsLayout->setSpacing( 6 );
158 GroupButtonsLayout->setMargin( 11 );
160 buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
161 buttonOk->setAutoDefault( TRUE );
162 buttonOk->setDefault( TRUE );
163 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
165 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
166 buttonApply->setAutoDefault( TRUE );
167 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
169 GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
171 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
172 buttonCancel->setAutoDefault( TRUE );
173 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
175 SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
177 /***************************************************************/
182 //=================================================================================
183 // function : ~SMESHGUI_AddSubMeshDlg()
184 // purpose : Destroys the object and frees any allocated resources
185 //=================================================================================
186 SMESHGUI_AddSubMeshDlg::~SMESHGUI_AddSubMeshDlg()
188 // no need to delete child widgets, Qt does it all for us
192 //=================================================================================
195 //=================================================================================
196 void SMESHGUI_AddSubMeshDlg::Init( SALOME_Selection* Sel )
199 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
200 mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
202 myGeomFilter = new SALOME_TypeFilter( "GEOM" );
203 myMeshFilter = new SMESH_TypeFilter( MESH );
204 myAlgorithmFilter = new SMESH_TypeFilter( ALGORITHM );
205 myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
207 /* signals and slots connections */
208 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
209 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
210 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
212 connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
213 connect( SelectButtonC1A2, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
214 connect( SelectButtonC1A1Hyp, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
215 connect( SelectButtonC1A1Algo, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
217 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
218 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
219 connect( mySMESHGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
222 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
226 LineEdit_NameMesh->setText( tr( "SMESH_SUBMESH" ) );
227 LineEdit_NameMesh->setFocus() ;
228 myEditCurrentArgument = LineEditC1A1 ;
229 mySelection->ClearFilters() ;
230 mySelection->AddFilter( myMeshFilter ) ;
232 SelectionIntoArgument();
235 //=================================================================================
236 // function : ClickOnOk()
238 //=================================================================================
239 void SMESHGUI_AddSubMeshDlg::ClickOnOk()
241 if ( this->ClickOnApply() )
242 this->ClickOnCancel() ;
245 //=================================================================================
246 // function : ClickOnApply()
248 //=================================================================================
249 bool SMESHGUI_AddSubMeshDlg::ClickOnApply()
251 if (mySMESHGUI->ActiveStudyLocked())
254 QString myNameSubMesh = LineEdit_NameMesh->text().stripWhiteSpace();
255 if ( myNameSubMesh.isEmpty() ) {
256 QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
260 if ( myMesh->_is_nil() || myGeomShape->_is_nil() || ( !HypoList.count() && !AlgoList.count() ) )
263 SALOMEDS::SObject_var aMeshSO = SMESH::FindSObject( myMesh );
264 GEOM::GEOM_Object_var myMainShape = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
265 if ( myMainShape->_is_nil() )
270 QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
276 SMESH::SMESH_subMesh_var aSubMesh = SMESH::AddSubMesh( myMesh, myGeomShape, myNameSubMesh ) ;
279 if ( !aSubMesh->_is_nil() ) {
281 int nbAlgo = AlgoList.count();
282 int nbHyps = HypoList.count() + nbAlgo;
283 for( int i = 0; i < nbHyps; i++ ) {
284 SALOMEDS::SObject_var aHypSO = SMESH::GetActiveStudyDocument()->FindObjectID
285 ( i < nbAlgo ? AlgoList[i] : HypoList[i-nbAlgo] );
286 if ( !aHypSO->_is_nil() ) {
287 CORBA::Object_var anObject = aHypSO->GetObject();
288 if ( !CORBA::is_nil( anObject ) ) {
289 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
290 if ( !aHyp->_is_nil() )
291 if ( SMESH::AddHypothesisOnSubMesh( aSubMesh, aHyp ) )
295 SCRUTE( CORBA::is_nil( anObject ));
299 SCRUTE( aHypSO->_is_nil() );
304 SCRUTE( aSubMesh->_is_nil() );
307 // commit transaction
309 return ( nbSuccess > 0 );
313 //=================================================================================
314 // function : ClickOnCancel()
316 //=================================================================================
317 void SMESHGUI_AddSubMeshDlg::ClickOnCancel()
322 //=======================================================================
323 //function : IsFatherOf
325 //=======================================================================
327 static bool IsFatherOf( SALOMEDS::SObject_ptr SO, SALOMEDS::SObject_ptr fatherSO ) {
328 if ( !SO->_is_nil() && !fatherSO->_is_nil() ) {
329 SALOMEDS::SObject_var aSO = SO->GetFather();
330 while( strlen( aSO->GetID() ) >= strlen( fatherSO->GetID() ) ) {
331 if ( QString( aSO->GetID() ) == QString( fatherSO->GetID() ) )
333 aSO = aSO->GetFather();
339 //=================================================================================
340 // function : SelectionIntoArgument()
341 // purpose : Called when selection as changed or other case
342 //=================================================================================
343 void SMESHGUI_AddSubMeshDlg::SelectionIntoArgument()
345 QString aString = "";
346 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelection, aString) ;
348 if ( myEditCurrentArgument == LineEditC1A1 ) {
351 myMesh = SMESH::SMESH_Mesh::_nil();
355 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
356 myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO) ;
357 if( myMesh->_is_nil() ) {
361 myGeomShape = GEOM::GEOM_Object::_nil();
362 LineEditC1A2->setText( "" );
364 else if ( myEditCurrentArgument == LineEditC1A2 ) {
367 myGeomShape = GEOM::GEOM_Object::_nil();
371 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
372 myGeomShape = SMESH::IObjectToInterface<GEOM::GEOM_Object>(IO) ;
373 if( myGeomShape->_is_nil() || !GEOMBase::IsShape( myGeomShape ) )
375 myGeomShape = GEOM::GEOM_Object::_nil();
378 if ( !myMesh->_is_nil() ) {
379 SALOMEDS::SObject_var aMeshSO = SMESH::FindSObject( myMesh );
380 GEOM::GEOM_Object_var aMainGeomShape = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
381 SALOMEDS::SObject_var aMainGeomShapeSO = SMESH::FindSObject( aMainGeomShape );
382 if ( aMainGeomShapeSO->_is_nil() || !IsFatherOf( SMESH::GetActiveStudyDocument()->FindObjectID( IO->getEntry() ), aMainGeomShapeSO ) ) {
383 myGeomShape = GEOM::GEOM_Object::_nil();
389 else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
393 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
394 for ( ; Itinit.More(); Itinit.Next() ) {
395 HypoList.append( Itinit.Value()->getEntry() );
398 aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
404 else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
408 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
409 for ( ; Itinit.More(); Itinit.Next() ) {
410 AlgoList.append( Itinit.Value()->getEntry() );
413 aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
420 myEditCurrentArgument->setText(aString) ;
422 UpdateControlState();
426 //=================================================================================
427 // function : SetEditCurrentArgument()
429 //=================================================================================
430 void SMESHGUI_AddSubMeshDlg::SetEditCurrentArgument()
432 QPushButton* send = (QPushButton*)sender();
433 if(send == SelectButtonC1A1) {
434 LineEditC1A1->setFocus() ;
435 myEditCurrentArgument = LineEditC1A1;
436 mySelection->ClearFilters() ;
437 mySelection->AddFilter(myMeshFilter) ;
438 } else if (send == SelectButtonC1A2) {
439 LineEditC1A2->setFocus() ;
440 myEditCurrentArgument = LineEditC1A2;
441 mySelection->ClearFilters() ;
442 mySelection->AddFilter(myGeomFilter) ;
443 } else if( send == SelectButtonC1A1Hyp ) {
444 LineEditC1A1Hyp->setFocus() ;
445 myEditCurrentArgument = LineEditC1A1Hyp ;
446 mySelection->ClearFilters() ;
447 mySelection->AddFilter(myHypothesisFilter) ;
448 } else if( send == SelectButtonC1A1Algo ) {
449 LineEditC1A1Algo->setFocus() ;
450 myEditCurrentArgument = LineEditC1A1Algo ;
451 mySelection->ClearFilters() ;
452 mySelection->AddFilter(myAlgorithmFilter) ;
454 SelectionIntoArgument() ;
457 //=================================================================================
458 // function : DeactivateActiveDialog()
460 //=================================================================================
461 void SMESHGUI_AddSubMeshDlg::DeactivateActiveDialog()
463 if ( GroupC1->isEnabled() ) {
464 disconnect( mySelection, 0, this, 0 );
465 GroupC1->setEnabled(false) ;
466 GroupButtons->setEnabled(false) ;
471 //=================================================================================
472 // function : ActivateThisDialog()
474 //=================================================================================
475 void SMESHGUI_AddSubMeshDlg::ActivateThisDialog()
477 mySMESHGUI->EmitSignalDeactivateDialog() ;
478 GroupC1->setEnabled(true) ;
479 GroupButtons->setEnabled(true) ;
480 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
484 //=================================================================================
485 // function : enterEvent()
487 //=================================================================================
488 void SMESHGUI_AddSubMeshDlg::enterEvent(QEvent* e)
490 if ( !GroupC1->isEnabled() )
491 ActivateThisDialog() ;
495 //=================================================================================
496 // function : closeEvent()
498 //=================================================================================
499 void SMESHGUI_AddSubMeshDlg::closeEvent( QCloseEvent* e )
501 disconnect( mySelection, 0, this, 0 );
502 mySMESHGUI->ResetState() ;
503 mySelection->ClearFilters() ;
504 QDialog::closeEvent( e );
507 //=================================================================================
508 // function : UpdateControlState()
510 //=================================================================================
511 void SMESHGUI_AddSubMeshDlg::UpdateControlState()
513 bool isEnabled = ( !myMesh->_is_nil() && !myGeomShape->_is_nil() && ( HypoList.count() || AlgoList.count() ) );
514 bool isImportedMesh = false;
515 if ( !myMesh->_is_nil() ) {
516 SALOMEDS::SObject_var aMeshSO = SMESH::FindSObject( myMesh );
517 GEOM::GEOM_Object_var myGeomShape = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
518 isImportedMesh = myGeomShape->_is_nil();
521 buttonOk ->setEnabled( isEnabled && !isImportedMesh );
522 buttonApply->setEnabled( isEnabled && !isImportedMesh );