Salome HOME
NRI : First integration.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_SuppressFacesDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_SuppressFacesDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_SuppressFacesDlg.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_SuppressFacesDlg()
33 // purpose  : Constructs a GeometryGUI_SuppressFacesDlg  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_SuppressFacesDlg::GeometryGUI_SuppressFacesDlg( 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     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_DLG_SUPRESS_FACE")));
47     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_SELECT")));
48     
49     if ( !name )
50         setName( "GeometryGUI_SuppressFacesDlg" );
51     resize( 322, 203 ); 
52     setCaption( tr( "GEOM_SUPRESSFACE_TITLE"  ) );
53     setSizeGripEnabled( TRUE );
54     GeometryGUI_SuppressFacesDlgLayout = new QGridLayout( this ); 
55     GeometryGUI_SuppressFacesDlgLayout->setSpacing( 6 );
56     GeometryGUI_SuppressFacesDlgLayout->setMargin( 11 );
57
58     /***************************************************************/
59     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
60     GroupConstructors->setTitle( tr( "GEOM_SUPRESSFACE"  ) );
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_SuppressFacesDlgLayout->addWidget( GroupConstructors, 0, 0 );
79
80     /***************************************************************/
81     GroupC1 = new QGroupBox( this, "GroupC1" );
82     GroupC1->setTitle( tr( "GEOM_ARGUMENTS"  ) );
83     GroupC1->setMinimumSize( QSize( 0, 0 ) );
84     GroupC1->setFrameShape( QGroupBox::Box );
85     GroupC1->setFrameShadow( QGroupBox::Sunken );
86     GroupC1->setColumnLayout(0, Qt::Vertical );
87     GroupC1->layout()->setSpacing( 0 );
88     GroupC1->layout()->setMargin( 0 );
89     GroupC1Layout = new QGridLayout( GroupC1->layout() );
90     GroupC1Layout->setAlignment( Qt::AlignTop );
91     GroupC1Layout->setSpacing( 6 );
92     GroupC1Layout->setMargin( 11 );
93     Layout2 = new QHBoxLayout; 
94     Layout2->setSpacing( 6 );
95     Layout2->setMargin( 0 );
96     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
97     TextLabelC1A1->setText( tr( "GEOM_MAIN_OBJECT"  ) );
98     TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
99     TextLabelC1A1->setFrameShape( QLabel::NoFrame );
100     TextLabelC1A1->setFrameShadow( QLabel::Plain );
101     Layout2->addWidget( TextLabelC1A1 );
102     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
103     SelectButtonC1A1->setText( tr( ""  ) );
104     SelectButtonC1A1->setPixmap( image1 );
105     SelectButtonC1A1->setToggleButton( FALSE );
106     SelectButtonC1A1->setMaximumSize( QSize( 28, 32767 ) );
107     Layout2->addWidget( SelectButtonC1A1 );
108     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
109     LineEditC1A1->setAlignment( int( QLineEdit::AlignLeft ) );
110     Layout2->addWidget( LineEditC1A1 );
111     GroupC1Layout->addLayout( Layout2, 0, 0 );
112     CheckBox1 = new QCheckBox( GroupC1, "CheckBox1" );
113     CheckBox1->setText( tr( "GEOM_SUPRESSFACE_SELECT"  ) );
114     CheckBox1->setChecked( FALSE );
115     GroupC1Layout->addWidget( CheckBox1, 1, 0 );
116     GeometryGUI_SuppressFacesDlgLayout->addWidget( GroupC1, 1, 0 );
117
118    /***************************************************************/
119     GroupButtons = new QGroupBox( this, "GroupButtons" );
120     GroupButtons->setTitle( tr( ""  ) );
121     GroupButtons->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)5, GroupButtons->sizePolicy().hasHeightForWidth() ) );
122     GroupButtons->setColumnLayout(0, Qt::Vertical );
123     GroupButtons->layout()->setSpacing( 0 );
124     GroupButtons->layout()->setMargin( 0 );
125     GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
126     GroupButtonsLayout->setAlignment( Qt::AlignTop );
127     GroupButtonsLayout->setSpacing( 6 );
128     GroupButtonsLayout->setMargin( 11 );
129     //
130     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
131     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
132     buttonOk->setAutoDefault( TRUE );
133     buttonOk->setDefault( TRUE );
134     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );    
135     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
136     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
137     buttonApply->setAutoDefault( TRUE );
138     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );    
139     QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
140     GroupButtonsLayout->addItem( spacer_2, 0, 2 );
141     buttonClose = new QPushButton( GroupButtons, "buttonClose" );
142     buttonClose->setText( tr( "GEOM_BUT_CLOSE"  ) );
143     buttonClose->setAutoDefault( TRUE );
144     GroupButtonsLayout->addWidget( buttonClose, 0, 3 );
145     GeometryGUI_SuppressFacesDlgLayout->addWidget( GroupButtons, 2, 0 );
146
147     /* Initialisations */
148     Init(Sel, ic) ;
149
150 }
151
152
153 //=================================================================================
154 // function : ~GeometryGUI_SuppressFacesDlg()
155 // purpose  : Destroys the object and frees any allocated resources
156 //=================================================================================
157 GeometryGUI_SuppressFacesDlg::~GeometryGUI_SuppressFacesDlg()
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_SuppressFacesDlg::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", "Geometry");
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( SelectButtonC1A1,  SIGNAL( clicked() ),                      this, SLOT( SetEditCurrentArgument() ));
200   connect( CheckBox1,         SIGNAL( stateChanged(int) ),              this, SLOT( ActivateUserSelection() ));
201   connect( mySelection,       SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ));
202   connect( myGeomGUI,         SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
203   connect( myGeomGUI,         SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnClose() ));
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() ; /* display Dialog */
210   return ;
211 }
212
213
214 //=================================================================================
215 // function : ConstructorsClicked()
216 // purpose  : Radio button management
217 //=================================================================================
218 void GeometryGUI_SuppressFacesDlg::ConstructorsClicked(int constructorId)
219 {
220   return ;
221 }
222
223
224 //=================================================================================
225 // function : ClickOnOk()
226 // purpose  : Same than click on apply but close this dialog.
227 //=================================================================================
228 void GeometryGUI_SuppressFacesDlg::ClickOnOk()
229 {
230   this->ClickOnApply() ;
231   accept();
232
233   return ;
234 }
235
236
237
238 //=================================================================================
239 // function : ClickOnApply()
240 // purpose  :
241 //=================================================================================
242 void GeometryGUI_SuppressFacesDlg::ClickOnApply()
243 {
244   bool testResult = false ;
245   switch(myConstructorId)
246     { 
247     case 0 :
248       {
249         if( myOkShape && myOkSelectSubMode ) {
250           testResult = myGeomGUI->OnSuppressFaces( myShape, myShapeIOR, myLocalContextId, myUseLocalContext ) ; 
251         }
252         if( !testResult ) {
253           myGeomGUI->GetDesktop()->putInfo(tr("GEOM_PRP_ABORT")) ;
254         }
255         else {
256           myGeomGUI->GetDesktop()->putInfo(tr("GEOM_PRP_DONE")) ;
257         }
258         /* Reset arguments to allow a new selection */
259         this->ResetStateOfDialog() ;
260         break ;
261       }
262     }
263   return ;
264 }
265
266
267
268 //=================================================================================
269 // function : ClickOnClose()
270 // purpose  :
271 //=================================================================================
272 void GeometryGUI_SuppressFacesDlg::ClickOnClose()
273 {
274   disconnect( mySelection, 0, this, 0 );
275   myGeomGUI->ResetState() ;
276   
277   if(myUseLocalContext) {
278     myIC->CloseLocalContext(myLocalContextId) ;
279     this->myUseLocalContext = false ;
280     myGeomGUI->OnDisplayAll(true) ;
281   }
282   reject() ;
283   return ;
284 }
285
286
287 //=================================================================================
288 // function : SelectionIntoArgument()
289 // purpose  : Called when selection as changed or other case
290 //          : used only by SelectButtonC1A1 (LineEditC1A1)
291 //=================================================================================
292 void GeometryGUI_SuppressFacesDlg::SelectionIntoArgument()
293 {
294   
295   /* Reset argument and local context when selection as changed */
296   this->ResetStateOfDialog() ;
297   
298   QString aString = ""; /* name of selection */
299   
300   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
301   if ( nbSel != 1 )
302     return ;
303   
304   /* nbSel == 1 */
305   TopoDS_Shape S ;
306   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject() ;
307
308   if( !myGeomGUI->GetTopoFromSelection(mySelection, S) )
309     return ;
310   
311   if( !IO->hasEntry() ) {
312     myGeomGUI->GetDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY")) ;
313     return ;
314   }
315   
316   /* Test the exact type of topology to suppress faces into */
317   if ( !S.IsNull() && ( S.ShapeType() == TopAbs_SOLID || S.ShapeType() == TopAbs_SHELL || S.ShapeType() == TopAbs_COMPOUND ) ) {
318     
319     if ( IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject)) ) {
320       Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast( IO );
321       myShapeIOR = GIObject->getIOR(); /* the Geom IOR string of selection */
322       LineEditC1A1->setText(aString) ;
323       myShape = S ;
324       myOkShape = true ;
325       return;
326     } 
327     
328     if ( IO->hasEntry() ) {
329       SALOMEDS::Study_var aStudy = myGeomGUI->GetActiveStudy()->getStudyDocument();
330       SALOMEDS::SObject_var obj = aStudy->FindObjectID( IO->getEntry() );
331       SALOMEDS::GenericAttribute_var anAttr;
332       SALOMEDS::AttributeIOR_var     anIOR;
333       if ( !obj->_is_nil() ) {
334         if (obj->FindAttribute(anAttr, "AttributeIOR")) {
335           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
336           myShapeIOR = anIOR->Value();
337           myOkShape = true ;
338           myShape = S ;
339           LineEditC1A1->setText(aString) ;
340           return;
341         }
342       }
343     }
344
345   }
346   return ;
347 }
348
349
350 //=================================================================================
351 // function : SetEditCurrentArgument()
352 // purpose  :
353 //=================================================================================
354 void GeometryGUI_SuppressFacesDlg::SetEditCurrentArgument()
355 {
356   QPushButton* send = (QPushButton*)sender();
357   switch (myConstructorId)
358     {
359     case 0: /* default constructor */
360       {
361         if(send == SelectButtonC1A1) {
362           LineEditC1A1->setFocus() ;
363           myEditCurrentArgument = LineEditC1A1;   
364           SelectionIntoArgument() ;
365         }
366         break;
367       }
368     }
369   return ;
370 }
371
372
373 //=================================================================================
374 // function : LineEditReturnPressed()
375 // purpose  :
376 //=================================================================================
377 void GeometryGUI_SuppressFacesDlg::LineEditReturnPressed()
378 {
379   return ;
380 }
381
382
383 //=================================================================================
384 // function : DeactivateActiveDialog()
385 // purpose  :
386 //=================================================================================
387 void GeometryGUI_SuppressFacesDlg::DeactivateActiveDialog()
388 {
389   if ( GroupConstructors->isEnabled() ) {
390  
391    this->ResetStateOfDialog() ;
392
393     disconnect( mySelection, 0, this, 0 );
394     GroupConstructors->setEnabled(false) ;
395     GroupC1->setEnabled(false) ;
396     GroupButtons->setEnabled(false) ;
397     myGeomGUI->ResetState() ;    
398     myGeomGUI->SetActiveDialogBox(0) ;
399     myGeomGUI->OnDisplayAll(true) ;
400   }
401   return ;
402 }
403
404
405 //=================================================================================
406 // function : ActivateThisDialog()
407 // purpose  :
408 //=================================================================================
409 void GeometryGUI_SuppressFacesDlg::ActivateThisDialog()
410 {
411   /* Emit a signal to deactivate other active dialog */
412   myGeomGUI->EmitSignalDeactivateDialog() ;
413   GroupConstructors->setEnabled(true) ;
414   GroupC1->setEnabled(true) ;
415   GroupButtons->setEnabled(true) ;
416   connect ( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
417   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
418   return ;
419 }
420
421
422 //=================================================================================
423 // function : enterEvent()
424 // purpose  : Mouse enter onto the dialog to activate it
425 //=================================================================================
426 void GeometryGUI_SuppressFacesDlg::enterEvent(QEvent* e)
427 {
428   if ( GroupConstructors->isEnabled() )
429     return ;  
430   ActivateThisDialog() ;
431   return ;
432 }
433
434
435 //=================================================================================
436 // function : closeEvent()
437 // purpose  :
438 //=================================================================================
439 void GeometryGUI_SuppressFacesDlg::closeEvent( QCloseEvent* e )
440 {
441   /* same than click on cancel button */
442   this->ClickOnClose() ;
443   return ;
444 }
445
446
447 //=================================================================================
448 // function : ActivateUserSelection()
449 // purpose  : Activate selection of faces when CheckBox1->isChecked()...
450 //=================================================================================
451 void GeometryGUI_SuppressFacesDlg::ActivateUserSelection()
452 {
453   if ( myGeomGUI->GetActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) {
454     if( !this->myOkShape ) {
455       this->ResetStateOfDialog() ;
456       myGeomGUI->GetDesktop()->putInfo(tr("GEOM_PRP_SELECT_FIRST")) ;
457       return ;
458     }
459   
460     this->myOkSelectSubMode = CheckBox1->isChecked() ;
461     
462     if( this->myUseLocalContext ) {
463       myIC->CloseLocalContext(myLocalContextId) ;
464       this->myUseLocalContext = false ;
465       myGeomGUI->OnDisplayAll(true) ;
466     }
467     
468     if( myOkShape && myOkSelectSubMode ) {
469       /* local context is defined into the method : 4 = FACES sub selection */
470       myGeomGUI->PrepareSubShapeSelection( int(TopAbs_FACE), this->myLocalContextId ) ;    
471       myUseLocalContext = true ;
472       myGeomGUI->GetDesktop()->putInfo(tr("GEOM_PRP_SELECT_FACE")) ;
473     }
474   } else {
475     CheckBox1->setChecked( false );
476   }
477
478   return ;
479 }
480
481
482
483 //=================================================================================
484 // function : ResetStateOfDialog()
485 // purpose  : Completely reset the state of method including local context
486 //=================================================================================
487 void GeometryGUI_SuppressFacesDlg::ResetStateOfDialog()
488 {
489   this->myOkShape = false ;
490   this->myEditCurrentArgument->setText("") ;
491
492   /* Select sub shapes mode not checked */
493   this->myOkSelectSubMode = false ;    
494   this->CheckBox1->setChecked( FALSE );
495
496   /* Close its local contact if opened */
497   if( this->myUseLocalContext ) {
498     myIC->CloseLocalContext(this->myLocalContextId) ;
499     this->myUseLocalContext = false ;
500     myGeomGUI->OnDisplayAll(true) ;
501   }
502   return ;
503 }