Salome HOME
NRI : Replace Geometry by GEOM.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_BndBoxDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_BndBoxDlg.cxx
3 //  Created   : Mon Mar 04 13:16:16 2002
4 //  Author    : Nicolas REJNERI
5
6 //  Project   : SALOME
7 //  Module    : GEOMGUI
8 //  Copyright : Open CASCADE 2002
9 //  $Header$
10
11
12 #include "GeometryGUI_BndBoxDlg.h"
13
14 #include "GeometryGUI.h"
15 #include "QAD_Application.h"
16 #include "QAD_Desktop.h"
17 #include "utilities.h"
18
19 #include <BRepPrimAPI_MakeBox.hxx>
20 #include <BRepBndLib.hxx>
21
22 #include <qbuttongroup.h>
23 #include <qgroupbox.h>
24 #include <qlabel.h>
25 #include <qlineedit.h>
26 #include <qpushbutton.h>
27 #include <qradiobutton.h>
28 #include <qlayout.h>
29 #include <qvariant.h>
30 #include <qtooltip.h>
31 #include <qwhatsthis.h>
32 #include <qimage.h>
33 #include <qpixmap.h>
34
35 //=================================================================================
36 // class    : GeometryGUI_BndBoxDlg()
37 // purpose  : Constructs a GeometryGUI_BndBoxDlg which is a child of 'parent', with the 
38 //            name 'name' and widget flags set to 'f'.
39 //            The dialog will by default be modeless, unless you set 'modal' to
40 //            TRUE to construct a modal dialog.
41 //=================================================================================
42 GeometryGUI_BndBoxDlg::GeometryGUI_BndBoxDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
43     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
44 {
45     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_BOUNDING_BOX")));
46     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
47
48     if ( !name )
49         setName( "GeometryGUI_BndBoxDlg" );
50     resize( 303, 275 ); 
51     setCaption( tr( "GEOM_BNDBOX_TITLE"  ) );
52     setSizeGripEnabled( TRUE );
53     GeometryGUI_BndBoxDlgLayout = new QGridLayout( this ); 
54     GeometryGUI_BndBoxDlgLayout->setSpacing( 6 );
55     GeometryGUI_BndBoxDlgLayout->setMargin( 11 );
56     
57     /***************************************************************/
58     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
59     GroupConstructors->setTitle( tr( "GEOM_BNDBOX"  ) );
60     GroupConstructors->setExclusive( TRUE );
61     GroupConstructors->setColumnLayout(0, Qt::Vertical );
62     GroupConstructors->layout()->setSpacing( 0 );
63     GroupConstructors->layout()->setMargin( 0 );
64     GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
65     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
66     GroupConstructorsLayout->setSpacing( 6 );
67     GroupConstructorsLayout->setMargin( 11 );
68     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
69     Constructor1->setText( tr( ""  ) );
70     Constructor1->setPixmap( image0 );
71     Constructor1->setChecked( TRUE );
72     Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, 
73                                               Constructor1->sizePolicy().hasHeightForWidth() ) );
74     Constructor1->setMinimumSize( QSize( 60, 0 ) );
75     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
76     QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
77     GroupConstructorsLayout->addItem( spacer, 0, 1 );
78     GeometryGUI_BndBoxDlgLayout->addWidget( GroupConstructors, 0, 0 );
79
80     /***************************************************************/
81     GroupConstructor1 = new QGroupBox( this, "GroupConstructor1" );
82     GroupConstructor1->setTitle( tr( "GEOM_BNDBOX_OBJDIM"  ) );
83     GroupConstructor1->setColumnLayout(0, Qt::Vertical );
84     GroupConstructor1->layout()->setSpacing( 0 );
85     GroupConstructor1->layout()->setMargin( 0 );
86     GroupConstructor1Layout = new QGridLayout( GroupConstructor1->layout() );
87     GroupConstructor1Layout->setAlignment( Qt::AlignTop );
88     GroupConstructor1Layout->setSpacing( 6 );
89     GroupConstructor1Layout->setMargin( 11 );
90     LineEditC1A1 = new QLineEdit( GroupConstructor1, "LineEditC1A1" );
91     LineEditC1A1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0, LineEditC1A1->sizePolicy().hasHeightForWidth() ) );
92 //    GroupConstructor1Layout->addWidget( LineEditC1A1, 0, 2 );
93     SelectButtonC1A1 = new QPushButton( GroupConstructor1, "SelectButtonC1A1" );
94     SelectButtonC1A1->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
95     SelectButtonC1A1->setText( tr( ""  ) );
96     SelectButtonC1A1->setPixmap( image1 );
97 //    GroupConstructor1Layout->addWidget( SelectButtonC1A1, 0, 1 );
98
99     TextLabelC1A1 = new QLabel( GroupConstructor1, "TextLabelC1A1" );
100     TextLabelC1A1->setText( tr( "GEOM_OBJECT"  ) );
101     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
102     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
103     TextLabelC1A1->setFrameShadow( QLabel::Plain );
104 //    GroupConstructor1Layout->addWidget( TextLabelC1A1, 0, 0 );
105
106     QHBoxLayout* bl = new QHBoxLayout;
107     bl->setMargin(0); bl->setSpacing(6);
108     bl->addWidget(TextLabelC1A1); bl->addWidget(SelectButtonC1A1); bl->addWidget(LineEditC1A1);
109     GroupConstructor1Layout->addMultiCellLayout(bl, 0, 0, 0, 2);
110
111     TextLabel_Min = new QLabel( GroupConstructor1, "TextLabel_Min" );
112     TextLabel_Min->setText( tr( "GEOM_MIN"  ) );
113     TextLabel_Min->setMinimumSize( QSize( 50, 0 ) );
114     TextLabel_Min->setFrameShape( QLabel::NoFrame );
115     TextLabel_Min->setFrameShadow( QLabel::Plain );
116     GroupConstructor1Layout->addWidget( TextLabel_Min, 1, 1 );
117
118     TextLabel_Max = new QLabel( GroupConstructor1, "TextLabel_Max" );
119     TextLabel_Max->setText( tr( "GEOM_MAX"  ) );
120     TextLabel_Max->setMinimumSize( QSize( 50, 0 ) );
121     TextLabel_Max->setFrameShape( QLabel::NoFrame );
122     TextLabel_Max->setFrameShadow( QLabel::Plain );
123     GroupConstructor1Layout->addWidget( TextLabel_Max, 1, 2 );
124
125     TextLabel_X = new QLabel( GroupConstructor1, "TextLabel_X" );
126     TextLabel_X->setText( tr( "GEOM_X"  ) );
127     TextLabel_X->setMinimumSize( QSize( 50, 0 ) );
128     TextLabel_X->setFrameShape( QLabel::NoFrame );
129     TextLabel_X->setFrameShadow( QLabel::Plain );
130     GroupConstructor1Layout->addWidget( TextLabel_X, 2, 0 );
131     LineEdit_MinX = new QLineEdit( GroupConstructor1, "LineEdit_MinX" );
132     LineEdit_MinX->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0,
133                                                  LineEdit_MinX->sizePolicy().hasHeightForWidth() ) );
134     //LineEdit_MinX->setEnabled( FALSE );
135     LineEdit_MinX->setReadOnly( TRUE );
136     GroupConstructor1Layout->addWidget( LineEdit_MinX, 2, 1 );
137     LineEdit_MaxX = new QLineEdit( GroupConstructor1, "LineEdit_MaxX" );
138     LineEdit_MaxX->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0,
139                                                  LineEdit_MaxX->sizePolicy().hasHeightForWidth() ) );
140     //LineEdit_MaxX->setEnabled( FALSE );
141     LineEdit_MaxX->setReadOnly( TRUE );
142     GroupConstructor1Layout->addWidget( LineEdit_MaxX, 2, 2 );
143
144     TextLabel_Y = new QLabel( GroupConstructor1, "TextLabel_Y" );
145     TextLabel_Y->setText( tr( "GEOM_Y"  ) );
146     TextLabel_Y->setMinimumSize( QSize( 50, 0 ) );
147     TextLabel_Y->setFrameShape( QLabel::NoFrame );
148     TextLabel_Y->setFrameShadow( QLabel::Plain );
149     GroupConstructor1Layout->addWidget( TextLabel_Y, 3, 0 );
150     LineEdit_MinY = new QLineEdit( GroupConstructor1, "LineEdit_MinY" );
151     LineEdit_MinY->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0,
152                                                  LineEdit_MinY->sizePolicy().hasHeightForWidth() ) );
153     //LineEdit_MinY->setEnabled( FALSE );
154     LineEdit_MinY->setReadOnly( TRUE );
155     GroupConstructor1Layout->addWidget( LineEdit_MinY, 3, 1 );
156     LineEdit_MaxY = new QLineEdit( GroupConstructor1, "LineEdit_MaxY" );
157     LineEdit_MaxY->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0,
158                                                  LineEdit_MaxY->sizePolicy().hasHeightForWidth() ) );
159     //LineEdit_MaxY->setEnabled( FALSE );
160     LineEdit_MaxY->setReadOnly( TRUE );
161     GroupConstructor1Layout->addWidget( LineEdit_MaxY, 3, 2 );
162
163     TextLabel_Z = new QLabel( GroupConstructor1, "TextLabel_Z" );
164     TextLabel_Z->setText( tr( "GEOM_Z"  ) );
165     TextLabel_Z->setMinimumSize( QSize( 50, 0 ) );
166     TextLabel_Z->setFrameShape( QLabel::NoFrame );
167     TextLabel_Z->setFrameShadow( QLabel::Plain );
168     GroupConstructor1Layout->addWidget( TextLabel_Z, 4, 0 );
169     LineEdit_MinZ = new QLineEdit( GroupConstructor1, "LineEdit_MinZ" );
170     LineEdit_MinZ->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0,
171                                                  LineEdit_MinZ->sizePolicy().hasHeightForWidth() ) );
172     //LineEdit_MinZ->setEnabled( FALSE );
173     LineEdit_MinZ->setReadOnly( TRUE );
174     GroupConstructor1Layout->addWidget( LineEdit_MinZ, 4, 1 );
175     LineEdit_MaxZ = new QLineEdit( GroupConstructor1, "LineEdit_MaxZ" );
176     LineEdit_MaxZ->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)3, (QSizePolicy::SizeType)0,
177                                                  LineEdit_MaxZ->sizePolicy().hasHeightForWidth() ) );
178     //LineEdit_MaxZ->setEnabled( FALSE );
179     LineEdit_MaxZ->setReadOnly( TRUE );
180     GroupConstructor1Layout->addWidget( LineEdit_MaxZ, 4, 2 );
181
182     GeometryGUI_BndBoxDlgLayout->addWidget( GroupConstructor1, 1, 0 );
183
184     /***************************************************************/
185     GroupButtons = new QGroupBox( this, "GroupButtons" );
186     GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
187     GroupButtons->setTitle( tr( ""  ) );
188     GroupButtons->setColumnLayout(0, Qt::Vertical );
189     GroupButtons->layout()->setSpacing( 0 );
190     GroupButtons->layout()->setMargin( 0 );
191     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
192     GroupButtonsLayout->setAlignment( Qt::AlignTop );
193     GroupButtonsLayout->setSpacing( 6 );
194     GroupButtonsLayout->setMargin( 11 );
195     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
196     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
197     buttonCancel->setAutoDefault( TRUE );
198     GroupButtonsLayout->addWidget( buttonCancel, 0, 1 );
199
200     QSpacerItem* spacer_8 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
201     GroupButtonsLayout->addItem( spacer_8, 0, 0 );
202     QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
203     GroupButtonsLayout->addItem( spacer_9, 0, 2 );
204
205     GeometryGUI_BndBoxDlgLayout->addWidget( GroupButtons, 2, 0 );
206     /***************************************************************/
207
208     Init(Sel) ; /* Initialisations */
209 }
210
211
212 //=================================================================================
213 // function : ~GeometryGUI_BndBoxDlg()
214 // purpose  : Destroys the object and frees any allocated resources
215 //=================================================================================
216 GeometryGUI_BndBoxDlg::~GeometryGUI_BndBoxDlg()
217 {
218     // no need to delete child widgets, Qt does it all for us
219 }
220
221
222 //=================================================================================
223 // function : Init()
224 // purpose  :
225 //=================================================================================
226 void GeometryGUI_BndBoxDlg::Init( SALOME_Selection* Sel )
227 {
228   myConstructorId = 0 ;
229   Constructor1->setChecked( TRUE );
230   myEditCurrentArgument = LineEditC1A1 ;        
231   mySelection = Sel;
232   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
233   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
234   
235   // TODO : previous selection into argument ?
236
237   /* Filter definitions */
238   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
239   myGeom = GEOM::GEOM_Gen::_narrow(comp);
240
241   /* signals and slots connections */
242   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
243   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
244   connect( SelectButtonC1A1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
245   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
246
247   connect( myGeomGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
248   /* to close dialog if study change */
249   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
250
251   mySimulationTopoDs.Nullify() ;
252
253   /* Move widget on the botton right corner of main widget */
254   int x, y ;
255   myGeomGUI->DefineDlgPosition( this, x, y ) ;
256   this->move( x, y ) ;  
257   this->show() ; /* displays Dialog */
258
259   return ;
260 }
261
262
263 //=================================================================================
264 // function : ConstructorsClicked()
265 // purpose  : Radio button management
266 //=================================================================================
267 void GeometryGUI_BndBoxDlg::ConstructorsClicked(int constructorId)
268 {
269   return ;
270 }
271
272
273
274 //=================================================================================
275 // function : ClickOnCancel()
276 // purpose  :
277 //=================================================================================
278 void GeometryGUI_BndBoxDlg::ClickOnCancel()
279 {
280   myGeomGUI->EraseSimulationShape() ;
281   mySimulationTopoDs.Nullify() ;
282   disconnect( mySelection, 0, this, 0 );
283   myGeomGUI->ResetState() ;
284   reject() ;
285   return ;
286 }
287
288
289 //=================================================================================
290 // function : SelectionIntoArgument()
291 // purpose  : Called when selection as changed or other case
292 //=================================================================================
293 void GeometryGUI_BndBoxDlg::SelectionIntoArgument()
294 {
295   LineEdit_MinX->setText("") ;
296   LineEdit_MinY->setText("") ;
297   LineEdit_MinZ->setText("") ;
298   LineEdit_MaxX->setText("") ;
299   LineEdit_MaxY->setText("") ;
300   LineEdit_MaxZ->setText("") ;  
301   myEditCurrentArgument->setText("") ;
302   mySimulationTopoDs.Nullify() ;
303
304   QString aString = ""; /* future the name of selection */
305
306   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
307   if ( nbSel != 1 ) {
308     return ;
309   }
310
311   /*  nbSel == 1  */ 
312   TopoDS_Shape S;
313   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
314     return ;
315   
316   if( S.IsNull() ) {
317     myEditCurrentArgument->setText( "" );
318     return ;
319   }
320  
321   LineEditC1A1->setText(aString) ;
322   this->CalculateAndDisplayBndBox(S) ;
323
324   return ;
325 }
326
327
328 //=================================================================================
329 // function : SetEditCurrentArgument()
330 // purpose  :
331 //=================================================================================
332 void GeometryGUI_BndBoxDlg::SetEditCurrentArgument()
333 {
334   QPushButton* send = (QPushButton*)sender();
335   switch (myConstructorId)
336     {
337     case 0: /* default constructor */
338       { 
339         if(send == SelectButtonC1A1) {
340           LineEditC1A1->setFocus() ;
341           myEditCurrentArgument = LineEditC1A1;
342         }
343         SelectionIntoArgument() ;
344         break;
345       }
346     }
347   return ;
348 }
349
350
351
352 //=================================================================================
353 // function : LineEditReturnPressed()
354 // purpose  :
355 //=================================================================================
356 void GeometryGUI_BndBoxDlg::LineEditReturnPressed()
357 {
358   QLineEdit* send = (QLineEdit*)sender();  
359   if( send == LineEditC1A1 )
360     myEditCurrentArgument = LineEditC1A1 ;
361   else
362     return ;
363   
364   /* User name of object input management                          */
365   /* If successfull the selection is changed and signal emitted... */
366   /* so SelectionIntoArgument() is automatically called.           */
367   const QString objectUserName = myEditCurrentArgument->text() ;
368   QWidget* thisWidget = (QWidget*)this ;
369   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
370     myEditCurrentArgument->setText( objectUserName ) ;
371   }
372
373   return ;
374 }
375
376
377 //=================================================================================
378 // function : DeactivateActiveDialog()
379 // purpose  :
380 //=================================================================================
381 void GeometryGUI_BndBoxDlg::DeactivateActiveDialog()
382 {
383   if ( GroupConstructors->isEnabled() ) {    
384     disconnect( mySelection, 0, this, 0 );
385     GroupConstructors->setEnabled(false) ;
386     GroupConstructor1->setEnabled(false) ;
387     GroupButtons->setEnabled(false) ;
388   }
389   return ;
390 }
391
392
393 //=================================================================================
394 // function : ActivateThisDialog()
395 // purpose  :
396 //=================================================================================
397 void GeometryGUI_BndBoxDlg::ActivateThisDialog()
398 {
399   /* Emit a signal to deactivate the active dialog */
400   myGeomGUI->EmitSignalDeactivateDialog() ;   
401   GroupConstructors->setEnabled(true) ;
402   GroupConstructor1->setEnabled(true) ;
403   GroupButtons->setEnabled(true) ;
404   connect( mySelection, SIGNAL( currentSelectionChanged() ),     this, SLOT( SelectionIntoArgument() ) );
405   return ;
406 }
407
408
409 //=================================================================================
410 // function : enterEvent()
411 // purpose  :
412 //=================================================================================
413 void GeometryGUI_BndBoxDlg::enterEvent(QEvent* e)
414 {
415   if ( GroupConstructors->isEnabled() )
416     return ;  
417   ActivateThisDialog() ;
418   return ;
419 }
420
421
422 //=================================================================================
423 // function : closeEvent()
424 // purpose  :
425 //=================================================================================
426 void GeometryGUI_BndBoxDlg::closeEvent( QCloseEvent* e )
427 {
428   /* same than click on cancel button */
429   this->ClickOnCancel() ;
430   return ;
431 }
432
433
434 //=================================================================================
435 // function : CalculateAndDisplayBndBox()
436 // purpose  :
437 //=================================================================================
438 void GeometryGUI_BndBoxDlg::CalculateAndDisplayBndBox(const TopoDS_Shape& S)
439 {
440   LineEdit_MinX->setText("") ;
441   LineEdit_MinY->setText("") ;
442   LineEdit_MinZ->setText("") ;
443   LineEdit_MaxX->setText("") ;
444   LineEdit_MaxY->setText("") ;
445   LineEdit_MaxZ->setText("") ;
446   if( S.IsNull() ) 
447     return ;
448
449   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
450   Bnd_Box B;
451   try {
452     BRepBndLib::Add(S,B);
453     B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
454     LineEdit_MinX->setText( tr("%1").arg( axmin, 12, 'f', 6 ) ) ;
455     LineEdit_MinY->setText( tr("%1").arg( aymin, 12, 'f', 6 ) ) ;
456     LineEdit_MinZ->setText( tr("%1").arg( azmin, 12, 'f', 6 ) ) ;
457     LineEdit_MaxX->setText( tr("%1").arg( axmax, 12, 'f', 6 ) ) ;
458     LineEdit_MaxY->setText( tr("%1").arg( aymax, 12, 'f', 6 ) ) ;
459     LineEdit_MaxZ->setText( tr("%1").arg( azmax, 12, 'f', 6 ) ) ;
460     
461     mySimulationTopoDs = BRepPrimAPI_MakeBox( gp_Pnt(axmin,aymin,azmin),
462                                               gp_Pnt(axmax,aymax,azmax) ).Shape();
463     myGeomGUI->DisplaySimulationShape( mySimulationTopoDs ) ; 
464   }
465   catch(Standard_Failure) {
466     MESSAGE("Catch intercepted in CalculateAndDisplayBndBox()" << endl ) ;
467   }
468   return ;
469 }