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