Salome HOME
45871a7db05a1762baa7f50ac7f5f911ad111731
[modules/geom.git] / src / OperationGUI / OperationGUI_PartitionDlg.cxx
1 // Copyright (C) 2007-2011  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_2Sel1List2Check( 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   GroupPoints->CheckButton2->setText( tr( "GEOM_NO_SELF_INTERSECTION" ) );
83
84   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
85   layout->setMargin( 0 ); layout->setSpacing( 6 );
86   layout->addWidget( GroupPoints );
87
88   /***************************************************************/
89
90   setHelpFileName( "partition_page.html" );
91  
92   Init();
93 }
94
95
96 //=================================================================================
97 // function : ~OperationGUI_PartitionDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 OperationGUI_PartitionDlg::~OperationGUI_PartitionDlg()
101 {
102   // no need to delete child widgets, Qt does it all for us
103 }
104
105 void OperationGUI_PartitionDlg::SetListMaterials( GEOM::ListOfLong ListMaterials )
106 {
107   myListMaterials = ListMaterials; 
108 }
109   
110 GEOM::ListOfLong OperationGUI_PartitionDlg::GetListMaterials()
111
112   return myListMaterials; 
113 }
114
115 //=================================================================================
116 // function : Init()
117 // purpose  :
118 //=================================================================================
119 void OperationGUI_PartitionDlg::Init()
120 {
121   /* type for sub shape selection */
122   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SOLID" ) );
123   GroupPoints->ComboBox1->setItemData(GroupPoints->ComboBox1->count()-1, GEOM::SOLID);
124   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SHELL" ) );
125   GroupPoints->ComboBox1->setItemData(GroupPoints->ComboBox1->count()-1, GEOM::SHELL);
126   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_FACE" ) );
127   GroupPoints->ComboBox1->setItemData(GroupPoints->ComboBox1->count()-1, GEOM::FACE);
128   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_WIRE" ) );
129   GroupPoints->ComboBox1->setItemData(GroupPoints->ComboBox1->count()-1, GEOM::WIRE);
130   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_EDGE" ) );
131   GroupPoints->ComboBox1->setItemData(GroupPoints->ComboBox1->count()-1, GEOM::EDGE);
132   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_VERTEX" ) );
133   GroupPoints->ComboBox1->setItemData(GroupPoints->ComboBox1->count()-1, GEOM::VERTEX);
134   GroupPoints->CheckButton1->setChecked( false );
135   
136   mainFrame()->GroupBoxPublish->show();
137
138   /* signals and slots connections */
139   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
140   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
141
142   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
143   
144   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
145   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
146   
147   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
148   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
149   
150   connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
151
152   connect( myGeomGUI->getApp()->selectionMgr(),
153            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
154
155   connect( GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(processPreview()) );
156   connect( GroupPoints->CheckButton2, SIGNAL(toggled(bool)), this, SLOT(processPreview()) );
157   
158   initName( tr( "GEOM_PARTITION" ) );
159
160   ConstructorsClicked( 0 );
161   GroupPoints->PushButton1->click();
162 }
163
164
165 //=================================================================================
166 // function : ConstructorsClicked()
167 // purpose  : Radio button management
168 //=================================================================================
169 void OperationGUI_PartitionDlg::ConstructorsClicked( int constructorId )
170 {
171   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
172   globalSelection();
173   
174   //myListShapes.length( 0 );
175   //myListTools.length( 0 );  
176   //myListKeepInside.length( 0 );   // obsolete
177   //myListRemoveInside.length( 0 ); // obsolete
178   //myListMaterials.length( 0 );    // obsolete
179   
180   switch ( constructorId ) {
181   case 0: /*Full partition */
182     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
183     GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
184     GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECTS" ) );
185     GroupPoints->TextLabel3->show();
186     GroupPoints->ComboBox1->show();
187     GroupPoints->ComboBox1->setCurrentIndex( 0 );
188     GroupPoints->CheckButton1->show();
189     GroupPoints->CheckButton2->show();
190     GroupPoints->PushButton1->setDown( true );
191     GroupPoints->PushButton2->setDown( false );
192     GroupPoints->LineEdit1->setEnabled(true);
193     GroupPoints->LineEdit2->setEnabled(false);
194     break;
195   case 1: /*Half-space partition */
196     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION_HALFSPACE" ) );
197     GroupPoints->TextLabel3->hide();
198     GroupPoints->ComboBox1->hide();
199     GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
200     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE" ) );
201     GroupPoints->CheckButton1->hide();
202     GroupPoints->CheckButton2->hide();
203     GroupPoints->PushButton1->setDown( true );
204     GroupPoints->LineEdit1->setEnabled(true);
205     break;
206   } 
207
208   myEditCurrentArgument = GroupPoints->LineEdit1;
209   GroupPoints->LineEdit1->clear();
210   GroupPoints->LineEdit2->clear();
211
212   qApp->processEvents();
213   updateGeometry();
214   resize( minimumSizeHint() );
215
216   myEditCurrentArgument->setFocus();
217   connect( myGeomGUI->getApp()->selectionMgr(), 
218            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
219   SelectionIntoArgument();
220 }
221
222
223 //=================================================================================
224 // function : ClickOnOk()
225 // purpose  :
226 //=================================================================================
227 void OperationGUI_PartitionDlg::ClickOnOk()
228 {
229   setIsApplyAndClose( true );
230   if ( ClickOnApply() )
231     ClickOnCancel();
232 }
233
234
235 //=================================================================================
236 // function : ClickOnApply()
237 // purpose  :
238 //=================================================================================
239 bool OperationGUI_PartitionDlg::ClickOnApply()
240 {
241   if ( !onAccept() )
242     return false;
243   
244
245   initName();
246   //  0020854: EDF 1398 GEOM: Ergonomy of Partition GUI window 
247   //  ConstructorsClicked( getConstructorId() );
248   return true;
249 }
250
251
252 //=================================================================================
253 // function : SelectionIntoArgument()
254 // purpose  : Called when selection as changed or other case
255 //=================================================================================
256 void OperationGUI_PartitionDlg::SelectionIntoArgument()
257 {
258   myEditCurrentArgument->setText( "" );
259   QString aString = "";
260   
261   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
262   SALOME_ListIO aSelList;
263   aSelMgr->selectedObjects(aSelList);
264
265   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
266     
267   if ( getConstructorId() == 1 ) {
268     // for half-selection, only one shape can be selected as an object
269     // and only one plane as a tool
270     if ( nbSel != 1 ) {
271       if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
272         myListShapes.length( 0 );
273         // myListMaterials.length( 0 ); // obsolete
274         return;
275       }
276       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
277         myListTools.length( 0 );
278         return;
279       }
280     }
281   }
282   else {
283     if ( nbSel < 1 ) {
284       if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
285         myListShapes.length( 0 );
286         //myListMaterials.length( 0 ); // obsolete
287         return;
288       }
289       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
290         myListTools.length( 0 );
291         return;
292       }
293     }
294   }
295   
296   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
297     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
298     //myListMaterials.length( 0 ); // obsolete
299     
300     GEOM::shape_type type = GEOM::SHAPE;
301     for (int i = 0; i < myListShapes.length(); i++)
302       type = qMin( type, myListShapes[i]->GetMaxShapeType() );
303     int idx = qMax( 0, GroupPoints->ComboBox1->findData( type ) );
304     GroupPoints->ComboBox1->setCurrentIndex( idx );
305
306     if ( !myListShapes.length() )
307       return;
308   }
309   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
310     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListTools, true);
311     if ( !myListTools.length() )
312       return;
313     else if ( myListTools.length() == 1 && getConstructorId() == 1 ) {
314       GEOM::GEOM_IMeasureOperations_var mOp = getGeomEngine()->GetIMeasureOperations( getStudyId() );
315       GEOM::ListOfLong_var   intList;
316       GEOM::ListOfDouble_var dblList;
317       GEOM::GEOM_IKindOfShape::shape_kind kind = mOp->KindOfShape( myListTools[0].in(), intList.out(), dblList.out() );
318       mOp->UnRegister();
319       if ( kind < GEOM::GEOM_IKindOfShape::DISK_CIRCLE || kind > GEOM::GEOM_IKindOfShape::PLANAR ) {
320         myListTools.length( 0 );
321         return;
322       }
323     }
324   }
325   
326   myEditCurrentArgument->setText( aString );
327   processPreview();
328 }
329
330
331 //=================================================================================
332 // function : SetEditCurrentArgument()
333 // purpose  :
334 //=================================================================================
335 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
336 {
337   QPushButton* send = (QPushButton*)sender();
338   
339   if ( send == GroupPoints->PushButton1 ) {
340     myEditCurrentArgument = GroupPoints->LineEdit1;
341     GroupPoints->PushButton2->setDown(false);
342     GroupPoints->LineEdit1->setEnabled(true);
343     GroupPoints->LineEdit2->setEnabled(false);
344   }
345   else if ( send == GroupPoints->PushButton2 ) {
346     myGeomGUI->getApp()->selectionMgr()->clearSelected(); //clear prewious selection
347     myEditCurrentArgument = GroupPoints->LineEdit2;
348     GroupPoints->PushButton1->setDown(false);
349     GroupPoints->LineEdit1->setEnabled(false);
350     GroupPoints->LineEdit2->setEnabled(true);
351     if ( getConstructorId() == 1 )
352       globalSelection( GEOM_PLANE  );
353   }
354  
355   globalSelection( GEOM_ALLSHAPES );
356       
357   myEditCurrentArgument->setFocus();
358   SelectionIntoArgument();
359   send->setDown(true);
360   processPreview();
361 }
362
363
364 //=================================================================================
365 // function : LineEditReturnPressed()
366 // purpose  :
367 //=================================================================================
368 void OperationGUI_PartitionDlg::LineEditReturnPressed()
369 {
370   QLineEdit* send = (QLineEdit*)sender();
371
372   if ( send == GroupPoints->LineEdit1 || 
373        send == GroupPoints->LineEdit2 ) {
374       myEditCurrentArgument = send;
375       GEOMBase_Skeleton::LineEditReturnPressed();
376   }
377 }
378
379
380 //=================================================================================
381 // function : ActivateThisDialog()
382 // purpose  :
383 //=================================================================================
384 void OperationGUI_PartitionDlg::ActivateThisDialog()
385 {
386   GEOMBase_Skeleton::ActivateThisDialog();
387   connect( myGeomGUI->getApp()->selectionMgr(), 
388            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
389
390   ConstructorsClicked( getConstructorId() ); 
391   processPreview();
392 }
393
394
395 //=================================================================================
396 // function : enterEvent()
397 // purpose  :
398 //=================================================================================
399 void OperationGUI_PartitionDlg::enterEvent( QEvent* )
400 {
401   if ( !mainFrame()->GroupConstructors->isEnabled() )
402     this->ActivateThisDialog();
403 }
404
405
406 //=================================================================================
407 // function : createOperation
408 // purpose  :
409 //=================================================================================
410 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
411 {
412   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
413 }
414
415
416 //=================================================================================
417 // function : isValid
418 // purpose  :
419 //=================================================================================
420 bool OperationGUI_PartitionDlg::isValid( QString& )
421 {
422   return ( myListShapes.length() || myListTools.length() ); // || myListKeepInside.length() || myListRemoveInside.length()  // obsolete
423 }
424
425
426 //=================================================================================
427 // function : execute
428 // purpose  :
429 //=================================================================================
430 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
431 {
432   bool res = false;
433   GEOM::GEOM_Object_var anObj;
434
435   int aLimit               = GetLimit();
436   int aKeepNonlimitShapes  = GroupPoints->CheckButton1->isChecked();
437   bool aNoSelfIntersection = GroupPoints->CheckButton2->isChecked();
438
439   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow( getOperation() );
440
441   switch ( getConstructorId() ) {
442   case 0:
443     anObj = aNoSelfIntersection ?
444       anOper->MakePartitionNonSelfIntersectedShape( myListShapes, myListTools,
445                                                     myListKeepInside, myListRemoveInside,
446                                                     aLimit, false, myListMaterials, aKeepNonlimitShapes ) :
447       anOper->MakePartition( myListShapes, myListTools,
448                              myListKeepInside, myListRemoveInside,
449                              aLimit, false, myListMaterials, aKeepNonlimitShapes );
450     res = true;
451     break;
452   case 1:
453     anObj = anOper->MakeHalfPartition( myListShapes[0].in(), myListTools[0].in() );
454     res = true;
455     break;
456   }
457
458   if ( !anObj->_is_nil() ) {
459     TopoDS_Shape aShape;
460     GEOMBase::GetShape( anObj, aShape, TopAbs_SHAPE );
461     TopoDS_Iterator It( aShape, Standard_True, Standard_True );
462     int nbSubshapes = 0;
463     for ( ; It.More(); It.Next() )
464       nbSubshapes++;
465
466     if ( nbSubshapes )
467       objects.push_back( anObj._retn() );
468     else
469       SUIT_MessageBox::warning( this,
470                                 QObject::tr( "GEOM_ERROR" ),
471                                 QObject::tr( "GEOM_WRN_PARTITION_RESULT_EMPTY" ) );
472   }
473   
474   return res;
475 }
476
477 //=================================================================================
478 // function : restoreSubShapes
479 // purpose  :
480 //=================================================================================
481 void OperationGUI_PartitionDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
482                                                   SALOMEDS::SObject_ptr theSObject )
483 {
484   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
485     // empty list of arguments means that all arguments should be restored
486     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
487                                          /*theFindMethod=*/GEOM::FSM_GetInPlaceByHistory,
488                                          /*theInheritFirstArg=*/myListShapes.length() == 1,
489                                          mainFrame()->CheckBoxAddPrefix->isChecked() ); // ? false
490   }
491 }
492
493 //=======================================================================
494 //function : ComboTextChanged
495 //purpose  : 
496 //=======================================================================
497 void OperationGUI_PartitionDlg::ComboTextChanged()
498 {
499   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
500   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
501   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
502   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
503   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
504   //GroupPoints->PushButton3->setEnabled(IsEnabled);
505   //GroupPoints->PushButton4->setEnabled(IsEnabled);
506   processPreview();
507 }
508
509 //=================================================================================
510 // function : GetLimit()
511 // purpose  : 
512 //=================================================================================
513 int OperationGUI_PartitionDlg::GetLimit() const
514 {
515   int aLimit = GroupPoints->ComboBox1->currentIndex();
516
517   switch ( aLimit ) {
518   case 0:  aLimit = GEOM::SOLID ; break;
519   case 1:  aLimit = GEOM::SHELL ; break;
520   case 2:  aLimit = GEOM::FACE  ; break;
521   case 3:  aLimit = GEOM::WIRE  ; break;
522   case 4:  aLimit = GEOM::EDGE  ; break;
523   case 5:  aLimit = GEOM::VERTEX; break;
524   default: aLimit = GEOM::SHAPE ;
525   }
526
527   return aLimit;
528 }