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