Salome HOME
Update copyrights 2014.
[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(true);
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( mySelfInte,                SIGNAL(toggled(bool)), this, SLOT(processPreview()) );
171
172   initName( tr( "GEOM_PARTITION" ) );
173
174   ConstructorsClicked( 0 );
175   GroupPoints->PushButton1->click();
176 }
177
178
179 //=================================================================================
180 // function : ConstructorsClicked()
181 // purpose  : Radio button management
182 //=================================================================================
183 void OperationGUI_PartitionDlg::ConstructorsClicked( int constructorId )
184 {
185   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
186   globalSelection();
187
188   //myListShapes.length( 0 );
189   //myListTools.length( 0 );
190   //myListKeepInside.length( 0 );   // obsolete
191   //myListRemoveInside.length( 0 ); // obsolete
192   //myListMaterials.length( 0 );    // obsolete
193
194   switch ( constructorId ) {
195   case 0: /*Full partition */
196     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
197     GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
198     GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECTS" ) );
199     GroupPoints->TextLabel3->show();
200     GroupPoints->ComboBox1->show();
201     GroupPoints->ComboBox1->setCurrentIndex( 0 );
202     GroupPoints->CheckButton1->show();
203     GroupPoints->CheckButton2->show();
204     GroupPoints->PushButton1->setDown( true );
205     GroupPoints->PushButton2->setDown( false );
206     GroupPoints->LineEdit1->setEnabled(true);
207     GroupPoints->LineEdit2->setEnabled(false);
208     break;
209   case 1: /*Half-space partition */
210     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION_HALFSPACE" ) );
211     GroupPoints->TextLabel3->hide();
212     GroupPoints->ComboBox1->hide();
213     GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECT" ) );
214     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE" ) );
215     GroupPoints->CheckButton1->hide();
216     GroupPoints->CheckButton2->hide();
217     GroupPoints->PushButton1->setDown( true );
218     GroupPoints->LineEdit1->setEnabled(true);
219     break;
220   }
221
222   myEditCurrentArgument = GroupPoints->LineEdit1;
223   GroupPoints->LineEdit1->clear();
224   GroupPoints->LineEdit2->clear();
225
226   qApp->processEvents();
227   updateGeometry();
228   resize( minimumSizeHint() );
229
230   myEditCurrentArgument->setFocus();
231   connect( myGeomGUI->getApp()->selectionMgr(),
232            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
233   SelectionIntoArgument();
234 }
235
236
237 //=================================================================================
238 // function : ClickOnOk()
239 // purpose  :
240 //=================================================================================
241 void OperationGUI_PartitionDlg::ClickOnOk()
242 {
243   setIsApplyAndClose( true );
244   if ( ClickOnApply() )
245     ClickOnCancel();
246 }
247
248
249 //=================================================================================
250 // function : ClickOnApply()
251 // purpose  :
252 //=================================================================================
253 bool OperationGUI_PartitionDlg::ClickOnApply()
254 {
255   if ( !onAccept() )
256     return false;
257
258   initName();
259   //  0020854: EDF 1398 GEOM: Ergonomy of Partition GUI window
260   //  ConstructorsClicked( getConstructorId() );
261   return true;
262 }
263
264
265 //=================================================================================
266 // function : SelectionIntoArgument()
267 // purpose  : Called when selection as changed or other case
268 //=================================================================================
269 void OperationGUI_PartitionDlg::SelectionIntoArgument()
270 {
271   myEditCurrentArgument->setText( "" );
272   QString aString = "";
273
274   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
275   SALOME_ListIO aSelList;
276   aSelMgr->selectedObjects(aSelList);
277
278   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
279
280   if ( getConstructorId() == 1 ) {
281     // for half-selection, only one shape can be selected as an object
282     // and only one plane as a tool
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   else {
296     if ( nbSel < 1 ) {
297       if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
298         myListShapes.length( 0 );
299         //myListMaterials.length( 0 ); // obsolete
300         return;
301       }
302       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
303         myListTools.length( 0 );
304         return;
305       }
306     }
307   }
308
309   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
310     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
311     //myListMaterials.length( 0 ); // obsolete
312
313     GEOM::shape_type type = GEOM::SHAPE;
314     for (int i = 0; i < myListShapes.length(); i++)
315       type = qMin( type, myListShapes[i]->GetMaxShapeType() );
316     int idx = qMax( 0, GroupPoints->ComboBox1->findData( type ) );
317     GroupPoints->ComboBox1->setCurrentIndex( idx );
318
319     if ( !myListShapes.length() )
320       return;
321   }
322   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
323     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListTools, true);
324     if ( !myListTools.length() )
325       return;
326     else if ( myListTools.length() == 1 && getConstructorId() == 1 ) {
327       GEOM::GEOM_IMeasureOperations_var mOp = getGeomEngine()->GetIMeasureOperations( getStudyId() );
328       GEOM::ListOfLong_var   intList;
329       GEOM::ListOfDouble_var dblList;
330       GEOM::GEOM_IKindOfShape::shape_kind kind = mOp->KindOfShape( myListTools[0].in(), intList.out(), dblList.out() );
331       mOp->UnRegister();
332       if ( kind < GEOM::GEOM_IKindOfShape::DISK_CIRCLE || kind > GEOM::GEOM_IKindOfShape::PLANAR ) {
333         myListTools.length( 0 );
334         return;
335       }
336     }
337   }
338
339   myEditCurrentArgument->setText( aString );
340   processPreview();
341 }
342
343
344 //=================================================================================
345 // function : SetEditCurrentArgument()
346 // purpose  :
347 //=================================================================================
348 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
349 {
350   QPushButton* send = (QPushButton*)sender();
351
352   if ( send == GroupPoints->PushButton1 ) {
353     myEditCurrentArgument = GroupPoints->LineEdit1;
354     GroupPoints->PushButton2->setDown(false);
355     GroupPoints->LineEdit1->setEnabled(true);
356     GroupPoints->LineEdit2->setEnabled(false);
357   }
358   else if ( send == GroupPoints->PushButton2 ) {
359     myGeomGUI->getApp()->selectionMgr()->clearSelected(); //clear prewious selection
360     myEditCurrentArgument = GroupPoints->LineEdit2;
361     GroupPoints->PushButton1->setDown(false);
362     GroupPoints->LineEdit1->setEnabled(false);
363     GroupPoints->LineEdit2->setEnabled(true);
364     if ( getConstructorId() == 1 )
365       globalSelection( GEOM_PLANE  );
366   }
367
368   globalSelection( GEOM_ALLSHAPES );
369
370   myEditCurrentArgument->setFocus();
371   SelectionIntoArgument();
372   send->setDown(true);
373   processPreview();
374 }
375
376
377 //=================================================================================
378 // function : LineEditReturnPressed()
379 // purpose  :
380 //=================================================================================
381 void OperationGUI_PartitionDlg::LineEditReturnPressed()
382 {
383   QLineEdit* send = (QLineEdit*)sender();
384
385   if ( send == GroupPoints->LineEdit1 ||
386        send == GroupPoints->LineEdit2 ) {
387       myEditCurrentArgument = send;
388       GEOMBase_Skeleton::LineEditReturnPressed();
389   }
390 }
391
392
393 //=================================================================================
394 // function : ActivateThisDialog()
395 // purpose  :
396 //=================================================================================
397 void OperationGUI_PartitionDlg::ActivateThisDialog()
398 {
399   GEOMBase_Skeleton::ActivateThisDialog();
400   connect( myGeomGUI->getApp()->selectionMgr(),
401            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
402
403   ConstructorsClicked( getConstructorId() );
404   processPreview();
405 }
406
407
408 //=================================================================================
409 // function : enterEvent()
410 // purpose  :
411 //=================================================================================
412 void OperationGUI_PartitionDlg::enterEvent( QEvent* )
413 {
414   if ( !mainFrame()->GroupConstructors->isEnabled() )
415     this->ActivateThisDialog();
416 }
417
418
419 //=================================================================================
420 // function : createOperation
421 // purpose  :
422 //=================================================================================
423 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
424 {
425   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
426 }
427
428
429 //=================================================================================
430 // function : isValid
431 // purpose  :
432 //=================================================================================
433 bool OperationGUI_PartitionDlg::isValid (QString& )
434 {
435   return (getConstructorId() == 0 ?
436           myListShapes.length() :
437           myListShapes.length() && myListTools.length());
438 }
439
440
441 //=================================================================================
442 // function : execute
443 // purpose  :
444 //=================================================================================
445 bool OperationGUI_PartitionDlg::execute (ObjectList& objects)
446 {
447   bool res = false;
448   GEOM::GEOM_Object_var anObj;
449   bool isDetectSelfInte = mySelfInte->isChecked();
450
451   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
452
453   switch ( getConstructorId() ) {
454   case 0:
455     {
456       int aLimit               = GetLimit();
457       int aKeepNonlimitShapes  = GroupPoints->CheckButton1->isChecked();
458       bool aNoSelfIntersection = GroupPoints->CheckButton2->isChecked();
459
460       anObj = aNoSelfIntersection ?
461         anOper->MakePartitionNonSelfIntersectedShape(myListShapes, myListTools,
462                                                      myListKeepInside, myListRemoveInside,
463                                                      aLimit, false, myListMaterials, aKeepNonlimitShapes,
464                                                      isDetectSelfInte) :
465         anOper->MakePartition(myListShapes, myListTools,
466                               myListKeepInside, myListRemoveInside,
467                               aLimit, false, myListMaterials, aKeepNonlimitShapes,
468                               isDetectSelfInte);
469       res = true;
470     }
471     break;
472   case 1:
473     {
474       anObj = anOper->MakeHalfPartition( myListShapes[0].in(), myListTools[0].in(),
475                                          isDetectSelfInte );
476       res = true;
477     }
478     break;
479   }
480
481   if ( !anObj->_is_nil() ) {
482     TopoDS_Shape aShape;
483     GEOMBase::GetShape( anObj, aShape, TopAbs_SHAPE );
484     TopoDS_Iterator It( aShape, Standard_True, Standard_True );
485     int nbSubshapes = 0;
486     for (; It.More(); It.Next())
487       nbSubshapes++;
488
489     if ( nbSubshapes )
490       objects.push_back( anObj._retn() );
491     else
492       SUIT_MessageBox::warning( this,
493                                 QObject::tr( "GEOM_ERROR" ),
494                                 QObject::tr( "GEOM_WRN_PARTITION_RESULT_EMPTY" ) );
495   }
496
497   return res;
498 }
499
500 //=================================================================================
501 // function : restoreSubShapes
502 // purpose  :
503 //=================================================================================
504 void OperationGUI_PartitionDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
505                                                   SALOMEDS::SObject_ptr theSObject )
506 {
507   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
508     // empty list of arguments means that all arguments should be restored
509     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
510                                          /*theFindMethod=*/GEOM::FSM_GetInPlaceByHistory,
511                                          /*theInheritFirstArg=*/myListShapes.length() == 1 && myListTools.length() == 0,
512                                          mainFrame()->CheckBoxAddPrefix->isChecked() ); // ? false
513   }
514 }
515
516 //=======================================================================
517 //function : ComboTextChanged
518 //purpose  :
519 //=======================================================================
520 void OperationGUI_PartitionDlg::ComboTextChanged()
521 {
522   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
523   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
524   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
525   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
526   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
527   //GroupPoints->PushButton3->setEnabled(IsEnabled);
528   //GroupPoints->PushButton4->setEnabled(IsEnabled);
529   processPreview();
530 }
531
532 //=================================================================================
533 // function : GetLimit()
534 // purpose  :
535 //=================================================================================
536 int OperationGUI_PartitionDlg::GetLimit() const
537 {
538   int aLimit = GroupPoints->ComboBox1->currentIndex();
539
540   switch ( aLimit ) {
541   case 0:  aLimit = GEOM::SOLID ; break;
542   case 1:  aLimit = GEOM::SHELL ; break;
543   case 2:  aLimit = GEOM::FACE  ; break;
544   case 3:  aLimit = GEOM::WIRE  ; break;
545   case 4:  aLimit = GEOM::EDGE  ; break;
546   case 5:  aLimit = GEOM::VERTEX; break;
547   default: aLimit = GEOM::SHAPE ;
548   }
549
550   return aLimit;
551 }