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