Salome HOME
NRI : Merge from V1_2.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_MirrorDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_MirrorDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_MirrorDlg.h"
11
12 #include "GeometryGUI.h"
13
14 #include "QAD_Application.h"
15 #include "QAD_Desktop.h"
16 #include "utilities.h"
17
18 #include <Geom_Plane.hxx>
19 #include <BRep_Tool.hxx>
20
21 #include <qbuttongroup.h>
22 #include <qgroupbox.h>
23 #include <qlabel.h>
24 #include <qlineedit.h>
25 #include <qpushbutton.h>
26 #include <qradiobutton.h>
27 #include <qlayout.h>
28 #include <qvariant.h>
29 #include <qtooltip.h>
30 #include <qwhatsthis.h>
31 #include <qimage.h>
32 #include <qpixmap.h>
33
34
35
36
37 //=================================================================================
38 // class    : GeometryGUI_MirrorDlg()
39 // purpose  : Constructs a GeometryGUI_MirrorDlg 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 GeometryGUI_MirrorDlg::GeometryGUI_MirrorDlg( 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     
48     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_MIRROR")));
49     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
50
51     if ( !name )
52         setName( "GeometryGUI_MirrorDlg" );
53     resize( 303, 225 ); 
54     setCaption( tr( "GEOM_MIRROR_TITLE"  ) );
55     setSizeGripEnabled( TRUE );
56     GeometryGUI_MirrorDlgLayout = new QGridLayout( this ); 
57     GeometryGUI_MirrorDlgLayout->setSpacing( 6 );
58     GeometryGUI_MirrorDlgLayout->setMargin( 11 );
59
60     /***************************************************************/
61     GroupButtons = new QGroupBox( this, "GroupButtons" );
62     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
63     GroupButtons->setTitle( tr( ""  ) );
64     GroupButtons->setColumnLayout(0, Qt::Vertical );
65     GroupButtons->layout()->setSpacing( 0 );
66     GroupButtons->layout()->setMargin( 0 );
67     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
68     GroupButtonsLayout->setAlignment( Qt::AlignTop );
69     GroupButtonsLayout->setSpacing( 6 );
70     GroupButtonsLayout->setMargin( 11 );
71     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
72     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
73     buttonCancel->setAutoDefault( TRUE );
74     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
75     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
76     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
77     buttonApply->setAutoDefault( TRUE );
78     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
79     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
80     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
81     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
82     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
83     buttonOk->setAutoDefault( TRUE );
84     buttonOk->setDefault( TRUE );
85     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
86
87     GeometryGUI_MirrorDlgLayout->addWidget( GroupButtons, 2, 0 );
88
89     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
90     GroupConstructors->setTitle( tr( "GEOM_MIRROR"  ) );
91     GroupConstructors->setExclusive( TRUE );
92     GroupConstructors->setColumnLayout(0, Qt::Vertical );
93     GroupConstructors->layout()->setSpacing( 0 );
94     GroupConstructors->layout()->setMargin( 0 );
95     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
96     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
97     GroupConstructorsLayout->setSpacing( 6 );
98     GroupConstructorsLayout->setMargin( 11 );
99     QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
100     GroupConstructorsLayout->addItem( spacer_2, 0, 1 );
101     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
102     Constructor1->setText( tr( ""  ) );
103     Constructor1->setPixmap( image0 );
104     Constructor1->setChecked( TRUE );
105     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
106     Constructor1->setMinimumSize( QSize( 50, 0 ) );
107     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
108     GeometryGUI_MirrorDlgLayout->addWidget( GroupConstructors, 0, 0 );
109
110     /***************************************************************/   
111     GroupC1 = new QGroupBox( this, "GroupC1" );
112     GroupC1->setTitle( tr( "GEOM_ARGUMENTS"  ) );
113     GroupC1->setColumnLayout(0, Qt::Vertical );
114     GroupC1->layout()->setSpacing( 0 );
115     GroupC1->layout()->setMargin( 0 );
116     GroupC1Layout = new QGridLayout( GroupC1->layout() );
117     GroupC1Layout->setAlignment( Qt::AlignTop );
118     GroupC1Layout->setSpacing( 6 );
119     GroupC1Layout->setMargin( 11 );
120     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
121     TextLabelC1A1->setText( tr( "GEOM_OBJECT"  ) );
122     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
123     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
124     TextLabelC1A1->setFrameShadow( QLabel::Plain );
125     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
126     TextLabelC1A2 = new QLabel( GroupC1, "TextLabelC1A2" );
127     TextLabelC1A2->setText( tr( "GEOM_PLANE_MIRROR"  ) );
128     TextLabelC1A2->setMinimumSize( QSize( 50, 0 ) );
129     TextLabelC1A2->setFrameShape( QLabel::NoFrame );
130     TextLabelC1A2->setFrameShadow( QLabel::Plain );
131     GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
132     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
133     SelectButtonC1A1->setText( tr( ""  ) );
134     SelectButtonC1A1->setPixmap( image1 );
135     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
136     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
137     SelectButtonC1A2->setText( tr( ""  ) );
138     SelectButtonC1A2->setPixmap( image1 );
139     GroupC1Layout->addWidget( SelectButtonC1A2, 1, 1 );
140     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
141     LineEditC1A1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1->sizePolicy().hasHeightForWidth() ) );
142     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
143     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
144     GroupC1Layout->addWidget( LineEditC1A2, 1, 2 );
145     GeometryGUI_MirrorDlgLayout->addWidget( GroupC1, 1, 0 );
146
147     /* Initialisation */
148     Init( Sel ) ;
149 }
150
151
152 //=================================================================================
153 // function : ~GeometryGUI_MirrorDlg()
154 // purpose  : Destroys the object and frees any allocated resources
155 //=================================================================================
156 GeometryGUI_MirrorDlg::~GeometryGUI_MirrorDlg()
157 {  
158   /* no need to delete child widgets, Qt does it all for us */
159 }
160
161
162
163 //=================================================================================
164 // function : Init()
165 // purpose  :
166 //=================================================================================
167 void GeometryGUI_MirrorDlg::Init( SALOME_Selection* Sel )
168 {  
169   mySelection = Sel ;
170   myShape1.Nullify() ;
171   myShape2.Nullify() ;
172   mySimulationTopoDs.Nullify() ;
173   myConstructorId = 0 ;
174   
175   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
176   GroupC1->show();
177
178   myEditCurrentArgument = LineEditC1A1 ;        
179   Constructor1->setChecked( TRUE );
180   myOkShape1 = myOkShape2 = false ;
181
182   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
183
184   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
185   myGeom = GEOM::GEOM_Gen::_narrow(comp);  
186   /* Filter definition */
187   
188   myFaceFilter = new GEOM_FaceFilter( StdSelect_Plane, myGeom );
189
190   // TODO previous selection into argument ?
191
192   /* signals and slots connections */
193   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
194   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
195   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
196   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
197   connect( SelectButtonC1A1, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
198   connect( SelectButtonC1A2, SIGNAL (clicked() ),  this, SLOT( SetEditCurrentArgument() ) ) ;
199
200   connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
201   connect( LineEditC1A2, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
202
203   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
204   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
205   /* to close dialog if study change */
206   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
207  
208   /* Move widget on the botton right corner of main widget */
209   int x, y ;
210   myGeomGUI->DefineDlgPosition( this, x, y ) ;
211   this->move( x, y ) ;
212   this->show() ; /* Displays Dialog */ 
213
214   return ;
215 }
216
217
218
219 //=================================================================================
220 // function : ConstructorsClicked()
221 // purpose  : Radio button management
222 //=================================================================================
223 void GeometryGUI_MirrorDlg::ConstructorsClicked(int constructorId)
224 {
225   GeometryGUI::GetGeometryGUI()->EraseSimulationShape() ;
226
227   switch (constructorId)
228     {
229     case 0:
230       {
231         GroupC1->show();
232         myConstructorId = constructorId ;
233         myEditCurrentArgument = LineEditC1A1 ;
234         LineEditC1A2->setText(tr("")) ;
235         Constructor1->setChecked( TRUE );
236         myOkShape1 =  myOkShape2 = false ;
237         break;
238       }
239     }
240  return ;
241 }
242
243 //=================================================================================
244 // function : ClickOnOk()
245 // purpose  :
246 //=================================================================================
247 void GeometryGUI_MirrorDlg::ClickOnOk()
248 {
249   this->ClickOnApply() ;
250   this->ClickOnCancel() ;
251
252   return ;
253 }
254
255 //=================================================================================
256 // function : ClickOnApply()
257 // purpose  :
258 //=================================================================================
259 void GeometryGUI_MirrorDlg::ClickOnApply()
260 {
261   myGeomGUI->GetDesktop()->putInfo( tr("") ) ; 
262   switch(myConstructorId)
263     { 
264     case 0 :
265       {
266         if(myOkShape1 && myOkShape2) {
267            myGeomGUI->MakeMirrorAndDisplay(myGeomShape1 ,myGeomShape2 ) ;
268         }
269         break ;
270       }
271     }
272   // accept();
273   return ;
274 }
275
276
277 //=================================================================================
278 // function : ClickOnCancel()
279 // purpose  :
280 //=================================================================================
281 void GeometryGUI_MirrorDlg::ClickOnCancel()
282 {
283   mySelection->ClearFilters() ;
284   myGeomGUI->EraseSimulationShape() ;
285   mySimulationTopoDs.Nullify() ;
286   disconnect( mySelection, 0, this, 0 );
287   myGeomGUI->ResetState() ;
288   reject() ;
289   return ;
290 }
291
292
293
294 //=================================================================================
295 // function : SelectionIntoArgument()
296 // purpose  : Called when selection has changed
297 //=================================================================================
298 void GeometryGUI_MirrorDlg::SelectionIntoArgument()
299 {
300   myEditCurrentArgument->setText("") ;
301   myGeomGUI->EraseSimulationShape() ; 
302   mySimulationTopoDs.Nullify() ;
303
304   QString aString = ""; /* name of selection */
305
306   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
307   if ( nbSel != 1 ) {
308     switch (myConstructorId) 
309       {
310       case 0:
311         {
312           if ( myEditCurrentArgument == LineEditC1A1 ) {
313             myOkShape1 = false ;
314           }
315           else if ( myEditCurrentArgument == LineEditC1A2 ) {
316             myOkShape2 = false ;
317           }
318           break ;
319         } 
320       }
321     return ;
322   }
323
324   /*  nbSel == 1  */ 
325   TopoDS_Shape S; 
326   Standard_Boolean testResult ;
327   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
328   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
329     return ;
330   
331   if ( myEditCurrentArgument == LineEditC1A1 ) {
332     myGeomShape1 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
333     if( !testResult )
334       return ;
335     myShape1 = S ;
336     LineEditC1A1->setText(aString) ;
337     myOkShape1 = true ;
338   }    
339   else if ( myEditCurrentArgument == LineEditC1A2 ) {
340     myGeomShape2 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
341     if( !testResult )
342       return ;
343     myShape2 = S ;
344     LineEditC1A2->setText(aString) ;
345     myOkShape2 = true ;
346   }
347
348   if(myOkShape1 && myOkShape2) {
349     MakeMirrorSimulationAndDisplay( myShape1,  myShape2 ) ;
350   }
351
352   return ; 
353 }
354
355
356
357 //=================================================================================
358 // function : MakeMirrorSimulationAndDisplay()
359 // purpose  : S1 is a shape and S2 a mirror.
360 //=================================================================================
361 void GeometryGUI_MirrorDlg::MakeMirrorSimulationAndDisplay( const TopoDS_Shape& S1, const TopoDS_Shape& S2 )
362 {
363   this->mySimulationTopoDs.Nullify() ;
364   
365   try {
366     Handle(Geom_Surface) surf = BRep_Tool::Surface(TopoDS::Face(S2)) ;
367     Handle(Geom_Plane) myPlane = Handle(Geom_Plane)::DownCast(surf) ;
368     const gp_Ax3 pos = myPlane->Position() ;
369     const gp_Pnt loc = pos.Location() ;  /* location of the plane */
370     const gp_Dir dir = pos.Direction() ; /* Main direction of the plane (Z axis) */
371     
372     /* plane used for mirroring */
373     gp_Ax2 pln(loc, dir) ;
374     gp_Trsf theTransformation ;
375     theTransformation.SetMirror(pln) ;
376     BRepBuilderAPI_Transform myBRepTransformation( S1, theTransformation, Standard_False ) ;
377     
378     this->mySimulationTopoDs = myBRepTransformation.Shape() ;
379     if( this->mySimulationTopoDs.IsNull() )
380       return ;
381     else
382       myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ; 
383   }
384   catch(Standard_Failure) {
385     MESSAGE( "Exception catched in MakeMirrorSimulationAndDisplay" ) ;
386     return ;
387   }
388   return ;
389 }
390
391
392
393 //=================================================================================
394 // function : SetEditCurrentArgument()
395 // purpose  :
396 //=================================================================================
397 void GeometryGUI_MirrorDlg::SetEditCurrentArgument()
398 {
399   QPushButton* send = (QPushButton*)sender();
400
401   switch (myConstructorId)
402     {
403     case 0:
404       { 
405         if( send == SelectButtonC1A1 ) {
406           LineEditC1A1->setFocus() ;
407           myEditCurrentArgument = LineEditC1A1 ;
408           mySelection->ClearFilters() ;   
409           SelectionIntoArgument() ;
410         }
411         else if(send == SelectButtonC1A2) {
412           LineEditC1A2->setFocus() ;
413           myEditCurrentArgument = LineEditC1A2;
414           mySelection->AddFilter(myFaceFilter) ;
415           SelectionIntoArgument() ;
416         }       
417         break;
418       }
419     }
420   return ;
421 }
422
423
424
425 //=================================================================================
426 // function : LineEditReturnPressed()
427 // purpose  :
428 //=================================================================================
429 void GeometryGUI_MirrorDlg::LineEditReturnPressed()
430 {
431   QLineEdit* send = (QLineEdit*)sender();  
432   if( send == LineEditC1A1 )
433     myEditCurrentArgument = LineEditC1A1 ;
434   else if ( send == LineEditC1A2 )
435     myEditCurrentArgument = LineEditC1A2 ; 
436   else
437     return ;
438   
439   /* User name of object input management                          */
440   /* If successfull the selection is changed and signal emitted... */
441   /* so SelectionIntoArgument() is automatically called.           */
442   const QString objectUserName = myEditCurrentArgument->text() ;
443   QWidget* thisWidget = (QWidget*)this ;
444   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
445     myEditCurrentArgument->setText( objectUserName ) ;
446   }
447   return ;
448 }
449
450
451
452 //=================================================================================
453 // function : DeactivateActiveDialog()
454 // purpose  :
455 //=================================================================================
456 void GeometryGUI_MirrorDlg::DeactivateActiveDialog()
457 {
458   if ( GroupConstructors->isEnabled() ) {
459     GroupConstructors->setEnabled(false) ;
460     GroupC1->setEnabled(false) ;
461     GroupButtons->setEnabled(false) ;  
462     disconnect( mySelection, 0, this, 0 );
463   }
464   return ;
465 }
466
467
468
469 //=================================================================================
470 // function : closeEvent()
471 // purpose  :
472 //=================================================================================
473 void GeometryGUI_MirrorDlg::closeEvent( QCloseEvent* e )
474 {
475   this->ClickOnCancel() ; /* same than click on cancel button */
476 }
477
478
479 //=================================================================================
480 // function : enterEvent()
481 // purpose  : when mouse enter onto the QWidget
482 //=================================================================================
483 void GeometryGUI_MirrorDlg::enterEvent( QEvent *  )
484 {
485   if ( GroupConstructors->isEnabled() )
486     return ;
487   ActivateThisDialog() ;
488 }
489
490
491
492 //=================================================================================
493 // function : ActivateThisDialog()
494 // purpose  :
495 //=================================================================================
496 void GeometryGUI_MirrorDlg::ActivateThisDialog()
497 {
498   /* Emit a signal to deactivate any active dialog */
499   myGeomGUI->EmitSignalDeactivateDialog() ;
500   GroupConstructors->setEnabled(true) ;
501   GroupC1->setEnabled(true) ;
502   GroupButtons->setEnabled(true) ;  
503   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
504   return ;
505 }