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