1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GeometryGUI_CenterMassDlg.cxx
25 // Author : Lucien PIGNOLONI
30 #include "GeometryGUI_CenterMassDlg.h"
31 #include "GeometryGUI.h"
33 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <qbuttongroup.h>
36 #include <qgroupbox.h>
38 #include <qlineedit.h>
39 #include <qpushbutton.h>
40 #include <qradiobutton.h>
44 #include <qwhatsthis.h>
46 #include <qvalidator.h>
49 //=================================================================================
50 // class : GeometryGUI_CenterMassDlg()
51 // purpose : Constructs a GeometryGUI_CenterMassDlg which is a child of 'parent', with the
52 // name 'name' and widget flags set to 'f'.
53 // The dialog will by default be modeless, unless you set 'modal' to
54 // TRUE to construct a modal dialog.
55 //=================================================================================
56 GeometryGUI_CenterMassDlg::GeometryGUI_CenterMassDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
57 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
59 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_CENTERMASS")));
60 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
63 setName( "GeometryGUI_CenterMassDlg" );
65 setCaption( tr( "GEOM_CMASS_TITLE" ) );
66 setSizeGripEnabled( TRUE );
67 GeometryGUI_CenterMassDlgLayout = new QGridLayout( this );
68 GeometryGUI_CenterMassDlgLayout->setSpacing( 6 );
69 GeometryGUI_CenterMassDlgLayout->setMargin( 11 );
71 /***************************************************************/
72 GroupButtons = new QGroupBox( this, "GroupButtons" );
73 GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
74 GroupButtons->setTitle( tr( "" ) );
75 GroupButtons->setColumnLayout(0, Qt::Vertical );
76 GroupButtons->layout()->setSpacing( 0 );
77 GroupButtons->layout()->setMargin( 0 );
78 GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
79 GroupButtonsLayout->setAlignment( Qt::AlignTop );
80 GroupButtonsLayout->setSpacing( 6 );
81 GroupButtonsLayout->setMargin( 11 );
82 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
83 buttonCancel->setText( tr( "GEOM_BUT_CLOSE" ) );
84 buttonCancel->setAutoDefault( TRUE );
85 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
86 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
87 buttonApply->setText( tr( "GEOM_BUT_APPLY" ) );
88 buttonApply->setAutoDefault( TRUE );
89 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
90 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
91 GroupButtonsLayout->addItem( spacer, 0, 2 );
92 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
93 buttonOk->setText( tr( "GEOM_BUT_OK" ) );
94 buttonOk->setAutoDefault( TRUE );
95 buttonOk->setDefault( TRUE );
96 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
97 GeometryGUI_CenterMassDlgLayout->addWidget( GroupButtons, 2, 0 );
99 /***************************************************************/
100 GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
101 GroupConstructors->setTitle( tr( "GEOM_CMASS" ) );
102 GroupConstructors->setExclusive( TRUE );
103 GroupConstructors->setColumnLayout(0, Qt::Vertical );
104 GroupConstructors->layout()->setSpacing( 0 );
105 GroupConstructors->layout()->setMargin( 0 );
106 GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
107 GroupConstructorsLayout->setAlignment( Qt::AlignTop );
108 GroupConstructorsLayout->setSpacing( 6 );
109 GroupConstructorsLayout->setMargin( 11 );
110 Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
111 Constructor1->setText( tr( "" ) );
112 Constructor1->setPixmap( image0 );
113 Constructor1->setChecked( TRUE );
114 Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
115 Constructor1->setMinimumSize( QSize( 50, 0 ) );
116 GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
117 QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
118 GroupConstructorsLayout->addItem( spacer_2, 0, 1 );
119 GeometryGUI_CenterMassDlgLayout->addWidget( GroupConstructors, 0, 0 );
121 /***************************************************************/
122 GroupC1 = new QGroupBox( this, "GroupC1" );
123 GroupC1->setTitle( tr( "GEOM_OBJECT_RESULT" ) );
124 GroupC1->setMinimumSize( QSize( 0, 0 ) );
125 GroupC1->setFrameShape( QGroupBox::Box );
126 GroupC1->setFrameShadow( QGroupBox::Sunken );
127 GroupC1->setColumnLayout(0, Qt::Vertical );
128 GroupC1->layout()->setSpacing( 0 );
129 GroupC1->layout()->setMargin( 0 );
130 GroupC1Layout = new QGridLayout( GroupC1->layout() );
131 GroupC1Layout->setAlignment( Qt::AlignTop );
132 GroupC1Layout->setSpacing( 6 );
133 GroupC1Layout->setMargin( 11 );
134 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
135 SelectButtonC1A1->setText( tr( "" ) );
136 SelectButtonC1A1->setPixmap( image1 );
137 SelectButtonC1A1->setToggleButton( FALSE );
138 GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
139 LineEdit_X = new QLineEdit( GroupC1, "LineEdit_X" );
140 LineEdit_X->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_X->sizePolicy().hasHeightForWidth() ) );
141 LineEdit_X->setMinimumSize( QSize( 70, 0 ) );
142 //LineEdit_X->setEnabled( FALSE );
143 LineEdit_X->setReadOnly( TRUE );
144 GroupC1Layout->addWidget( LineEdit_X, 1, 2 );
145 TextLabel_Z = new QLabel( GroupC1, "TextLabel_Z" );
146 TextLabel_Z->setText( tr( "GEOM_Z" ) );
147 TextLabel_Z->setMinimumSize( QSize( 15, 0 ) );
148 TextLabel_Z->setFrameShape( QLabel::NoFrame );
149 TextLabel_Z->setFrameShadow( QLabel::Plain );
150 TextLabel_Z->setMaximumSize( QSize( 15, 32767 ) );
151 GroupC1Layout->addWidget( TextLabel_Z, 1, 5 );
152 LineEdit_Z = new QLineEdit( GroupC1, "LineEdit_Z" );
153 LineEdit_Z->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Z->sizePolicy().hasHeightForWidth() ) );
154 LineEdit_Z->setMinimumSize( QSize( 70, 0 ) );
155 //LineEdit_Z->setEnabled( FALSE );
156 LineEdit_Z->setReadOnly( TRUE );
157 GroupC1Layout->addWidget( LineEdit_Z, 1, 6 );
158 TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
159 TextLabelC1A1->setText( tr( "GEOM_OBJECT" ) );
160 TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
161 TextLabelC1A1->setFrameShape( QLabel::NoFrame );
162 TextLabelC1A1->setFrameShadow( QLabel::Plain );
163 GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
164 LineEdit_Y = new QLineEdit( GroupC1, "LineEdit_Y" );
165 LineEdit_Y->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Y->sizePolicy().hasHeightForWidth() ) );
166 LineEdit_Y->setMinimumSize( QSize( 70, 0 ) );
167 //LineEdit_Y->setEnabled( FALSE );
168 LineEdit_Y->setReadOnly( TRUE );
169 GroupC1Layout->addWidget( LineEdit_Y, 1, 4 );
170 TextLabel_Y = new QLabel( GroupC1, "TextLabel_Y" );
171 TextLabel_Y->setText( tr( "GEOM_Y" ) );
172 TextLabel_Y->setMinimumSize( QSize( 15, 0 ) );
173 TextLabel_Y->setFrameShape( QLabel::NoFrame );
174 TextLabel_Y->setFrameShadow( QLabel::Plain );
175 TextLabel_Y->setMaximumSize( QSize( 15, 32767 ) );
176 GroupC1Layout->addWidget( TextLabel_Y, 1, 3 );
177 TextLabel_X = new QLabel( GroupC1, "TextLabel_X" );
178 TextLabel_X->setText( tr( "GEOM_X" ) );
179 TextLabel_X->setMinimumSize( QSize( 15, 0 ) );
180 TextLabel_X->setFrameShape( QLabel::NoFrame );
181 TextLabel_X->setFrameShadow( QLabel::Plain );
182 TextLabel_X->setMaximumSize( QSize( 15, 32767 ) );
183 GroupC1Layout->addWidget( TextLabel_X, 1, 1 );
184 TextLabel_Center = new QLabel( GroupC1, "TextLabel_Center" );
185 TextLabel_Center->setText( tr( "GEOM_CENTER" ) );
186 TextLabel_Center->setMinimumSize( QSize( 50, 0 ) );
187 TextLabel_Center->setFrameShape( QLabel::NoFrame );
188 TextLabel_Center->setFrameShadow( QLabel::Plain );
189 GroupC1Layout->addWidget( TextLabel_Center, 1, 0 );
190 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
191 LineEditC1A1->setMinimumSize( QSize( 260, 0 ) );
192 GroupC1Layout->addMultiCellWidget( LineEditC1A1, 0, 0, 2, 6 );
193 GeometryGUI_CenterMassDlgLayout->addWidget( GroupC1, 1, 0 );
194 /***************************************************************/
196 Init(Sel) ; /* Initialisations */
201 //=================================================================================
202 // function : ~GeometryGUI_CenterMassDlg()
203 // purpose : Destroys the object and frees any allocated resources
204 //=================================================================================
205 GeometryGUI_CenterMassDlg::~GeometryGUI_CenterMassDlg()
207 // no need to delete child widgets, Qt does it all for us
212 //=================================================================================
215 //=================================================================================
216 void GeometryGUI_CenterMassDlg::Init( SALOME_Selection* Sel )
218 LineEdit_X->setMaxLength( 9 );
219 LineEdit_Y->setMaxLength( 9 );
220 LineEdit_Z->setMaxLength( 9 );
221 QDoubleValidator *Va = new QDoubleValidator( -999999, +999999, 3, LineEdit_X ) ;
222 QDoubleValidator *Vb = new QDoubleValidator( -999999, +999999, 3, LineEdit_Y ) ;
223 QDoubleValidator *Vc = new QDoubleValidator( -999999, +999999, 3, LineEdit_Z ) ;
224 LineEdit_X->setValidator( Va ) ;
225 LineEdit_Y->setValidator( Vb ) ;
226 LineEdit_Z->setValidator( Vc ) ;
228 myConstructorId = 0 ;
230 LineEdit_X->setText("") ;
231 LineEdit_Y->setText("") ;
232 LineEdit_Z->setText("") ;
234 Constructor1->setChecked( TRUE );
235 myEditCurrentArgument = LineEditC1A1 ;
237 myGeomGUI = GeometryGUI::GetGeometryGUI() ;
238 myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
239 mySimulationTopoDs.Nullify() ;
241 myOkCenterMass = false ;
243 // TODO : previous selection into argument ?
245 /* Filter definitions */
246 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
247 myGeom = GEOM::GEOM_Gen::_narrow(comp);
249 /* signals and slots connections */
250 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) ) ;
251 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
252 connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
253 connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
254 connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
255 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
257 connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
258 /* to close dialog if study change */
259 connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
261 /* Move widget on the botton right corner of main widget */
263 myGeomGUI->DefineDlgPosition( this, x, y ) ;
265 this->show() ; /* displays Dialog */
271 //=================================================================================
272 // function : ConstructorsClicked()
273 // purpose : Radio button management
274 //=================================================================================
275 void GeometryGUI_CenterMassDlg::ConstructorsClicked(int constructorId)
282 //=================================================================================
283 // function : ClickOnCancel()
285 //=================================================================================
286 void GeometryGUI_CenterMassDlg::ClickOnCancel()
288 myGeomGUI->EraseSimulationShape() ;
289 mySimulationTopoDs.Nullify() ;
290 disconnect( mySelection, 0, this, 0 );
291 myGeomGUI->ResetState() ;
296 //=================================================================================
297 // function : ClickOnOk()
299 //=================================================================================
300 void GeometryGUI_CenterMassDlg::ClickOnOk()
302 this->ClickOnApply() ;
303 this->ClickOnCancel() ;
308 //=================================================================================
309 // function : ClickOnApply()
311 //=================================================================================
312 void GeometryGUI_CenterMassDlg::ClickOnApply()
314 myGeomGUI->EraseSimulationShape() ;
315 mySimulationTopoDs.Nullify() ;
316 myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
317 if( myOkCenterMass) {
318 myGeomGUI->MakeCDGAndDisplay( myGeomShape ) ;
326 //=================================================================================
327 // function : SelectionIntoArgument()
328 // purpose : Called when selection as changed or other case
329 //=================================================================================
330 void GeometryGUI_CenterMassDlg::SelectionIntoArgument()
332 myGeomGUI->EraseSimulationShape() ;
333 myEditCurrentArgument->setText("") ;
334 myOkCenterMass = false ;
335 Standard_Boolean testResult ;
337 LineEdit_X->setText("") ;
338 LineEdit_Y->setText("") ;
339 LineEdit_Z->setText("") ;
341 QString aString = ""; /* future the name of selection */
343 int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
349 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
350 if( !myGeomGUI->GetTopoFromSelection(mySelection, this->myShape) )
353 myGeomShape = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
356 myEditCurrentArgument->setText(aString) ;
357 if( this->CalculateAndDisplayCenterMass() ) {
358 myOkCenterMass = true ;
365 //=================================================================================
366 // function : SetEditCurrentArgument()
368 //=================================================================================
369 void GeometryGUI_CenterMassDlg::SetEditCurrentArgument()
371 QPushButton* send = (QPushButton*)sender();
372 switch (myConstructorId)
374 case 0: /* default constructor */
376 if(send == SelectButtonC1A1) {
377 LineEditC1A1->setFocus() ;
378 myEditCurrentArgument = LineEditC1A1;
380 SelectionIntoArgument() ;
389 //=================================================================================
390 // function : LineEditReturnPressed()
392 //=================================================================================
393 void GeometryGUI_CenterMassDlg::LineEditReturnPressed()
395 QLineEdit* send = (QLineEdit*)sender();
396 if( send == LineEditC1A1 )
397 myEditCurrentArgument = LineEditC1A1 ;
401 /* User name of object input management */
402 /* If successfull the selection is changed and signal emitted... */
403 /* so SelectionIntoArgument() is automatically called. */
404 const QString objectUserName = myEditCurrentArgument->text() ;
405 QWidget* thisWidget = (QWidget*)this ;
406 if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
407 myEditCurrentArgument->setText( objectUserName ) ;
413 //=================================================================================
414 // function : DeactivateActiveDialog()
416 //=================================================================================
417 void GeometryGUI_CenterMassDlg::DeactivateActiveDialog()
419 if ( GroupConstructors->isEnabled() ) {
420 myGeomGUI->EraseSimulationShape() ;
421 disconnect( mySelection, 0, this, 0 );
422 GroupConstructors->setEnabled(false) ;
423 GroupC1->setEnabled(false) ;
424 GroupButtons->setEnabled(false) ;
430 //=================================================================================
431 // function : ActivateThisDialog()
433 //=================================================================================
434 void GeometryGUI_CenterMassDlg::ActivateThisDialog()
436 /* Emit a signal to deactivate the active dialog */
437 myGeomGUI->EmitSignalDeactivateDialog() ;
438 GroupConstructors->setEnabled(true) ;
439 GroupC1->setEnabled(true) ;
440 GroupButtons->setEnabled(true) ;
441 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
442 if( !mySimulationTopoDs.IsNull() )
443 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
448 //=================================================================================
449 // function : enterEvent()
451 //=================================================================================
452 void GeometryGUI_CenterMassDlg::enterEvent(QEvent* e)
454 if ( GroupConstructors->isEnabled() )
456 ActivateThisDialog() ;
461 //=================================================================================
462 // function : closeEvent()
464 //=================================================================================
465 void GeometryGUI_CenterMassDlg::closeEvent( QCloseEvent* e )
467 /* same than click on cancel button */
468 this->ClickOnCancel() ;
473 //=================================================================================
474 // function : CalculateAndDisplayCenterMass()
476 //=================================================================================
477 bool GeometryGUI_CenterMassDlg::CalculateAndDisplayCenterMass()
479 myGeomGUI->EraseSimulationShape() ;
480 mySimulationTopoDs.Nullify() ;
487 if ( this->myShape.ShapeType() == TopAbs_VERTEX) {
488 myGeomGUI->VertexToPoint( this->myShape, this->myCenterMass );
490 else if ( this->myShape.ShapeType() == TopAbs_EDGE || this->myShape.ShapeType() == TopAbs_WIRE ) {
491 BRepGProp::LinearProperties(this->myShape, System);
492 this->myCenterMass = System.CentreOfMass() ;
494 else if ( this->myShape.ShapeType() == TopAbs_FACE || this->myShape.ShapeType() == TopAbs_SHELL ) {
495 BRepGProp::SurfaceProperties(this->myShape, System);
496 this->myCenterMass = System.CentreOfMass() ;
499 BRepGProp::VolumeProperties(this->myShape, System);
500 this->myCenterMass = System.CentreOfMass() ;
503 BRepBuilderAPI_MakeVertex V(this->myCenterMass) ;
504 mySimulationTopoDs = V.Shape() ;
506 resString = tr("%1").arg( myCenterMass.X(), 12, 'f', 6 ) ;
507 LineEdit_X->setText(resString) ;
509 resString = tr("%1").arg( myCenterMass.Y(), 12, 'f', 6 ) ;
510 LineEdit_Y->setText(resString) ;
512 resString = tr("%1").arg( myCenterMass.Z(), 12, 'f', 6 ) ;
513 LineEdit_Z->setText(resString) ;
516 if( !mySimulationTopoDs.IsNull() ) {
517 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
521 catch(Standard_Failure) {
522 MESSAGE("Catch intercepted in CalculateAndDisplayCenterMass()" << endl ) ;