Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / OperationGUI / OperationGUI_PartitionDlg.cxx
1 // Copyright (C) 2007-2012  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   initName();
245   //  0020854: EDF 1398 GEOM: Ergonomy of Partition GUI window
246   //  ConstructorsClicked( getConstructorId() );
247   return true;
248 }
249
250
251 //=================================================================================
252 // function : SelectionIntoArgument()
253 // purpose  : Called when selection as changed or other case
254 //=================================================================================
255 void OperationGUI_PartitionDlg::SelectionIntoArgument()
256 {
257   myEditCurrentArgument->setText( "" );
258   QString aString = "";
259
260   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
261   SALOME_ListIO aSelList;
262   aSelMgr->selectedObjects(aSelList);
263
264   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
265
266   if ( getConstructorId() == 1 ) {
267     // for half-selection, only one shape can be selected as an object
268     // and only one plane as a tool
269     if ( nbSel != 1 ) {
270       if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
271         myListShapes.length( 0 );
272         // myListMaterials.length( 0 ); // obsolete
273         return;
274       }
275       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
276         myListTools.length( 0 );
277         return;
278       }
279     }
280   }
281   else {
282     if ( nbSel < 1 ) {
283       if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
284         myListShapes.length( 0 );
285         //myListMaterials.length( 0 ); // obsolete
286         return;
287       }
288       else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
289         myListTools.length( 0 );
290         return;
291       }
292     }
293   }
294
295   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
296     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
297     //myListMaterials.length( 0 ); // obsolete
298
299     GEOM::shape_type type = GEOM::SHAPE;
300     for (int i = 0; i < myListShapes.length(); i++)
301       type = qMin( type, myListShapes[i]->GetMaxShapeType() );
302     int idx = qMax( 0, GroupPoints->ComboBox1->findData( type ) );
303     GroupPoints->ComboBox1->setCurrentIndex( idx );
304
305     if ( !myListShapes.length() )
306       return;
307   }
308   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
309     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListTools, true);
310     if ( !myListTools.length() )
311       return;
312     else if ( myListTools.length() == 1 && getConstructorId() == 1 ) {
313       GEOM::GEOM_IMeasureOperations_var mOp = getGeomEngine()->GetIMeasureOperations( getStudyId() );
314       GEOM::ListOfLong_var   intList;
315       GEOM::ListOfDouble_var dblList;
316       GEOM::GEOM_IKindOfShape::shape_kind kind = mOp->KindOfShape( myListTools[0].in(), intList.out(), dblList.out() );
317       mOp->UnRegister();
318       if ( kind < GEOM::GEOM_IKindOfShape::DISK_CIRCLE || kind > GEOM::GEOM_IKindOfShape::PLANAR ) {
319         myListTools.length( 0 );
320         return;
321       }
322     }
323   }
324
325   myEditCurrentArgument->setText( aString );
326   processPreview();
327 }
328
329
330 //=================================================================================
331 // function : SetEditCurrentArgument()
332 // purpose  :
333 //=================================================================================
334 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
335 {
336   QPushButton* send = (QPushButton*)sender();
337
338   if ( send == GroupPoints->PushButton1 ) {
339     myEditCurrentArgument = GroupPoints->LineEdit1;
340     GroupPoints->PushButton2->setDown(false);
341     GroupPoints->LineEdit1->setEnabled(true);
342     GroupPoints->LineEdit2->setEnabled(false);
343   }
344   else if ( send == GroupPoints->PushButton2 ) {
345     myGeomGUI->getApp()->selectionMgr()->clearSelected(); //clear prewious selection
346     myEditCurrentArgument = GroupPoints->LineEdit2;
347     GroupPoints->PushButton1->setDown(false);
348     GroupPoints->LineEdit1->setEnabled(false);
349     GroupPoints->LineEdit2->setEnabled(true);
350     if ( getConstructorId() == 1 )
351       globalSelection( GEOM_PLANE  );
352   }
353
354   globalSelection( GEOM_ALLSHAPES );
355
356   myEditCurrentArgument->setFocus();
357   SelectionIntoArgument();
358   send->setDown(true);
359   processPreview();
360 }
361
362
363 //=================================================================================
364 // function : LineEditReturnPressed()
365 // purpose  :
366 //=================================================================================
367 void OperationGUI_PartitionDlg::LineEditReturnPressed()
368 {
369   QLineEdit* send = (QLineEdit*)sender();
370
371   if ( send == GroupPoints->LineEdit1 ||
372        send == GroupPoints->LineEdit2 ) {
373       myEditCurrentArgument = send;
374       GEOMBase_Skeleton::LineEditReturnPressed();
375   }
376 }
377
378
379 //=================================================================================
380 // function : ActivateThisDialog()
381 // purpose  :
382 //=================================================================================
383 void OperationGUI_PartitionDlg::ActivateThisDialog()
384 {
385   GEOMBase_Skeleton::ActivateThisDialog();
386   connect( myGeomGUI->getApp()->selectionMgr(),
387            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
388
389   ConstructorsClicked( getConstructorId() );
390   processPreview();
391 }
392
393
394 //=================================================================================
395 // function : enterEvent()
396 // purpose  :
397 //=================================================================================
398 void OperationGUI_PartitionDlg::enterEvent( QEvent* )
399 {
400   if ( !mainFrame()->GroupConstructors->isEnabled() )
401     this->ActivateThisDialog();
402 }
403
404
405 //=================================================================================
406 // function : createOperation
407 // purpose  :
408 //=================================================================================
409 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
410 {
411   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
412 }
413
414
415 //=================================================================================
416 // function : isValid
417 // purpose  :
418 //=================================================================================
419 bool OperationGUI_PartitionDlg::isValid (QString& )
420 {
421   return (getConstructorId() == 0 ?
422           myListShapes.length() :
423           myListShapes.length() && myListTools.length());
424 }
425
426
427 //=================================================================================
428 // function : execute
429 // purpose  :
430 //=================================================================================
431 bool OperationGUI_PartitionDlg::execute (ObjectList& objects)
432 {
433   bool res = false;
434   GEOM::GEOM_Object_var anObj;
435
436   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
437
438   switch ( getConstructorId() ) {
439   case 0:
440     {
441       int aLimit               = GetLimit();
442       int aKeepNonlimitShapes  = GroupPoints->CheckButton1->isChecked();
443       bool aNoSelfIntersection = GroupPoints->CheckButton2->isChecked();
444
445       anObj = aNoSelfIntersection ?
446         anOper->MakePartitionNonSelfIntersectedShape(myListShapes, myListTools,
447                                                      myListKeepInside, myListRemoveInside,
448                                                      aLimit, false, myListMaterials, aKeepNonlimitShapes) :
449         anOper->MakePartition(myListShapes, myListTools,
450                               myListKeepInside, myListRemoveInside,
451                               aLimit, false, myListMaterials, aKeepNonlimitShapes);
452       res = true;
453     }
454     break;
455   case 1:
456     {
457       anObj = anOper->MakeHalfPartition( myListShapes[0].in(), myListTools[0].in() );
458       res = true;
459     }
460     break;
461   }
462
463   if ( !anObj->_is_nil() ) {
464     TopoDS_Shape aShape;
465     GEOMBase::GetShape( anObj, aShape, TopAbs_SHAPE );
466     TopoDS_Iterator It( aShape, Standard_True, Standard_True );
467     int nbSubshapes = 0;
468     for (; It.More(); It.Next())
469       nbSubshapes++;
470
471     if ( nbSubshapes )
472       objects.push_back( anObj._retn() );
473     else
474       SUIT_MessageBox::warning( this,
475                                 QObject::tr( "GEOM_ERROR" ),
476                                 QObject::tr( "GEOM_WRN_PARTITION_RESULT_EMPTY" ) );
477   }
478
479   return res;
480 }
481
482 //=================================================================================
483 // function : restoreSubShapes
484 // purpose  :
485 //=================================================================================
486 void OperationGUI_PartitionDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
487                                                   SALOMEDS::SObject_ptr theSObject )
488 {
489   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
490     // empty list of arguments means that all arguments should be restored
491     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
492                                          /*theFindMethod=*/GEOM::FSM_GetInPlaceByHistory,
493                                          /*theInheritFirstArg=*/myListShapes.length() == 1,
494                                          mainFrame()->CheckBoxAddPrefix->isChecked() ); // ? false
495   }
496 }
497
498 //=======================================================================
499 //function : ComboTextChanged
500 //purpose  :
501 //=======================================================================
502 void OperationGUI_PartitionDlg::ComboTextChanged()
503 {
504   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
505   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
506   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
507   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
508   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
509   //GroupPoints->PushButton3->setEnabled(IsEnabled);
510   //GroupPoints->PushButton4->setEnabled(IsEnabled);
511   processPreview();
512 }
513
514 //=================================================================================
515 // function : GetLimit()
516 // purpose  :
517 //=================================================================================
518 int OperationGUI_PartitionDlg::GetLimit() const
519 {
520   int aLimit = GroupPoints->ComboBox1->currentIndex();
521
522   switch ( aLimit ) {
523   case 0:  aLimit = GEOM::SOLID ; break;
524   case 1:  aLimit = GEOM::SHELL ; break;
525   case 2:  aLimit = GEOM::FACE  ; break;
526   case 3:  aLimit = GEOM::WIRE  ; break;
527   case 4:  aLimit = GEOM::EDGE  ; break;
528   case 5:  aLimit = GEOM::VERTEX; break;
529   default: aLimit = GEOM::SHAPE ;
530   }
531
532   return aLimit;
533 }