Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/geom.git] / src / GEOMGUI / GeometryGUI_FillingHoleDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_FillingHoleDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_FillingHoleDlg.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 <qcheckbox.h>
19 #include <qgroupbox.h>
20 #include <qlabel.h>
21 #include <qlineedit.h>
22 #include <qpushbutton.h>
23 #include <qradiobutton.h>
24 #include <qlayout.h>
25 #include <qvariant.h>
26 #include <qtooltip.h>
27 #include <qwhatsthis.h>
28 #include <qimage.h>
29 #include <qpixmap.h>
30
31 //=================================================================================
32 // class    : GeometryGUI_FillingHoleDlg()
33 // purpose  : Constructs a  GeometryGUI_FillingHoleDlg which is a child of 'parent', with the
34 //            name 'name' and widget flags set to 'f'.
35 //            The dialog will by default be modeless, unless you set 'modal' to
36 //            TRUE to construct a modal dialog.
37 //=================================================================================
38 GeometryGUI_FillingHoleDlg::GeometryGUI_FillingHoleDlg( QWidget* parent, 
39                                                         const char* name,
40                                                         SALOME_Selection* Sel,
41                                                         Handle (AIS_InteractiveContext) ic,                                       
42                                                         bool modal,
43                                                         WFlags fl )
44   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
45 {
46   
47   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_SEWING")));
48   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
49   
50   if ( !name )
51     setName( "GeometryGUI_FillingHoleDlg" );
52   resize( 303, 203 ); 
53   setCaption( tr( "Filling hole"  ) );
54   setSizeGripEnabled( TRUE );
55   GeometryGUI_FillingHoleDlgLayout = new QGridLayout( this ); 
56   GeometryGUI_FillingHoleDlgLayout->setSpacing( 6 );
57   GeometryGUI_FillingHoleDlgLayout->setMargin( 11 );
58
59   /***************************************************************/
60   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
61   GroupConstructors->setTitle( tr( ""  ) );
62   GroupConstructors->setExclusive( TRUE );
63   GroupConstructors->setColumnLayout(0, Qt::Vertical );
64   GroupConstructors->layout()->setSpacing( 0 );
65   GroupConstructors->layout()->setMargin( 0 );
66   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
67   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
68   GroupConstructorsLayout->setSpacing( 6 );
69   GroupConstructorsLayout->setMargin( 11 );
70   Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
71   Constructor1->setText( tr( ""  ) );
72   Constructor1->setPixmap( image0 );
73   Constructor1->setChecked( TRUE );
74   Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
75   Constructor1->setMinimumSize( QSize( 50, 0 ) );
76   GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
77   QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
78   GroupConstructorsLayout->addItem( spacer, 0, 1 );
79   GeometryGUI_FillingHoleDlgLayout->addWidget( GroupConstructors, 0, 0 );
80
81   /***************************************************************/
82   GroupC1 = new QGroupBox( this, "GroupC1" );
83   GroupC1->setTitle( tr( ""  ) );
84   GroupC1->setMinimumSize( QSize( 0, 0 ) );
85   GroupC1->setFrameShape( QGroupBox::Box );
86   GroupC1->setFrameShadow( QGroupBox::Sunken );
87   GroupC1->setColumnLayout(0, Qt::Vertical );
88   GroupC1->layout()->setSpacing( 0 );
89   GroupC1->layout()->setMargin( 0 );
90   GroupC1Layout = new QGridLayout( GroupC1->layout() );
91   GroupC1Layout->setAlignment( Qt::AlignTop );
92   GroupC1Layout->setSpacing( 6 );
93   GroupC1Layout->setMargin( 11 );
94   Layout2 = new QHBoxLayout; 
95   Layout2->setSpacing( 6 );
96   Layout2->setMargin( 0 );
97   TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
98   TextLabelC1A1->setText( tr( "Main object"  ) );
99   TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
100   TextLabelC1A1->setFrameShape( QLabel::NoFrame );
101   TextLabelC1A1->setFrameShadow( QLabel::Plain );
102   Layout2->addWidget( TextLabelC1A1 );
103   SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
104   SelectButtonC1A1->setText( tr( ""  ) );
105   SelectButtonC1A1->setPixmap( image1 );
106   SelectButtonC1A1->setToggleButton( FALSE );
107   SelectButtonC1A1->setMaximumSize( QSize( 28, 32767 ) );
108   Layout2->addWidget( SelectButtonC1A1 );
109   LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
110   LineEditC1A1->setAlignment( int( QLineEdit::AlignLeft ) );
111   Layout2->addWidget( LineEditC1A1 );
112   GroupC1Layout->addLayout( Layout2, 0, 0 );
113   CheckBox1 = new QCheckBox( GroupC1, "CheckBox1" );
114   CheckBox1->setText( tr( "Select edges of hole on main object"  ) );
115   CheckBox1->setChecked( FALSE );
116   GroupC1Layout->addWidget( CheckBox1, 1, 0 );
117   GeometryGUI_FillingHoleDlgLayout->addWidget( GroupC1, 1, 0 );
118
119   /***************************************************************/
120   GroupButtons = new QGroupBox( this, "GroupButtons" );
121   GroupButtons->setTitle( tr( ""  ) );
122   GroupButtons->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)5, GroupButtons->sizePolicy().hasHeightForWidth() ) );
123   GroupButtons->setColumnLayout(0, Qt::Vertical );
124   GroupButtons->layout()->setSpacing( 0 );
125   GroupButtons->layout()->setMargin( 0 );
126   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
127   GroupButtonsLayout->setAlignment( Qt::AlignTop );
128   GroupButtonsLayout->setSpacing( 6 );
129   GroupButtonsLayout->setMargin( 11 );
130   buttonClose = new QPushButton( GroupButtons, "buttonClose" );
131   buttonClose->setText( tr( "&Close"  ) );
132   buttonClose->setAutoDefault( TRUE );
133   GroupButtonsLayout->addWidget( buttonClose, 0, 3 );
134   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
135   buttonOk->setText( tr( "&Ok"  ) );
136   buttonOk->setAutoDefault( TRUE );
137   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
138   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
139   buttonApply->setText( tr( "&Apply"  ) );
140   buttonApply->setAutoDefault( TRUE );
141   buttonApply->setDefault( TRUE );
142   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
143   QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
144   GroupButtonsLayout->addItem( spacer_2, 0, 2 );
145   GeometryGUI_FillingHoleDlgLayout->addWidget( GroupButtons, 2, 0 );
146   
147   /* Initialisations */
148   Init(Sel, ic) ;
149   
150 }
151
152
153 //=================================================================================
154 // function : ~GeometryGUI_FillingHoleDlg()
155 // purpose  : Destroys the object and frees any allocated resources
156 //=================================================================================
157 GeometryGUI_FillingHoleDlg::~GeometryGUI_FillingHoleDlg()
158 {
159     // no need to delete child widgets, Qt does it all for us
160 }
161
162
163
164 //=================================================================================
165 // function : Init()
166 // purpose  :
167 //=================================================================================
168 void GeometryGUI_FillingHoleDlg::Init( SALOME_Selection* Sel, Handle (AIS_InteractiveContext) ic )
169 {
170
171   GroupC1->show();
172   myConstructorId = 0 ;
173   Constructor1->setChecked( TRUE );
174   myEditCurrentArgument = LineEditC1A1 ;
175   mySelection = Sel;
176   myShape.Nullify() ;
177
178   myIC = ic ;
179   myUseLocalContext = false ;
180   myOkShape = false ;
181   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
182
183   /* Select sub shapes mode not checked */
184   CheckBox1->setChecked( FALSE );
185   myOkSelectSubMode = false ;
186
187   // TODO : previous selection into argument ?
188
189   /* Filter definitions */
190   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
191   myGeom = GEOM::GEOM_Gen::_narrow(comp);
192
193   /* signals and slots connections */
194   connect( buttonOk,          SIGNAL( clicked() ),    this, SLOT( ClickOnOk() ) );
195   connect( buttonApply,       SIGNAL( clicked() ),    this, SLOT( ClickOnApply() ) );
196   connect( buttonClose,       SIGNAL( clicked() ),    this, SLOT( ClickOnClose() ) );
197   connect( GroupConstructors, SIGNAL( clicked(int) ), this, SLOT( ConstructorsClicked(int) ));
198
199   connect( LineEditC1A1,      SIGNAL( returnPressed() ),                this, SLOT( LineEditReturnPressed() ) ) ;
200   connect( SelectButtonC1A1,  SIGNAL( clicked() ),                      this, SLOT( SetEditCurrentArgument() ));
201   connect( CheckBox1,         SIGNAL( stateChanged(int) ),              this, SLOT( ActivateUserSelection() ));
202
203   connect( mySelection,       SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ));
204   connect( myGeomGUI,         SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
205   connect( myGeomGUI,         SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnClose() ));
206  
207   /* Move widget on the botton right corner of main widget */
208   int x, y ;
209   myGeomGUI->DefineDlgPosition( this, x, y ) ;
210   this->move( x, y ) ;
211   this->show() ; /* display Dialog */
212
213   return ;
214 }
215
216
217 //=================================================================================
218 // function : ConstructorsClicked()
219 // purpose  : Radio button management
220 //=================================================================================
221 void GeometryGUI_FillingHoleDlg::ConstructorsClicked(int constructorId)
222 {
223   return ;
224 }
225
226
227 //=================================================================================
228 // function : ClickOnOk()
229 // purpose  : Same than click on apply but close this dialog.
230 //=================================================================================
231 void GeometryGUI_FillingHoleDlg::ClickOnOk()
232 {
233   this->ClickOnApply() ;
234   accept();
235
236   return ;
237 }
238
239
240
241 //=================================================================================
242 // function : ClickOnApply()
243 // purpose  :
244 //=================================================================================
245 void GeometryGUI_FillingHoleDlg::ClickOnApply()
246 {
247   bool testResult = false ;
248   myGeomGUI->GetDesktop()->putInfo( tr("") ) ; 
249
250   switch(myConstructorId)
251     { 
252     case 0 :
253       {
254         if( myOkShape && myOkSelectSubMode ) {
255           testResult = myGeomGUI->OnFillingHole( myShape, myShapeIOR, myLocalContextId, myUseLocalContext ) ; 
256         }
257         if( !testResult ) {
258           myGeomGUI->GetDesktop()->putInfo(tr("Operation aborted")) ;
259         }
260         else {
261           myGeomGUI->GetDesktop()->putInfo(tr("Operation done"));
262         }
263         /* Reset arguments to allow a new selection */
264         this->ResetStateOfDialog() ;
265         break ;
266       }
267     }
268   return ;
269 }
270
271
272
273 //=================================================================================
274 // function : ClickOnClose()
275 // purpose  :
276 //=================================================================================
277 void GeometryGUI_FillingHoleDlg::ClickOnClose()
278 {
279   disconnect( mySelection, 0, this, 0 );
280   myGeomGUI->ResetState() ;
281   
282   if(myUseLocalContext) {
283     myIC->CloseLocalContext(myLocalContextId) ;
284     this->myUseLocalContext = false ;
285     myGeomGUI->OnDisplayAll(true) ;
286   }
287   reject() ;
288   return ;
289 }
290
291
292 //=================================================================================
293 // function : SelectionIntoArgument()
294 // purpose  : Called when selection as changed or other case
295 //          : used only by SelectButtonC1A1 (LineEditC1A1)
296 //=================================================================================
297 void GeometryGUI_FillingHoleDlg::SelectionIntoArgument()
298 {
299   
300   /* Reset argument and local context when selection as changed */
301   this->ResetStateOfDialog() ;
302   
303   QString aString = ""; /* name of selection */
304   
305   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
306   if ( nbSel != 1 )
307     return ;
308   
309   /* nbSel == 1 */
310   TopoDS_Shape S ;
311   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
312
313   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
314     return ;
315   
316 //    if( !IO->hasEntry() ) {
317 //      myGeomGUI->GetDesktop()->putInfo(tr("Main shape must be in the study before")) ;
318 //      return ;
319 //    }
320   
321   /* Test the exact type of topology to fill an hole */
322   if ( !S.IsNull() && ( S.ShapeType() == TopAbs_SOLID || S.ShapeType() == TopAbs_SHELL || S.ShapeType() == TopAbs_COMPOUND ) ) {
323     
324     if ( IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject)) ) {
325       Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast( IO );
326       myShapeIOR = GIObject->getIOR(); /* the Geom IOR string of selection */
327       LineEditC1A1->setText(aString) ;
328       myShape = S ;
329       myOkShape = true ;
330       return;
331     } 
332     
333     if ( IO->hasEntry() ) {
334       SALOMEDS::Study_var aStudy = myGeomGUI->GetActiveStudy()->getStudyDocument();
335       SALOMEDS::SObject_var obj = aStudy->FindObjectID( IO->getEntry() );
336       SALOMEDS::GenericAttribute_var anAttr;
337       SALOMEDS::AttributeIOR_var     anIOR;
338       if ( !obj->_is_nil() ) {
339         if (obj->FindAttribute(anAttr, "AttributeIOR")) {
340           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
341           myShapeIOR = anIOR->Value();
342           myOkShape = true ;
343           myShape = S ;
344           LineEditC1A1->setText(aString) ;
345           return;
346         }
347       }
348     }
349
350   }
351   return ;
352 }
353
354
355 //=================================================================================
356 // function : SetEditCurrentArgument()
357 // purpose  :
358 //=================================================================================
359 void GeometryGUI_FillingHoleDlg::SetEditCurrentArgument()
360 {
361   QPushButton* send = (QPushButton*)sender();
362   switch (myConstructorId)
363     {
364     case 0: /* default constructor */
365       {
366         if(send == SelectButtonC1A1) {
367           LineEditC1A1->setFocus() ;
368           myEditCurrentArgument = LineEditC1A1;   
369           SelectionIntoArgument() ;
370         }
371         break;
372       }
373     }
374   return ;
375 }
376
377
378 //=================================================================================
379 // function : LineEditReturnPressed()
380 // purpose  :
381 //=================================================================================
382 void GeometryGUI_FillingHoleDlg::LineEditReturnPressed()
383 {
384   QLineEdit* send = (QLineEdit*)sender();  
385   if( send == LineEditC1A1 )
386     myEditCurrentArgument = LineEditC1A1 ;
387   else
388     return ;
389   
390   /* User name of object input management                          */
391   /* If successfull the selection is changed and signal emitted... */
392   /* so SelectionIntoArgument() is automatically called.           */
393   const QString objectUserName = myEditCurrentArgument->text() ;
394   QWidget* thisWidget = (QWidget*)this ;
395   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
396     myEditCurrentArgument->setText( objectUserName ) ;
397   }
398   return ;
399 }
400
401
402 //=================================================================================
403 // function : DeactivateActiveDialog()
404 // purpose  :
405 //=================================================================================
406 void GeometryGUI_FillingHoleDlg::DeactivateActiveDialog()
407 {
408   if ( GroupConstructors->isEnabled() ) {
409  
410    this->ResetStateOfDialog() ;
411
412     disconnect( mySelection, 0, this, 0 );
413     GroupConstructors->setEnabled(false) ;
414     GroupC1->setEnabled(false) ;
415     GroupButtons->setEnabled(false) ;
416     myGeomGUI->ResetState() ;    
417     myGeomGUI->SetActiveDialogBox(0) ;
418     myGeomGUI->OnDisplayAll(true) ;
419   }
420   return ;
421 }
422
423
424 //=================================================================================
425 // function : ActivateThisDialog()
426 // purpose  :
427 //=================================================================================
428 void GeometryGUI_FillingHoleDlg::ActivateThisDialog()
429 {
430   /* Emit a signal to deactivate other active dialog */
431   myGeomGUI->EmitSignalDeactivateDialog() ;
432   GroupConstructors->setEnabled(true) ;
433   GroupC1->setEnabled(true) ;
434   GroupButtons->setEnabled(true) ;
435   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
436   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
437   return ;
438 }
439
440
441 //=================================================================================
442 // function : enterEvent()
443 // purpose  : Mouse enter onto the dialog to activate it
444 //=================================================================================
445 void GeometryGUI_FillingHoleDlg::enterEvent(QEvent* e)
446 {
447   if ( GroupConstructors->isEnabled() )
448     return ;  
449   ActivateThisDialog() ;
450   return ;
451 }
452
453
454 //=================================================================================
455 // function : closeEvent()
456 // purpose  :
457 //=================================================================================
458 void GeometryGUI_FillingHoleDlg::closeEvent( QCloseEvent* e )
459 {
460   /* same than click on cancel button */
461   this->ClickOnClose() ;
462   return ;
463 }
464
465
466 //=================================================================================
467 // function : ActivateUserSelection()
468 // purpose  : Activate selection of faces when CheckBox1->isChecked()...
469 //=================================================================================
470 void GeometryGUI_FillingHoleDlg::ActivateUserSelection()
471 {
472   
473   if( !this->myOkShape ) {
474     this->ResetStateOfDialog() ;
475     myGeomGUI->GetDesktop()->putInfo(tr("Select main shape first")) ;
476     return ;
477   }
478   
479   this->myOkSelectSubMode = CheckBox1->isChecked() ;
480   
481   if( this->myUseLocalContext ) {
482     myIC->CloseLocalContext(myLocalContextId) ;
483     this->myUseLocalContext = false ;
484     myGeomGUI->OnDisplayAll(true) ;
485   }
486
487   if( myOkShape && myOkSelectSubMode ) {
488     /* local context is defined into the method : GEOM::EDGE sub selection */
489     TopAbs_ShapeEnum aType = TopAbs_EDGE ;
490     myGeomGUI->PrepareSubShapeSelection( int(aType), this->myLocalContextId ) ;    
491     myUseLocalContext = true ;
492     myGeomGUI->GetDesktop()->putInfo(tr("Select edges to fill an hole and click on Ok/Apply")) ;
493   }
494   return ;
495 }
496
497
498
499 //=================================================================================
500 // function : ResetStateOfDialog()
501 // purpose  : Completely reset the state of method including local context
502 //=================================================================================
503 void GeometryGUI_FillingHoleDlg::ResetStateOfDialog()
504 {
505   this->myOkShape = false ;
506   this->myEditCurrentArgument->setText("") ;
507
508   /* Select sub shapes mode not checked */
509   this->myOkSelectSubMode = false ;    
510   this->CheckBox1->setChecked( FALSE );
511
512   /* Close its local contact if opened */
513   if( this->myUseLocalContext ) {
514     myIC->CloseLocalContext(this->myLocalContextId) ;
515     this->myUseLocalContext = false ;
516     myGeomGUI->OnDisplayAll(true) ;
517   }
518   return ;
519 }
520
521
522