Salome HOME
NRI : Replace Geometry by GEOM.
[modules/geom.git] / src / GEOMGUI / GeometryGUI_PartitionDlg.cxx
1 using namespace std;
2 //  File      : GeometryGUI_PartitionDlg.cxx
3 //  Created   :
4 //  Author    : Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GeometryGUI
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "GeometryGUI_PartitionDlg.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 <qgroupbox.h>
19 #include <qlabel.h>
20 #include <qlineedit.h>
21 #include <qpushbutton.h>
22 #include <qradiobutton.h>
23 #include <qlayout.h>
24 #include <qimage.h>
25 #include <qpixmap.h>
26
27 #define   MIN_EDIT_SIZE       150
28
29 //=================================================================================
30 // class    : GeometryGUI_PartitionDlg()
31 // purpose  : Constructs a GeometryGUI_PartitionDlg which is a child of 'parent', with the 
32 //            name 'name' and widget flags set to 'f'.
33 //            The dialog will by default be modeless, unless you set 'modal' to
34 //            TRUE to construct a modal dialog.
35 //=================================================================================
36 GeometryGUI_PartitionDlg::GeometryGUI_PartitionDlg( QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl )
37     : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
38 {
39     QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_DLG_PARTITION")));
40     QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "GEOM",tr("ICON_SELECT")));
41 //    QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap( "GeometryGUI",tr("ICON_DLG_PARTITION_KEEP_FACES")));
42
43     if ( !name )
44         setName( "GeometryGUI_PartitionDlg" );
45     setCaption( tr( "GEOM_PARTITION_TITLE"  ) );
46     setSizeGripEnabled( TRUE );
47     QGridLayout* GeometryGUI_PartitionDlgLayout = new QGridLayout( this ); 
48     GeometryGUI_PartitionDlgLayout->setSpacing( 6 );
49     GeometryGUI_PartitionDlgLayout->setMargin( 11 );
50     
51     /***************************************************************/
52     /* Constructor group */
53     /***************************************************************/
54     GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
55     GroupConstructors->setTitle( tr( "GEOM_PARTITION"  ) );
56     GroupConstructors->setColumnLayout(0, Qt::Vertical );
57     GroupConstructors->layout()->setSpacing( 0 );
58     GroupConstructors->layout()->setMargin( 0 );
59     QGridLayout* GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
60     GroupConstructorsLayout->setAlignment( Qt::AlignTop );
61     GroupConstructorsLayout->setSpacing( 6 );
62     GroupConstructorsLayout->setMargin( 11 );
63
64     Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
65     Constructor1->setPixmap( image0 );
66     Constructor1->setChecked( TRUE );
67     Constructor1->setMinimumSize( QSize( 50, 0 ) );
68     GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
69     GroupConstructorsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 1, 0 );
70
71     GeometryGUI_PartitionDlgLayout->addWidget( GroupConstructors, 0, 0 );
72
73     /***************************************************************/
74     /* Arguments group */
75     /***************************************************************/
76     /* Shapes and Tools */
77     GroupC1 = new QGroupBox( this, "GroupC1" );
78     GroupC1->setTitle( tr( "GEOM_PARTITION"  ) );
79     GroupC1->setColumnLayout(0, Qt::Vertical );
80     GroupC1->layout()->setSpacing( 0 );
81     GroupC1->layout()->setMargin( 0 );
82     QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
83     GroupC1Layout->setAlignment( Qt::AlignTop );
84     GroupC1Layout->setSpacing( 6 );
85     GroupC1Layout->setMargin( 11 );
86
87     TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
88     TextLabelC1A1->setText( tr( "GEOM_OBJECTS"  ) );
89     GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
90     SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
91     SelectButtonC1A1->setPixmap( image1 );
92     SelectButtonC1A1->setToggleButton( FALSE );
93     GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
94     LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
95     LineEditC1A1->setMinimumSize(MIN_EDIT_SIZE, 0);
96     GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
97     
98     TextLabelC1A2 = new QLabel( GroupC1, "TextLabelC1A2" );
99     TextLabelC1A2->setText( tr( "GEOM_TOOL_OBJECT"  ) );
100     GroupC1Layout->addWidget( TextLabelC1A2, 1, 0 );
101     SelectButtonC1A2 = new QPushButton( GroupC1, "SelectButtonC1A2" );
102     SelectButtonC1A2->setPixmap( image1 );
103     SelectButtonC1A2->setToggleButton( FALSE );
104     GroupC1Layout->addWidget( SelectButtonC1A2, 1, 1 );
105     LineEditC1A2 = new QLineEdit( GroupC1, "LineEditC1A2" );
106     LineEditC1A2->setMinimumSize(MIN_EDIT_SIZE, 0);
107     GroupC1Layout->addWidget( LineEditC1A2, 1, 2 );
108
109     /* Limit */
110     TextLabelComboBox1 = new QLabel( GroupC1, "TextLabelComboBox1" );
111     TextLabelComboBox1->setText( tr( "RECONSTRUCTION_LIMIT") );
112     GroupC1Layout->addWidget( TextLabelComboBox1, 2, 0 );
113     ComboBox1 = new QComboBox( FALSE, GroupC1, "ComboBox1" );
114     ComboBox1->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) );
115     ComboBox1->setMinimumSize(MIN_EDIT_SIZE, 0);
116     GroupC1Layout->addWidget( ComboBox1, 2, 2 );
117
118     GeometryGUI_PartitionDlgLayout->addWidget( GroupC1, 1, 0 );
119
120     /***************************************************************/
121     /* Result suppresion */
122     /***************************************************************/
123     GroupC2 = new QGroupBox( this, "GroupC2" );
124     GroupC2->setTitle( tr( "SUPPRESS_RESULT") );
125     GroupC2->setColumnLayout(0, Qt::Vertical );
126     GroupC2->layout()->setSpacing( 0 );
127     GroupC2->layout()->setMargin( 0 );
128     QGridLayout* GroupC2Layout = new QGridLayout( GroupC2->layout() );
129     GroupC2Layout->setAlignment( Qt::AlignTop );
130     GroupC2Layout->setSpacing( 6 );
131     GroupC2Layout->setMargin( 11 );
132
133     TextLabelC2A1 = new QLabel( GroupC2, "TextLabelC2A1" );
134     TextLabelC2A1->setText( tr( "SUPPRESS_RESULT_INSIDE") );
135     GroupC2Layout->addWidget( TextLabelC2A1, 0, 0 );
136     SelectButtonC2A1 = new QPushButton( GroupC2, "SelectButtonC2A1" );
137     SelectButtonC2A1->setPixmap( image1 );
138     GroupC2Layout->addWidget( SelectButtonC2A1, 0, 1 );
139     LineEditC2A1 = new QLineEdit( GroupC2, "LineEditC2A1" );
140     GroupC2Layout->addWidget( LineEditC2A1, 0, 2 );
141
142     TextLabelC2A2 = new QLabel( GroupC2, "TextLabelC2A2" );
143     TextLabelC2A2->setText( tr( "SUPPRESS_RESULT_OUTSIDE") );
144     GroupC2Layout->addWidget( TextLabelC2A2, 1, 0 );
145     SelectButtonC2A2 = new QPushButton( GroupC2, "SelectButtonC2A2" );
146     SelectButtonC2A2->setPixmap( image1 );
147     GroupC2Layout->addWidget( SelectButtonC2A2, 1, 1 );
148     LineEditC2A2 = new QLineEdit( GroupC2, "LineEditC2A2" );
149     GroupC2Layout->addWidget( LineEditC2A2, 1, 2 );
150
151     GeometryGUI_PartitionDlgLayout->addWidget( GroupC2, 2, 0 );
152     
153     /***************************************************************/
154     /* <OK>, <Apply>, <Cancel> buttons */
155     /***************************************************************/
156     GroupButtons = new QGroupBox( this, "GroupButtons" );
157     GroupButtons->setColumnLayout(0, Qt::Vertical );
158     GroupButtons->layout()->setSpacing( 0 );
159     GroupButtons->layout()->setMargin( 0 );
160     QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
161     GroupButtonsLayout->setAlignment( Qt::AlignTop );
162     GroupButtonsLayout->setSpacing( 6 );
163     GroupButtonsLayout->setMargin( 11 );
164     
165     buttonOk = new QPushButton( GroupButtons, "buttonOk" );
166     buttonOk->setText( tr( "GEOM_BUT_OK"  ) );
167     buttonOk->setAutoDefault( TRUE );
168     buttonOk->setDefault( TRUE );
169     GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
170
171     buttonApply = new QPushButton( GroupButtons, "buttonApply" );
172     buttonApply->setText( tr( "GEOM_BUT_APPLY"  ) );
173     buttonApply->setAutoDefault( TRUE );
174     GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
175
176     GroupButtonsLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 2 );
177
178     buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
179     buttonCancel->setText( tr( "GEOM_BUT_CLOSE"  ) );
180     buttonCancel->setAutoDefault( TRUE );
181     GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
182
183     GeometryGUI_PartitionDlgLayout->addWidget( GroupButtons, 3, 0 );
184
185     /***************************************************************/
186     Init(Sel) ; /* Initialisations */
187 }
188
189
190 //=================================================================================
191 // function : ~GeometryGUI_PartitionDlg()
192 // purpose  : Destroys the object and frees any allocated resources
193 //=================================================================================
194 GeometryGUI_PartitionDlg::~GeometryGUI_PartitionDlg()
195 {
196     // no need to delete child widgets, Qt does it all for us
197 }
198
199
200
201 //=================================================================================
202 // function : Init()
203 // purpose  :
204 //=================================================================================
205 void GeometryGUI_PartitionDlg::Init( SALOME_Selection* Sel )
206 {
207   myEditCurrentArgument = LineEditC1A1 ;        
208   mySelection = Sel;
209   myGeomGUI = GeometryGUI::GetGeometryGUI() ;
210   myOkListShapes = myOkListTools = myOkKeepShape = myOkRemoveShape = false ;
211
212   myGeomGUI->SetActiveDialogBox( (QDialog*)this ) ;
213
214   /* type for sub shape selection */
215   ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_SHAPE"  ) );
216 //  ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_SOLID"  ) );
217   ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_SHELL"  ) );
218   ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_FACE"   ) );
219   ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_WIRE"   ) );
220   ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_EDGE"   ) );
221   ComboBox1->insertItem( tr( "RECONSTRUCTION_LIMIT_VERTEX" ) );
222
223   /* Current item is 'Shape' */
224   ComboBox1->setCurrentItem(0);
225   myLimit = ComboBox1->currentItem();
226
227   /* Filter definitions */
228   Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
229   myGeom = GEOM::GEOM_Gen::_narrow(comp);
230
231   /* signals and slots connections */
232   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
233   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
234   connect( buttonApply,  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
235 //  connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );  
236   connect( ComboBox1, SIGNAL( activated(int) ), this, SLOT( ComboTextChanged() ) );
237
238   connect( SelectButtonC1A1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
239   connect( SelectButtonC1A2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
240
241   connect( SelectButtonC2A1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
242   connect( SelectButtonC2A2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) ) ;
243
244   connect( LineEditC1A1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
245   connect( LineEditC1A2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
246   connect( LineEditC2A1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
247   connect( LineEditC2A2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) ) ;
248
249   connect( myGeomGUI,   SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
250   connect( mySelection, SIGNAL( currentSelectionChanged() ),      this, SLOT( SelectionIntoArgument() ) );
251   /* to close dialog if study change */
252   connect( myGeomGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
253  
254   /* Move widget on the botton right corner of main widget */
255   int x, y ;
256   myGeomGUI->DefineDlgPosition( this, x, y ) ;
257   this->move( x, y ) ;
258   this->show() ; /* displays Dialog */
259
260   ComboTextChanged();
261   return ;
262 }
263
264 //=================================================================================
265 // function : ClickOnOk()
266 // purpose  :
267 //=================================================================================
268 void GeometryGUI_PartitionDlg::ClickOnOk()
269 {
270   this->ClickOnApply() ;
271   this->ClickOnCancel() ;
272
273   return ;
274 }
275
276 //=================================================================================
277 // function : ClickOnApply()
278 // purpose  :
279 //=================================================================================
280 void GeometryGUI_PartitionDlg::ClickOnApply()
281 {
282   myGeomGUI->GetDesktop()->putInfo( tr("") ) ;
283   if (myOkListShapes || myOkListTools || myOkKeepShape || myOkRemoveShape)
284   {
285     GEOM::shape_type limit;
286     switch (myLimit) {
287     case 0: limit = GEOM::SHAPE ; break;  
288     case 1: limit = GEOM::SOLID ; break;
289     case 2: limit = GEOM::SHELL ; break;
290     case 3: limit = GEOM::FACE  ; break;
291     case 4: limit = GEOM::WIRE  ; break; 
292     case 5: limit = GEOM::EDGE  ; break; 
293     case 6: limit = GEOM::VERTEX; break; 
294     default:limit = GEOM::SHAPE;
295     }
296     myGeomGUI->MakePartitionAndDisplay (myListShapes,
297                                         myListTools,
298                                         myListKeepInside,
299                                         myListRemoveInside,
300                                         limit);
301
302   }
303   // accept();
304   return ;
305 }
306
307
308 //=================================================================================
309 // function : ClickOnCancel()
310 // purpose  :
311 //=================================================================================
312 void GeometryGUI_PartitionDlg::ClickOnCancel()
313 {
314   myGeomGUI->EraseSimulationShape() ;
315   disconnect( mySelection, 0, this, 0 );
316   myGeomGUI->ResetState() ;
317   reject() ;
318   return ;
319 }
320
321
322 //=================================================================================
323 // function : ConstructorsClicked()
324 // purpose  :
325 //=================================================================================
326 // void GeometryGUI_PartitionDlg::ConstructorsClicked(int constructorId)
327 // {
328 // }
329
330
331 //=================================================================================
332 // function : SelectionIntoArgument()
333 // purpose  : Called when selection as changed or other case
334 //=================================================================================
335 void GeometryGUI_PartitionDlg::SelectionIntoArgument()
336 {
337   myEditCurrentArgument->setText("") ;
338   QString aString = ""; /* name of selection */
339   int nbSel = myGeomGUI->GetNameOfSelectedIObjects(mySelection, aString) ;
340
341   if ( nbSel < 1 ) {
342     if      ( myEditCurrentArgument == LineEditC1A1 ) {
343       myOkListShapes = false ;
344     }
345     else if ( myEditCurrentArgument == LineEditC1A2 ) {
346       myOkListTools = false ;
347     }
348     else if ( myEditCurrentArgument == LineEditC2A1 ) {
349       myOkListTools = false ;
350     }
351     else if ( myEditCurrentArgument == LineEditC2A2 ) {
352       myOkListTools = false ;
353     }
354     return ;
355   }
356         
357   if ( myEditCurrentArgument == LineEditC1A1  ) {
358     myGeomGUI->ConvertListOfIOInListOfIOR(mySelection->StoredIObjects(), myListShapes) ;
359     myEditCurrentArgument->setText(aString) ;
360     myOkListShapes = true ;
361   }
362   else if ( myEditCurrentArgument == LineEditC1A2  ) {
363     myGeomGUI->ConvertListOfIOInListOfIOR(mySelection->StoredIObjects(), myListTools)  ;
364     myEditCurrentArgument->setText(aString) ;
365     myOkListTools = true ;
366   }
367   else if ( myEditCurrentArgument == LineEditC2A1 ) {
368     myGeomGUI->ConvertListOfIOInListOfIOR(mySelection->StoredIObjects(), myListRemoveInside)  ;
369     myEditCurrentArgument->setText(aString) ;
370     myOkKeepShape = true ;
371   }
372   else if ( myEditCurrentArgument == LineEditC2A2 ) {
373     myGeomGUI->ConvertListOfIOInListOfIOR(mySelection->StoredIObjects(), myListKeepInside)  ;
374     myEditCurrentArgument->setText(aString) ;
375     myOkRemoveShape = true ;
376   }
377   /* no simulation */
378   return ;
379 }
380
381
382 //=================================================================================
383 // function : SetEditCurrentArgument()
384 // purpose  :
385 //=================================================================================
386 void GeometryGUI_PartitionDlg::SetEditCurrentArgument()
387 {
388   QPushButton* send = (QPushButton*)sender();
389   
390   if (send == SelectButtonC1A1) {
391     LineEditC1A1->setFocus() ;
392     myEditCurrentArgument = LineEditC1A1 ;
393   }
394   else if(send == SelectButtonC1A2) {
395     LineEditC1A2->setFocus() ;
396     myEditCurrentArgument = LineEditC1A2 ;
397   }
398   else if(send == SelectButtonC2A1) {
399     LineEditC2A1->setFocus() ;
400     myEditCurrentArgument = LineEditC2A1 ;
401   }
402   else if(send == SelectButtonC2A2) {
403     LineEditC2A2->setFocus() ;
404     myEditCurrentArgument = LineEditC2A2 ;
405   }
406   SelectionIntoArgument() ;
407   
408   return ;
409 }
410
411 //=================================================================================
412 // function : LineEditReturnPressed()
413 // purpose  :
414 //=================================================================================
415 void GeometryGUI_PartitionDlg::LineEditReturnPressed()
416 {  
417   QLineEdit* send = (QLineEdit*)sender();  
418   if( send == LineEditC1A1 )
419     myEditCurrentArgument = LineEditC1A1 ;
420   else if ( send == LineEditC1A2 )
421     myEditCurrentArgument = LineEditC1A2 ; 
422   else if ( send == LineEditC2A1 )
423     myEditCurrentArgument = LineEditC2A1 ; 
424   else if ( send == LineEditC2A2 )
425     myEditCurrentArgument = LineEditC2A2 ; 
426   else
427     return ;
428   
429   /* User name of object input management                          */
430   /* If successfull the selection is changed and signal emitted... */
431   /* so SelectionIntoArgument() is automatically called.           */
432   const QString objectUserName = myEditCurrentArgument->text() ;
433   QWidget* thisWidget = (QWidget*)this ;
434   if( myGeomGUI->SelectionByNameInDialogs( thisWidget, objectUserName, mySelection ) ) {
435     myEditCurrentArgument->setText( objectUserName ) ;
436   }
437   return ;
438 }
439
440
441 //=================================================================================
442 // function : DeactivateActiveDialog()
443 // purpose  :
444 //=================================================================================
445
446 void GeometryGUI_PartitionDlg::DeactivateActiveDialog()
447 {
448   if ( GroupConstructors->isEnabled() ) {
449     GroupConstructors->setEnabled(false) ;
450     GroupC1->setEnabled(false) ;
451     GroupC2->setEnabled(false) ;
452     GroupButtons->setEnabled(false) ;
453   }
454   return ;
455 }
456
457
458 //=================================================================================
459 // function : ActivateThisDialog()
460 // purpose  :
461 //=================================================================================
462 void GeometryGUI_PartitionDlg::ActivateThisDialog()
463 {
464   /* Emit a signal to deactivate the active dialog */
465   myGeomGUI->EmitSignalDeactivateDialog() ;   
466   GroupConstructors->setEnabled(true) ;
467   GroupC1->setEnabled(true) ;
468   GroupC2->setEnabled(true) ;
469   GroupButtons->setEnabled(true) ;
470   return ;
471 }
472
473
474 //=================================================================================
475 // function : enterEvent()
476 // purpose  :
477 //=================================================================================
478 void GeometryGUI_PartitionDlg::enterEvent(QEvent* e)
479 {
480   if ( GroupConstructors->isEnabled() )
481     return ;  
482   ActivateThisDialog() ;
483   return ;
484 }
485
486
487 //=================================================================================
488 // function : closeEvent()
489 // purpose  :
490 //=================================================================================
491 void GeometryGUI_PartitionDlg::closeEvent( QCloseEvent* e )
492 {
493   /* same than click on cancel button */
494   this->ClickOnCancel() ;
495   return ;
496 }
497
498 //=======================================================================
499 //function : ComboTextChanged
500 //purpose  : 
501 //=======================================================================
502
503 void GeometryGUI_PartitionDlg::ComboTextChanged()
504 {
505
506  myLimit = ComboBox1->currentItem();
507  GroupC2->setEnabled( ComboBox1->currentItem() < 3 );
508
509  return ;
510 }