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_InitMeshDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_InitMeshDlg.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_SMESHGenUtils.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_Mesh_var InitMesh(GEOM::GEOM_Object_ptr theShapeObject,
61 const char* theMeshName)
63 SMESH::SMESH_Mesh_var aMesh;
65 SMESH::SMESH_Gen_var aSMESHGen = SMESH::GetSMESHGen();
66 if ( !aSMESHGen->_is_nil() && !theShapeObject->_is_nil() ) {
67 aMesh = aSMESHGen->CreateMesh( theShapeObject );
68 if ( !aMesh->_is_nil() ) {
69 SALOMEDS::SObject_var aMeshSObject = SMESH::FindSObject( aMesh.in() );
70 SMESH::SetName( aMeshSObject, theMeshName );
74 catch( const SALOME::SALOME_Exception& S_ex ) {
75 QtCatchCorbaException( S_ex );
81 //=================================================================================
82 // class : SMESHGUI_InitMeshDlg()
83 // purpose : Constructs a SMESHGUI_InitMeshDlg which is a child of 'parent', with the
84 // name 'name' and widget flags set to 'f'.
85 // The dialog will by default be modeless, unless you set 'modal' to
86 // TRUE to construct a modal dialog.
87 //=================================================================================
88 SMESHGUI_InitMeshDlg::SMESHGUI_InitMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
89 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
91 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
93 setName( "SMESHGUI_InitMeshDlg" );
94 setCaption( tr( "SMESH_INIT_MESH" ) );
95 setSizeGripEnabled( TRUE );
96 QGridLayout* SMESHGUI_InitMeshDlgLayout = new QGridLayout( this );
97 SMESHGUI_InitMeshDlgLayout->setSpacing( 6 );
98 SMESHGUI_InitMeshDlgLayout->setMargin( 11 );
100 /***************************************************************/
101 GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
102 GroupC1->setColumnLayout(0, Qt::Vertical );
103 GroupC1->layout()->setSpacing( 0 );
104 GroupC1->layout()->setMargin( 0 );
105 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
106 GroupC1Layout->setAlignment( Qt::AlignTop );
107 GroupC1Layout->setSpacing( 6 );
108 GroupC1Layout->setMargin( 11 );
110 TextLabel_NameMesh = new QLabel( tr( "SMESH_NAME" ), GroupC1, "TextLabel_NameMesh" );
111 GroupC1Layout->addWidget( TextLabel_NameMesh, 0, 0 );
112 LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
113 GroupC1Layout->addWidget( LineEdit_NameMesh, 0, 2 );
115 TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A1" );
116 GroupC1Layout->addWidget( TextLabelC1A1, 1, 0 );
117 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
118 SelectButtonC1A1->setPixmap( image0 );
119 SelectButtonC1A1->setToggleButton( FALSE );
120 GroupC1Layout->addWidget( SelectButtonC1A1, 1, 1 );
121 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
122 LineEditC1A1->setReadOnly( true );
123 GroupC1Layout->addWidget( LineEditC1A1, 1, 2 );
125 TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
126 GroupC1Layout->addWidget( TextLabelC1A1Hyp, 2, 0 );
127 SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
128 SelectButtonC1A1Hyp->setPixmap( image0 );
129 GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 2, 1 );
130 LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
131 LineEditC1A1Hyp->setReadOnly( true );
132 GroupC1Layout->addWidget( LineEditC1A1Hyp, 2, 2 );
134 TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
135 GroupC1Layout->addWidget( TextLabelC1A1Algo, 3, 0 );
136 SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
137 SelectButtonC1A1Algo->setPixmap( image0 );
138 GroupC1Layout->addWidget( SelectButtonC1A1Algo, 3, 1 );
139 LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
140 LineEditC1A1Algo->setReadOnly( true );
141 GroupC1Layout->addWidget( LineEditC1A1Algo, 3, 2 );
143 SMESHGUI_InitMeshDlgLayout->addWidget( GroupC1, 1, 0 );
145 /***************************************************************/
146 GroupButtons = new QGroupBox( this, "GroupButtons" );
147 GroupButtons->setColumnLayout(0, Qt::Vertical );
148 GroupButtons->layout()->setSpacing( 0 );
149 GroupButtons->layout()->setMargin( 0 );
150 QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
151 GroupButtonsLayout->setAlignment( Qt::AlignTop );
152 GroupButtonsLayout->setSpacing( 6 );
153 GroupButtonsLayout->setMargin( 11 );
155 buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
156 buttonOk->setAutoDefault( TRUE );
157 buttonOk->setDefault( TRUE );
158 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
160 buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
161 buttonApply->setAutoDefault( TRUE );
162 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
164 GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
166 buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
167 buttonCancel->setAutoDefault( TRUE );
168 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
170 SMESHGUI_InitMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
172 /***************************************************************/
177 //=================================================================================
178 // function : ~SMESHGUI_InitMeshDlg()
179 // purpose : Destroys the object and frees any allocated resources
180 //=================================================================================
181 SMESHGUI_InitMeshDlg::~SMESHGUI_InitMeshDlg()
183 // no need to delete child widgets, Qt does it all for us
187 //=================================================================================
190 //=================================================================================
191 void SMESHGUI_InitMeshDlg::Init( SALOME_Selection* Sel )
194 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
195 mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
197 myGeomFilter = new SALOME_TypeFilter( "GEOM" );
198 myAlgorithmFilter = new SMESH_TypeFilter( ALGORITHM );
199 myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
201 /* signals and slots connections */
202 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
203 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
204 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
206 connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
207 connect( SelectButtonC1A1Hyp, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
208 connect( SelectButtonC1A1Algo, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
210 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
211 connect( mySMESHGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
212 connect( mySMESHGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
215 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
219 LineEdit_NameMesh->setText( GetDefaultMeshName() );
220 LineEdit_NameMesh->setFocus() ;
221 myEditCurrentArgument = LineEditC1A1 ;
222 mySelection->ClearFilters() ;
223 mySelection->AddFilter( myGeomFilter ) ;
225 SelectionIntoArgument();
227 UpdateControlState();
231 //=================================================================================
232 // function : ClickOnOk()
234 //=================================================================================
235 void SMESHGUI_InitMeshDlg::ClickOnOk()
237 if ( this->ClickOnApply() )
238 this->ClickOnCancel() ;
241 //=================================================================================
242 // function : ClickOnApply()
244 //=================================================================================
245 bool SMESHGUI_InitMeshDlg::ClickOnApply()
247 if (mySMESHGUI->ActiveStudyLocked())
250 QString myNameMesh = LineEdit_NameMesh->text().stripWhiteSpace();
251 if ( myNameMesh.isEmpty() ) {
252 QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
256 if ( myGeomShape->_is_nil() || !HypoList.count() || !AlgoList.count() )
261 QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
267 SMESH::SMESH_Mesh_var aMesh = SMESH::InitMesh( myGeomShape, myNameMesh ) ;
269 if ( !aMesh->_is_nil() ) {
271 for( int i = 0; i < HypoList.count(); i++ ) {
272 SALOMEDS::SObject_var aHypSO = SMESH::GetActiveStudyDocument()->FindObjectID( HypoList[i] );
273 if ( !aHypSO->_is_nil() ) {
274 CORBA::Object_var anObject = aHypSO->GetObject();
275 if ( !CORBA::is_nil( anObject ) ) {
276 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
277 if ( !aHyp->_is_nil() ) {
278 if ( !SMESH::AddHypothesisOnMesh( aMesh, aHyp ) ) {
288 for( int i = 0; i < AlgoList.count(); i++ ) {
289 SALOMEDS::SObject_var aHypSO = SMESH::GetActiveStudyDocument()->FindObjectID( AlgoList[i] );
290 if ( !aHypSO->_is_nil() ) {
291 CORBA::Object_var anObject = aHypSO->GetObject();
292 if ( !CORBA::is_nil( anObject ) ) {
293 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
294 if ( !aHyp->_is_nil() ) {
295 if ( !SMESH::AddHypothesisOnMesh( aMesh, aHyp ) ) {
305 // commit transaction
307 LineEdit_NameMesh->setText( GetDefaultMeshName() );
312 //=================================================================================
313 // function : ClickOnCancel()
315 //=================================================================================
316 void SMESHGUI_InitMeshDlg::ClickOnCancel()
322 //=================================================================================
323 // function : SelectionIntoArgument()
324 // purpose : Called when selection as changed or other case
325 //=================================================================================
326 void SMESHGUI_InitMeshDlg::SelectionIntoArgument()
328 QString aString = "";
330 int nbSel = SMESH::GetNameOfSelectedIObjects( mySelection, aString );
332 if ( myEditCurrentArgument == LineEditC1A1 )
337 myGeomShape = GEOM::GEOM_Object::_nil();
342 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
343 myGeomShape = SMESH::IObjectToInterface<GEOM::GEOM_Object>( IO ) ;
344 if ( myGeomShape->_is_nil() || !GEOMBase::IsShape( myGeomShape ) )
346 myGeomShape = GEOM::GEOM_Object::_nil();
351 else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
355 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
356 for ( ; Itinit.More(); Itinit.Next() ) {
357 HypoList.append( Itinit.Value()->getEntry() );
360 aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
366 else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
370 SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
371 for ( ; Itinit.More(); Itinit.Next() ) {
372 AlgoList.append( Itinit.Value()->getEntry() );
375 aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
382 myEditCurrentArgument->setText(aString) ;
384 UpdateControlState();
388 //=================================================================================
389 // function : SetEditCurrentArgument()
391 //=================================================================================
392 void SMESHGUI_InitMeshDlg::SetEditCurrentArgument()
394 QPushButton* send = (QPushButton*)sender();
395 if(send == SelectButtonC1A1) {
396 LineEditC1A1->setFocus() ;
397 myEditCurrentArgument = LineEditC1A1;
398 mySelection->ClearFilters() ;
399 mySelection->AddFilter(myGeomFilter) ;
400 } else if( send == SelectButtonC1A1Hyp ) {
401 LineEditC1A1Hyp->setFocus() ;
402 myEditCurrentArgument = LineEditC1A1Hyp ;
403 mySelection->ClearFilters() ;
404 mySelection->AddFilter(myHypothesisFilter) ;
405 } else if( send == SelectButtonC1A1Algo ) {
406 LineEditC1A1Algo->setFocus() ;
407 myEditCurrentArgument = LineEditC1A1Algo ;
408 mySelection->ClearFilters() ;
409 mySelection->AddFilter(myAlgorithmFilter) ;
411 SelectionIntoArgument() ;
414 //=================================================================================
415 // function : DeactivateActiveDialog()
417 //=================================================================================
418 void SMESHGUI_InitMeshDlg::DeactivateActiveDialog()
420 if ( GroupC1->isEnabled() ) {
421 disconnect( mySelection, 0, this, 0 );
422 GroupC1->setEnabled(false) ;
423 GroupButtons->setEnabled(false) ;
428 //=================================================================================
429 // function : ActivateThisDialog()
431 //=================================================================================
432 void SMESHGUI_InitMeshDlg::ActivateThisDialog()
434 mySMESHGUI->EmitSignalDeactivateDialog() ;
435 GroupC1->setEnabled(true) ;
436 GroupButtons->setEnabled(true) ;
437 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
441 //=================================================================================
442 // function : enterEvent()
444 //=================================================================================
445 void SMESHGUI_InitMeshDlg::enterEvent(QEvent* e)
447 if ( !GroupC1->isEnabled() )
448 ActivateThisDialog() ;
452 //=================================================================================
453 // function : closeEvent()
455 //=================================================================================
456 void SMESHGUI_InitMeshDlg::closeEvent( QCloseEvent* e )
458 disconnect( mySelection, 0, this, 0 );
459 mySMESHGUI->ResetState() ;
460 mySelection->ClearFilters() ;
461 QDialog::closeEvent( e );
464 //=================================================================================
465 // function : UpdateControlState()
467 //=================================================================================
468 void SMESHGUI_InitMeshDlg::UpdateControlState()
470 bool isEnabled = ( !myGeomShape->_is_nil() && HypoList.count() && AlgoList.count() );
472 buttonOk ->setEnabled( isEnabled );
473 buttonApply->setEnabled( isEnabled );
476 //=================================================================================
477 // function : GetDefaultMeshName()
478 // purpose : Generates default mesh name(Mesh_1, Mesh_2, etc.)
479 //=================================================================================
480 QString SMESHGUI_InitMeshDlg::GetDefaultMeshName()
482 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
485 SALOMEDS::SObject_var obj;
489 aMeshName = QString(tr("SMESH_OBJECT_MESH")) +"_"+QString::number(++aNumber);
490 obj = aStudy->FindObject(aMeshName);
492 while (!obj->_is_nil());