Salome HOME
Fix memory leaks
[modules/geom.git] / src / OperationGUI / OperationGUI_PartitionDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : OperationGUI_PartitionDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "OperationGUI_PartitionDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <GEOMImpl_Types.hxx>
33
34 #include <SUIT_Desktop.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SUIT_MessageBox.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40
41 #include <TopoDS_Iterator.hxx>
42 #include <TopoDS_Shape.hxx>
43
44 //=================================================================================
45 // class    : OperationGUI_PartitionDlg()
46 // purpose  : Constructs a OperationGUI_PartitionDlg which is a child of 'parent', with the 
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 OperationGUI_PartitionDlg::OperationGUI_PartitionDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
52   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
53 {
54   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
55   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_PARTITION" ) ) );
56   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_PARTITION_PLANE" ) ) );
57   QPixmap image2( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58
59   setWindowTitle( tr( "GEOM_PARTITION_TITLE" ) );
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_PARTITION" ) );
63   mainFrame()->RadioButton1->setIcon( image0 );
64   mainFrame()->RadioButton2->setIcon( image1 );
65   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
66   mainFrame()->RadioButton3->close();
67
68   // Full partition (contains half-space partition)
69   GroupPoints = new DlgRef_2Sel1List1Check( centralWidget() );
70   GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
71   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
72   GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECT" ) );
73   GroupPoints->TextLabel3->setText( tr( "GEOM_RECONSTRUCTION_LIMIT" ) );
74   GroupPoints->PushButton1->setIcon( image2 );
75   GroupPoints->PushButton2->setIcon( image2 );
76   GroupPoints->LineEdit1->setReadOnly( true );
77   GroupPoints->LineEdit2->setReadOnly( true );
78   GroupPoints->LineEdit1->setEnabled(true);
79   GroupPoints->LineEdit2->setEnabled(false);
80   GroupPoints->CheckButton1->setText( tr( "GEOM_KEEP_NONLIMIT_SHAPES" ) );
81
82   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
83   layout->setMargin( 0 ); layout->setSpacing( 6 );
84   layout->addWidget( GroupPoints );
85
86   /***************************************************************/
87
88   setHelpFileName( "partition_page.html" );
89  
90   Init();
91 }
92
93
94 //=================================================================================
95 // function : ~OperationGUI_PartitionDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 OperationGUI_PartitionDlg::~OperationGUI_PartitionDlg()
99 {
100   // no need to delete child widgets, Qt does it all for us
101 }
102
103 void OperationGUI_PartitionDlg::SetListMaterials( GEOM::ListOfLong ListMaterials )
104 {
105   myListMaterials = ListMaterials; 
106 }
107   
108 GEOM::ListOfLong OperationGUI_PartitionDlg::GetListMaterials()
109
110   return myListMaterials; 
111 }
112
113 //=================================================================================
114 // function : Init()
115 // purpose  :
116 //=================================================================================
117 void OperationGUI_PartitionDlg::Init()
118 {
119   /* type for sub shape selection */
120   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SOLID" ) );
121   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SHELL" ) );
122   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_FACE" ) );
123   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_WIRE" ) );
124   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_EDGE" ) );
125   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_VERTEX" ) );
126   GroupPoints->CheckButton1->setChecked( false );
127   
128   mainFrame()->GroupBoxPublish->show();
129
130   /* signals and slots connections */
131   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
132   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
133
134   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
135   
136   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
137   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
138   
139   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
140   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
141   
142   connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
143
144   connect( myGeomGUI->getApp()->selectionMgr(),
145            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
146   
147   initName( tr( "GEOM_PARTITION" ) );
148
149   ConstructorsClicked( 0 );
150   GroupPoints->PushButton1->click();
151 }
152
153
154 //=================================================================================
155 // function : ConstructorsClicked()
156 // purpose  : Radio button management
157 //=================================================================================
158 void OperationGUI_PartitionDlg::ConstructorsClicked( int constructorId )
159 {
160   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
161   globalSelection();
162   
163   myListShapes.length( 0 );
164   myListTools.length( 0 );  
165   myListKeepInside.length( 0 );
166   myListRemoveInside.length( 0 );
167   myListMaterials.length( 0 );
168   
169   switch ( constructorId ) {
170   case 0: /*Full partition */
171     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
172     GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECT" ) );
173     GroupPoints->TextLabel3->show();
174     GroupPoints->ComboBox1->show();
175     GroupPoints->ComboBox1->setCurrentIndex( 0 );
176     GroupPoints->CheckButton1->show();
177     GroupPoints->PushButton1->setDown( true );
178     GroupPoints->PushButton2->setDown( false );
179     GroupPoints->LineEdit1->setEnabled(true);
180     GroupPoints->LineEdit2->setEnabled(false);
181     break;
182   case 1: /*Half-space partition */
183     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION_HALFSPACE" ) );
184     GroupPoints->TextLabel3->hide();
185     GroupPoints->ComboBox1->hide();
186     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE" ) );
187     GroupPoints->CheckButton1->hide();
188     GroupPoints->PushButton1->setDown( true );
189     GroupPoints->LineEdit1->setEnabled(true);
190     break;
191   } 
192
193   myEditCurrentArgument = GroupPoints->LineEdit1;
194   GroupPoints->LineEdit1->clear();
195   GroupPoints->LineEdit2->clear();
196
197   qApp->processEvents();
198   updateGeometry();
199   resize( minimumSizeHint() );
200
201   myEditCurrentArgument->setFocus();
202   connect( myGeomGUI->getApp()->selectionMgr(), 
203            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
204 }
205
206
207 //=================================================================================
208 // function : ClickOnOk()
209 // purpose  :
210 //=================================================================================
211 void OperationGUI_PartitionDlg::ClickOnOk()
212 {
213   if ( ClickOnApply() )
214     ClickOnCancel();
215 }
216
217
218 //=================================================================================
219 // function : ClickOnApply()
220 // purpose  :
221 //=================================================================================
222 bool OperationGUI_PartitionDlg::ClickOnApply()
223 {
224   if ( !onAccept() )
225     return false;
226   
227   initName();
228   ConstructorsClicked( getConstructorId() );
229   return true;
230 }
231
232
233 //=================================================================================
234 // function : SelectionIntoArgument()
235 // purpose  : Called when selection as changed or other case
236 //=================================================================================
237 void OperationGUI_PartitionDlg::SelectionIntoArgument()
238 {
239   myEditCurrentArgument->setText( "" );
240   QString aString = "";
241   
242   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
243   SALOME_ListIO aSelList;
244   aSelMgr->selectedObjects(aSelList);
245
246   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
247     
248   if ( nbSel < 1 ) {
249     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
250       myListShapes.length( 0 );
251       myListMaterials.length( 0 );
252     }
253     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
254       myListTools.length( 0 );
255   }
256   
257   // One and only one plane can be selected
258   
259   if ( getConstructorId() == 1 &&
260        myEditCurrentArgument == GroupPoints->LineEdit2 && 
261        nbSel != 1 ) {
262     myListTools.length( 0 );
263     return;
264   }
265   
266   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
267     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
268     myListMaterials.length( 0 );
269     if ( !myListShapes.length() )
270       return;
271   }
272   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
273     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListTools, true);
274     if ( !myListTools.length() )
275       return;
276   }
277   
278   myEditCurrentArgument->setText( aString );
279 }
280
281
282 //=================================================================================
283 // function : SetEditCurrentArgument()
284 // purpose  :
285 //=================================================================================
286 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
287 {
288   QPushButton* send = (QPushButton*)sender();
289   
290   if ( send == GroupPoints->PushButton1 ) {
291     myEditCurrentArgument = GroupPoints->LineEdit1;
292     GroupPoints->PushButton2->setDown(false);
293     GroupPoints->LineEdit1->setEnabled(true);
294     GroupPoints->LineEdit2->setEnabled(false);
295   }
296   else if ( send == GroupPoints->PushButton2 ) {
297     myGeomGUI->getApp()->selectionMgr()->clearSelected(); //clear prewious selection
298     myEditCurrentArgument = GroupPoints->LineEdit2;
299     GroupPoints->PushButton1->setDown(false);
300     GroupPoints->LineEdit1->setEnabled(false);
301     GroupPoints->LineEdit2->setEnabled(true);
302     if ( getConstructorId() == 1 )
303       globalSelection( GEOM_PLANE  );
304   }
305  
306   globalSelection( GEOM_ALLSHAPES );
307       
308   myEditCurrentArgument->setFocus();
309   SelectionIntoArgument();
310   send->setDown(true);
311 }
312
313
314 //=================================================================================
315 // function : LineEditReturnPressed()
316 // purpose  :
317 //=================================================================================
318 void OperationGUI_PartitionDlg::LineEditReturnPressed()
319 {
320   QLineEdit* send = (QLineEdit*)sender();
321
322   if ( send == GroupPoints->LineEdit1 || 
323        send == GroupPoints->LineEdit2 ) {
324       myEditCurrentArgument = send;
325       GEOMBase_Skeleton::LineEditReturnPressed();
326   }
327 }
328
329
330 //=================================================================================
331 // function : ActivateThisDialog()
332 // purpose  :
333 //=================================================================================
334 void OperationGUI_PartitionDlg::ActivateThisDialog()
335 {
336   GEOMBase_Skeleton::ActivateThisDialog();
337   connect( myGeomGUI->getApp()->selectionMgr(), 
338            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
339
340   ConstructorsClicked( getConstructorId() ); 
341 }
342
343
344 //=================================================================================
345 // function : enterEvent()
346 // purpose  :
347 //=================================================================================
348 void OperationGUI_PartitionDlg::enterEvent( QEvent* )
349 {
350   if ( !mainFrame()->GroupConstructors->isEnabled() )
351     this->ActivateThisDialog();
352 }
353
354
355 //=================================================================================
356 // function : createOperation
357 // purpose  :
358 //=================================================================================
359 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
360 {
361   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
362 }
363
364
365 //=================================================================================
366 // function : isValid
367 // purpose  :
368 //=================================================================================
369 bool OperationGUI_PartitionDlg::isValid( QString& )
370 {
371   return ( myListShapes.length()     || myListTools.length() ||
372            myListKeepInside.length() || myListRemoveInside.length() );
373 }
374
375
376 //=================================================================================
377 // function : execute
378 // purpose  :
379 //=================================================================================
380 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
381 {
382   bool res = false;
383
384   GEOM::GEOM_Object_var anObj;
385   QString msg;
386
387   int aLimit = GetLimit();
388   int aConstructorId = getConstructorId();
389   int aKeepNonlimitShapes = 0;
390
391   if ( aConstructorId == 1 ) {
392     aLimit = GEOM::SHAPE;
393   }
394   else {
395     if ( GroupPoints->CheckButton1->isChecked() ) {
396       aKeepNonlimitShapes = 1;
397     }
398     else {
399       aKeepNonlimitShapes = 0;
400     }
401   }
402
403   if ( isValid( msg ) ) {
404     GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
405     anObj = anOper->MakePartition( myListShapes, myListTools,
406                                    myListKeepInside, myListRemoveInside,
407                                    aLimit, false, myListMaterials, aKeepNonlimitShapes );
408     res = true;
409   }
410
411   if ( !anObj->_is_nil() ) {
412     TopoDS_Shape aShape;
413     GEOMBase::GetShape(anObj, aShape, TopAbs_SHAPE);
414     TopoDS_Iterator It (aShape, Standard_True, Standard_True);
415     int nbSubshapes=0;
416     for (; It.More(); It.Next())
417       nbSubshapes++;
418
419     if (nbSubshapes)
420       objects.push_back( anObj._retn() );
421     else
422       SUIT_MessageBox::warning(this,
423                                QObject::tr("GEOM_ERROR"),
424                                QObject::tr("GEOM_WRN_PARTITION_RESULT_EMPTY"));
425   }
426
427   return res;
428 }
429
430 //=================================================================================
431 // function : restoreSubShapes
432 // purpose  :
433 //=================================================================================
434 void OperationGUI_PartitionDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
435                                                   SALOMEDS::SObject_ptr theSObject )
436 {
437   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
438     // empty list of arguments means that all arguments should be restored
439     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
440                                          /*theFindMethod=*/GEOM::FSM_GetInPlaceByHistory,
441                                          /*theInheritFirstArg=*/myListShapes.length() == 1 ); // ? false
442   }
443 }
444
445 //=======================================================================
446 //function : ComboTextChanged
447 //purpose  : 
448 //=======================================================================
449 void OperationGUI_PartitionDlg::ComboTextChanged()
450 {
451   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
452   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
453   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
454   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
455   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
456   //GroupPoints->PushButton3->setEnabled(IsEnabled);
457   //GroupPoints->PushButton4->setEnabled(IsEnabled);
458 }
459
460 //=================================================================================
461 // function : GetLimit()
462 // purpose  : 
463 //=================================================================================
464 int OperationGUI_PartitionDlg::GetLimit() const
465 {
466   int aLimit = GroupPoints->ComboBox1->currentIndex();
467
468   switch ( aLimit ) {
469   case 0:  aLimit = GEOM::SOLID ; break;
470   case 1:  aLimit = GEOM::SHELL ; break;
471   case 2:  aLimit = GEOM::FACE  ; break;
472   case 3:  aLimit = GEOM::WIRE  ; break;
473   case 4:  aLimit = GEOM::EDGE  ; break;
474   case 5:  aLimit = GEOM::VERTEX; break;
475   default: aLimit = GEOM::SHAPE ;
476   }
477
478   return aLimit;
479 }