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