Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / OperationGUI / OperationGUI_PartitionDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : OperationGUI_PartitionDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "OperationGUI_PartitionDlg.h"
30 #include "OperationGUI_MaterialDlg.h"
31
32 #include "GEOMImpl_Types.hxx"
33
34 #include "QAD_Desktop.h"
35
36 #include <qcheckbox.h>
37
38 #include "utilities.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(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
48   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
49 {
50   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_PARTITION")));
51   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_PARTITION_PLANE")));
52   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
53
54   setCaption(tr("GEOM_PARTITION_TITLE"));
55
56   /***************************************************************/
57   GroupConstructors->setTitle(tr("GEOM_PARTITION"));
58   RadioButton1->setPixmap(image0);
59   RadioButton2->setPixmap(image1);
60   RadioButton3->close(TRUE);
61
62   // Full partition (contains half-space partition)
63   GroupPoints = new DlgRef_4Sel1List1Check_QTD(this, "GroupPoints");
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION"));
65   GroupPoints->GroupBox2->setTitle(tr("SUPPRESS_RESULT"));
66   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
67   GroupPoints->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
68   GroupPoints->TextLabel3->setText(tr("GEOM_RECONSTRUCTION_LIMIT"));
69   GroupPoints->TextLabel4->setText(tr("GEOM_SUPPRESS_RESULT_INSIDE"));
70   GroupPoints->TextLabel5->setText(tr("GEOM_SUPPRESS_RESULT_OUTSIDE"));
71   GroupPoints->CheckBox1->setText(tr("GEOM_REMOVE_WEBS"));
72   GroupPoints->PushButton1->setPixmap(image2);
73   GroupPoints->PushButton2->setPixmap(image2);
74   GroupPoints->PushButton3->setPixmap(image2);
75   GroupPoints->PushButton4->setPixmap(image2);
76   GroupPoints->PushButton5->setText(tr("GEOM_SET_MATERIALS"));
77   GroupPoints->LineEdit1->setReadOnly( true );
78   GroupPoints->LineEdit2->setReadOnly( true );
79   GroupPoints->LineEdit3->setReadOnly( true );
80   GroupPoints->LineEdit4->setReadOnly( true );
81
82   Layout1->addWidget(GroupPoints, 2, 0);
83   /***************************************************************/
84  
85   Init();
86 }
87
88
89 //=================================================================================
90 // function : ~OperationGUI_PartitionDlg()
91 // purpose  : Destroys the object and frees any allocated resources
92 //=================================================================================
93 OperationGUI_PartitionDlg::~OperationGUI_PartitionDlg()
94 {
95   // no need to delete child widgets, Qt does it all for us
96 }
97
98
99 //=================================================================================
100 // function : Init()
101 // purpose  :
102 //=================================================================================
103 void OperationGUI_PartitionDlg::Init()
104 {
105   /* type for sub shape selection */
106   GroupPoints->ComboBox1->insertItem(tr("GEOM_RECONSTRUCTION_LIMIT_SOLID"));
107   GroupPoints->ComboBox1->insertItem(tr("GEOM_RECONSTRUCTION_LIMIT_SHELL"));
108   GroupPoints->ComboBox1->insertItem(tr("GEOM_RECONSTRUCTION_LIMIT_FACE"));
109   GroupPoints->ComboBox1->insertItem(tr("GEOM_RECONSTRUCTION_LIMIT_WIRE"));
110   GroupPoints->ComboBox1->insertItem(tr("GEOM_RECONSTRUCTION_LIMIT_EDGE"));
111   GroupPoints->ComboBox1->insertItem(tr("GEOM_RECONSTRUCTION_LIMIT_VERTEX"));
112   
113   /* signals and slots connections */
114   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
115   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
116   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
117   
118   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
119   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
120   connect(GroupPoints->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
121   connect(GroupPoints->PushButton4, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
122   connect(GroupPoints->PushButton5, SIGNAL(clicked()), this, SLOT(SetMaterials()));
123   
124   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
125   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
126   connect(GroupPoints->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
127   connect(GroupPoints->LineEdit4, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
128   
129   connect(GroupPoints->CheckBox1, SIGNAL(toggled(bool)), this, SLOT(onRemoveWebs(bool)));
130   
131   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
132   
133   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
134   
135   initName( tr( "GEOM_PARTITION" ) );
136   ConstructorsClicked( 0 );
137 }
138
139
140 //=================================================================================
141 // function : ConstructorsClicked()
142 // purpose  : Radio button management
143 //=================================================================================
144 void OperationGUI_PartitionDlg::ConstructorsClicked(int constructorId)
145 {
146   disconnect(mySelection, 0, this, 0);
147   globalSelection();
148   
149   myListShapes.length(0);
150   myListTools.length(0);  
151   myListKeepInside.length(0);
152   myListRemoveInside.length(0);
153   myListMaterials.length(0);
154   
155   switch (constructorId)
156     {
157     case 0: /*Full partition */
158       {
159         GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION"));
160         GroupPoints->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
161         resize(0, 0);
162         GroupPoints->TextLabel3->show();
163         GroupPoints->ComboBox1->show();
164         GroupPoints->GroupBox2->show();
165         
166         GroupPoints->ComboBox1->setCurrentItem(0);
167         GroupPoints->LineEdit3->clear();
168         GroupPoints->LineEdit4->clear();
169         break;
170       }
171     case 1: /*Half-space partition */
172       {
173
174         GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION_HALFSPACE"));
175         GroupPoints->GroupBox2->hide();
176         GroupPoints->TextLabel3->hide();
177         GroupPoints->ComboBox1->hide();
178         GroupPoints->TextLabel2->setText(tr("GEOM_PLANE"));
179         resize(0, 0);
180         break;
181       } 
182     }
183   
184   myEditCurrentArgument = GroupPoints->LineEdit1;
185   GroupPoints->LineEdit1->clear();
186   GroupPoints->LineEdit2->clear();
187   onRemoveWebs(false);
188
189   myEditCurrentArgument->setFocus();
190   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
191   MESSAGE(width()<<" "<<height());
192 }
193
194
195 //=================================================================================
196 // function : ClickOnOk()
197 // purpose  :
198 //=================================================================================
199 void OperationGUI_PartitionDlg::ClickOnOk()
200 {
201   if ( ClickOnApply() )
202     ClickOnCancel();
203 }
204
205
206 //=================================================================================
207 // function : ClickOnApply()
208 // purpose  :
209 //=================================================================================
210 bool OperationGUI_PartitionDlg::ClickOnApply()
211 {
212   if ( !onAccept() )
213     return false;
214   
215   initName();
216   ConstructorsClicked( getConstructorId() );
217   return true;
218 }
219
220
221 //=======================================================================
222 // function : ClickOnCancel()
223 // purpose  :
224 //=======================================================================
225 void OperationGUI_PartitionDlg::ClickOnCancel()
226 {
227   GEOMBase_Skeleton::ClickOnCancel();
228 }
229
230
231 //=================================================================================
232 // function : SelectionIntoArgument()
233 // purpose  : Called when selection as changed or other case
234 //=================================================================================
235 void OperationGUI_PartitionDlg::SelectionIntoArgument()
236 {
237   myEditCurrentArgument->setText( "" );
238   QString aString = "";
239   
240   int nbSel = GEOMBase::GetNameOfSelectedIObjects( mySelection, aString, true );
241     
242   if ( nbSel < 1 )
243   {
244     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
245     {
246       myListShapes.length( 0 );
247       myListMaterials.length( 0 );
248     }
249     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
250       myListTools.length( 0 );
251     else if ( myEditCurrentArgument == GroupPoints->LineEdit3 )
252       myListRemoveInside.length( 0 );
253     else if ( myEditCurrentArgument == GroupPoints->LineEdit4 )
254       myListKeepInside.length( 0 );
255   }
256   
257   // One and only one plane can be selected
258   
259   if ( getConstructorId() == 1 && myEditCurrentArgument == GroupPoints->LineEdit2 && nbSel != 1 )
260   {
261     myListTools.length( 0 );
262     return;
263   }
264   
265   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
266   {
267     GEOMBase::ConvertListOfIOInListOfGO( mySelection->StoredIObjects(), myListShapes, true );
268     myListMaterials.length( 0 );
269     if ( !myListShapes.length() )
270       return;
271   }
272   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
273   {
274     GEOMBase::ConvertListOfIOInListOfGO( mySelection->StoredIObjects(), myListTools, true );
275     if ( !myListTools.length() )
276       return;
277   }
278   else if(myEditCurrentArgument == GroupPoints->LineEdit3)
279   {
280     GEOMBase::ConvertListOfIOInListOfGO( mySelection->StoredIObjects(), myListRemoveInside, true );
281     if (!myListRemoveInside.length())
282       return;
283   }
284   else if(myEditCurrentArgument == GroupPoints->LineEdit4)
285   {
286     GEOMBase::ConvertListOfIOInListOfGO( mySelection->StoredIObjects(),  myListKeepInside, true );
287     if ( !myListKeepInside.length() )
288       return;
289   }
290   
291   myEditCurrentArgument->setText( aString );
292 }
293
294
295 //=================================================================================
296 // function : SetEditCurrentArgument()
297 // purpose  :
298 //=================================================================================
299 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
300 {
301   QPushButton* send = (QPushButton*)sender();
302   
303   if(send == GroupPoints->PushButton1) 
304     myEditCurrentArgument = GroupPoints->LineEdit1;
305   else if(send == GroupPoints->PushButton2)
306   {
307     myEditCurrentArgument = GroupPoints->LineEdit2;
308     if( getConstructorId()==1 )
309       globalSelection( GEOM_PLANE  );
310   }
311   else if(send == GroupPoints->PushButton3)
312     myEditCurrentArgument = GroupPoints->LineEdit3;
313   else if(send == GroupPoints->PushButton4)
314     myEditCurrentArgument = GroupPoints->LineEdit4;
315  
316   globalSelection( GEOM_ALLSHAPES );
317       
318   myEditCurrentArgument->setFocus();
319   SelectionIntoArgument();
320 }
321
322
323 //=================================================================================
324 // function : LineEditReturnPressed()
325 // purpose  :
326 //=================================================================================
327 void OperationGUI_PartitionDlg::LineEditReturnPressed()
328 {
329   QLineEdit* send = (QLineEdit*)sender();
330   
331   if(send == GroupPoints->LineEdit1 || 
332      send == GroupPoints->LineEdit2 ||
333      send == GroupPoints->LineEdit3 ||
334      send == GroupPoints->LineEdit4 )
335     {
336       myEditCurrentArgument = send;
337       GEOMBase_Skeleton::LineEditReturnPressed();
338     }
339 }
340
341
342 //=================================================================================
343 // function : ActivateThisDialog()
344 // purpose  :
345 //=================================================================================
346 void OperationGUI_PartitionDlg::ActivateThisDialog()
347 {
348   GEOMBase_Skeleton::ActivateThisDialog();
349   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
350
351   ConstructorsClicked( getConstructorId() ); 
352 }
353
354
355 //=================================================================================
356 // function : DeactivateActiveDialog()
357 // purpose  : public slot to deactivate if active
358 //=================================================================================
359 void OperationGUI_PartitionDlg::DeactivateActiveDialog()
360 {
361   GEOMBase_Skeleton::DeactivateActiveDialog();
362 }
363
364
365 //=================================================================================
366 // function : enterEvent()
367 // purpose  :
368 //=================================================================================
369 void OperationGUI_PartitionDlg::enterEvent(QEvent* e)
370 {
371   if(!GroupConstructors->isEnabled())
372     this->ActivateThisDialog();
373 }
374
375
376 //=================================================================================
377 // function : createOperation
378 // purpose  :
379 //=================================================================================
380 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
381 {
382   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
383 }
384
385
386 //=================================================================================
387 // function : isValid
388 // purpose  :
389 //=================================================================================
390 bool OperationGUI_PartitionDlg::isValid( QString& msg )
391 {
392   return (myListShapes.length()      || myListTools.length() ||
393           myListKeepInside.length()  || myListRemoveInside.length());
394 }
395
396
397 //=================================================================================
398 // function : execute
399 // purpose  :
400 //=================================================================================
401 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
402 {
403   bool res = false;
404   
405   GEOM::GEOM_Object_var anObj;
406   QString msg;
407   
408   int aLimit = GetLimit();
409   int aConstructorId = getConstructorId();
410   
411   if (aConstructorId==1)
412     aLimit = GEOM::SHAPE;
413   
414   if (isValid( msg ))    
415     {
416       
417       if ( aConstructorId==0 && !toRemoveWebs() )
418         myListMaterials.length(0);
419       
420       anObj = GEOM::GEOM_IBooleanOperations::_narrow( getOperation() )->MakePartition(myListShapes,
421                                                                                       myListTools,
422                                                                                       myListKeepInside,
423                                                                                       myListRemoveInside,
424                                                                                       aLimit,
425                                                                                       toRemoveWebs(),
426                                                                                       myListMaterials);
427       res = true;
428     }
429   
430   if ( !anObj->_is_nil() )
431     objects.push_back( anObj._retn() );
432   
433   return res;
434 }
435
436
437 //=================================================================================
438 // function : closeEvent
439 // purpose  :
440 //=================================================================================
441 void OperationGUI_PartitionDlg::closeEvent( QCloseEvent* e )
442 {
443   GEOMBase_Skeleton::closeEvent( e );
444 }
445
446
447 //=======================================================================
448 //function : ComboTextChanged
449 //purpose  : 
450 //=======================================================================
451
452 void OperationGUI_PartitionDlg::ComboTextChanged()
453 {
454   bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
455   GroupPoints->LineEdit3->setEnabled(IsEnabled);
456   GroupPoints->LineEdit4->setEnabled(IsEnabled);
457   GroupPoints->TextLabel4->setEnabled(IsEnabled);
458   GroupPoints->TextLabel5->setEnabled(IsEnabled);
459   GroupPoints->PushButton3->setEnabled(IsEnabled);
460   GroupPoints->PushButton4->setEnabled(IsEnabled);
461 }
462
463
464 //=================================================================================
465 // function : SetMaterials()
466 // purpose  : 
467 //=================================================================================
468 void OperationGUI_PartitionDlg::SetMaterials()
469 {
470   SALOME_Selection* Sel =
471     SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
472   MESSAGE("OperationGUI_MaterialDlg ...");
473   OperationGUI_MaterialDlg *aDlg =
474     new OperationGUI_MaterialDlg(this, "", Sel, myListShapes, true);    
475   MESSAGE("OperationGUI_MaterialDlg");
476   return;
477 }
478
479
480 //=================================================================================
481 // function : onRemoveWebs()
482 // purpose  : 
483 //=================================================================================
484 void OperationGUI_PartitionDlg::onRemoveWebs(bool isChecked)
485 {
486   if (GroupPoints->CheckBox1->isChecked() != isChecked)
487     GroupPoints->CheckBox1->setChecked(isChecked);
488   
489   GroupPoints->PushButton5->setEnabled(isChecked);
490 }
491
492
493 //=================================================================================
494 // function : toRemoveWebs()
495 // purpose  : 
496 //=================================================================================
497 bool OperationGUI_PartitionDlg::toRemoveWebs() const
498 {
499   return GroupPoints->CheckBox1->isChecked();
500 }
501
502
503 //=================================================================================
504 // function : GetLimit()
505 // purpose  : 
506 //=================================================================================
507 int OperationGUI_PartitionDlg::GetLimit() const
508 {
509   int aLimit = GroupPoints->ComboBox1->currentItem();
510   
511   switch(aLimit)
512     {
513     case 0 : 
514       {
515         aLimit = GEOM::SOLID;
516         break;
517       }
518     case 1 :
519       {
520         aLimit = GEOM::SHELL;
521         break;
522       }
523     case 2 :
524       {
525         aLimit = GEOM::FACE;
526         break;
527       }
528     case 3 :
529       {
530         aLimit = GEOM::WIRE;
531         break;
532       }
533     case 4 :
534       {
535         aLimit = GEOM::EDGE;
536         break;
537       }
538     case 5 :
539       {
540         aLimit = GEOM::VERTEX;
541         break;
542       }
543     default :
544       aLimit = GEOM::SHAPE;
545     }
546   
547   return aLimit;
548 }