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_DistanceDlg.cxx
25 // Author : Nicolas REJNERI
30 #include "GeometryGUI_DistanceDlg.h"
32 #include "GeometryGUI.h"
33 #include "QAD_Application.h"
34 #include "QAD_Desktop.h"
35 #include "QAD_RightFrame.h"
36 #include "utilities.h"
38 #include "OCCViewer_Viewer3d.h"
39 #include "OCCViewer_ViewFrame.h"
41 // Open CASCADE Includes
42 #include <BRepBuilderAPI_MakeEdge.hxx>
43 #include <BRepBuilderAPI_MakeVertex.hxx>
44 #include <AIS_ListIteratorOfListOfInteractive.hxx>
47 #include <qmessagebox.h>
48 #include <qbuttongroup.h>
50 #include <qgroupbox.h>
52 #include <qlineedit.h>
53 #include <qpushbutton.h>
54 #include <qradiobutton.h>
58 #include <qwhatsthis.h>
64 //=================================================================================
65 // class : GeometryGUI_DistanceDlg()
66 // purpose : Constructs a GeometryGUI_DistanceDlg which is a child of 'parent', with the
67 // name 'name' and widget flags set to 'f'.
68 // The dialog will by default be modeless, unless you set 'modal' to
69 // TRUE to construct a modal dialog.
70 //=================================================================================
71 GeometryGUI_DistanceDlg::GeometryGUI_DistanceDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
72 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
74 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_MINDIST")));
75 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
78 setName( "GeometryGUI_DistanceDlg" );
80 setCaption( tr( "GEOM_MINDIST_TITLE" ) );
81 setSizeGripEnabled( TRUE );
83 GeometryGUI_DistanceDlgLayout = new QGridLayout( this );
84 GeometryGUI_DistanceDlgLayout->setSpacing( 6 );
85 GeometryGUI_DistanceDlgLayout->setMargin( 11 );
87 /***************************************************************/
88 GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
89 GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
90 GroupConstructors->setExclusive( TRUE );
91 GroupConstructors->setColumnLayout(0, Qt::Vertical );
92 GroupConstructors->layout()->setSpacing( 0 );
93 GroupConstructors->layout()->setMargin( 0 );
94 GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
95 GroupConstructorsLayout->setAlignment( Qt::AlignTop );
96 GroupConstructorsLayout->setSpacing( 6 );
97 GroupConstructorsLayout->setMargin( 11 );
98 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
99 GroupConstructorsLayout->addItem( spacer, 0, 1 );
100 Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
101 Constructor1->setText( tr( "" ) );
102 Constructor1->setPixmap( image0 );
103 Constructor1->setChecked( TRUE );
104 Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
105 GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
106 GeometryGUI_DistanceDlgLayout->addWidget( GroupConstructors, 0, 0 );
108 /***************************************************************/
109 GroupConstructor1 = new QGroupBox( this, "GroupConstructor1" );
110 GroupConstructor1->setTitle( tr( "GEOM_MINDIST_OBJ" ) );
111 GroupConstructor1->setColumnLayout(0, Qt::Vertical );
112 GroupConstructor1->layout()->setSpacing( 0 );
113 GroupConstructor1->layout()->setMargin( 0 );
114 GroupConstructor1Layout = new QGridLayout( GroupConstructor1->layout() );
115 GroupConstructor1Layout->setAlignment( Qt::AlignTop );
116 GroupConstructor1Layout->setSpacing( 6 );
117 GroupConstructor1Layout->setMargin( 11 );
118 LineEditC1A2Shape = new QLineEdit( GroupConstructor1, "LineEditC1A2Shape" );
119 LineEditC1A2Shape->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A2Shape->sizePolicy().hasHeightForWidth() ) );
120 GroupConstructor1Layout->addWidget( LineEditC1A2Shape, 1, 2 );
121 LineEditC1A1Shape = new QLineEdit( GroupConstructor1, "LineEditC1A1Shape" );
122 LineEditC1A1Shape->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1Shape->sizePolicy().hasHeightForWidth() ) );
123 GroupConstructor1Layout->addWidget( LineEditC1A1Shape, 0, 2 );
124 SelectButtonC1A1Shape = new QPushButton( GroupConstructor1, "SelectButtonC1A1Shape" );
125 SelectButtonC1A1Shape->setText( tr( "" ) );
126 SelectButtonC1A1Shape->setPixmap( image1 );
127 GroupConstructor1Layout->addWidget( SelectButtonC1A1Shape, 0, 1 );
128 SelectButtonC1A2Shape = new QPushButton( GroupConstructor1, "SelectButtonC1A2Shape" );
129 SelectButtonC1A2Shape->setText( tr( "" ) );
130 SelectButtonC1A2Shape->setPixmap( image1 );
131 GroupConstructor1Layout->addWidget( SelectButtonC1A2Shape, 1, 1 );
132 TextLabelC1A2Shape = new QLabel( GroupConstructor1, "TextLabelC1A2Shape" );
133 TextLabelC1A2Shape->setText( tr( "GEOM_OBJECT_I" ).arg("2") );
134 TextLabelC1A2Shape->setMinimumSize( QSize( 50, 0 ) );
135 TextLabelC1A2Shape->setFrameShape( QLabel::NoFrame );
136 TextLabelC1A2Shape->setFrameShadow( QLabel::Plain );
137 GroupConstructor1Layout->addWidget( TextLabelC1A2Shape, 1, 0 );
138 TextLabelC1A1Shape = new QLabel( GroupConstructor1, "TextLabelC1A1Shape" );
139 TextLabelC1A1Shape->setText( tr( "GEOM_OBJECT_I" ).arg("1") );
140 TextLabelC1A1Shape->setMinimumSize( QSize( 50, 0 ) );
141 TextLabelC1A1Shape->setFrameShape( QLabel::NoFrame );
142 TextLabelC1A1Shape->setFrameShadow( QLabel::Plain );
143 GroupConstructor1Layout->addWidget( TextLabelC1A1Shape, 0, 0 );
144 GeometryGUI_DistanceDlgLayout->addWidget( GroupConstructor1, 1, 0 );
146 TextLabel_Length = new QLabel( GroupConstructor1, "TextLabel_Length" );
147 TextLabel_Length->setText( tr( "GEOM_LENGTH" ) );
148 TextLabel_Length->setMinimumSize( QSize( 50, 0 ) );
149 TextLabel_Length->setFrameShape( QLabel::NoFrame );
150 TextLabel_Length->setFrameShadow( QLabel::Plain );
151 GroupConstructor1Layout->addWidget( TextLabel_Length, 2, 0 );
152 LineEdit_Length = new QLineEdit( GroupConstructor1, "LineEdit_Length" );
153 LineEdit_Length->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEdit_Length->sizePolicy().hasHeightForWidth() ) );
154 // LineEdit_Length->setEnabled( FALSE );
155 LineEdit_Length->setReadOnly( TRUE );
156 GroupConstructor1Layout->addWidget( LineEdit_Length, 2, 2 );
158 /***************************************************************/
159 GroupButtons = new QGroupBox( this, "GroupButtons" );
160 GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
161 GroupButtons->setTitle( tr( "" ) );
162 GroupButtons->setColumnLayout(0, Qt::Vertical );
163 GroupButtons->layout()->setSpacing( 0 );
164 GroupButtons->layout()->setMargin( 0 );
165 GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
166 GroupButtonsLayout->setAlignment( Qt::AlignTop );
167 GroupButtonsLayout->setSpacing( 6 );
168 GroupButtonsLayout->setMargin( 11 );
169 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
170 buttonCancel->setText( tr( "GEOM_BUT_CLOSE" ) );
171 buttonCancel->setAutoDefault( TRUE );
172 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
173 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
174 buttonApply->setText( tr( "GEOM_BUT_APPLY" ) );
175 buttonApply->setAutoDefault( TRUE );
176 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
177 QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
178 GroupButtonsLayout->addItem( spacer_9, 0, 2 );
179 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
180 buttonOk->setText( tr( "GEOM_BUT_OK" ) );
181 buttonOk->setAutoDefault( TRUE );
182 buttonOk->setDefault( TRUE );
183 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
184 GeometryGUI_DistanceDlgLayout->addWidget( GroupButtons, 2, 0 );
191 //=================================================================================
192 // function : ~GeometryGUI_DistanceDlg()
193 // purpose : Destroys the object and frees any allocated resources
194 //=================================================================================
195 GeometryGUI_DistanceDlg::~GeometryGUI_DistanceDlg()
197 /* no need to delete child widgets, Qt does it all for us */
198 this->destroy(TRUE, TRUE) ;
203 //=================================================================================
206 //=================================================================================
207 void GeometryGUI_DistanceDlg::Init( SALOME_Selection* Sel )
212 myConstructorId = 0 ;
214 myGeomGUI = GeometryGUI::GetGeometryGUI() ;
216 GroupConstructor1->show();
217 myConstructorId = 0 ;
218 myEditCurrentArgument = LineEditC1A1Shape ;
219 Constructor1->setChecked( TRUE );
220 myOkShape1 = myOkShape2 = false ;
222 myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
224 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
225 myGeom = GEOM::GEOM_Gen::_narrow(comp);
227 // TODO previous selection into argument ?
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( SelectButtonC1A1Shape, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
235 connect( SelectButtonC1A2Shape, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
237 connect( LineEditC1A1Shape, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
238 connect( LineEditC1A2Shape, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
240 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
241 connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
242 /* to close dialog if study change */
243 connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
245 /* Move widget on the botton right corner of main widget */
247 myGeomGUI->DefineDlgPosition( this, x, y ) ;
249 this->show() ; /* Displays Dialog */
256 //=================================================================================
257 // function : ConstructorsClicked()
258 // purpose : Radio button management
259 //=================================================================================
260 void GeometryGUI_DistanceDlg::ConstructorsClicked(int constructorId)
263 myGeomGUI->EraseSimulationShape() ;
265 switch (constructorId)
269 GroupConstructor1->show();
270 myConstructorId = constructorId ;
271 myEditCurrentArgument = LineEditC1A1Shape ;
272 LineEditC1A2Shape->setText(tr("")) ;
273 Constructor1->setChecked( TRUE );
274 myOkShape1 = myOkShape2 = false ;
281 //=================================================================================
282 // function : ClickOnOk()
284 //=================================================================================
285 void GeometryGUI_DistanceDlg::ClickOnOk()
287 this->ClickOnApply() ;
288 this->ClickOnCancel() ;
293 //=================================================================================
294 // function : ClickOnApply()
296 //=================================================================================
297 void GeometryGUI_DistanceDlg::ClickOnApply()
300 myGeomGUI->EraseSimulationShape() ;
301 mySimulationTopoDs.Nullify() ;
302 myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
303 switch(myConstructorId)
307 if(myOkShape1 && myOkShape2) {
308 this->MakeDistanceSimulationAndDisplay(myShape1 ,myShape2) ;
319 //=================================================================================
320 // function : ClickOnCancel()
322 //=================================================================================
323 void GeometryGUI_DistanceDlg::ClickOnCancel()
326 myGeomGUI->EraseSimulationShape() ;
327 mySimulationTopoDs.Nullify() ;
328 disconnect( mySelection, 0, this, 0 );
329 myGeomGUI->ResetState() ;
336 //=================================================================================
337 // function : SelectionIntoArgument()
338 // purpose : Called when selection has changed
339 //=================================================================================
340 void GeometryGUI_DistanceDlg::SelectionIntoArgument()
342 myGeomGUI->EraseSimulationShape() ;
343 mySimulationTopoDs.Nullify() ;
345 LineEdit_Length->setText("") ;
346 myEditCurrentArgument->setText("") ; /* by default */
347 QString aString = ""; /* the name of selection */
349 int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
351 switch (myConstructorId)
355 if ( myEditCurrentArgument == LineEditC1A1Shape ) {
358 else if ( myEditCurrentArgument == LineEditC1A2Shape ) {
369 Standard_Boolean testResult ;
370 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
372 if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
375 if ( myEditCurrentArgument == LineEditC1A1Shape ) {
376 myGeomShape1 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
380 LineEditC1A1Shape->setText(aString) ;
383 else if ( myEditCurrentArgument == LineEditC1A2Shape ) {
384 myGeomShape2 = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
388 LineEditC1A2Shape->setText(aString) ;
396 //=================================================================================
397 // function : SetEditCurrentArgument()
399 //=================================================================================
400 void GeometryGUI_DistanceDlg::SetEditCurrentArgument()
402 QPushButton* send = (QPushButton*)sender();
404 switch (myConstructorId)
406 case 0: /* default constructor */
408 if( send == SelectButtonC1A1Shape ) {
409 LineEditC1A1Shape->setFocus() ;
410 myEditCurrentArgument = LineEditC1A1Shape ;
412 else if(send == SelectButtonC1A2Shape) {
413 LineEditC1A2Shape->setFocus() ;
414 myEditCurrentArgument = LineEditC1A2Shape;
416 SelectionIntoArgument() ;
425 //=================================================================================
426 // function : LineEditReturnPressed()
428 //=================================================================================
429 void GeometryGUI_DistanceDlg::LineEditReturnPressed()
431 QLineEdit* send = (QLineEdit*)sender();
432 if( send == LineEditC1A1Shape )
433 myEditCurrentArgument = LineEditC1A1Shape ;
434 else if ( send == LineEditC1A2Shape )
435 myEditCurrentArgument = LineEditC1A2Shape ;
439 /* User name of object input management */
440 /* If successfull the selection is changed and signal emitted... */
441 /* so SelectionIntoArgument() is automatically called. */
442 const QString objectUserName = myEditCurrentArgument->text() ;
443 QWidget* thisWidget = (QWidget*)this ;
444 if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
445 myEditCurrentArgument->setText( objectUserName ) ;
452 //=================================================================================
453 // function : DeactivateActiveDialog()
455 //=================================================================================
456 void GeometryGUI_DistanceDlg::DeactivateActiveDialog()
458 if ( GroupConstructors->isEnabled() ) {
460 GroupConstructors->setEnabled(false) ;
461 GroupConstructor1->setEnabled(false) ;
462 GroupButtons->setEnabled(false) ;
463 disconnect( mySelection, 0, this, 0 );
470 //=================================================================================
471 // function : closeEvent()
473 //=================================================================================
474 void GeometryGUI_DistanceDlg::closeEvent( QCloseEvent* e )
476 this->ClickOnCancel() ; /* same than click on cancel button */
480 //=================================================================================
481 // function : enterEvent()
482 // purpose : when mouse enter onto the QWidget
483 //=================================================================================
484 void GeometryGUI_DistanceDlg::enterEvent( QEvent * )
486 if ( GroupConstructors->isEnabled() )
488 ActivateThisDialog() ;
493 //=================================================================================
494 // function : ActivateThisDialog()
496 //=================================================================================
497 void GeometryGUI_DistanceDlg::ActivateThisDialog()
499 /* Emit a signal to deactivate any active dialog */
500 myGeomGUI->EmitSignalDeactivateDialog() ;
501 GroupConstructors->setEnabled(true) ;
502 GroupConstructor1->setEnabled(true) ;
503 GroupButtons->setEnabled(true) ;
505 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
507 if( !mySimulationTopoDs.IsNull() )
508 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
515 //=================================================================================
516 // function : MakeDistanceSimulationAndDisplay()
518 //=================================================================================
519 void GeometryGUI_DistanceDlg::MakeDistanceSimulationAndDisplay(const TopoDS_Shape& S1, const TopoDS_Shape& S2)
521 LineEdit_Length->setText("") ;
523 myGeomGUI->EraseSimulationShape() ;
525 BRepExtrema_DistShapeShape dst( S1, S2 );
528 for (i=1; i<= dst.NbSolution(); i++) {
530 P1 = (dst.PointOnShape1(i));
531 P2 = (dst.PointOnShape2(i));
533 Standard_Real Dist = P1.Distance(P2);
535 BRepBuilderAPI_MakeVertex MakeVertex(P1);
536 mySimulationTopoDs = MakeVertex.Vertex();
537 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
539 LineEdit_Length->setText("0.0") ;
541 BRepBuilderAPI_MakeEdge MakeEdge(P1, P2);
542 mySimulationTopoDs = MakeEdge.Edge();
544 TopoDS_Vertex V1 = BRepBuilderAPI_MakeVertex(P1);
545 TopoDS_Vertex V2 = BRepBuilderAPI_MakeVertex(P2);
548 S.sprintf("%.1f",Dist);
549 Handle(AIS_LengthDimension) Distance = new AIS_LengthDimension (V1,V2, new Geom_Plane (0.,0.,1.,0.),
550 Dist, TCollection_ExtendedString(strdup(S)));
552 LineEdit_Length->setText(S) ;
554 if ( myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC )
557 OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
558 Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
559 ic->Display( Distance );
560 ic->UpdateCurrentViewer();
564 myGeomGUI->GetDesktop()->putInfo( tr( "GEOM_PRP_MIN_DIST" ) );
568 //=================================================================================
569 // function : EraseDistance()
571 //=================================================================================
572 void GeometryGUI_DistanceDlg::EraseDistance()
574 int count = myGeomGUI->GetActiveStudy()->getStudyFramesCount();
575 for ( int i = 0; i < count; i++ )
576 if (myGeomGUI->GetActiveStudy()->getStudyFrame(i)->getTypeView() == VIEW_OCC ) {
577 OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)myGeomGUI->GetActiveStudy()->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
578 Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
580 AIS_ListOfInteractive L;
581 ic->DisplayedObjects(AIS_KOI_Relation,-1,L);
582 AIS_ListIteratorOfListOfInteractive ite(L);
584 ic->Remove( ite.Value() );
585 ic->UpdateCurrentViewer();