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"
36 #include "QAD_Application.h"
37 #include "QAD_Desktop.h"
38 #include "QAD_MessageBox.h"
39 #include "QAD_WaitCursor.h"
40 #include "QAD_Operation.h"
42 #include "SALOME_ListIteratorOfListIO.hxx"
43 #include "SALOMEGUI_QtCatchCorbaException.hxx"
45 #include "utilities.h"
48 #include <qgroupbox.h>
50 #include <qlineedit.h>
51 #include <qpushbutton.h>
58 SMESH::SMESH_subMesh_var AddSubMesh(SMESH::SMESH_Mesh_ptr theMesh,
59 GEOM::GEOM_Object_ptr theShapeObject,
60 const char* theMeshName )
62 SMESH::SMESH_subMesh_var aSubMesh;
64 if ( !theMesh->_is_nil() && !theShapeObject->_is_nil() )
65 aSubMesh = theMesh->GetSubMesh( theShapeObject, theMeshName );
67 catch( const SALOME::SALOME_Exception& S_ex ) {
68 QtCatchCorbaException( S_ex );
70 return aSubMesh._retn();
74 //=================================================================================
75 // class : SMESHGUI_AddSubMeshDlg()
76 // purpose : Constructs a SMESHGUI_AddSubMeshDlg which is a child of 'parent', with the
77 // name 'name' and widget flags set to 'f'.
78 // The dialog will by default be modeless, unless you set 'modal' to
79 // TRUE to construct a modal dialog.
80 //=================================================================================
81 SMESHGUI_AddSubMeshDlg::SMESHGUI_AddSubMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
82 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
84 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
86 setName( "SMESHGUI_AddSubMeshDlg" );
87 setCaption( tr( "SMESH_ADD_SUBMESH" ) );
88 setSizeGripEnabled( TRUE );
89 QGridLayout* SMESHGUI_AddSubMeshDlgLayout = new QGridLayout( this );
90 SMESHGUI_AddSubMeshDlgLayout->setSpacing( 6 );
91 SMESHGUI_AddSubMeshDlgLayout->setMargin( 11 );
93 /***************************************************************/
94 GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
95 GroupC1->setColumnLayout(0, Qt::Vertical );
96 GroupC1->layout()->setSpacing( 0 );
97 GroupC1->layout()->setMargin( 0 );
98 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
99 GroupC1Layout->setAlignment( Qt::AlignTop );
100 GroupC1Layout->setSpacing( 6 );
101 GroupC1Layout->setMargin( 11 );
103 TextLabel_NameMesh = new QLabel( tr( "SMESH_NAME" ), GroupC1, "TextLabel_NameMesh" );
104 GroupC1Layout->addWidget( TextLabel_NameMesh, 0, 0 );
105 LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
106 GroupC1Layout->addWidget( LineEdit_NameMesh, 0, 2 );
108 TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_MESH" ), GroupC1, "TextLabelC1A1" );
109 GroupC1Layout->addWidget( TextLabelC1A1, 1, 0 );
110 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
111 SelectButtonC1A1->setPixmap( image0 );
112 SelectButtonC1A1->setToggleButton( FALSE );
113 GroupC1Layout->addWidget( SelectButtonC1A1, 1, 1 );
114 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
115 LineEditC1A1->setReadOnly( true );
116 GroupC1Layout->addWidget( LineEditC1A1, 1, 2 );
118 TextLabelC1A2 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A2" );
119 GroupC1Layout->addWidget( TextLabelC1A2, 2, 0 );
120 SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
121 SelectButtonC1A2->setPixmap( image0 );
122 SelectButtonC1A2->setToggleButton( FALSE );
123 GroupC1Layout->addWidget( SelectButtonC1A2, 2, 1 );
124 LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
125 LineEditC1A2->setReadOnly( true );
126 GroupC1Layout->addWidget( LineEditC1A2, 2, 2 );
128 TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
129 GroupC1Layout->addWidget( TextLabelC1A1Hyp, 3, 0 );
130 SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
131 SelectButtonC1A1Hyp->setPixmap( image0 );
132 GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 3, 1 );
133 LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
134 LineEditC1A1Hyp->setReadOnly( true );
135 GroupC1Layout->addWidget( LineEditC1A1Hyp, 3, 2 );
137 TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
138 GroupC1Layout->addWidget( TextLabelC1A1Algo, 4, 0 );
139 SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
140 SelectButtonC1A1Algo->setPixmap( image0 );
141 GroupC1Layout->addWidget( SelectButtonC1A1Algo, 4, 1 );
142 LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
143 LineEditC1A1Algo->setReadOnly( true );
144 GroupC1Layout->addWidget( LineEditC1A1Algo, 4, 2 );
146 SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupC1, 1, 0 );
148 /***************************************************************/
149 GroupButtons = new QGroupBox( this, "GroupButtons" );
150 GroupButtons->setColumnLayout(0, Qt::Vertical );
151 GroupButtons->layout()->setSpacing( 0 );
152 GroupButtons->layout()->setMargin( 0 );
153 QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
154 GroupButtonsLayout->setAlignment( Qt::AlignTop );
155 GroupButtonsLayout->setSpacing( 6 );
156 GroupButtonsLayout->setMargin( 11 );
158 buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
159 buttonOk->setAutoDefault( TRUE );
160 buttonOk->setDefault( TRUE );
161 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
163 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
164 buttonApply->setAutoDefault( TRUE );
165 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
167 GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
169 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
170 buttonCancel->setAutoDefault( TRUE );
171 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
173 SMESHGUI_AddSubMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
175 /***************************************************************/
180 //=================================================================================
181 // function : ~SMESHGUI_AddSubMeshDlg()
182 // purpose : Destroys the object and frees any allocated resources
183 //=================================================================================
184 SMESHGUI_AddSubMeshDlg::~SMESHGUI_AddSubMeshDlg()
186 // no need to delete child widgets, Qt does it all for us
190 //=================================================================================
193 //=================================================================================
194 void SMESHGUI_AddSubMeshDlg::Init( SALOME_Selection* Sel )
197 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
198 mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
200 myGeomFilter = new SALOME_TypeFilter( "GEOM" );
201 myMeshFilter = new SMESH_TypeFilter( MESH );
202 myAlgorithmFilter = new SMESH_TypeFilter( ALGORITHM );
203 myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
205 /* signals and slots connections */
206 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
207 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
208 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
210 connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
211 connect( SelectButtonC1A2, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
212 connect( SelectButtonC1A1Hyp, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
213 connect( SelectButtonC1A1Algo, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
215 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
216 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
217 connect( mySMESHGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
220 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
224 LineEdit_NameMesh->setText( tr( "SMESH_SUBMESH" ) );
225 LineEdit_NameMesh->setFocus() ;
226 myEditCurrentArgument = LineEditC1A1 ;
227 mySelection->ClearFilters() ;
228 mySelection->AddFilter( myMeshFilter ) ;
230 SelectionIntoArgument();
233 //=================================================================================
234 // function : ClickOnOk()
236 //=================================================================================
237 void SMESHGUI_AddSubMeshDlg::ClickOnOk()
239 if ( this->ClickOnApply() )
240 this->ClickOnCancel() ;
243 //=================================================================================
244 // function : ClickOnApply()
246 //=================================================================================
247 bool SMESHGUI_AddSubMeshDlg::ClickOnApply()
249 if (mySMESHGUI->ActiveStudyLocked())
252 QString myNameSubMesh = LineEdit_NameMesh->text().stripWhiteSpace();
253 if ( myNameSubMesh.isEmpty() ) {
254 QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
258 if ( myMesh->_is_nil() || myGeomShape->_is_nil() || ( !HypoList.count() && !AlgoList.count() ) )
261 SALOMEDS::SObject_var aMeshSO = SMESH::FindSObject( myMesh );
262 GEOM::GEOM_Object_var myMainShape = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
263 if ( myMainShape->_is_nil() )
268 QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
274 SMESH::SMESH_subMesh_var aSubMesh = SMESH::AddSubMesh( myMesh, myGeomShape, myNameSubMesh ) ;
277 if ( !aSubMesh->_is_nil() ) {
279 int nbAlgo = AlgoList.count();
280 int nbHyps = HypoList.count() + nbAlgo;
281 for( int i = 0; i < nbHyps; i++ ) {
282 SALOMEDS::SObject_var aHypSO = SMESH::GetActiveStudyDocument()->FindObjectID
283 ( i < nbAlgo ? AlgoList[i] : HypoList[i-nbAlgo] );
284 if ( !aHypSO->_is_nil() ) {
285 CORBA::Object_var anObject = aHypSO->GetObject();
286 if ( !CORBA::is_nil( anObject ) ) {
287 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
288 if ( !aHyp->_is_nil() )
289 if ( SMESH::AddHypothesisOnSubMesh( aSubMesh, aHyp ) )
293 SCRUTE( CORBA::is_nil( anObject ));
297 SCRUTE( aHypSO->_is_nil() );
302 SCRUTE( aSubMesh->_is_nil() );
305 // commit transaction
307 return ( nbSuccess > 0 );
311 //=================================================================================
312 // function : ClickOnCancel()
314 //=================================================================================
315 void SMESHGUI_AddSubMeshDlg::ClickOnCancel()
320 //=======================================================================
321 //function : IsFatherOf
323 //=======================================================================
325 static bool IsFatherOf( SALOMEDS::SObject_ptr SO, SALOMEDS::SObject_ptr fatherSO ) {
326 if ( !SO->_is_nil() && !fatherSO->_is_nil() ) {
327 SALOMEDS::SObject_var aSO = SO->GetFather();
328 while( strlen( aSO->GetID() ) >= strlen( fatherSO->GetID() ) ) {
329 if ( QString( aSO->GetID() ) == QString( fatherSO->GetID() ) )
331 aSO = aSO->GetFather();
337 //=================================================================================
338 // function : SelectionIntoArgument()
339 // purpose : Called when selection as changed or other case
340 //=================================================================================
341 void SMESHGUI_AddSubMeshDlg::SelectionIntoArgument()
343 QString aString = "";
344 int nbSel = SMESH::GetNameOfSelectedIObjects(mySelection, aString) ;
346 if ( myEditCurrentArgument == LineEditC1A1 ) {
349 myMesh = SMESH::SMESH_Mesh::_nil();
353 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
354 myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO) ;
355 if( myMesh->_is_nil() ) {
359 myGeomShape = GEOM::GEOM_Object::_nil();
360 LineEditC1A2->setText( "" );
362 else if ( myEditCurrentArgument == LineEditC1A2 ) {
365 myGeomShape = GEOM::GEOM_Object::_nil();
369 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
370 myGeomShape = SMESH::IObjectToInterface<GEOM::GEOM_Object>(IO) ;
371 if( myGeomShape->_is_nil() ) {
374 if ( !myMesh->_is_nil() ) {
375 SALOMEDS::SObject_var aMeshSO = SMESH::FindSObject( myMesh );
376 GEOM::GEOM_Object_var aMainGeomShape = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
377 SALOMEDS::SObject_var aMainGeomShapeSO = SMESH::FindSObject( aMainGeomShape );
378 if ( aMainGeomShapeSO->_is_nil() || !IsFatherOf( SMESH::GetActiveStudyDocument()->FindObjectID( IO->getEntry() ), aMainGeomShapeSO ) ) {
379 myGeomShape = GEOM::GEOM_Object::_nil();
385 else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
389 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
390 for ( ; Itinit.More(); Itinit.Next() ) {
391 HypoList.append( Itinit.Value()->getEntry() );
394 aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
400 else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
404 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
405 for ( ; Itinit.More(); Itinit.Next() ) {
406 AlgoList.append( Itinit.Value()->getEntry() );
409 aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
416 myEditCurrentArgument->setText(aString) ;
418 UpdateControlState();
422 //=================================================================================
423 // function : SetEditCurrentArgument()
425 //=================================================================================
426 void SMESHGUI_AddSubMeshDlg::SetEditCurrentArgument()
428 QPushButton* send = (QPushButton*)sender();
429 if(send == SelectButtonC1A1) {
430 LineEditC1A1->setFocus() ;
431 myEditCurrentArgument = LineEditC1A1;
432 mySelection->ClearFilters() ;
433 mySelection->AddFilter(myMeshFilter) ;
434 } else if (send == SelectButtonC1A2) {
435 LineEditC1A2->setFocus() ;
436 myEditCurrentArgument = LineEditC1A2;
437 mySelection->ClearFilters() ;
438 mySelection->AddFilter(myGeomFilter) ;
439 } else if( send == SelectButtonC1A1Hyp ) {
440 LineEditC1A1Hyp->setFocus() ;
441 myEditCurrentArgument = LineEditC1A1Hyp ;
442 mySelection->ClearFilters() ;
443 mySelection->AddFilter(myHypothesisFilter) ;
444 } else if( send == SelectButtonC1A1Algo ) {
445 LineEditC1A1Algo->setFocus() ;
446 myEditCurrentArgument = LineEditC1A1Algo ;
447 mySelection->ClearFilters() ;
448 mySelection->AddFilter(myAlgorithmFilter) ;
450 SelectionIntoArgument() ;
453 //=================================================================================
454 // function : DeactivateActiveDialog()
456 //=================================================================================
457 void SMESHGUI_AddSubMeshDlg::DeactivateActiveDialog()
459 if ( GroupC1->isEnabled() ) {
460 disconnect( mySelection, 0, this, 0 );
461 GroupC1->setEnabled(false) ;
462 GroupButtons->setEnabled(false) ;
467 //=================================================================================
468 // function : ActivateThisDialog()
470 //=================================================================================
471 void SMESHGUI_AddSubMeshDlg::ActivateThisDialog()
473 mySMESHGUI->EmitSignalDeactivateDialog() ;
474 GroupC1->setEnabled(true) ;
475 GroupButtons->setEnabled(true) ;
476 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
480 //=================================================================================
481 // function : enterEvent()
483 //=================================================================================
484 void SMESHGUI_AddSubMeshDlg::enterEvent(QEvent* e)
486 if ( !GroupC1->isEnabled() )
487 ActivateThisDialog() ;
491 //=================================================================================
492 // function : closeEvent()
494 //=================================================================================
495 void SMESHGUI_AddSubMeshDlg::closeEvent( QCloseEvent* e )
497 disconnect( mySelection, 0, this, 0 );
498 mySMESHGUI->ResetState() ;
499 mySelection->ClearFilters() ;
500 QDialog::closeEvent( e );
503 //=================================================================================
504 // function : UpdateControlState()
506 //=================================================================================
507 void SMESHGUI_AddSubMeshDlg::UpdateControlState()
509 bool isEnabled = ( !myMesh->_is_nil() && !myGeomShape->_is_nil() && ( HypoList.count() || AlgoList.count() ) );
510 bool isImportedMesh = false;
511 if ( !myMesh->_is_nil() ) {
512 SALOMEDS::SObject_var aMeshSO = SMESH::FindSObject( myMesh );
513 GEOM::GEOM_Object_var myGeomShape = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
514 isImportedMesh = myGeomShape->_is_nil();
517 buttonOk ->setEnabled( isEnabled && !isImportedMesh );
518 buttonApply->setEnabled( isEnabled && !isImportedMesh );