2 // File : GeometryGUI_DistanceDlg.cxx
3 // Created : Mon Mar 04 22:32:20 2002
4 // Author : Nicolas REJNERI
7 // Copyright : Open CASCADE 2002
11 #include "GeometryGUI_DistanceDlg.h"
13 #include "GeometryGUI.h"
14 #include "QAD_Application.h"
15 #include "QAD_Desktop.h"
16 #include "QAD_RightFrame.h"
17 #include "utilities.h"
19 #include "OCCViewer_Viewer3d.h"
20 #include "OCCViewer_ViewFrame.h"
22 // Open CASCADE Includes
23 #include <BRepBuilderAPI_MakeEdge.hxx>
24 #include <BRepBuilderAPI_MakeVertex.hxx>
25 #include <AIS_ListIteratorOfListOfInteractive.hxx>
28 #include <qmessagebox.h>
29 #include <qbuttongroup.h>
31 #include <qgroupbox.h>
33 #include <qlineedit.h>
34 #include <qpushbutton.h>
35 #include <qradiobutton.h>
39 #include <qwhatsthis.h>
45 //=================================================================================
46 // class : GeometryGUI_DistanceDlg()
47 // purpose : Constructs a GeometryGUI_DistanceDlg which is a child of 'parent', with the
48 // name 'name' and widget flags set to 'f'.
49 // The dialog will by default be modeless, unless you set 'modal' to
50 // TRUE to construct a modal dialog.
51 //=================================================================================
52 GeometryGUI_DistanceDlg::GeometryGUI_DistanceDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
53 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
55 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_DLG_MINDIST")));
56 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_SELECT")));
59 setName( "GeometryGUI_DistanceDlg" );
61 setCaption( tr( "GEOM_MINDIST_TITLE" ) );
62 setSizeGripEnabled( TRUE );
64 GeometryGUI_DistanceDlgLayout = new QGridLayout( this );
65 GeometryGUI_DistanceDlgLayout->setSpacing( 6 );
66 GeometryGUI_DistanceDlgLayout->setMargin( 11 );
68 /***************************************************************/
69 GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
70 GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
71 GroupConstructors->setExclusive( TRUE );
72 GroupConstructors->setColumnLayout(0, Qt::Vertical );
73 GroupConstructors->layout()->setSpacing( 0 );
74 GroupConstructors->layout()->setMargin( 0 );
75 GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
76 GroupConstructorsLayout->setAlignment( Qt::AlignTop );
77 GroupConstructorsLayout->setSpacing( 6 );
78 GroupConstructorsLayout->setMargin( 11 );
79 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
80 GroupConstructorsLayout->addItem( spacer, 0, 1 );
81 Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
82 Constructor1->setText( tr( "" ) );
83 Constructor1->setPixmap( image0 );
84 Constructor1->setChecked( TRUE );
85 Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
86 GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
87 GeometryGUI_DistanceDlgLayout->addWidget( GroupConstructors, 0, 0 );
89 /***************************************************************/
90 GroupConstructor1 = new QGroupBox( this, "GroupConstructor1" );
91 GroupConstructor1->setTitle( tr( "GEOM_MINDIST_OBJ" ) );
92 GroupConstructor1->setColumnLayout(0, Qt::Vertical );
93 GroupConstructor1->layout()->setSpacing( 0 );
94 GroupConstructor1->layout()->setMargin( 0 );
95 GroupConstructor1Layout = new QGridLayout( GroupConstructor1->layout() );
96 GroupConstructor1Layout->setAlignment( Qt::AlignTop );
97 GroupConstructor1Layout->setSpacing( 6 );
98 GroupConstructor1Layout->setMargin( 11 );
99 LineEditC1A2Shape = new QLineEdit( GroupConstructor1, "LineEditC1A2Shape" );
100 LineEditC1A2Shape->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A2Shape->sizePolicy().hasHeightForWidth() ) );
101 GroupConstructor1Layout->addWidget( LineEditC1A2Shape, 1, 2 );
102 LineEditC1A1Shape = new QLineEdit( GroupConstructor1, "LineEditC1A1Shape" );
103 LineEditC1A1Shape->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1Shape->sizePolicy().hasHeightForWidth() ) );
104 GroupConstructor1Layout->addWidget( LineEditC1A1Shape, 0, 2 );
105 SelectButtonC1A1Shape = new QPushButton( GroupConstructor1, "SelectButtonC1A1Shape" );
106 SelectButtonC1A1Shape->setText( tr( "" ) );
107 SelectButtonC1A1Shape->setPixmap( image1 );
108 GroupConstructor1Layout->addWidget( SelectButtonC1A1Shape, 0, 1 );
109 SelectButtonC1A2Shape = new QPushButton( GroupConstructor1, "SelectButtonC1A2Shape" );
110 SelectButtonC1A2Shape->setText( tr( "" ) );
111 SelectButtonC1A2Shape->setPixmap( image1 );
112 GroupConstructor1Layout->addWidget( SelectButtonC1A2Shape, 1, 1 );
113 TextLabelC1A2Shape = new QLabel( GroupConstructor1, "TextLabelC1A2Shape" );
114 TextLabelC1A2Shape->setText( tr( "GEOM_OBJECT_I" ).arg("2") );
115 TextLabelC1A2Shape->setMinimumSize( QSize( 50, 0 ) );
116 TextLabelC1A2Shape->setFrameShape( QLabel::NoFrame );
117 TextLabelC1A2Shape->setFrameShadow( QLabel::Plain );
118 GroupConstructor1Layout->addWidget( TextLabelC1A2Shape, 1, 0 );
119 TextLabelC1A1Shape = new QLabel( GroupConstructor1, "TextLabelC1A1Shape" );
120 TextLabelC1A1Shape->setText( tr( "GEOM_OBJECT_I" ).arg("1") );
121 TextLabelC1A1Shape->setMinimumSize( QSize( 50, 0 ) );
122 TextLabelC1A1Shape->setFrameShape( QLabel::NoFrame );
123 TextLabelC1A1Shape->setFrameShadow( QLabel::Plain );
124 GroupConstructor1Layout->addWidget( TextLabelC1A1Shape, 0, 0 );
125 GeometryGUI_DistanceDlgLayout->addWidget( GroupConstructor1, 1, 0 );
127 TextLabel_Length = new QLabel( GroupConstructor1, "TextLabel_Length" );
128 TextLabel_Length->setText( tr( "GEOM_LENGTH" ) );
129 TextLabel_Length->setMinimumSize( QSize( 50, 0 ) );
130 TextLabel_Length->setFrameShape( QLabel::NoFrame );
131 TextLabel_Length->setFrameShadow( QLabel::Plain );
132 GroupConstructor1Layout->addWidget( TextLabel_Length, 2, 0 );
133 LineEdit_Length = new QLineEdit( GroupConstructor1, "LineEdit_Length" );
134 LineEdit_Length->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Length->sizePolicy().hasHeightForWidth() ) );
135 // LineEdit_Length->setEnabled( FALSE );
136 LineEdit_Length->setReadOnly( TRUE );
137 GroupConstructor1Layout->addWidget( LineEdit_Length, 2, 2 );
139 /***************************************************************/
140 GroupButtons = new QGroupBox( this, "GroupButtons" );
141 GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
142 GroupButtons->setTitle( tr( "" ) );
143 GroupButtons->setColumnLayout(0, Qt::Vertical );
144 GroupButtons->layout()->setSpacing( 0 );
145 GroupButtons->layout()->setMargin( 0 );
146 GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
147 GroupButtonsLayout->setAlignment( Qt::AlignTop );
148 GroupButtonsLayout->setSpacing( 6 );
149 GroupButtonsLayout->setMargin( 11 );
150 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
151 buttonCancel->setText( tr( "GEOM_BUT_CLOSE" ) );
152 buttonCancel->setAutoDefault( TRUE );
153 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
154 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
155 buttonApply->setText( tr( "GEOM_BUT_APPLY" ) );
156 buttonApply->setAutoDefault( TRUE );
157 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
158 QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
159 GroupButtonsLayout->addItem( spacer_9, 0, 2 );
160 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
161 buttonOk->setText( tr( "GEOM_BUT_OK" ) );
162 buttonOk->setAutoDefault( TRUE );
163 buttonOk->setDefault( TRUE );
164 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
165 GeometryGUI_DistanceDlgLayout->addWidget( GroupButtons, 2, 0 );
172 //=================================================================================
173 // function : ~GeometryGUI_DistanceDlg()
174 // purpose : Destroys the object and frees any allocated resources
175 //=================================================================================
176 GeometryGUI_DistanceDlg::~GeometryGUI_DistanceDlg()
178 /* no need to delete child widgets, Qt does it all for us */
179 this->destroy(TRUE, TRUE) ;
184 //=================================================================================
187 //=================================================================================
188 void GeometryGUI_DistanceDlg::Init( SALOME_Selection* Sel )
193 myConstructorId = 0 ;
195 myGeomGUI = GeometryGUI::GetGeometryGUI() ;
197 GroupConstructor1->show();
198 myConstructorId = 0 ;
199 myEditCurrentArgument = LineEditC1A1Shape ;
200 Constructor1->setChecked( TRUE );
201 myOkShape1 = myOkShape2 = false ;
203 myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
205 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "Geometry");
206 myGeom = GEOM::GEOM_Gen::_narrow(comp);
208 // TODO previous selection into argument ?
210 /* signals and slots connections */
211 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
212 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
213 connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
214 connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
215 connect( SelectButtonC1A1Shape, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
216 connect( SelectButtonC1A2Shape, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
218 connect( LineEditC1A1Shape, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
219 connect( LineEditC1A2Shape, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
221 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
222 connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
223 /* to close dialog if study change */
224 connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
226 /* Move widget on the botton right corner of main widget */
228 myGeomGUI->DefineDlgPosition( this, x, y ) ;
230 this->show() ; /* Displays Dialog */
237 //=================================================================================
238 // function : ConstructorsClicked()
239 // purpose : Radio button management
240 //=================================================================================
241 void GeometryGUI_DistanceDlg::ConstructorsClicked(int constructorId)
244 myGeomGUI->EraseSimulationShape() ;
246 switch (constructorId)
250 GroupConstructor1->show();
251 myConstructorId = constructorId ;
252 myEditCurrentArgument = LineEditC1A1Shape ;
253 LineEditC1A2Shape->setText(tr("")) ;
254 Constructor1->setChecked( TRUE );
255 myOkShape1 = myOkShape2 = false ;
262 //=================================================================================
263 // function : ClickOnOk()
265 //=================================================================================
266 void GeometryGUI_DistanceDlg::ClickOnOk()
268 this->ClickOnApply() ;
269 this->ClickOnCancel() ;
274 //=================================================================================
275 // function : ClickOnApply()
277 //=================================================================================
278 void GeometryGUI_DistanceDlg::ClickOnApply()
281 myGeomGUI->EraseSimulationShape() ;
282 mySimulationTopoDs.Nullify() ;
283 myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
284 switch(myConstructorId)
288 if(myOkShape1 && myOkShape2) {
289 this->MakeDistanceSimulationAndDisplay(myShape1 ,myShape2) ;
300 //=================================================================================
301 // function : ClickOnCancel()
303 //=================================================================================
304 void GeometryGUI_DistanceDlg::ClickOnCancel()
307 myGeomGUI->EraseSimulationShape() ;
308 mySimulationTopoDs.Nullify() ;
309 disconnect( mySelection, 0, this, 0 );
310 myGeomGUI->ResetState() ;
317 //=================================================================================
318 // function : SelectionIntoArgument()
319 // purpose : Called when selection has changed
320 //=================================================================================
321 void GeometryGUI_DistanceDlg::SelectionIntoArgument()
323 myGeomGUI->EraseSimulationShape() ;
324 mySimulationTopoDs.Nullify() ;
326 LineEdit_Length->setText("") ;
327 myEditCurrentArgument->setText("") ; /* by default */
328 QString aString = ""; /* the name of selection */
330 int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
332 switch (myConstructorId)
336 if ( myEditCurrentArgument == LineEditC1A1Shape ) {
339 else if ( myEditCurrentArgument == LineEditC1A2Shape ) {
350 Standard_Boolean testResult ;
351 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
353 if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
356 if ( myEditCurrentArgument == LineEditC1A1Shape ) {
357 myGeomShape1 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
361 LineEditC1A1Shape->setText(aString) ;
364 else if ( myEditCurrentArgument == LineEditC1A2Shape ) {
365 myGeomShape2 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
369 LineEditC1A2Shape->setText(aString) ;
377 //=================================================================================
378 // function : SetEditCurrentArgument()
380 //=================================================================================
381 void GeometryGUI_DistanceDlg::SetEditCurrentArgument()
383 QPushButton* send = (QPushButton*)sender();
385 switch (myConstructorId)
387 case 0: /* default constructor */
389 if( send == SelectButtonC1A1Shape ) {
390 LineEditC1A1Shape->setFocus() ;
391 myEditCurrentArgument = LineEditC1A1Shape ;
393 else if(send == SelectButtonC1A2Shape) {
394 LineEditC1A2Shape->setFocus() ;
395 myEditCurrentArgument = LineEditC1A2Shape;
397 SelectionIntoArgument() ;
406 //=================================================================================
407 // function : LineEditReturnPressed()
409 //=================================================================================
410 void GeometryGUI_DistanceDlg::LineEditReturnPressed()
412 QLineEdit* send = (QLineEdit*)sender();
413 if( send == LineEditC1A1Shape )
414 myEditCurrentArgument = LineEditC1A1Shape ;
415 else if ( send == LineEditC1A2Shape )
416 myEditCurrentArgument = LineEditC1A2Shape ;
420 /* User name of object input management */
421 /* If successfull the selection is changed and signal emitted... */
422 /* so SelectionIntoArgument() is automatically called. */
423 const QString objectUserName = myEditCurrentArgument->text() ;
424 QWidget* thisWidget = (QWidget*)this ;
425 if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
426 myEditCurrentArgument->setText( objectUserName ) ;
433 //=================================================================================
434 // function : DeactivateActiveDialog()
436 //=================================================================================
437 void GeometryGUI_DistanceDlg::DeactivateActiveDialog()
439 if ( GroupConstructors->isEnabled() ) {
441 GroupConstructors->setEnabled(false) ;
442 GroupConstructor1->setEnabled(false) ;
443 GroupButtons->setEnabled(false) ;
444 disconnect( mySelection, 0, this, 0 );
451 //=================================================================================
452 // function : closeEvent()
454 //=================================================================================
455 void GeometryGUI_DistanceDlg::closeEvent( QCloseEvent* e )
457 this->ClickOnCancel() ; /* same than click on cancel button */
461 //=================================================================================
462 // function : enterEvent()
463 // purpose : when mouse enter onto the QWidget
464 //=================================================================================
465 void GeometryGUI_DistanceDlg::enterEvent( QEvent * )
467 if ( GroupConstructors->isEnabled() )
469 ActivateThisDialog() ;
474 //=================================================================================
475 // function : ActivateThisDialog()
477 //=================================================================================
478 void GeometryGUI_DistanceDlg::ActivateThisDialog()
480 /* Emit a signal to deactivate any active dialog */
481 myGeomGUI->EmitSignalDeactivateDialog() ;
482 GroupConstructors->setEnabled(true) ;
483 GroupConstructor1->setEnabled(true) ;
484 GroupButtons->setEnabled(true) ;
486 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
488 if( !mySimulationTopoDs.IsNull() )
489 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
496 //=================================================================================
497 // function : MakeDistanceSimulationAndDisplay()
499 //=================================================================================
500 void GeometryGUI_DistanceDlg::MakeDistanceSimulationAndDisplay(const TopoDS_Shape& S1, const TopoDS_Shape& S2)
502 LineEdit_Length->setText("") ;
504 myGeomGUI->EraseSimulationShape() ;
506 BRepExtrema_DistShapeShape dst( S1, S2 );
509 for (i=1; i<= dst.NbSolution(); i++) {
511 P1 = (dst.PointOnShape1(i));
512 P2 = (dst.PointOnShape2(i));
514 Standard_Real Dist = P1.Distance(P2);
516 BRepBuilderAPI_MakeVertex MakeVertex(P1);
517 mySimulationTopoDs = MakeVertex.Vertex();
518 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
520 LineEdit_Length->setText("0.0") ;
522 BRepBuilderAPI_MakeEdge MakeEdge(P1, P2);
523 mySimulationTopoDs = MakeEdge.Edge();
525 TopoDS_Vertex V1 = BRepBuilderAPI_MakeVertex(P1);
526 TopoDS_Vertex V2 = BRepBuilderAPI_MakeVertex(P2);
529 S.sprintf("%.1f",Dist);
530 Handle(AIS_LengthDimension) Distance = new AIS_LengthDimension (V1,V2, new Geom_Plane (0.,0.,1.,0.),
531 Dist, TCollection_ExtendedString(strdup(S)));
533 LineEdit_Length->setText(S) ;
535 if ( myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC )
538 OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
539 Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
540 ic->Display( Distance );
541 ic->UpdateCurrentViewer();
545 myGeomGUI->GetDesktop()->putInfo( tr( "GEOM_PRP_MIN_DIST" ) );
549 //=================================================================================
550 // function : EraseDistance()
552 //=================================================================================
553 void GeometryGUI_DistanceDlg::EraseDistance()
555 int count = myGeomGUI->GetActiveStudy()->getStudyFramesCount();
556 for ( int i = 0; i < count; i++ )
557 if (myGeomGUI->GetActiveStudy()->getStudyFrame(i)->getTypeView() == VIEW_OCC ) {
558 OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myGeomGUI->GetActiveStudy()->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
559 Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
561 AIS_ListOfInteractive L;
562 ic->DisplayedObjects(AIS_KOI_Relation,-1,L);
563 AIS_ListIteratorOfListOfInteractive ite(L);
565 ic->Remove( ite.Value() );
566 ic->UpdateCurrentViewer();