Salome HOME
Merging with WPdev
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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(GeometryGUI* theGeometryGUI, QWidget* parent)
53   :GEOMBase_Skeleton(theGeometryGUI, parent, "OperationGUI_PartitionDlg", false,
54                      WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
55 {
56   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
57   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_PARTITION")));
58   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_PARTITION_PLANE")));
59   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_PARTITION_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_PARTITION"));
65   RadioButton1->setPixmap(image0);
66   RadioButton2->setPixmap(image1);
67   RadioButton3->close(TRUE);
68
69   // Full partition (contains half-space partition)
70   GroupPoints = new DlgRef_2Sel1List_QTD(this, "GroupPoints");
71   GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION"));
72   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
73   GroupPoints->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
74   GroupPoints->TextLabel3->setText(tr("GEOM_RECONSTRUCTION_LIMIT"));
75   GroupPoints->PushButton1->setPixmap(image2);
76   GroupPoints->PushButton2->setPixmap(image2);
77   GroupPoints->LineEdit1->setReadOnly( true );
78   GroupPoints->LineEdit2->setReadOnly( true );
79
80   Layout1->addWidget(GroupPoints, 2, 0);
81   /***************************************************************/
82
83   setHelpFileName("partition.htm"); 
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   
121   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
122   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
123   
124   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
125   
126   connect(myGeomGUI->getApp()->selectionMgr(), 
127           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
128   
129   initName( tr( "GEOM_PARTITION" ) );
130   ConstructorsClicked( 0 );
131 }
132
133
134 //=================================================================================
135 // function : ConstructorsClicked()
136 // purpose  : Radio button management
137 //=================================================================================
138 void OperationGUI_PartitionDlg::ConstructorsClicked(int constructorId)
139 {
140   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
141   globalSelection();
142   
143   myListShapes.length(0);
144   myListTools.length(0);  
145   myListKeepInside.length(0);
146   myListRemoveInside.length(0);
147   myListMaterials.length(0);
148   
149   switch (constructorId)
150     {
151     case 0: /*Full partition */
152       {
153         GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION"));
154         GroupPoints->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
155         resize(0, 0);
156         GroupPoints->TextLabel3->show();
157         GroupPoints->ComboBox1->show();
158         
159         GroupPoints->ComboBox1->setCurrentItem(0);
160         break;
161       }
162     case 1: /*Half-space partition */
163       {
164
165         GroupPoints->GroupBox1->setTitle(tr("GEOM_PARTITION_HALFSPACE"));
166         GroupPoints->TextLabel3->hide();
167         GroupPoints->ComboBox1->hide();
168         GroupPoints->TextLabel2->setText(tr("GEOM_PLANE"));
169         resize(0, 0);
170         break;
171       } 
172     }
173
174   myEditCurrentArgument = GroupPoints->LineEdit1;
175   GroupPoints->LineEdit1->clear();
176   GroupPoints->LineEdit2->clear();
177
178   myEditCurrentArgument->setFocus();
179   connect(myGeomGUI->getApp()->selectionMgr(), 
180           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
181   MESSAGE(width()<<" "<<height());
182 }
183
184
185 //=================================================================================
186 // function : ClickOnOk()
187 // purpose  :
188 //=================================================================================
189 void OperationGUI_PartitionDlg::ClickOnOk()
190 {
191   if ( ClickOnApply() )
192     ClickOnCancel();
193 }
194
195
196 //=================================================================================
197 // function : ClickOnApply()
198 // purpose  :
199 //=================================================================================
200 bool OperationGUI_PartitionDlg::ClickOnApply()
201 {
202   if ( !onAccept() )
203     return false;
204   
205   initName();
206   ConstructorsClicked( getConstructorId() );
207   return true;
208 }
209
210
211 //=================================================================================
212 // function : SelectionIntoArgument()
213 // purpose  : Called when selection as changed or other case
214 //=================================================================================
215 void OperationGUI_PartitionDlg::SelectionIntoArgument()
216 {
217   myEditCurrentArgument->setText( "" );
218   QString aString = "";
219   
220   int nbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aString, true );
221     
222   if ( nbSel < 1 )
223   {
224     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
225     {
226       myListShapes.length( 0 );
227       myListMaterials.length( 0 );
228     }
229     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
230       myListTools.length( 0 );
231   }
232   
233   // One and only one plane can be selected
234   
235   if ( getConstructorId() == 1 && myEditCurrentArgument == GroupPoints->LineEdit2 && nbSel != 1 )
236   {
237     myListTools.length( 0 );
238     return;
239   }
240   
241   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
242   {
243     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myListShapes, true );
244     myListMaterials.length( 0 );
245     if ( !myListShapes.length() )
246       return;
247   }
248   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
249   {
250     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myListTools, true );
251     if ( !myListTools.length() )
252       return;
253   }
254   
255   myEditCurrentArgument->setText( aString );
256 }
257
258
259 //=================================================================================
260 // function : SetEditCurrentArgument()
261 // purpose  :
262 //=================================================================================
263 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
264 {
265   QPushButton* send = (QPushButton*)sender();
266   
267   if(send == GroupPoints->PushButton1) 
268     myEditCurrentArgument = GroupPoints->LineEdit1;
269   else if(send == GroupPoints->PushButton2)
270   {
271     myEditCurrentArgument = GroupPoints->LineEdit2;
272     if( getConstructorId()==1 )
273       globalSelection( GEOM_PLANE  );
274   }
275  
276   globalSelection( GEOM_ALLSHAPES );
277       
278   myEditCurrentArgument->setFocus();
279   SelectionIntoArgument();
280 }
281
282
283 //=================================================================================
284 // function : LineEditReturnPressed()
285 // purpose  :
286 //=================================================================================
287 void OperationGUI_PartitionDlg::LineEditReturnPressed()
288 {
289   QLineEdit* send = (QLineEdit*)sender();
290
291   if(send == GroupPoints->LineEdit1 || 
292      send == GroupPoints->LineEdit2 )
293     {
294       myEditCurrentArgument = send;
295       GEOMBase_Skeleton::LineEditReturnPressed();
296     }
297 }
298
299
300 //=================================================================================
301 // function : ActivateThisDialog()
302 // purpose  :
303 //=================================================================================
304 void OperationGUI_PartitionDlg::ActivateThisDialog()
305 {
306   GEOMBase_Skeleton::ActivateThisDialog();
307   connect(myGeomGUI->getApp()->selectionMgr(), 
308           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
309
310   ConstructorsClicked( getConstructorId() ); 
311 }
312
313
314 //=================================================================================
315 // function : enterEvent()
316 // purpose  :
317 //=================================================================================
318 void OperationGUI_PartitionDlg::enterEvent(QEvent* e)
319 {
320   if (!GroupConstructors->isEnabled())
321     this->ActivateThisDialog();
322 }
323
324
325 //=================================================================================
326 // function : createOperation
327 // purpose  :
328 //=================================================================================
329 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
330 {
331   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
332 }
333
334
335 //=================================================================================
336 // function : isValid
337 // purpose  :
338 //=================================================================================
339 bool OperationGUI_PartitionDlg::isValid( QString& msg )
340 {
341   return (myListShapes.length()     || myListTools.length() ||
342           myListKeepInside.length() || myListRemoveInside.length());
343 }
344
345
346 //=================================================================================
347 // function : execute
348 // purpose  :
349 //=================================================================================
350 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
351 {
352   bool res = false;
353
354   GEOM::GEOM_Object_var anObj;
355   QString msg;
356
357   int aLimit = GetLimit();
358   int aConstructorId = getConstructorId();
359
360   if (aConstructorId == 1)
361     aLimit = GEOM::SHAPE;
362
363   if (isValid( msg )) {
364
365     anObj = GEOM::GEOM_IBooleanOperations::_narrow(getOperation())->
366       MakePartition(myListShapes, myListTools,
367                     myListKeepInside, myListRemoveInside,
368                     aLimit, false, myListMaterials);
369     res = true;
370   }
371
372   if (!anObj->_is_nil())
373     objects.push_back( anObj._retn() );
374
375   return res;
376 }
377
378
379 //=================================================================================
380 // function : closeEvent
381 // purpose  :
382 //=================================================================================
383 void OperationGUI_PartitionDlg::closeEvent( QCloseEvent* e )
384 {
385   GEOMBase_Skeleton::closeEvent( e );
386 }
387
388
389 //=======================================================================
390 //function : ComboTextChanged
391 //purpose  : 
392 //=======================================================================
393 void OperationGUI_PartitionDlg::ComboTextChanged()
394 {
395   bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
396   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
397   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
398   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
399   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
400   //GroupPoints->PushButton3->setEnabled(IsEnabled);
401   //GroupPoints->PushButton4->setEnabled(IsEnabled);
402 }
403
404
405 //=================================================================================
406 // function : GetLimit()
407 // purpose  : 
408 //=================================================================================
409 int OperationGUI_PartitionDlg::GetLimit() const
410 {
411   int aLimit = GroupPoints->ComboBox1->currentItem();
412
413   switch(aLimit)
414   {
415   case 0:  aLimit = GEOM::SOLID ; break;
416   case 1:  aLimit = GEOM::SHELL ; break;
417   case 2:  aLimit = GEOM::FACE  ; break;
418   case 3:  aLimit = GEOM::WIRE  ; break;
419   case 4:  aLimit = GEOM::EDGE  ; break;
420   case 5:  aLimit = GEOM::VERTEX; break;
421   default: aLimit = GEOM::SHAPE ;
422   }
423
424   return aLimit;
425 }