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