2 // File : GeometryGUI_OrientationDlg.cxx
4 // Author : Lucien PIGNOLONI
6 // Module : GeometryGUI
7 // Copyright : OPEN CASCADE
10 #include "GeometryGUI_OrientationDlg.h"
12 #include "GeometryGUI.h"
13 #include "QAD_Application.h"
14 #include "QAD_Desktop.h"
15 #include "QAD_Config.h"
16 #include "utilities.h"
18 #include <TopoDS_Compound.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepBuilderAPI_MakeEdge.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <TopExp_Explorer.hxx>
24 #include <qbuttongroup.h>
25 #include <qcheckbox.h>
26 #include <qgroupbox.h>
28 #include <qlineedit.h>
29 #include <qpushbutton.h>
30 #include <qradiobutton.h>
34 #include <qvalidator.h>
35 #include <qwhatsthis.h>
39 //=================================================================================
40 // class : GeometryGUI_OrientationDlg()
41 // purpose : Constructs a GeometryGUI_OrientationDlg which is a child of 'parent', with the
42 // name 'name' and widget flags set to 'f'.
43 // The dialog will by default be modeless, unless you set 'modal' to
44 // TRUE to construct a modal dialog.
45 //=================================================================================
46 GeometryGUI_OrientationDlg::GeometryGUI_OrientationDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
47 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
49 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_DLG_ORIENTATION")));
50 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_SELECT")));
52 setName( "GeometryGUI_OrientationDlg" );
54 setCaption( tr( "GEOM_ORIENTATION_TITLE" ) );
55 setSizeGripEnabled( TRUE );
56 GeometryGUI_OrientationDlgLayout = new QGridLayout( this );
57 GeometryGUI_OrientationDlgLayout->setSpacing( 6 );
58 GeometryGUI_OrientationDlgLayout->setMargin( 11 );
60 /***************************************************************/
61 GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
62 GroupConstructors->setTitle( tr( "GEOM_ORIENTATION" ) );
63 GroupConstructors->setExclusive( TRUE );
64 GroupConstructors->setColumnLayout(0, Qt::Vertical );
65 GroupConstructors->layout()->setSpacing( 0 );
66 GroupConstructors->layout()->setMargin( 0 );
67 GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
68 GroupConstructorsLayout->setAlignment( Qt::AlignTop );
69 GroupConstructorsLayout->setSpacing( 6 );
70 GroupConstructorsLayout->setMargin( 11 );
71 Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
72 Constructor1->setText( tr( "" ) );
73 Constructor1->setPixmap( image0 );
74 Constructor1->setChecked( TRUE );
75 Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
76 Constructor1->setMinimumSize( QSize( 50, 0 ) );
77 GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
78 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
79 GroupConstructorsLayout->addItem( spacer, 0, 1 );
80 GeometryGUI_OrientationDlgLayout->addWidget( GroupConstructors, 0, 0 );
82 /***************************************************************/
83 GroupButtons = new QGroupBox( this, "GroupButtons" );
84 GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
85 GroupButtons->setTitle( tr( "" ) );
86 GroupButtons->setColumnLayout(0, Qt::Vertical );
87 GroupButtons->layout()->setSpacing( 0 );
88 GroupButtons->layout()->setMargin( 0 );
89 GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
90 GroupButtonsLayout->setAlignment( Qt::AlignTop );
91 GroupButtonsLayout->setSpacing( 6 );
92 GroupButtonsLayout->setMargin( 11 );
93 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
94 buttonCancel->setText( tr( "GEOM_BUT_CLOSE" ) );
95 buttonCancel->setAutoDefault( TRUE );
96 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
97 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
98 buttonApply->setText( tr( "GEOM_BUT_APPLY" ) );
99 buttonApply->setAutoDefault( TRUE );
100 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
101 QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
102 GroupButtonsLayout->addItem( spacer_9, 0, 2 );
103 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
104 buttonOk->setText( tr( "GEOM_BUT_OK" ) );
105 buttonOk->setAutoDefault( TRUE );
106 buttonOk->setDefault( TRUE );
107 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
108 GeometryGUI_OrientationDlgLayout->addWidget( GroupButtons, 2, 0 );
110 /***************************************************************/
111 GroupC1 = new QGroupBox( this, "GroupC1" );
112 GroupC1->setTitle( tr( "GEOM_ARGUMENTS" ) );
113 GroupC1->setMinimumSize( QSize( 0, 0 ) );
114 GroupC1->setFrameShape( QGroupBox::Box );
115 GroupC1->setFrameShadow( QGroupBox::Sunken );
116 GroupC1->setColumnLayout(0, Qt::Vertical );
117 GroupC1->layout()->setSpacing( 0 );
118 GroupC1->layout()->setMargin( 0 );
119 GroupC1Layout = new QGridLayout( GroupC1->layout() );
120 GroupC1Layout->setAlignment( Qt::AlignTop );
121 GroupC1Layout->setSpacing( 6 );
122 GroupC1Layout->setMargin( 11 );
123 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
124 GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
126 /* Spin box construction */
127 SpinBox_C1A2 = new GeometryGUI_SpinBox( GroupC1, "GeomSpinBox_C1A2" ) ;
128 GroupC1Layout->addWidget( SpinBox_C1A2, 1, 2 );
130 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
131 SelectButtonC1A1->setText( tr( "" ) );
132 SelectButtonC1A1->setPixmap( image1 );
133 SelectButtonC1A1->setToggleButton( FALSE );
134 GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
135 TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
136 TextLabelC1A1->setText( tr( "GEOM_OBJECT" ) );
137 TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
138 TextLabelC1A1->setFrameShape( QLabel::NoFrame );
139 TextLabelC1A1->setFrameShadow( QLabel::Plain );
140 GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
141 TextLabelC1A2 = new QLabel( GroupC1, "TextLabelC1A2" );
142 TextLabelC1A2->setText( tr( "GEOM_VECTOR_LENGTH" ) );
143 TextLabelC1A2->setMinimumSize( QSize( 50, 0 ) );
144 GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
145 CheckBoxReverse = new QCheckBox( GroupC1, "CheckBoxReverse" );
146 CheckBoxReverse->setText( tr( "GEOM_ORIENTATION_OPT" ) );
147 GroupC1Layout->addMultiCellWidget( CheckBoxReverse, 2, 2, 0, 2 );
148 GeometryGUI_OrientationDlgLayout->addWidget( GroupC1, 1, 0 );
149 /***************************************************************/
151 Init(Sel) ; /* Initialisations */
155 //=================================================================================
156 // function : ~GeometryGUI_OrientationDlg()
157 // purpose : Destroys the object and frees any allocated resources
158 //=================================================================================
159 GeometryGUI_OrientationDlg::~GeometryGUI_OrientationDlg()
161 // no need to delete child widgets, Qt does it all for us
165 //=================================================================================
168 //=================================================================================
169 void GeometryGUI_OrientationDlg::Init( SALOME_Selection* Sel )
173 QString St = QAD_CONFIG->getSetting( "Geometry:SettingsGeomStep" ) ;
174 step = St.toDouble() ;
176 /* min, max, step and decimals for spin boxes */
177 SpinBox_C1A2->RangeStepAndValidator( 0.001, 999.999, step, 3 ) ;
178 SpinBox_C1A2->SetValue( 25.0 ) ; /* = myLength */
181 myConstructorId = 0 ;
182 Constructor1->setChecked( TRUE );
183 CheckBoxReverse->setChecked( FALSE );
184 myEditCurrentArgument = LineEditC1A1 ;
186 myGeomGUI = GeometryGUI::GetGeometryGUI() ;
191 mySimulationTopoDs.Nullify() ;
193 myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
195 // TODO : previous selection into argument ?
197 /* Filter definitions */
198 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "Geometry");
199 myGeom = GEOM::GEOM_Gen::_narrow(comp);
201 /* signals and slots connections */
202 connect( buttonOk, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
203 connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
204 connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
205 connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
206 connect( SelectButtonC1A1, SIGNAL (clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
207 connect( CheckBoxReverse, SIGNAL (stateChanged(int) ), this, SLOT( ReverseOrientation(int) ) ) ;
209 connect( LineEditC1A1, SIGNAL ( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
210 connect( SpinBox_C1A2, SIGNAL ( valueChanged( double) ), this, SLOT( ValueChangedInSpinBox( double) ) ) ;
212 connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
213 connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
214 /* to close dialog if study change */
215 connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
217 /* Move widget on the botton right corner of main widget */
219 myGeomGUI->DefineDlgPosition( this, x, y ) ;
221 this->show() ; /* displays Dialog */
227 //=================================================================================
228 // function : ConstructorsClicked()
229 // purpose : Radio button management
230 //=================================================================================
231 void GeometryGUI_OrientationDlg::ConstructorsClicked(int constructorId)
236 //=================================================================================
237 // function : ClickOnOk()
239 //=================================================================================
240 void GeometryGUI_OrientationDlg::ClickOnOk()
242 this->ClickOnApply() ;
243 this->ClickOnCancel() ;
248 //=================================================================================
249 // function : ClickOnApply()
251 //=================================================================================
252 void GeometryGUI_OrientationDlg::ClickOnApply()
254 /* Leave simulation display in this method ! */
255 myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
257 switch(myConstructorId)
261 if(myOkShape && CheckBoxReverse->isChecked() && myOkLength == true ) {
262 myGeomGUI->MakeOrientationChangeAndDisplay( myGeomShape ) ;
263 CheckBoxReverse->setChecked( FALSE );
273 //=================================================================================
274 // function : ClickOnCancel()
276 //=================================================================================
277 void GeometryGUI_OrientationDlg::ClickOnCancel()
279 myGeomGUI->EraseSimulationShape() ;
280 mySimulationTopoDs.Nullify() ;
281 disconnect( mySelection, 0, this, 0 );
282 myGeomGUI->ResetState() ;
287 //=================================================================================
288 // function : LineEditReturnPressed()
290 //=================================================================================
291 void GeometryGUI_OrientationDlg::LineEditReturnPressed()
293 QLineEdit* send = (QLineEdit*)sender();
294 if( send == LineEditC1A1 )
295 myEditCurrentArgument = LineEditC1A1 ;
299 /* User name of object input management */
300 /* If successfull the selection is changed and signal emitted... */
301 /* so SelectionIntoArgument() is automatically called. */
302 const QString objectUserName = myEditCurrentArgument->text() ;
303 QWidget* thisWidget = (QWidget*)this ;
304 if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
305 myEditCurrentArgument->setText( objectUserName ) ;
312 //=================================================================================
313 // function : SelectionIntoArgument()
314 // purpose : Called when selection as changed or other case
315 //=================================================================================
316 void GeometryGUI_OrientationDlg::SelectionIntoArgument()
318 myGeomGUI->EraseSimulationShape() ;
319 this->mySimulationTopoDs.Nullify() ;
321 /* Name of future selection */
322 QString aString = "";
324 int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
326 if ( myEditCurrentArgument == LineEditC1A1 ) {
327 LineEditC1A1->setText("") ;
328 this->myOkShape = false ;
335 Standard_Boolean testResult ;
336 Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
337 if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
341 if ( myEditCurrentArgument == LineEditC1A1 ) {
342 myGeomShape = myGeomGUI->ConvertIOinGEOMShape(IO, testResult) ;
345 LineEditC1A1->setText(aString) ;
350 if( myOkShape && myOkLength ) {
351 MakeOrientationSimulationAndDisplay( this->myShape, this->myLength ) ;
357 //=================================================================================
358 // function : SetEditCurrentArgument()
360 //=================================================================================
361 void GeometryGUI_OrientationDlg::SetEditCurrentArgument()
363 QPushButton* send = (QPushButton*)sender();
364 switch (myConstructorId)
366 case 0: /* default constructor */
368 if(send == SelectButtonC1A1) {
369 LineEditC1A1->setFocus() ;
370 myEditCurrentArgument = LineEditC1A1 ;
372 SelectionIntoArgument() ;
380 //=================================================================================
381 // function : ValueChangedInSpinBox()
383 //=================================================================================
384 void GeometryGUI_OrientationDlg::ValueChangedInSpinBox( double newValue )
386 myGeomGUI->EraseSimulationShape() ;
387 mySimulationTopoDs.Nullify() ;
389 QObject* send = (QObject*)sender() ;
390 if( send == SpinBox_C1A2 ) {
391 this->myLength = newValue ;
395 if ( myConstructorId == 0 && myOkShape && myOkLength == true ) {
396 MakeOrientationSimulationAndDisplay( this->myShape, this->myLength ) ;
402 //=================================================================================
403 // function : DeactivateActiveDialog()
405 //=================================================================================
406 void GeometryGUI_OrientationDlg::DeactivateActiveDialog()
408 if ( GroupConstructors->isEnabled() ) {
409 GroupConstructors->setEnabled(false) ;
410 GroupButtons->setEnabled(false) ;
411 GroupC1->setEnabled(false) ;
412 disconnect( mySelection, 0, this, 0 );
413 myGeomGUI->EraseSimulationShape() ;
419 //=================================================================================
420 // function : ActivateThisDialog()
422 //=================================================================================
423 void GeometryGUI_OrientationDlg::ActivateThisDialog()
425 /* Emit a signal to deactivate the active dialog */
426 myGeomGUI->EmitSignalDeactivateDialog() ;
427 GroupConstructors->setEnabled(true) ;
428 GroupButtons->setEnabled(true) ;
429 GroupC1->setEnabled(true) ;
431 connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
432 if( !mySimulationTopoDs.IsNull() )
433 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
439 //=================================================================================
440 // function : enterEvent()
442 //=================================================================================
443 void GeometryGUI_OrientationDlg::enterEvent(QEvent* e)
445 if ( GroupConstructors->isEnabled() )
447 ActivateThisDialog() ;
453 //=================================================================================
454 // function : closeEvent()
456 //=================================================================================
457 void GeometryGUI_OrientationDlg::closeEvent( QCloseEvent* e )
459 /* same than click on cancel button */
460 this->ClickOnCancel() ;
466 //===========================================================================================
467 // function : ReverseOrientation()
469 //===========================================================================================
470 void GeometryGUI_OrientationDlg::ReverseOrientation(int state)
472 if( this->myOkShape && myOkLength == true ) {
473 MakeOrientationSimulationAndDisplay( this->myShape, this-> myLength ) ;
479 //===========================================================================================
480 // function : MakeOrientationSimulationAndDisplay()
481 // purpose : Create 'aCompound1' and 'aCompound2' each contains edges oriented
482 // : respectively FORWARD and REVERSE for all faces of 'aTopoDS'
483 // : These edges represent normal vectors on faces of 'aTopoDS'
484 // : For a unique edge an arrow is displayed to show its orientation.
485 //===========================================================================================
486 void GeometryGUI_OrientationDlg::MakeOrientationSimulationAndDisplay(const TopoDS_Shape& aTopoDS, Standard_Real length )
488 myGeomGUI->EraseSimulationShape() ;
489 mySimulationTopoDs.Nullify() ;
491 TopoDS_Compound aCompound1, aCompound2 ;
492 TopoDS_Compound NullComp ;
493 BRep_Builder aBuilder1, aBuilder2;
494 aCompound1 = aCompound2 = NullComp ;
495 aBuilder1.MakeCompound(aCompound1) ;
496 aBuilder2.MakeCompound(aCompound2) ;
498 if( aTopoDS.IsNull() )
501 /* Case of an edge */
502 if( aTopoDS.ShapeType() == TopAbs_EDGE ) {
503 /* Try to display a cone simulation shape to show direction of edge */
504 TopoDS_Shape tmpShape = aTopoDS ;
505 if( this->CheckBoxReverse->isChecked() ) {
506 if( aTopoDS.Orientation() == TopAbs_FORWARD)
507 tmpShape.Orientation(TopAbs_REVERSED) ;
509 tmpShape.Orientation(TopAbs_FORWARD) ;
511 if( myGeomGUI->CreateArrowForLinearEdge( tmpShape, mySimulationTopoDs ) ) {
512 myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ;
520 TopExp_Explorer ex( aTopoDS, TopAbs_FACE );
524 const TopoDS_Face& F = TopoDS::Face(ex.Current());
525 /* find the center of the minmax */
526 BRepAdaptor_Surface SF(F);
527 Standard_Real u, v, x;
529 u = SF.FirstUParameter();
530 x = SF.LastUParameter();
531 if ( Precision::IsInfinite(u) ) {
532 if( Precision::IsInfinite(x) ) u = 0.0 ; else u = x ;
534 else if ( !Precision::IsInfinite(x) )
537 v = SF.FirstVParameter();
538 x = SF.LastVParameter();
539 if ( Precision::IsInfinite(v) ) {
540 if ( Precision::IsInfinite(x) ) v = 0.0 ; else v = x ;
542 else if ( !Precision::IsInfinite(x) )
545 SF.D1( u, v, P1, V1, V2 );
549 V.Multiply( length/x );
551 V.SetCoord( length/2.0, 0.0, 0.0 ) ;
552 MESSAGE ("Null normal in Orientation " << endl ) ;
556 /* test orientation of each face and displays forward (aCompound1) */
557 if( F.Orientation() == TopAbs_FORWARD )
561 BRepBuilderAPI_MakeEdge anEdge(P1, P2) ;
562 aBuilder1.Add( aCompound1, anEdge ) ;
565 /* test orientation of each face and displays forward (aCompound2) */
566 if( F.Orientation() == TopAbs_FORWARD )
570 anEdge = BRepBuilderAPI_MakeEdge(P1, P2) ;
571 aBuilder2.Add( aCompound2, anEdge ) ;
577 /* display simulation compounds */
578 if( test > 0 && this->CheckBoxReverse->isChecked() ) {
579 mySimulationTopoDs = aCompound1 ;
581 else if ( test > 0 && !CheckBoxReverse->isChecked() ) {
582 mySimulationTopoDs = aCompound2 ;
584 if(!mySimulationTopoDs.IsNull() )
585 myGeomGUI->DisplaySimulationShape(mySimulationTopoDs) ;