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