Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[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 using namespace std;
30 #include "SMESHGUI_InitMeshDlg.h"
31 #include "SMESHGUI.h"
32 #include "SALOME_ListIteratorOfListIO.hxx"
33
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"
39
40 #include "utilities.h"
41
42 // QT Includes
43 #include <qgroupbox.h>
44 #include <qlabel.h>
45 #include <qlineedit.h>
46 #include <qpushbutton.h>
47 #include <qlayout.h>
48 #include <qpixmap.h>
49
50 //=================================================================================
51 // class    : SMESHGUI_InitMeshDlg()
52 // purpose  : Constructs a SMESHGUI_InitMeshDlg 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_InitMeshDlg::SMESHGUI_InitMeshDlg( 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 )
59 {
60     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
61     if ( !name )
62         setName( "SMESHGUI_InitMeshDlg" );
63     setCaption( tr( "SMESH_INIT_MESH"  ) );
64     setSizeGripEnabled( TRUE );
65     QGridLayout* SMESHGUI_InitMeshDlgLayout = new QGridLayout( this ); 
66     SMESHGUI_InitMeshDlgLayout->setSpacing( 6 );
67     SMESHGUI_InitMeshDlgLayout->setMargin( 11 );
68     
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 );
78
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 );
83
84     TextLabelC1A1 = new QLabel( tr( "SMESH_OBJECT_GEOM" ), 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 );
92
93     TextLabelC1A1Hyp = new QLabel( tr( "SMESH_OBJECT_HYPOTHESIS" ), GroupC1, "TextLabelC1A1Hyp" );
94     GroupC1Layout->addWidget( TextLabelC1A1Hyp, 2, 0 );
95     SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
96     SelectButtonC1A1Hyp->setPixmap( image0 );
97     GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 2, 1 );
98     LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
99     GroupC1Layout->addWidget( LineEditC1A1Hyp, 2, 2 );
100
101     TextLabelC1A1Algo = new QLabel( tr( "SMESH_OBJECT_ALGORITHM" ), GroupC1, "TextLabelC1A1Algo" );
102     GroupC1Layout->addWidget( TextLabelC1A1Algo, 3, 0 );
103     SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
104     SelectButtonC1A1Algo->setPixmap( image0 );
105     GroupC1Layout->addWidget( SelectButtonC1A1Algo, 3, 1 );
106     LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
107     GroupC1Layout->addWidget( LineEditC1A1Algo, 3, 2 );
108
109     SMESHGUI_InitMeshDlgLayout->addWidget( GroupC1, 1, 0 );
110
111     /***************************************************************/
112     GroupButtons = new QGroupBox( this, "GroupButtons" );
113     GroupButtons->setColumnLayout(0, Qt::Vertical );
114     GroupButtons->layout()->setSpacing( 0 );
115     GroupButtons->layout()->setMargin( 0 );
116     QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
117     GroupButtonsLayout->setAlignment( Qt::AlignTop );
118     GroupButtonsLayout->setSpacing( 6 );
119     GroupButtonsLayout->setMargin( 11 );
120
121     buttonOk = new QPushButton( tr( "SMESH_BUT_OK" ), GroupButtons, "buttonOk" );
122     buttonOk->setAutoDefault( TRUE );
123     buttonOk->setDefault( TRUE );
124     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
125
126     buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons, "buttonApply" );
127     buttonApply->setAutoDefault( TRUE );
128     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
129
130     GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
131
132     buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons, "buttonCancel" );
133     buttonCancel->setAutoDefault( TRUE );
134     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
135
136     SMESHGUI_InitMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
137
138     /***************************************************************/
139     Init( Sel ) ;
140 }
141
142
143 //=================================================================================
144 // function : ~SMESHGUI_InitMeshDlg()
145 // purpose  : Destroys the object and frees any allocated resources
146 //=================================================================================
147 SMESHGUI_InitMeshDlg::~SMESHGUI_InitMeshDlg()
148 {
149     // no need to delete child widgets, Qt does it all for us
150 }
151
152
153 //=================================================================================
154 // function : Init()
155 // purpose  :
156 //=================================================================================
157 void SMESHGUI_InitMeshDlg::Init( SALOME_Selection* Sel )
158 {
159   mySelection = Sel;
160   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
161   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
162
163   myGeomFilter       = new SALOME_TypeFilter( "GEOM" );
164   myAlgorithmFilter  = new SMESH_TypeFilter( ALGORITHM );
165   myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
166
167   /* signals and slots connections */
168   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
169   connect( buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
170   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
171
172   connect( SelectButtonC1A1,     SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
173   connect( SelectButtonC1A1Hyp,  SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
174   connect( SelectButtonC1A1Algo, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
175
176   connect( mySelection, SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ) );
177   connect( mySMESHGUI,  SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
178   connect( mySMESHGUI,  SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) ) ;
179   
180   int x, y ;
181   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
182   this->move( x, y ) ;
183   this->show() ; 
184
185   LineEdit_NameMesh->setText( tr( "SMESH_OBJECT_MESH" ) );
186   LineEdit_NameMesh->setFocus() ;
187   myEditCurrentArgument = LineEditC1A1 ;        
188   mySelection->ClearFilters() ;   
189   mySelection->AddFilter( myGeomFilter ) ;
190
191   SelectionIntoArgument();
192
193   UpdateControlState();
194 }
195
196
197 //=================================================================================
198 // function : ClickOnOk()
199 // purpose  :
200 //=================================================================================
201 void SMESHGUI_InitMeshDlg::ClickOnOk()
202 {
203   if ( this->ClickOnApply() )
204     this->ClickOnCancel() ;
205 }
206
207 //=================================================================================
208 // function : ClickOnApply()
209 // purpose  :
210 //=================================================================================
211 bool SMESHGUI_InitMeshDlg::ClickOnApply()
212 {
213   QString myNameMesh = LineEdit_NameMesh->text().stripWhiteSpace();
214   if ( myNameMesh.isEmpty() ) {
215     QAD_MessageBox::warn1( this, tr( "SMESH_WRN_WARNING" ), tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
216     return false;
217   }
218   
219   if ( myGeomShape->_is_nil() || !HypoList.count() || !AlgoList.count() )
220     return false;
221
222   QAD_WaitCursor wc;
223
224   QAD_Operation* op = new QAD_Operation( mySMESHGUI->GetActiveStudy() );
225
226   // start transaction
227   op->start();
228   
229   // create mesh
230   SMESH::SMESH_Mesh_var aMesh = mySMESHGUI->InitMesh( myGeomShape, myNameMesh ) ;
231   
232   if ( !aMesh->_is_nil() ) {
233     // assign hypotheses
234     for( int i = 0; i < HypoList.count(); i++ ) {
235       SALOMEDS::SObject_var aHypSO = mySMESHGUI->GetStudy()->FindObjectID( HypoList[i] );
236       if ( !aHypSO->_is_nil() ) {
237         CORBA::Object_var anObject = aHypSO->GetObject();
238         if ( !CORBA::is_nil( anObject ) ) {
239           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
240           if ( !aHyp->_is_nil() ) {
241             if ( !mySMESHGUI->AddHypothesisOnMesh( aMesh, aHyp ) ) {
242               // abort transaction
243               op->abort();
244               return false;
245             }
246           }
247         }
248       }
249     }
250     // assign algorithms
251     for( int i = 0; i < AlgoList.count(); i++ ) {
252       SALOMEDS::SObject_var aHypSO = mySMESHGUI->GetStudy()->FindObjectID( AlgoList[i] );
253       if ( !aHypSO->_is_nil() ) {
254         CORBA::Object_var anObject = aHypSO->GetObject();
255         if ( !CORBA::is_nil( anObject ) ) {
256           SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( anObject );
257           if ( !aHyp->_is_nil() ) {
258             if ( !mySMESHGUI->AddAlgorithmOnMesh( aMesh, aHyp ) ) {
259               // abort transaction
260               op->abort();
261               return false;
262             }
263           }
264         }
265       }
266     }
267   }
268   // commit transaction
269   op->finish();
270   return true;
271 }
272
273
274 //=================================================================================
275 // function : ClickOnCancel()
276 // purpose  :
277 //=================================================================================
278 void SMESHGUI_InitMeshDlg::ClickOnCancel()
279 {
280   close();
281 }
282
283
284 //=================================================================================
285 // function : SelectionIntoArgument()
286 // purpose  : Called when selection as changed or other case
287 //=================================================================================
288 void SMESHGUI_InitMeshDlg::SelectionIntoArgument()
289 {
290   QString aString = ""; 
291
292   int nbSel = mySMESHGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
293
294   if ( myEditCurrentArgument == LineEditC1A1 ) {
295     // geom shape
296     if ( nbSel != 1 ) {
297       myGeomShape = GEOM::GEOM_Shape::_nil();
298       aString = "";
299     } 
300     else {
301       Standard_Boolean testResult ;
302       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
303       myGeomShape = mySMESHGUI->ConvertIOinGEOMShape(IO, testResult) ;
304       if( !testResult )  {
305         myGeomShape = GEOM::GEOM_Shape::_nil();
306         aString = "";
307       }
308     }
309   }
310   else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
311     // hypotheses
312     HypoList.clear();
313     if ( nbSel >= 1 ) {
314       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
315       for ( ; Itinit.More(); Itinit.Next() ) {
316         HypoList.append( Itinit.Value()->getEntry() );
317       }
318       if ( nbSel > 1 )
319         aString = tr( "%1 Hypothesis" ).arg( nbSel ) ;
320     }
321     else {
322       aString = "";
323     }
324   }
325   else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
326     // algorithms
327     AlgoList.clear(); 
328     if ( nbSel >= 1 ) {
329       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
330       for ( ; Itinit.More(); Itinit.Next() ) {
331         AlgoList.append( Itinit.Value()->getEntry() );
332       }
333       if ( nbSel > 1 )
334         aString = tr( "%1 Algorithms" ).arg( nbSel ) ;
335     }
336     else {
337       aString = "";
338     }
339   }
340
341   myEditCurrentArgument->setText(aString) ;
342
343   UpdateControlState();
344 }
345
346
347 //=================================================================================
348 // function : SetEditCurrentArgument()
349 // purpose  :
350 //=================================================================================
351 void SMESHGUI_InitMeshDlg::SetEditCurrentArgument()
352 {
353   QPushButton* send = (QPushButton*)sender();
354   if(send == SelectButtonC1A1) {
355     LineEditC1A1->setFocus() ;
356     myEditCurrentArgument = LineEditC1A1;
357     mySelection->ClearFilters() ;         
358     mySelection->AddFilter(myGeomFilter) ;
359   } else if( send == SelectButtonC1A1Hyp ) {
360     LineEditC1A1Hyp->setFocus() ;
361     myEditCurrentArgument = LineEditC1A1Hyp ;
362     mySelection->ClearFilters() ;         
363     mySelection->AddFilter(myHypothesisFilter) ;
364   } else if( send == SelectButtonC1A1Algo ) {
365     LineEditC1A1Algo->setFocus() ;
366     myEditCurrentArgument = LineEditC1A1Algo ;
367     mySelection->ClearFilters() ;         
368     mySelection->AddFilter(myAlgorithmFilter) ;
369   }
370   SelectionIntoArgument() ;
371 }
372
373 //=================================================================================
374 // function : DeactivateActiveDialog()
375 // purpose  :
376 //=================================================================================
377 void SMESHGUI_InitMeshDlg::DeactivateActiveDialog()
378 {
379   if ( GroupC1->isEnabled() ) {
380     disconnect( mySelection, 0, this, 0 );
381     GroupC1->setEnabled(false) ;
382     GroupButtons->setEnabled(false) ;
383   }
384 }
385
386
387 //=================================================================================
388 // function : ActivateThisDialog()
389 // purpose  :
390 //=================================================================================
391 void SMESHGUI_InitMeshDlg::ActivateThisDialog()
392 {
393   mySMESHGUI->EmitSignalDeactivateDialog() ;   
394   GroupC1->setEnabled(true) ;
395   GroupButtons->setEnabled(true) ;
396   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
397 }
398
399
400 //=================================================================================
401 // function : enterEvent()
402 // purpose  :
403 //=================================================================================
404 void SMESHGUI_InitMeshDlg::enterEvent(QEvent* e)
405 {
406   if ( !GroupC1->isEnabled() )
407     ActivateThisDialog() ;
408 }
409
410
411 //=================================================================================
412 // function : closeEvent()
413 // purpose  :
414 //=================================================================================
415 void SMESHGUI_InitMeshDlg::closeEvent( QCloseEvent* e )
416 {
417   disconnect( mySelection, 0, this, 0 );
418   mySMESHGUI->ResetState() ;
419   mySelection->ClearFilters() ;
420   QDialog::closeEvent( e );
421 }
422
423 //=================================================================================
424 // function : UpdateControlState()
425 // purpose  :
426 //=================================================================================
427 void SMESHGUI_InitMeshDlg::UpdateControlState()
428 {
429   bool isEnabled = ( !myGeomShape->_is_nil() && HypoList.count() && AlgoList.count() );
430
431   buttonOk   ->setEnabled( isEnabled );
432   buttonApply->setEnabled( isEnabled );
433 }
434
435
436
437
438