Salome HOME
74721e94720aaf806a843e10fd87717c883443ae
[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);
241     
242   if(nbSel < 1) {
243     if(myEditCurrentArgument == GroupPoints->LineEdit1)
244       {
245         myListShapes.length(0);
246         myListMaterials.length(0);
247       }
248     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
249       myListTools.length(0);
250     else if(myEditCurrentArgument == GroupPoints->LineEdit3)
251       myListRemoveInside.length(0);
252     else if(myEditCurrentArgument == GroupPoints->LineEdit4)
253       myListKeepInside.length(0);
254   }
255   
256   // One and only one plane can be selected
257   
258   if(getConstructorId() == 1 && myEditCurrentArgument == GroupPoints->LineEdit2 && nbSel!=1) {
259     myListTools.length(0);
260     return;
261   }
262   
263   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
264     GEOMBase::ConvertListOfIOInListOfGO(mySelection->StoredIObjects(),  myListShapes);
265     myListMaterials.length(0);
266     if (!myListShapes.length())
267       return;
268   }
269   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
270     GEOMBase::ConvertListOfIOInListOfGO(mySelection->StoredIObjects(),  myListTools);
271     if (!myListTools.length())
272       return;
273   }
274   else if(myEditCurrentArgument == GroupPoints->LineEdit3) {
275     GEOMBase::ConvertListOfIOInListOfGO(mySelection->StoredIObjects(),  myListRemoveInside);
276     if (!myListRemoveInside.length())
277       return;
278   }
279   else if(myEditCurrentArgument == GroupPoints->LineEdit4) {
280     GEOMBase::ConvertListOfIOInListOfGO(mySelection->StoredIObjects(),  myListKeepInside);
281     if (!myListKeepInside.length())
282       return;
283   }
284   
285   myEditCurrentArgument->setText(aString);
286 }
287
288
289 //=================================================================================
290 // function : SetEditCurrentArgument()
291 // purpose  :
292 //=================================================================================
293 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
294 {
295   QPushButton* send = (QPushButton*)sender();
296   
297   if(send == GroupPoints->PushButton1) 
298     myEditCurrentArgument = GroupPoints->LineEdit1;
299   else if(send == GroupPoints->PushButton2)
300     {
301       myEditCurrentArgument = GroupPoints->LineEdit2;
302       if( getConstructorId()==1 )
303         globalSelection( GEOM_PLANE  );
304     }
305   else if(send == GroupPoints->PushButton3)
306     myEditCurrentArgument = GroupPoints->LineEdit3;
307   else if(send == GroupPoints->PushButton4)
308     myEditCurrentArgument = GroupPoints->LineEdit4;
309  
310   globalSelection();
311       
312   myEditCurrentArgument->setFocus();
313   SelectionIntoArgument();
314 }
315
316
317 //=================================================================================
318 // function : LineEditReturnPressed()
319 // purpose  :
320 //=================================================================================
321 void OperationGUI_PartitionDlg::LineEditReturnPressed()
322 {
323   QLineEdit* send = (QLineEdit*)sender();
324   
325   if(send == GroupPoints->LineEdit1 || 
326      send == GroupPoints->LineEdit2 ||
327      send == GroupPoints->LineEdit3 ||
328      send == GroupPoints->LineEdit4 )
329     {
330       myEditCurrentArgument = send;
331       GEOMBase_Skeleton::LineEditReturnPressed();
332     }
333 }
334
335
336 //=================================================================================
337 // function : ActivateThisDialog()
338 // purpose  :
339 //=================================================================================
340 void OperationGUI_PartitionDlg::ActivateThisDialog()
341 {
342   GEOMBase_Skeleton::ActivateThisDialog();
343   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
344
345   ConstructorsClicked( getConstructorId() ); 
346 }
347
348
349 //=================================================================================
350 // function : DeactivateActiveDialog()
351 // purpose  : public slot to deactivate if active
352 //=================================================================================
353 void OperationGUI_PartitionDlg::DeactivateActiveDialog()
354 {
355   GEOMBase_Skeleton::DeactivateActiveDialog();
356 }
357
358
359 //=================================================================================
360 // function : enterEvent()
361 // purpose  :
362 //=================================================================================
363 void OperationGUI_PartitionDlg::enterEvent(QEvent* e)
364 {
365   if(!GroupConstructors->isEnabled())
366     this->ActivateThisDialog();
367 }
368
369
370 //=================================================================================
371 // function : createOperation
372 // purpose  :
373 //=================================================================================
374 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
375 {
376   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
377 }
378
379
380 //=================================================================================
381 // function : isValid
382 // purpose  :
383 //=================================================================================
384 bool OperationGUI_PartitionDlg::isValid( QString& msg )
385 {
386   return (myListShapes.length()      || myListTools.length() ||
387           myListKeepInside.length()  || myListRemoveInside.length());
388 }
389
390
391 //=================================================================================
392 // function : execute
393 // purpose  :
394 //=================================================================================
395 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
396 {
397   bool res = false;
398   
399   GEOM::GEOM_Object_var anObj;
400   QString msg;
401   
402   int aLimit = GetLimit();
403   int aConstructorId = getConstructorId();
404   
405   if (aConstructorId==1)
406     aLimit = GEOM::SHAPE;
407   
408   if (isValid( msg ))    
409     {
410       
411       if ( aConstructorId==0 && !toRemoveWebs() )
412         myListMaterials.length(0);
413       
414       anObj = GEOM::GEOM_IBooleanOperations::_narrow( getOperation() )->MakePartition(myListShapes,
415                                                                                       myListTools,
416                                                                                       myListKeepInside,
417                                                                                       myListRemoveInside,
418                                                                                       aLimit,
419                                                                                       toRemoveWebs(),
420                                                                                       myListMaterials);
421       res = true;
422     }
423   
424   if ( !anObj->_is_nil() )
425     objects.push_back( anObj._retn() );
426   
427   return res;
428 }
429
430
431 //=================================================================================
432 // function : closeEvent
433 // purpose  :
434 //=================================================================================
435 void OperationGUI_PartitionDlg::closeEvent( QCloseEvent* e )
436 {
437   GEOMBase_Skeleton::closeEvent( e );
438 }
439
440
441 //=======================================================================
442 //function : ComboTextChanged
443 //purpose  : 
444 //=======================================================================
445
446 void OperationGUI_PartitionDlg::ComboTextChanged()
447 {
448   bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
449   GroupPoints->LineEdit3->setEnabled(IsEnabled);
450   GroupPoints->LineEdit4->setEnabled(IsEnabled);
451   GroupPoints->TextLabel4->setEnabled(IsEnabled);
452   GroupPoints->TextLabel5->setEnabled(IsEnabled);
453   GroupPoints->PushButton3->setEnabled(IsEnabled);
454   GroupPoints->PushButton4->setEnabled(IsEnabled);
455 }
456
457
458 //=================================================================================
459 // function : SetMaterials()
460 // purpose  : 
461 //=================================================================================
462 void OperationGUI_PartitionDlg::SetMaterials()
463 {
464   SALOME_Selection* Sel =
465     SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
466   MESSAGE("OperationGUI_MaterialDlg ...");
467   OperationGUI_MaterialDlg *aDlg =
468     new OperationGUI_MaterialDlg(this, "", Sel, myListShapes, true);    
469   MESSAGE("OperationGUI_MaterialDlg");
470   return;
471 }
472
473
474 //=================================================================================
475 // function : onRemoveWebs()
476 // purpose  : 
477 //=================================================================================
478 void OperationGUI_PartitionDlg::onRemoveWebs(bool isChecked)
479 {
480   if (GroupPoints->CheckBox1->isChecked() != isChecked)
481     GroupPoints->CheckBox1->setChecked(isChecked);
482   
483   GroupPoints->PushButton5->setEnabled(isChecked);
484 }
485
486
487 //=================================================================================
488 // function : toRemoveWebs()
489 // purpose  : 
490 //=================================================================================
491 bool OperationGUI_PartitionDlg::toRemoveWebs() const
492 {
493   return GroupPoints->CheckBox1->isChecked();
494 }
495
496
497 //=================================================================================
498 // function : GetLimit()
499 // purpose  : 
500 //=================================================================================
501 int OperationGUI_PartitionDlg::GetLimit() const
502 {
503   int aLimit = GroupPoints->ComboBox1->currentItem();
504   
505   switch(aLimit)
506     {
507     case 0 : 
508       {
509         aLimit = GEOM::SOLID;
510         break;
511       }
512     case 1 :
513       {
514         aLimit = GEOM::SHELL;
515         break;
516       }
517     case 2 :
518       {
519         aLimit = GEOM::FACE;
520         break;
521       }
522     case 3 :
523       {
524         aLimit = GEOM::WIRE;
525         break;
526       }
527     case 4 :
528       {
529         aLimit = GEOM::EDGE;
530         break;
531       }
532     case 5 :
533       {
534         aLimit = GEOM::VERTEX;
535         break;
536       }
537     default :
538       aLimit = GEOM::SHAPE;
539     }
540   
541   return aLimit;
542 }