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