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