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