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
30 #include "SMESHGUI_AddSubMeshDlg.h"
32 #include "SALOME_ListIteratorOfListIO.hxx"
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36 #include "QAD_MessageBox.h"
37 #include "QAD_WaitCursor.h"
38 #include "QAD_Operation.h"
40 #include "utilities.h"
43 #include <qgroupbox.h>
45 #include <qlineedit.h>
46 #include <qpushbutton.h>
50 //=================================================================================
51 // class : SMESHGUI_AddSubMeshDlg()
52 // purpose : Constructs a SMESHGUI_AddSubMeshDlg which is a child of 'parent', with the
53 // name 'name' and widget flags set to 'f'.
54 // The dialog will by default be modeless, unless you set 'modal' to
55 // TRUE to construct a modal dialog.
56 //=================================================================================
57 SMESHGUI_AddSubMeshDlg::SMESHGUI_AddSubMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
58 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
60 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
62 setName( "SMESHGUI_AddSubMeshDlg" );
63 setCaption( tr( "SMESH_ADD_SUBMESH" ) );
64 setSizeGripEnabled( TRUE );
65 QGridLayout* SMESHGUI_AddSubMeshDlgLayout = new QGridLayout( this );
66 SMESHGUI_AddSubMeshDlgLayout->setSpacing( 6 );
67 SMESHGUI_AddSubMeshDlgLayout->setMargin( 11 );
69 /***************************************************************/
70 GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
71 GroupC1->setColumnLayout(0, Qt::Vertical );
72 GroupC1->layout()->setSpacing( 0 );
73 GroupC1->layout()->setMargin( 0 );
74 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
75 GroupC1Layout->setAlignment( Qt::AlignTop );
76 GroupC1Layout->setSpacing( 6 );
77 GroupC1Layout->setMargin( 11 );
79 TextLabel_NameMesh = new QLabel( tr( "SMESH_NAME" ), GroupC1, "TextLabel_NameMesh" );
80 GroupC1Layout->addWidget( TextLabel_NameMesh, 0, 0 );
81 LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
82 GroupC1Layout->addWidget( LineEdit_NameMesh, 0, 2 );
84 TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_MESH" ), GroupC1, "TextLabelC1A1" );
85 GroupC1Layout->addWidget( TextLabelC1A1, 1, 0 );
86 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
87 SelectButtonC1A1->setPixmap( image0 );
88 SelectButtonC1A1->setToggleButton( FALSE );
89 GroupC1Layout->addWidget( SelectButtonC1A1, 1, 1 );
90 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
91 GroupC1Layout->addWidget( LineEditC1A1, 1, 2 );
93 TextLabelC1A2 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A2" );
94 GroupC1Layout->addWidget( TextLabelC1A2, 2, 0 );
95 SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
96 SelectButtonC1A2->setPixmap( image0 );
97 SelectButtonC1A2->setToggleButton( FALSE );
98 GroupC1Layout->addWidget( SelectButtonC1A2, 2, 1 );
99 LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
100 GroupC1Layout->addWidget( LineEditC1A2, 2, 2 );
102 TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
103 GroupC1Layout->addWidget( TextLabelC1A1Hyp, 3, 0 );
104 SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
105 SelectButtonC1A1Hyp->setPixmap( image0 );
106 GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 3, 1 );
107 LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
108 GroupC1Layout->addWidget( LineEditC1A1Hyp, 3, 2 );
110 TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
111 GroupC1Layout->addWidget( TextLabelC1A1Algo, 4, 0 );
112 SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
113 SelectButtonC1A1Algo->setPixmap( image0 );
114 GroupC1Layout->addWidget( SelectButtonC1A1Algo, 4, 1 );
115 LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
116 GroupC1Layout->addWidget( LineEditC1A1Algo, 4, 2 );
118 SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupC1, 1, 0 );
120 /***************************************************************/
121 GroupButtons = new QGroupBox( this, "GroupButtons" );
122 GroupButtons->setColumnLayout(0, Qt::Vertical );
123 GroupButtons->layout()->setSpacing( 0 );
124 GroupButtons->layout()->setMargin( 0 );
125 QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
126 GroupButtonsLayout->setAlignment( Qt::AlignTop );
127 GroupButtonsLayout->setSpacing( 6 );
128 GroupButtonsLayout->setMargin( 11 );
130 buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
131 buttonOk->setAutoDefault( TRUE );
132 buttonOk->setDefault( TRUE );
133 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
135 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
136 buttonApply->setAutoDefault( TRUE );
137 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
139 GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
141 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
142 buttonCancel->setAutoDefault( TRUE );
143 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
145 SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
147 /***************************************************************/
152 //=================================================================================
153 // function : ~SMESHGUI_AddSubMeshDlg()
154 // purpose : Destroys the object and frees any allocated resources
155 //=================================================================================
156 SMESHGUI_AddSubMeshDlg::~SMESHGUI_AddSubMeshDlg()
158 // no need to delete child widgets, Qt does it all for us
162 //=================================================================================
165 //=================================================================================
166 void SMESHGUI_AddSubMeshDlg::Init( SALOME_Selection* Sel )
169 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
170 mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
172 myGeomFilter = new SALOME_TypeFilter( "GEOM" );
173 myMeshFilter = new SMESH_TypeFilter( MESH );
174 myAlgorithmFilter = new SMESH_TypeFilter( ALGORITHM );
175 myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
177 /* signals and slots connections */
178 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
179 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
180 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
182 connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
183 connect( SelectButtonC1A2, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
184 connect( SelectButtonC1A1Hyp, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
185 connect( SelectButtonC1A1Algo, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
187 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
188 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
189 connect( mySMESHGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
192 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
196 LineEdit_NameMesh->setText( tr( "SMESH_SUBMESH" ) );
197 LineEdit_NameMesh->setFocus() ;
198 myEditCurrentArgument = LineEditC1A1 ;
199 mySelection->ClearFilters() ;
200 mySelection->AddFilter( myMeshFilter ) ;
202 SelectionIntoArgument();
205 //=================================================================================
206 // function : ClickOnOk()
208 //=================================================================================
209 void SMESHGUI_AddSubMeshDlg::ClickOnOk()
211 if ( this->ClickOnApply() )
212 this->ClickOnCancel() ;
215 //=================================================================================
216 // function : ClickOnApply()
218 //=================================================================================
219 bool SMESHGUI_AddSubMeshDlg::ClickOnApply()
221 QString myNameSubMesh = LineEdit_NameMesh->text().stripWhiteSpace();
222 if ( myNameSubMesh.isEmpty() ) {
223 QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
227 if ( myMesh->_is_nil() || myGeomShape->_is_nil() || ( !HypoList.count() && !AlgoList.count() ) )
230 SALOMEDS::SObject_var aMeshSO = mySMESHGUI->GetStudyAPI().FindObject( myMesh );
231 GEOM::GEOM_Shape_var myMainShape = mySMESHGUI->GetStudyAPI().GetShapeOnMeshOrSubMesh( aMeshSO );
232 if ( myMainShape->_is_nil() )
237 QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
243 SMESH::SMESH_subMesh_var aSubMesh = mySMESHGUI->AddSubMesh( myMesh, myGeomShape, myNameSubMesh ) ;
245 if ( !aSubMesh->_is_nil() ) {
247 for( int i = 0; i < HypoList.count(); i++ ) {
248 SALOMEDS::SObject_var aHypSO = mySMESHGUI->GetStudy()->FindObjectID( HypoList[i] );
249 if ( !aHypSO->_is_nil() ) {
250 CORBA::Object_var anObject = aHypSO->GetObject();
251 if ( !CORBA::is_nil( anObject ) ) {
252 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
253 if ( !aHyp->_is_nil() )
254 if ( !mySMESHGUI->AddHypothesisOnSubMesh( aSubMesh, aHyp ) ) {
263 for( int i = 0; i < AlgoList.count(); i++ ) {
264 SALOMEDS::SObject_var aHypSO = mySMESHGUI->GetStudy()->FindObjectID( AlgoList[i] );
265 if ( !aHypSO->_is_nil() ) {
266 CORBA::Object_var anObject = aHypSO->GetObject();
267 if ( !CORBA::is_nil( anObject ) ) {
268 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
269 if ( !aHyp->_is_nil() )
270 if ( !mySMESHGUI->AddAlgorithmOnSubMesh( aSubMesh, aHyp ) ) {
279 // commit transaction
285 //=================================================================================
286 // function : ClickOnCancel()
288 //=================================================================================
289 void SMESHGUI_AddSubMeshDlg::ClickOnCancel()
294 static bool IsFatherOf( SALOMEDS::SObject_ptr SO, SALOMEDS::SObject_ptr fatherSO ) {
295 if ( !SO->_is_nil() && !fatherSO->_is_nil() ) {
296 SALOMEDS::SObject_var aSO = SO->GetFather();
297 while( strlen( aSO->GetID() ) >= strlen( fatherSO->GetID() ) ) {
298 if ( QString( aSO->GetID() ) == QString( fatherSO->GetID() ) )
300 aSO = aSO->GetFather();
306 //=================================================================================
307 // function : SelectionIntoArgument()
308 // purpose : Called when selection as changed or other case
309 //=================================================================================
310 void SMESHGUI_AddSubMeshDlg::SelectionIntoArgument()
312 SMESHGUI_StudyAPI myStudyAPI = mySMESHGUI->GetStudyAPI();
313 QString aString = "";
315 int nbSel = mySMESHGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
317 if ( myEditCurrentArgument == LineEditC1A1 ) {
320 myMesh = SMESH::SMESH_Mesh::_nil();
324 Standard_Boolean testResult ;
325 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
326 myMesh = mySMESHGUI->ConvertIOinMesh(IO, testResult) ;
328 myMesh = SMESH::SMESH_Mesh::_nil();
332 myGeomShape = GEOM::GEOM_Shape::_nil();
333 LineEditC1A2->setText( "" );
335 else if ( myEditCurrentArgument == LineEditC1A2 ) {
338 myGeomShape = GEOM::GEOM_Shape::_nil();
342 Standard_Boolean testResult ;
343 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
344 myGeomShape = mySMESHGUI->ConvertIOinGEOMShape(IO, testResult) ;
346 myGeomShape = GEOM::GEOM_Shape::_nil();
349 if ( !myMesh->_is_nil() ) {
350 SALOMEDS::SObject_var aMeshSO = myStudyAPI.FindObject( myMesh );
351 GEOM::GEOM_Shape_var aMainGeomShape = myStudyAPI.GetShapeOnMeshOrSubMesh( aMeshSO );
352 SALOMEDS::SObject_var aMainGeomShapeSO = myStudyAPI.FindObject( aMainGeomShape );
353 if ( aMainGeomShapeSO->_is_nil() || !IsFatherOf( mySMESHGUI->GetStudy()->FindObjectID( IO->getEntry() ), aMainGeomShapeSO ) ) {
354 myGeomShape = GEOM::GEOM_Shape::_nil();
360 else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
364 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
365 for ( ; Itinit.More(); Itinit.Next() ) {
366 HypoList.append( Itinit.Value()->getEntry() );
369 aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
375 else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
379 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
380 for ( ; Itinit.More(); Itinit.Next() ) {
381 AlgoList.append( Itinit.Value()->getEntry() );
384 aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
391 myEditCurrentArgument->setText(aString) ;
393 UpdateControlState();
397 //=================================================================================
398 // function : SetEditCurrentArgument()
400 //=================================================================================
401 void SMESHGUI_AddSubMeshDlg::SetEditCurrentArgument()
403 QPushButton* send = (QPushButton*)sender();
404 if(send == SelectButtonC1A1) {
405 LineEditC1A1->setFocus() ;
406 myEditCurrentArgument = LineEditC1A1;
407 mySelection->ClearFilters() ;
408 mySelection->AddFilter(myMeshFilter) ;
409 } else if (send == SelectButtonC1A2) {
410 LineEditC1A2->setFocus() ;
411 myEditCurrentArgument = LineEditC1A2;
412 mySelection->ClearFilters() ;
413 mySelection->AddFilter(myGeomFilter) ;
414 } else if( send == SelectButtonC1A1Hyp ) {
415 LineEditC1A1Hyp->setFocus() ;
416 myEditCurrentArgument = LineEditC1A1Hyp ;
417 mySelection->ClearFilters() ;
418 mySelection->AddFilter(myHypothesisFilter) ;
419 } else if( send == SelectButtonC1A1Algo ) {
420 LineEditC1A1Algo->setFocus() ;
421 myEditCurrentArgument = LineEditC1A1Algo ;
422 mySelection->ClearFilters() ;
423 mySelection->AddFilter(myAlgorithmFilter) ;
425 SelectionIntoArgument() ;
428 //=================================================================================
429 // function : DeactivateActiveDialog()
431 //=================================================================================
432 void SMESHGUI_AddSubMeshDlg::DeactivateActiveDialog()
434 if ( GroupC1->isEnabled() ) {
435 disconnect( mySelection, 0, this, 0 );
436 GroupC1->setEnabled(false) ;
437 GroupButtons->setEnabled(false) ;
442 //=================================================================================
443 // function : ActivateThisDialog()
445 //=================================================================================
446 void SMESHGUI_AddSubMeshDlg::ActivateThisDialog()
448 mySMESHGUI->EmitSignalDeactivateDialog() ;
449 GroupC1->setEnabled(true) ;
450 GroupButtons->setEnabled(true) ;
451 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
455 //=================================================================================
456 // function : enterEvent()
458 //=================================================================================
459 void SMESHGUI_AddSubMeshDlg::enterEvent(QEvent* e)
461 if ( !GroupC1->isEnabled() )
462 ActivateThisDialog() ;
466 //=================================================================================
467 // function : closeEvent()
469 //=================================================================================
470 void SMESHGUI_AddSubMeshDlg::closeEvent( QCloseEvent* e )
472 disconnect( mySelection, 0, this, 0 );
473 mySMESHGUI->ResetState() ;
474 mySelection->ClearFilters() ;
475 QDialog::closeEvent( e );
478 //=================================================================================
479 // function : UpdateControlState()
481 //=================================================================================
482 void SMESHGUI_AddSubMeshDlg::UpdateControlState()
484 bool isEnabled = ( !myMesh->_is_nil() && !myGeomShape->_is_nil() && ( HypoList.count() || AlgoList.count() ) );
485 bool isImportedMesh = false;
486 if ( !myMesh->_is_nil() ) {
487 SALOMEDS::SObject_var aMeshSO = mySMESHGUI->GetStudyAPI().FindObject( myMesh );
488 GEOM::GEOM_Shape_var myGeomShape = mySMESHGUI->GetStudyAPI().GetShapeOnMeshOrSubMesh( aMeshSO );
489 isImportedMesh = myGeomShape->_is_nil();
492 buttonOk ->setEnabled( isEnabled && !isImportedMesh );
493 buttonApply->setEnabled( isEnabled && !isImportedMesh );