Salome HOME
Merge branch merge_1_2_d
[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 "utilities.h"
37
38 // QT Includes
39 #include <qbuttongroup.h>
40 #include <qgroupbox.h>
41 #include <qlabel.h>
42 #include <qlineedit.h>
43 #include <qpushbutton.h>
44 #include <qradiobutton.h>
45 #include <qlayout.h>
46 #include <qvariant.h>
47 #include <qtooltip.h>
48 #include <qwhatsthis.h>
49 #include <qimage.h>
50 #include <qpixmap.h>
51
52
53 //=================================================================================
54 // class    : SMESHGUI_InitMeshDlg()
55 // purpose  : Constructs a SMESHGUI_InitMeshDlg which is a child of 'parent', with the 
56 //            name 'name' and widget flags set to 'f'.
57 //            The dialog will by default be modeless, unless you set 'modal' to
58 //            TRUE to construct a modal dialog.
59 //=================================================================================
60 SMESHGUI_InitMeshDlg::SMESHGUI_InitMeshDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
61     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu  )
62 {
63     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_DLG_INIT_MESH")));
64     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
65     if ( !name )
66         setName( "SMESHGUI_InitMeshDlg" );
67     resize( 303, 175 ); 
68     setCaption( tr( "SMESH_INIT_MESH"  ) );
69     setSizeGripEnabled( TRUE );
70     SMESHGUI_InitMeshDlgLayout = new QGridLayout( this ); 
71     SMESHGUI_InitMeshDlgLayout->setSpacing( 6 );
72     SMESHGUI_InitMeshDlgLayout->setMargin( 11 );
73     
74     /***************************************************************/
75     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
76     GroupConstructors->setTitle( tr( "SMESH_INIT"  ) );
77     GroupConstructors->setExclusive( TRUE );
78     GroupConstructors->setColumnLayout(0, Qt::Vertical );
79     GroupConstructors->layout()->setSpacing( 0 );
80     GroupConstructors->layout()->setMargin( 0 );
81     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
82     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
83     GroupConstructorsLayout->setSpacing( 6 );
84     GroupConstructorsLayout->setMargin( 11 );
85     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
86     Constructor1->setText( tr( ""  ) );
87     Constructor1->setPixmap( image1 );
88     Constructor1->setChecked( TRUE );
89     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
90     Constructor1->setMinimumSize( QSize( 50, 0 ) );
91     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
92     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
93     GroupConstructorsLayout->addItem( spacer, 0, 1 );
94     SMESHGUI_InitMeshDlgLayout->addWidget( GroupConstructors, 0, 0 );
95     
96     /***************************************************************/
97     GroupButtons = new QGroupBox( this, "GroupButtons" );
98     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
99     GroupButtons->setTitle( tr( ""  ) );
100     GroupButtons->setColumnLayout(0, Qt::Vertical );
101     GroupButtons->layout()->setSpacing( 0 );
102     GroupButtons->layout()->setMargin( 0 );
103     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
104     GroupButtonsLayout->setAlignment( Qt::AlignTop );
105     GroupButtonsLayout->setSpacing( 6 );
106     GroupButtonsLayout->setMargin( 11 );
107     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
108     buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
109     buttonCancel->setAutoDefault( TRUE );
110     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
111     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
112     buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
113     buttonApply->setAutoDefault( TRUE );
114     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
115     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
116     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
117     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
118     buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
119     buttonOk->setAutoDefault( TRUE );
120     buttonOk->setDefault( TRUE );
121     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
122     SMESHGUI_InitMeshDlgLayout->addWidget( GroupButtons, 2, 0 );
123
124     /***************************************************************/
125     GroupC1 = new QGroupBox( this, "GroupC1" );
126     GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
127     GroupC1->setMinimumSize( QSize( 0, 0 ) );
128     GroupC1->setFrameShape( QGroupBox::Box );
129     GroupC1->setFrameShadow( QGroupBox::Sunken );
130     GroupC1->setColumnLayout(0, Qt::Vertical );
131     GroupC1->layout()->setSpacing( 0 );
132     GroupC1->layout()->setMargin( 0 );
133     GroupC1Layout = new QGridLayout( GroupC1->layout() );
134     GroupC1Layout->setAlignment( Qt::AlignTop );
135     GroupC1Layout->setSpacing( 6 );
136     GroupC1Layout->setMargin( 11 );
137
138     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
139     TextLabelC1A1->setText( tr( "SMESH_OBJECT_GEOM"  ) );
140     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
141     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
142     TextLabelC1A1->setFrameShadow( QLabel::Plain );
143     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
144     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
145     SelectButtonC1A1->setText( tr( ""  ) );
146     SelectButtonC1A1->setPixmap( image0 );
147     SelectButtonC1A1->setToggleButton( FALSE );
148     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
149     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
150     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
151
152     TextLabel_NameMesh = new QLabel( GroupC1, "TextLabel_NameMesh" );
153     TextLabel_NameMesh->setText( tr( "SMESH_NAME"  ) );
154     GroupC1Layout->addWidget( TextLabel_NameMesh, 1, 0 );
155     LineEdit_NameMesh = new QLineEdit( GroupC1, "LineEdit_NameMesh" );
156     GroupC1Layout->addWidget( LineEdit_NameMesh, 1, 2 );
157
158     TextLabelC1A1Hyp = new QLabel( GroupC1, "TextLabelC1A1Hyp" );
159     TextLabelC1A1Hyp->setText( tr( "SMESH_OBJECT_HYPOTHESIS" ) );
160     TextLabelC1A1Hyp->setMinimumSize( QSize( 50, 0 ) );
161     TextLabelC1A1Hyp->setFrameShape( QLabel::NoFrame );
162     TextLabelC1A1Hyp->setFrameShadow( QLabel::Plain );
163     GroupC1Layout->addWidget( TextLabelC1A1Hyp, 2, 0 );
164     SelectButtonC1A1Hyp = new QPushButton( GroupC1, "SelectButtonC1A1Hyp" );
165     SelectButtonC1A1Hyp->setText( tr( ""  ) );
166     SelectButtonC1A1Hyp->setPixmap( image0 );
167     GroupC1Layout->addWidget( SelectButtonC1A1Hyp, 2, 1 );
168     LineEditC1A1Hyp = new QLineEdit( GroupC1, "LineEditC1A1Hyp" );
169     LineEditC1A1Hyp->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1Hyp->sizePolicy().hasHeightForWidth() ) );
170     GroupC1Layout->addWidget( LineEditC1A1Hyp, 2, 2 );
171
172     TextLabelC1A1Algo = new QLabel( GroupC1, "TextLabelC1A1Algo" );
173     TextLabelC1A1Algo->setText( tr( "SMESH_OBJECT_ALGORITHM" ) );
174     TextLabelC1A1Algo->setMinimumSize( QSize( 50, 0 ) );
175     TextLabelC1A1Algo->setFrameShape( QLabel::NoFrame );
176     TextLabelC1A1Algo->setFrameShadow( QLabel::Plain );
177     GroupC1Layout->addWidget( TextLabelC1A1Algo, 3, 0 );
178     SelectButtonC1A1Algo = new QPushButton( GroupC1, "SelectButtonC1A1Algo" );
179     SelectButtonC1A1Algo->setText( tr( ""  ) );
180     SelectButtonC1A1Algo->setPixmap( image0 );
181     GroupC1Layout->addWidget( SelectButtonC1A1Algo, 3, 1 );
182     LineEditC1A1Algo = new QLineEdit( GroupC1, "LineEditC1A1Algo" );
183     LineEditC1A1Algo->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1Algo->sizePolicy().hasHeightForWidth() ) );
184     GroupC1Layout->addWidget( LineEditC1A1Algo, 3, 2 );
185
186     SMESHGUI_InitMeshDlgLayout->addWidget( GroupC1, 1, 0 );
187     /***************************************************************/
188
189     Init(Sel) ;
190 }
191
192
193 //=================================================================================
194 // function : ~SMESHGUI_InitMeshDlg()
195 // purpose  : Destroys the object and frees any allocated resources
196 //=================================================================================
197 SMESHGUI_InitMeshDlg::~SMESHGUI_InitMeshDlg()
198 {
199     // no need to delete child widgets, Qt does it all for us
200 }
201
202
203 //=================================================================================
204 // function : Init()
205 // purpose  :
206 //=================================================================================
207 void SMESHGUI_InitMeshDlg::Init( SALOME_Selection* Sel )
208 {
209   GroupC1->show();
210   myConstructorId = 0 ;
211   Constructor1->setChecked( TRUE );
212   myEditCurrentArgument = LineEditC1A1 ;        
213   mySelection = Sel;
214   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
215   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
216
217   myGeomFilter = new SALOME_TypeFilter( "GEOM" );
218   myAlgorithmFilter = new SMESH_TypeFilter( ALGORITHM );
219   myHypothesisFilter = new SMESH_TypeFilter( HYPOTHESIS );
220
221   myNameMesh = "Mesh";
222
223   /* signals and slots connections */
224   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
225   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
226   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
227   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
228
229   connect( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
230   connect( LineEdit_NameMesh, SIGNAL (textChanged(const QString&)  ), this, SLOT( TextChangedInLineEdit(const QString&) ) ) ;
231
232   connect( SelectButtonC1A1Hyp, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
233   connect( SelectButtonC1A1Algo, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
234
235   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
236   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
237   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
238   
239   int x, y ;
240   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
241   this->move( x, y ) ;
242   this->show() ; 
243
244   return ;
245 }
246
247
248 //=================================================================================
249 // function : ConstructorsClicked()
250 // purpose  : Radio button management
251 //=================================================================================
252 void SMESHGUI_InitMeshDlg::ConstructorsClicked(int constructorId)
253 {
254   return ;
255 }
256
257 //=================================================================================
258 // function : ClickOnOk()
259 // purpose  :
260 //=================================================================================
261 void SMESHGUI_InitMeshDlg::ClickOnOk()
262 {
263   this->ClickOnApply() ;
264   this->ClickOnCancel() ;
265
266   return ;
267 }
268
269 //=================================================================================
270 // function : ClickOnApply()
271 // purpose  :
272 //=================================================================================
273 void SMESHGUI_InitMeshDlg::ClickOnApply()
274 {
275   switch(myConstructorId)
276     { 
277     case 0 :
278       { 
279         MESSAGE ( " myNameMesh " << myNameMesh.isEmpty() )
280         MESSAGE ( " myGeomShape " << myGeomShape->_is_nil() )
281         if ( !myNameMesh.isEmpty() && !myNameMesh.isNull() && !myGeomShape->_is_nil() )
282           myMesh = mySMESHGUI->InitMesh( myGeomShape, myNameMesh ) ;
283         
284         MESSAGE ( " myMesh " << myMesh->_is_nil() ) 
285         MESSAGE ( " myOkHypothesis " )
286         MESSAGE ( " myOkAlgorithm " )
287         if( myOkHypothesis && !myMesh->_is_nil() ) {
288           SALOME_ListIteratorOfListIO It( HypoList );
289           for(;It.More();It.Next()) {
290             Handle(SALOME_InteractiveObject) IObject = It.Value();
291             Standard_Boolean testResult;
292             myHypothesis = mySMESHGUI->ConvertIOinSMESHHypothesis(IObject, testResult) ;
293             if( testResult )
294               mySMESHGUI->AddHypothesisOnMesh(myMesh, myHypothesis) ;
295           }
296         }
297
298         if( myOkAlgorithm && !myMesh->_is_nil() ) {
299           SALOME_ListIteratorOfListIO It( AlgoList );
300           for(;It.More();It.Next()) {
301             Handle(SALOME_InteractiveObject) IObject = It.Value();
302             Standard_Boolean testResult;
303             myAlgorithm = mySMESHGUI->ConvertIOinSMESHHypothesis(IObject, testResult) ;
304             if( testResult )
305               mySMESHGUI->AddAlgorithmOnMesh(myMesh, myAlgorithm) ;
306           }
307         }
308         break ;
309       }
310     }
311 }
312
313
314 //=================================================================================
315 // function : ClickOnCancel()
316 // purpose  :
317 //=================================================================================
318 void SMESHGUI_InitMeshDlg::ClickOnCancel()
319 {
320   mySelection->ClearFilters() ;
321   disconnect( mySelection, 0, this, 0 );
322   mySMESHGUI->ResetState() ;
323   reject() ;
324   return ;
325 }
326
327
328 //=================================================================================
329 // function : SelectionIntoArgument()
330 // purpose  : Called when selection as changed or other case
331 //=================================================================================
332 void SMESHGUI_InitMeshDlg::SelectionIntoArgument()
333 {
334   myEditCurrentArgument->setText("") ;
335   QString aString = ""; 
336   int nbSel = mySMESHGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
337   if ( myEditCurrentArgument == LineEditC1A1 ) {
338     if ( nbSel != 1 ) {
339       myOkHypothesis = false;
340       myOkAlgorithm = false;
341       myGeomShape = GEOM::GEOM_Shape::_nil();
342       return ;
343     } else {
344       Standard_Boolean testResult ;
345       Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
346       myGeomShape = mySMESHGUI->ConvertIOinGEOMShape(IO, testResult) ;
347       if( !testResult )  {
348         myOkHypothesis = false;
349         myOkAlgorithm = false;
350         return ;
351       }
352     }
353   }
354   else if ( myEditCurrentArgument == LineEditC1A1Hyp ) {
355     if ( nbSel >= 1 ) {
356       HypoList.Clear();
357       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
358       for (; Itinit.More(); Itinit.Next()) {
359         HypoList.Append(Itinit.Value());
360       }
361       myOkHypothesis = true ;
362       if (nbSel > 1)
363         aString = tr("%1 Hypothesis").arg(nbSel) ;
364       LineEditC1A1Hyp->setText(aString) ;
365     }
366     else {
367       myOkHypothesis = false ;
368       return ;
369     }
370   } else if ( myEditCurrentArgument == LineEditC1A1Algo ) {
371     if ( nbSel >= 1 ) {
372       AlgoList.Clear(); 
373       SALOME_ListIteratorOfListIO Itinit( mySelection->StoredIObjects() );
374       for (; Itinit.More(); Itinit.Next()) {
375         AlgoList.Append(Itinit.Value());
376       }
377       myOkAlgorithm = true ;
378       if (nbSel > 1)
379         aString = tr("%1 Algorithms").arg(nbSel) ;
380       LineEditC1A1Algo->setText(aString) ;
381     }
382     else {
383       myOkAlgorithm = false ;
384       return ;
385     }
386   }
387
388   myEditCurrentArgument->setText(aString) ;
389 }
390
391
392 //=================================================================================
393 // function : SetEditCurrentArgument()
394 // purpose  :
395 //=================================================================================
396 void SMESHGUI_InitMeshDlg::SetEditCurrentArgument()
397 {
398   QPushButton* send = (QPushButton*)sender();
399   switch (myConstructorId)
400     {
401     case 0: /* default constructor */
402       { 
403         if(send == SelectButtonC1A1) {
404           LineEditC1A1->setFocus() ;
405           myEditCurrentArgument = LineEditC1A1;
406           mySelection->ClearFilters() ;   
407           mySelection->AddFilter(myGeomFilter) ;
408         } else if( send == SelectButtonC1A1Hyp ) {
409           LineEditC1A1Hyp->setFocus() ;
410           myEditCurrentArgument = LineEditC1A1Hyp ;
411           mySelection->ClearFilters() ;   
412           mySelection->AddFilter(myHypothesisFilter) ;
413         } else if( send == SelectButtonC1A1Algo ) {
414           LineEditC1A1Algo->setFocus() ;
415           myEditCurrentArgument = LineEditC1A1Algo ;
416           mySelection->ClearFilters() ;   
417           mySelection->AddFilter(myAlgorithmFilter) ;
418         }
419         SelectionIntoArgument() ;
420         break;
421       }
422     }
423   return ;
424 }
425
426 //=================================================================================
427 // function : DeactivateActiveDialog()
428 // purpose  :
429 //=================================================================================
430 void SMESHGUI_InitMeshDlg::DeactivateActiveDialog()
431 {
432   if ( GroupConstructors->isEnabled() ) {
433     disconnect( mySelection, 0, this, 0 );
434     GroupConstructors->setEnabled(false) ;
435     GroupC1->setEnabled(false) ;
436     GroupButtons->setEnabled(false) ;
437   }
438 }
439
440
441 //=================================================================================
442 // function : ActivateThisDialog()
443 // purpose  :
444 //=================================================================================
445 void SMESHGUI_InitMeshDlg::ActivateThisDialog()
446 {
447   mySMESHGUI->EmitSignalDeactivateDialog() ;   
448   GroupConstructors->setEnabled(true) ;
449   GroupC1->setEnabled(true) ;
450   GroupButtons->setEnabled(true) ;
451   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
452   return ;
453 }
454
455
456 //=================================================================================
457 // function : enterEvent()
458 // purpose  :
459 //=================================================================================
460 void SMESHGUI_InitMeshDlg::enterEvent(QEvent* e)
461 {
462   if ( GroupConstructors->isEnabled() )
463     return ;  
464   ActivateThisDialog() ;
465   return ;
466 }
467
468
469 //=================================================================================
470 // function : closeEvent()
471 // purpose  :
472 //=================================================================================
473 void SMESHGUI_InitMeshDlg::closeEvent( QCloseEvent* e )
474 {
475   this->ClickOnCancel() ;
476   return ;
477 }
478
479 //=================================================================================
480 // function : TextChangedInLineEdit()
481 // purpose  :
482 //=================================================================================
483 void SMESHGUI_InitMeshDlg::TextChangedInLineEdit(const QString& newText)
484 {  
485   QLineEdit* send = (QLineEdit*)sender();
486   QString newT = strdup(newText) ;
487   
488   if (send == LineEdit_NameMesh) {
489     myNameMesh = newText;
490   }
491   return ;
492 }