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