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