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