Salome HOME
IPAL21695 TC5.1.4: GEOM Dialogs Behavior
[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_2Sel1List1Check( 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
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->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SHELL" ) );
123   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_FACE" ) );
124   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_WIRE" ) );
125   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_EDGE" ) );
126   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_VERTEX" ) );
127   GroupPoints->CheckButton1->setChecked( false );
128   
129   mainFrame()->GroupBoxPublish->show();
130
131   /* signals and slots connections */
132   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
133   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
134
135   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
136   
137   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
138   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
139   
140   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
141   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
142   
143   connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
144
145   connect( myGeomGUI->getApp()->selectionMgr(),
146            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
147   
148   initName( tr( "GEOM_PARTITION" ) );
149
150   ConstructorsClicked( 0 );
151   GroupPoints->PushButton1->click();
152 }
153
154
155 //=================================================================================
156 // function : ConstructorsClicked()
157 // purpose  : Radio button management
158 //=================================================================================
159 void OperationGUI_PartitionDlg::ConstructorsClicked( int constructorId )
160 {
161   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
162   globalSelection();
163   
164   myListShapes.length( 0 );
165   myListTools.length( 0 );  
166   myListKeepInside.length( 0 );
167   myListRemoveInside.length( 0 );
168   myListMaterials.length( 0 );
169   
170   switch ( constructorId ) {
171   case 0: /*Full partition */
172     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
173     GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECTS" ) );
174     GroupPoints->TextLabel3->show();
175     GroupPoints->ComboBox1->show();
176     GroupPoints->ComboBox1->setCurrentIndex( 0 );
177     GroupPoints->CheckButton1->show();
178     GroupPoints->PushButton1->setDown( true );
179     GroupPoints->PushButton2->setDown( false );
180     GroupPoints->LineEdit1->setEnabled(true);
181     GroupPoints->LineEdit2->setEnabled(false);
182     break;
183   case 1: /*Half-space partition */
184     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION_HALFSPACE" ) );
185     GroupPoints->TextLabel3->hide();
186     GroupPoints->ComboBox1->hide();
187     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE" ) );
188     GroupPoints->CheckButton1->hide();
189     GroupPoints->PushButton1->setDown( true );
190     GroupPoints->LineEdit1->setEnabled(true);
191     break;
192   } 
193
194   myEditCurrentArgument = GroupPoints->LineEdit1;
195   GroupPoints->LineEdit1->clear();
196   GroupPoints->LineEdit2->clear();
197
198   qApp->processEvents();
199   updateGeometry();
200   resize( minimumSizeHint() );
201
202   myEditCurrentArgument->setFocus();
203   connect( myGeomGUI->getApp()->selectionMgr(), 
204            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
205   SelectionIntoArgument();
206 }
207
208
209 //=================================================================================
210 // function : ClickOnOk()
211 // purpose  :
212 //=================================================================================
213 void OperationGUI_PartitionDlg::ClickOnOk()
214 {
215   if ( ClickOnApply() )
216     ClickOnCancel();
217 }
218
219
220 //=================================================================================
221 // function : ClickOnApply()
222 // purpose  :
223 //=================================================================================
224 bool OperationGUI_PartitionDlg::ClickOnApply()
225 {
226   if ( !onAccept() )
227     return false;
228   
229
230   initName();
231   //  0020854: EDF 1398 GEOM: Ergonomy of Partition GUI window 
232   //  ConstructorsClicked( getConstructorId() );
233   return true;
234 }
235
236
237 //=================================================================================
238 // function : SelectionIntoArgument()
239 // purpose  : Called when selection as changed or other case
240 //=================================================================================
241 void OperationGUI_PartitionDlg::SelectionIntoArgument()
242 {
243   myEditCurrentArgument->setText( "" );
244   QString aString = "";
245   
246   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
247   SALOME_ListIO aSelList;
248   aSelMgr->selectedObjects(aSelList);
249
250   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
251     
252   if ( nbSel < 1 ) {
253     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
254       myListShapes.length( 0 );
255       myListMaterials.length( 0 );
256     }
257     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
258       myListTools.length( 0 );
259   }
260   
261   // One and only one plane can be selected
262   
263   if ( getConstructorId() == 1 &&
264        myEditCurrentArgument == GroupPoints->LineEdit2 && 
265        nbSel != 1 ) {
266     myListTools.length( 0 );
267     return;
268   }
269   
270   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
271     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
272     myListMaterials.length( 0 );
273     if ( !myListShapes.length() )
274       return;
275   }
276   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
277     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListTools, true);
278     if ( !myListTools.length() )
279       return;
280   }
281   
282   myEditCurrentArgument->setText( aString );
283 }
284
285
286 //=================================================================================
287 // function : SetEditCurrentArgument()
288 // purpose  :
289 //=================================================================================
290 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
291 {
292   QPushButton* send = (QPushButton*)sender();
293   
294   if ( send == GroupPoints->PushButton1 ) {
295     myEditCurrentArgument = GroupPoints->LineEdit1;
296     GroupPoints->PushButton2->setDown(false);
297     GroupPoints->LineEdit1->setEnabled(true);
298     GroupPoints->LineEdit2->setEnabled(false);
299   }
300   else if ( send == GroupPoints->PushButton2 ) {
301     myGeomGUI->getApp()->selectionMgr()->clearSelected(); //clear prewious selection
302     myEditCurrentArgument = GroupPoints->LineEdit2;
303     GroupPoints->PushButton1->setDown(false);
304     GroupPoints->LineEdit1->setEnabled(false);
305     GroupPoints->LineEdit2->setEnabled(true);
306     if ( getConstructorId() == 1 )
307       globalSelection( GEOM_PLANE  );
308   }
309  
310   globalSelection( GEOM_ALLSHAPES );
311       
312   myEditCurrentArgument->setFocus();
313   SelectionIntoArgument();
314   send->setDown(true);
315 }
316
317
318 //=================================================================================
319 // function : LineEditReturnPressed()
320 // purpose  :
321 //=================================================================================
322 void OperationGUI_PartitionDlg::LineEditReturnPressed()
323 {
324   QLineEdit* send = (QLineEdit*)sender();
325
326   if ( send == GroupPoints->LineEdit1 || 
327        send == GroupPoints->LineEdit2 ) {
328       myEditCurrentArgument = send;
329       GEOMBase_Skeleton::LineEditReturnPressed();
330   }
331 }
332
333
334 //=================================================================================
335 // function : ActivateThisDialog()
336 // purpose  :
337 //=================================================================================
338 void OperationGUI_PartitionDlg::ActivateThisDialog()
339 {
340   GEOMBase_Skeleton::ActivateThisDialog();
341   connect( myGeomGUI->getApp()->selectionMgr(), 
342            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
343
344   ConstructorsClicked( getConstructorId() ); 
345 }
346
347
348 //=================================================================================
349 // function : enterEvent()
350 // purpose  :
351 //=================================================================================
352 void OperationGUI_PartitionDlg::enterEvent( QEvent* )
353 {
354   if ( !mainFrame()->GroupConstructors->isEnabled() )
355     this->ActivateThisDialog();
356 }
357
358
359 //=================================================================================
360 // function : createOperation
361 // purpose  :
362 //=================================================================================
363 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
364 {
365   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
366 }
367
368
369 //=================================================================================
370 // function : isValid
371 // purpose  :
372 //=================================================================================
373 bool OperationGUI_PartitionDlg::isValid( QString& )
374 {
375   return ( myListShapes.length()     || myListTools.length() ||
376            myListKeepInside.length() || myListRemoveInside.length() );
377 }
378
379
380 //=================================================================================
381 // function : execute
382 // purpose  :
383 //=================================================================================
384 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
385 {
386   bool res = false;
387
388   GEOM::GEOM_Object_var anObj;
389   QString msg;
390
391   int aLimit = GetLimit();
392   int aConstructorId = getConstructorId();
393   int aKeepNonlimitShapes = 0;
394
395   if ( aConstructorId == 1 ) {
396     aLimit = GEOM::SHAPE;
397   }
398   else {
399     if ( GroupPoints->CheckButton1->isChecked() ) {
400       aKeepNonlimitShapes = 1;
401     }
402     else {
403       aKeepNonlimitShapes = 0;
404     }
405   }
406
407   if ( isValid( msg ) ) {
408     GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
409     anObj = anOper->MakePartition( myListShapes, myListTools,
410                                    myListKeepInside, myListRemoveInside,
411                                    aLimit, false, myListMaterials, aKeepNonlimitShapes );
412     res = true;
413   }
414
415   if ( !anObj->_is_nil() ) {
416     TopoDS_Shape aShape;
417     GEOMBase::GetShape(anObj, aShape, TopAbs_SHAPE);
418     TopoDS_Iterator It (aShape, Standard_True, Standard_True);
419     int nbSubshapes=0;
420     for (; It.More(); It.Next())
421       nbSubshapes++;
422
423     if (nbSubshapes)
424       objects.push_back( anObj._retn() );
425     else
426       SUIT_MessageBox::warning(this,
427                                QObject::tr("GEOM_ERROR"),
428                                QObject::tr("GEOM_WRN_PARTITION_RESULT_EMPTY"));
429   }
430
431   return res;
432 }
433
434 //=================================================================================
435 // function : restoreSubShapes
436 // purpose  :
437 //=================================================================================
438 void OperationGUI_PartitionDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
439                                                   SALOMEDS::SObject_ptr theSObject )
440 {
441   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
442     // empty list of arguments means that all arguments should be restored
443     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
444                                          /*theFindMethod=*/GEOM::FSM_GetInPlaceByHistory,
445                                          /*theInheritFirstArg=*/myListShapes.length() == 1,
446                                          mainFrame()->CheckBoxAddPrefix->isChecked() ); // ? false
447   }
448 }
449
450 //=======================================================================
451 //function : ComboTextChanged
452 //purpose  : 
453 //=======================================================================
454 void OperationGUI_PartitionDlg::ComboTextChanged()
455 {
456   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
457   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
458   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
459   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
460   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
461   //GroupPoints->PushButton3->setEnabled(IsEnabled);
462   //GroupPoints->PushButton4->setEnabled(IsEnabled);
463 }
464
465 //=================================================================================
466 // function : GetLimit()
467 // purpose  : 
468 //=================================================================================
469 int OperationGUI_PartitionDlg::GetLimit() const
470 {
471   int aLimit = GroupPoints->ComboBox1->currentIndex();
472
473   switch ( aLimit ) {
474   case 0:  aLimit = GEOM::SOLID ; break;
475   case 1:  aLimit = GEOM::SHELL ; break;
476   case 2:  aLimit = GEOM::FACE  ; break;
477   case 3:  aLimit = GEOM::WIRE  ; break;
478   case 4:  aLimit = GEOM::EDGE  ; break;
479   case 5:  aLimit = GEOM::VERTEX; break;
480   default: aLimit = GEOM::SHAPE ;
481   }
482
483   return aLimit;
484 }