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