2 // File : GeometryGUI_CenterMassDlg.cxx
4 // Author : Lucien PIGNOLONI
6 // Module : GeometryGUI
7 // Copyright : OPEN CASCADE
10 #include "GeometryGUI_CenterMassDlg.h"
11 #include "GeometryGUI.h"
13 #include <BRepBuilderAPI_MakeVertex.hxx>
15 #include <qbuttongroup.h>
16 #include <qgroupbox.h>
18 #include <qlineedit.h>
19 #include <qpushbutton.h>
20 #include <qradiobutton.h>
24 #include <qwhatsthis.h>
26 #include <qvalidator.h>
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 )
39 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_DLG_CENTERMASS")));
40 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_SELECT")));
43 setName( "GeometryGUI_CenterMassDlg" );
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 );
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 );
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 );
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 /***************************************************************/
176 Init(Sel) ; /* Initialisations */
181 //=================================================================================
182 // function : ~GeometryGUI_CenterMassDlg()
183 // purpose : Destroys the object and frees any allocated resources
184 //=================================================================================
185 GeometryGUI_CenterMassDlg::~GeometryGUI_CenterMassDlg()
187 // no need to delete child widgets, Qt does it all for us
192 //=================================================================================
195 //=================================================================================
196 void GeometryGUI_CenterMassDlg::Init( SALOME_Selection* Sel )
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 ) ;
208 myConstructorId = 0 ;
210 LineEdit_X->setText("") ;
211 LineEdit_Y->setText("") ;
212 LineEdit_Z->setText("") ;
214 Constructor1->setChecked( TRUE );
215 myEditCurrentArgument = LineEditC1A1 ;
217 myGeomGUI = GeometryGUI::GetGeometryGUI() ;
218 myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
219 mySimulationTopoDs.Nullify() ;
221 myOkCenterMass = false ;
223 // TODO : previous selection into argument ?
225 /* Filter definitions */
226 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "Geometry");
227 myGeom = GEOM::GEOM_Gen::_narrow(comp);
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() ) );
237 connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
238 /* to close dialog if study change */
239 connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
241 /* Move widget on the botton right corner of main widget */
243 myGeomGUI->DefineDlgPosition( this, x, y ) ;
245 this->show() ; /* displays Dialog */
251 //=================================================================================
252 // function : ConstructorsClicked()
253 // purpose : Radio button management
254 //=================================================================================
255 void GeometryGUI_CenterMassDlg::ConstructorsClicked(int constructorId)
262 //=================================================================================
263 // function : ClickOnCancel()
265 //=================================================================================
266 void GeometryGUI_CenterMassDlg::ClickOnCancel()
268 myGeomGUI->EraseSimulationShape() ;
269 mySimulationTopoDs.Nullify() ;
270 disconnect( mySelection, 0, this, 0 );
271 myGeomGUI->ResetState() ;
276 //=================================================================================
277 // function : ClickOnOk()
279 //=================================================================================
280 void GeometryGUI_CenterMassDlg::ClickOnOk()
282 this->ClickOnApply() ;
283 this->ClickOnCancel() ;
288 //=================================================================================
289 // function : ClickOnApply()
291 //=================================================================================
292 void GeometryGUI_CenterMassDlg::ClickOnApply()
294 myGeomGUI->EraseSimulationShape() ;
295 mySimulationTopoDs.Nullify() ;
296 myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
297 if( myOkCenterMass) {
298 myGeomGUI->MakeCDGAndDisplay( myGeomShape ) ;
306 //=================================================================================
307 // function : SelectionIntoArgument()
308 // purpose : Called when selection as changed or other case
309 //=================================================================================
310 void GeometryGUI_CenterMassDlg::SelectionIntoArgument()
312 myGeomGUI->EraseSimulationShape() ;
313 myEditCurrentArgument->setText("") ;
314 myOkCenterMass = false ;
315 Standard_Boolean testResult ;
317 LineEdit_X->setText("") ;
318 LineEdit_Y->setText("") ;
319 LineEdit_Z->setText("") ;
321 QString aString = ""; /* future the name of selection */
323 int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
329 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
330 if( !myGeomGUI->GetTopoFromSelection(mySelection, this->myShape) )
333 myGeomShape = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
336 myEditCurrentArgument->setText(aString) ;
337 if( this->CalculateAndDisplayCenterMass() ) {
338 myOkCenterMass = true ;
345 //=================================================================================
346 // function : SetEditCurrentArgument()
348 //=================================================================================
349 void GeometryGUI_CenterMassDlg::SetEditCurrentArgument()
351 QPushButton* send = (QPushButton*)sender();
352 switch (myConstructorId)
354 case 0: /* default constructor */
356 if(send == SelectButtonC1A1) {
357 LineEditC1A1->setFocus() ;
358 myEditCurrentArgument = LineEditC1A1;
360 SelectionIntoArgument() ;
369 //=================================================================================
370 // function : LineEditReturnPressed()
372 //=================================================================================
373 void GeometryGUI_CenterMassDlg::LineEditReturnPressed()
375 QLineEdit* send = (QLineEdit*)sender();
376 if( send == LineEditC1A1 )
377 myEditCurrentArgument = LineEditC1A1 ;
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 ) ;
393 //=================================================================================
394 // function : DeactivateActiveDialog()
396 //=================================================================================
397 void GeometryGUI_CenterMassDlg::DeactivateActiveDialog()
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) ;
410 //=================================================================================
411 // function : ActivateThisDialog()
413 //=================================================================================
414 void GeometryGUI_CenterMassDlg::ActivateThisDialog()
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 ) ;
428 //=================================================================================
429 // function : enterEvent()
431 //=================================================================================
432 void GeometryGUI_CenterMassDlg::enterEvent(QEvent* e)
434 if ( GroupConstructors->isEnabled() )
436 ActivateThisDialog() ;
441 //=================================================================================
442 // function : closeEvent()
444 //=================================================================================
445 void GeometryGUI_CenterMassDlg::closeEvent( QCloseEvent* e )
447 /* same than click on cancel button */
448 this->ClickOnCancel() ;
453 //=================================================================================
454 // function : CalculateAndDisplayCenterMass()
456 //=================================================================================
457 bool GeometryGUI_CenterMassDlg::CalculateAndDisplayCenterMass()
459 myGeomGUI->EraseSimulationShape() ;
460 mySimulationTopoDs.Nullify() ;
467 if ( this->myShape.ShapeType() == TopAbs_VERTEX) {
468 myGeomGUI->VertexToPoint( this->myShape, this->myCenterMass );
470 else if ( this->myShape.ShapeType() == TopAbs_EDGE || this->myShape.ShapeType() == TopAbs_WIRE ) {
471 BRepGProp::LinearProperties(this->myShape, System);
472 this->myCenterMass = System.CentreOfMass() ;
474 else if ( this->myShape.ShapeType() == TopAbs_FACE || this->myShape.ShapeType() == TopAbs_SHELL ) {
475 BRepGProp::SurfaceProperties(this->myShape, System);
476 this->myCenterMass = System.CentreOfMass() ;
479 BRepGProp::VolumeProperties(this->myShape, System);
480 this->myCenterMass = System.CentreOfMass() ;
483 BRepBuilderAPI_MakeVertex V(this->myCenterMass) ;
484 mySimulationTopoDs = V.Shape() ;
486 resString = tr("%1").arg( myCenterMass.X(), 12, 'f', 6 ) ;
487 LineEdit_X->setText(resString) ;
489 resString = tr("%1").arg( myCenterMass.Y(), 12, 'f', 6 ) ;
490 LineEdit_Y->setText(resString) ;
492 resString = tr("%1").arg( myCenterMass.Z(), 12, 'f', 6 ) ;
493 LineEdit_Z->setText(resString) ;
496 if( !mySimulationTopoDs.IsNull() ) {
497 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
501 catch(Standard_Failure) {
502 MESSAGE("Catch intercepted in CalculateAndDisplayCenterMass()" << endl ) ;