Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_InitMeshDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_InitMeshDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_InitMeshDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_SMESHGenUtils.h"
34 #include "SMESHGUI_HypothesesUtils.h"
35
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"
41
42 #include "SALOME_ListIteratorOfListIO.hxx"
43 #include "SALOMEGUI_QtCatchCorbaException.hxx"
44
45 #include "utilities.h"
46
47 // QT Includes
48 #include <qgroupbox.h>
49 #include <qlabel.h>
50 #include <qlineedit.h>
51 #include <qpushbutton.h>
52 #include <qlayout.h>
53 #include <qpixmap.h>
54
55 using namespace std;
56
57 namespace SMESH{
58   SMESH::SMESH_Mesh_var InitMesh(GEOM::GEOM_Object_ptr theShapeObject, 
59                                  const char* theMeshName)
60   {
61     SMESH::SMESH_Mesh_var aMesh;
62     try {
63       SMESH::SMESH_Gen_var aSMESHGen = SMESH::GetSMESHGen();
64       if ( !aSMESHGen->_is_nil() && !theShapeObject->_is_nil() ) {
65         aMesh = aSMESHGen->CreateMesh( theShapeObject );
66         if ( !aMesh->_is_nil() ) {
67           SALOMEDS::SObject_var aMeshSObject = SMESH::FindSObject( aMesh.in() );
68           SMESH::SetName( aMeshSObject, theMeshName );
69         }
70       }
71     }
72     catch( const SALOME::SALOME_Exception& S_ex ) {
73       QtCatchCorbaException( S_ex );
74     }
75     return aMesh._retn();
76   }
77 }
78
79 //=================================================================================
80 // class    : SMESHGUI_InitMeshDlg()
81 // purpose  : Constructs a SMESHGUI_InitMeshDlg which is a child of 'parent', with the 
82 //            name 'name' and widget flags set to 'f'.
83 //            The dialog will by default be modeless, unless you set 'modal' to
84 //            TRUE to construct a modal dialog.
85 //=================================================================================
86 SMESHGUI_InitMeshDlg::SMESHGUI_InitMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
87     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
88 {
89     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
90     if ( !name )
91         setName( "SMESHGUI_InitMeshDlg" );
92     setCaption( tr( "SMESH_INIT_MESH"  ) );
93     setSizeGripEnabled( TRUE );
94     QGridLayout* SMESHGUI_InitMeshDlgLayout = new QGridLayout( this ); 
95     SMESHGUI_InitMeshDlgLayout->setSpacing( 6 );
96     SMESHGUI_InitMeshDlgLayout->setMargin( 11 );
97     
98     /***************************************************************/
99     GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), this, "GroupC1" );
100     GroupC1->setColumnLayout(0, Qt::Vertical );
101     GroupC1->layout()->setSpacing( 0 );
102     GroupC1->layout()->setMargin( 0 );
103     QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
104     GroupC1Layout->setAlignment( Qt::AlignTop );
105     GroupC1Layout->setSpacing( 6 );
106     GroupC1Layout->setMargin( 11 );
107
108     TextLabel_NameMesh = new QLabel( tr( "SMESH_NAME" ), GroupC1, "TextLabel_NameMesh" );
109     GroupC1Layout->addWidget( TextLabel_NameMesh, 0, 0 );
110     LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
111     GroupC1Layout->addWidget( LineEdit_NameMesh, 0, 2 );
112
113     TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), GroupC1, "TextLabelC1A1" );
114     GroupC1Layout->addWidget( TextLabelC1A1, 1, 0 );
115     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
116     SelectButtonC1A1->setPixmap( image0 );
117     SelectButtonC1A1->setToggleButton( FALSE );
118     GroupC1Layout->addWidget( SelectButtonC1A1, 1, 1 );
119     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
120     LineEditC1A1->setReadOnly( true );
121     GroupC1Layout->addWidget( LineEditC1A1, 1, 2 );
122
123     TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
124     GroupC1Layout->addWidget( TextLabelC1A1Hyp, 2, 0 );
125     SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
126     SelectButtonC1A1Hyp->setPixmap( image0 );
127     GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 2, 1 );
128     LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
129     LineEditC1A1Hyp->setReadOnly( true );
130     GroupC1Layout->addWidget( LineEditC1A1Hyp, 2, 2 );
131
132     TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
133     GroupC1Layout->addWidget( TextLabelC1A1Algo, 3, 0 );
134     SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
135     SelectButtonC1A1Algo->setPixmap( image0 );
136     GroupC1Layout->addWidget( SelectButtonC1A1Algo, 3, 1 );
137     LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
138     LineEditC1A1Algo->setReadOnly( true );
139     GroupC1Layout->addWidget( LineEditC1A1Algo, 3, 2 );
140
141     SMESHGUI_InitMeshDlgLayout->addWidget( GroupC1, 1, 0 );
142
143     /***************************************************************/
144     GroupButtons = new QGroupBox( this, "GroupButtons" );
145     GroupButtons->setColumnLayout(0, Qt::Vertical );
146     GroupButtons->layout()->setSpacing( 0 );
147     GroupButtons->layout()->setMargin( 0 );
148     QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
149     GroupButtonsLayout->setAlignment( Qt::AlignTop );
150     GroupButtonsLayout->setSpacing( 6 );
151     GroupButtonsLayout->setMargin( 11 );
152
153     buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
154     buttonOk->setAutoDefault( TRUE );
155     buttonOk->setDefault( TRUE );
156     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
157
158     buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
159     buttonApply->setAutoDefault( TRUE );
160     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
161
162     GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
163
164     buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
165     buttonCancel->setAutoDefault( TRUE );
166     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
167
168     SMESHGUI_InitMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
169
170     /***************************************************************/
171     Init( Sel ) ;
172 }
173
174
175 //=================================================================================
176 // function : ~SMESHGUI_InitMeshDlg()
177 // purpose  : Destroys the object and frees any allocated resources
178 //=================================================================================
179 SMESHGUI_InitMeshDlg::~SMESHGUI_InitMeshDlg()
180 {
181     // no need to delete child widgets, Qt does it all for us
182 }
183
184
185 //=================================================================================
186 // function : Init()
187 // purpose  :
188 //=================================================================================
189 void SMESHGUI_InitMeshDlg::Init( SALOME_Selection* Sel )
190 {
191   mySelection = Sel;
192   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
193   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
194
195   myGeomFilter       = new SALOME_TypeFilter( "GEOM" );
196   myAlgorithmFilter  = new SMESH_TypeFilter( ALGORITHM );
197   myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
198
199   /* signals and slots connections */
200   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
201   connect( buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
202   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
203
204   connect( SelectButtonC1A1,     SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
205   connect( SelectButtonC1A1Hyp,  SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
206   connect( SelectButtonC1A1Algo, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
207
208   connect( mySelection, SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ) );
209   connect( mySMESHGUI,  SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
210   connect( mySMESHGUI,  SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) ) ;
211   
212   int x, y ;
213   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
214   this->move( x, y ) ;
215   this->show() ; 
216
217   LineEdit_NameMesh->setText( tr( "SMESH_OBJECT_MESH" ) );
218   LineEdit_NameMesh->setFocus() ;
219   myEditCurrentArgument = LineEditC1A1 ;        
220   mySelection->ClearFilters() ;   
221   mySelection->AddFilter( myGeomFilter ) ;
222
223   SelectionIntoArgument();
224
225   UpdateControlState();
226 }
227
228
229 //=================================================================================
230 // function : ClickOnOk()
231 // purpose  :
232 //=================================================================================
233 void SMESHGUI_InitMeshDlg::ClickOnOk()
234 {
235   if ( this->ClickOnApply() )
236     this->ClickOnCancel() ;
237 }
238
239 //=================================================================================
240 // function : ClickOnApply()
241 // purpose  :
242 //=================================================================================
243 bool SMESHGUI_InitMeshDlg::ClickOnApply()
244 {
245   if (mySMESHGUI->ActiveStudyLocked())
246     return false;
247
248   QString myNameMesh = LineEdit_NameMesh->text().stripWhiteSpace();
249   if ( myNameMesh.isEmpty() ) {
250     QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
251     return false;
252   }
253   
254   if ( myGeomShape->_is_nil() || !HypoList.count() || !AlgoList.count() )
255     return false;
256
257   QAD_WaitCursor wc;
258
259   QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
260
261   // start transaction
262   op->start();
263   
264   // create mesh
265   SMESH::SMESH_Mesh_var aMesh = SMESH::InitMesh( myGeomShape, myNameMesh ) ;
266   
267   if ( !aMesh->_is_nil() ) {
268     // assign hypotheses
269     for( int i = 0; i < HypoList.count(); i++ ) {
270       SALOMEDS::SObject_var aHypSO = SMESH::GetActiveStudyDocument()->FindObjectID( HypoList[i] );
271       if ( !aHypSO->_is_nil() ) {
272         CORBA::Object_var anObject = aHypSO->GetObject();
273         if ( !CORBA::is_nil( anObject ) ) {
274           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
275           if ( !aHyp->_is_nil() ) {
276             if ( !SMESH::AddHypothesisOnMesh( aMesh, aHyp ) ) {
277               // abort transaction
278               op->abort();
279               return false;
280             }
281           }
282         }
283       }
284     }
285     // assign algorithms
286     for( int i = 0; i < AlgoList.count(); i++ ) {
287       SALOMEDS::SObject_var aHypSO = SMESH::GetActiveStudyDocument()->FindObjectID( AlgoList[i] );
288       if ( !aHypSO->_is_nil() ) {
289         CORBA::Object_var anObject = aHypSO->GetObject();
290         if ( !CORBA::is_nil( anObject ) ) {
291           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
292           if ( !aHyp->_is_nil() ) {
293             if ( !SMESH::AddHypothesisOnMesh( aMesh, aHyp ) ) {
294               // abort transaction
295               op->abort();
296               return false;
297             }
298           }
299         }
300       }
301     }
302   }
303   // commit transaction
304   op->finish();
305   return true;
306 }
307
308
309 //=================================================================================
310 // function : ClickOnCancel()
311 // purpose  :
312 //=================================================================================
313 void SMESHGUI_InitMeshDlg::ClickOnCancel()
314 {
315   close();
316 }
317
318
319 //=================================================================================
320 // function : SelectionIntoArgument()
321 // purpose  : Called when selection as changed or other case
322 //=================================================================================
323 void SMESHGUI_InitMeshDlg::SelectionIntoArgument()
324 {
325   QString aString = ""; 
326
327   int nbSel = SMESH::GetNameOfSelectedIObjects(mySelection, aString) ;
328
329   if ( myEditCurrentArgument == LineEditC1A1 ) {
330     // geom shape
331     if ( nbSel != 1 ) {
332       myGeomShape = GEOM::GEOM_Object::_nil();
333       aString = "";
334     } 
335     else {
336       Standard_Boolean testResult ;
337       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
338       myGeomShape = SMESH::IObjectToInterface<GEOM::GEOM_Object>(IO) ;
339       if( myGeomShape->_is_nil() )  {
340         aString = "";
341       }
342     }
343   }
344   else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
345     // hypotheses
346     HypoList.clear();
347     if ( nbSel >= 1 ) {
348       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
349       for ( ; Itinit.More(); Itinit.Next() ) {
350         HypoList.append( Itinit.Value()->getEntry() );
351       }
352       if ( nbSel > 1 )
353         aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
354     }
355     else {
356       aString = "";
357     }
358   }
359   else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
360     // algorithms
361     AlgoList.clear(); 
362     if ( nbSel >= 1 ) {
363       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
364       for ( ; Itinit.More(); Itinit.Next() ) {
365         AlgoList.append( Itinit.Value()->getEntry() );
366       }
367       if ( nbSel > 1 )
368         aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
369     }
370     else {
371       aString = "";
372     }
373   }
374
375   myEditCurrentArgument->setText(aString) ;
376
377   UpdateControlState();
378 }
379
380
381 //=================================================================================
382 // function : SetEditCurrentArgument()
383 // purpose  :
384 //=================================================================================
385 void SMESHGUI_InitMeshDlg::SetEditCurrentArgument()
386 {
387   QPushButton* send = (QPushButton*)sender();
388   if(send == SelectButtonC1A1) {
389     LineEditC1A1->setFocus() ;
390     myEditCurrentArgument = LineEditC1A1;
391     mySelection->ClearFilters() ;         
392     mySelection->AddFilter(myGeomFilter) ;
393   } else if( send == SelectButtonC1A1Hyp ) {
394     LineEditC1A1Hyp->setFocus() ;
395     myEditCurrentArgument = LineEditC1A1Hyp ;
396     mySelection->ClearFilters() ;         
397     mySelection->AddFilter(myHypothesisFilter) ;
398   } else if( send == SelectButtonC1A1Algo ) {
399     LineEditC1A1Algo->setFocus() ;
400     myEditCurrentArgument = LineEditC1A1Algo ;
401     mySelection->ClearFilters() ;         
402     mySelection->AddFilter(myAlgorithmFilter) ;
403   }
404   SelectionIntoArgument() ;
405 }
406
407 //=================================================================================
408 // function : DeactivateActiveDialog()
409 // purpose  :
410 //=================================================================================
411 void SMESHGUI_InitMeshDlg::DeactivateActiveDialog()
412 {
413   if ( GroupC1->isEnabled() ) {
414     disconnect( mySelection, 0, this, 0 );
415     GroupC1->setEnabled(false) ;
416     GroupButtons->setEnabled(false) ;
417   }
418 }
419
420
421 //=================================================================================
422 // function : ActivateThisDialog()
423 // purpose  :
424 //=================================================================================
425 void SMESHGUI_InitMeshDlg::ActivateThisDialog()
426 {
427   mySMESHGUI->EmitSignalDeactivateDialog() ;   
428   GroupC1->setEnabled(true) ;
429   GroupButtons->setEnabled(true) ;
430   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
431 }
432
433
434 //=================================================================================
435 // function : enterEvent()
436 // purpose  :
437 //=================================================================================
438 void SMESHGUI_InitMeshDlg::enterEvent(QEvent* e)
439 {
440   if ( !GroupC1->isEnabled() )
441     ActivateThisDialog() ;
442 }
443
444
445 //=================================================================================
446 // function : closeEvent()
447 // purpose  :
448 //=================================================================================
449 void SMESHGUI_InitMeshDlg::closeEvent( QCloseEvent* e )
450 {
451   disconnect( mySelection, 0, this, 0 );
452   mySMESHGUI->ResetState() ;
453   mySelection->ClearFilters() ;
454   QDialog::closeEvent( e );
455 }
456
457 //=================================================================================
458 // function : UpdateControlState()
459 // purpose  :
460 //=================================================================================
461 void SMESHGUI_InitMeshDlg::UpdateControlState()
462 {
463   bool isEnabled = ( !myGeomShape->_is_nil() && HypoList.count() && AlgoList.count() );
464
465   buttonOk   ->setEnabled( isEnabled );
466   buttonApply->setEnabled( isEnabled );
467 }
468
469
470
471
472