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