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