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