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