Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[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 // File   : OperationGUI_PartitionDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
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->CheckButton1->setText( tr( "GEOM_KEEP_NONLIMIT_SHAPES" ) );
75
76   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
77   layout->setMargin( 0 ); layout->setSpacing( 6 );
78   layout->addWidget( GroupPoints );
79
80   /***************************************************************/
81
82   setHelpFileName( "partition_page.html" );
83  
84   Init();
85 }
86
87
88 //=================================================================================
89 // function : ~OperationGUI_PartitionDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 OperationGUI_PartitionDlg::~OperationGUI_PartitionDlg()
93 {
94   // no need to delete child widgets, Qt does it all for us
95 }
96
97 void OperationGUI_PartitionDlg::SetListMaterials( GEOM::ListOfLong ListMaterials )
98 {
99   myListMaterials = ListMaterials; 
100 }
101   
102 GEOM::ListOfLong OperationGUI_PartitionDlg::GetListMaterials()
103
104   return myListMaterials; 
105 }
106
107 //=================================================================================
108 // function : Init()
109 // purpose  :
110 //=================================================================================
111 void OperationGUI_PartitionDlg::Init()
112 {
113   /* type for sub shape selection */
114   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SOLID" ) );
115   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_SHELL" ) );
116   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_FACE" ) );
117   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_WIRE" ) );
118   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_EDGE" ) );
119   GroupPoints->ComboBox1->addItem( tr( "GEOM_RECONSTRUCTION_LIMIT_VERTEX" ) );
120   GroupPoints->CheckButton1->setChecked( false );
121   
122   /* signals and slots connections */
123   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
124   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
125
126   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
127   
128   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
129   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
130   
131   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
132   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
133   
134   connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
135   
136   connect( GroupPoints->CheckButton1, SIGNAL( stateChanged( int ) ), this, SLOT( ReverseSense( int ) ) );
137
138   connect( myGeomGUI->getApp()->selectionMgr(),
139            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
140   
141   initName( tr( "GEOM_PARTITION" ) );
142
143   ConstructorsClicked( 0 );
144 }
145
146
147 //=================================================================================
148 // function : ConstructorsClicked()
149 // purpose  : Radio button management
150 //=================================================================================
151 void OperationGUI_PartitionDlg::ConstructorsClicked( int constructorId )
152 {
153   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
154   globalSelection();
155   
156   myListShapes.length( 0 );
157   myListTools.length( 0 );  
158   myListKeepInside.length( 0 );
159   myListRemoveInside.length( 0 );
160   myListMaterials.length( 0 );
161   
162   switch ( constructorId ) {
163   case 0: /*Full partition */
164     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
165     GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECT" ) );
166     GroupPoints->TextLabel3->show();
167     GroupPoints->ComboBox1->show();
168     GroupPoints->ComboBox1->setCurrentIndex( 0 );
169     GroupPoints->CheckButton1->show();
170     break;
171   case 1: /*Half-space partition */
172     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION_HALFSPACE" ) );
173     GroupPoints->TextLabel3->hide();
174     GroupPoints->ComboBox1->hide();
175     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE" ) );
176     GroupPoints->CheckButton1->hide();
177     break;
178   } 
179
180   myEditCurrentArgument = GroupPoints->LineEdit1;
181   GroupPoints->LineEdit1->clear();
182   GroupPoints->LineEdit2->clear();
183
184   qApp->processEvents();
185   updateGeometry();
186   resize( minimumSize() );
187
188   myEditCurrentArgument->setFocus();
189   connect( myGeomGUI->getApp()->selectionMgr(), 
190            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
191 }
192
193
194 //=================================================================================
195 // function : ClickOnOk()
196 // purpose  :
197 //=================================================================================
198 void OperationGUI_PartitionDlg::ClickOnOk()
199 {
200   if ( ClickOnApply() )
201     ClickOnCancel();
202 }
203
204
205 //=================================================================================
206 // function : ClickOnApply()
207 // purpose  :
208 //=================================================================================
209 bool OperationGUI_PartitionDlg::ClickOnApply()
210 {
211   if ( !onAccept() )
212     return false;
213   
214   initName();
215   ConstructorsClicked( getConstructorId() );
216   return true;
217 }
218
219
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose  : Called when selection as changed or other case
223 //=================================================================================
224 void OperationGUI_PartitionDlg::SelectionIntoArgument()
225 {
226   myEditCurrentArgument->setText( "" );
227   QString aString = "";
228   
229   int nbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aString, true );
230     
231   if ( nbSel < 1 ) {
232     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
233       myListShapes.length( 0 );
234       myListMaterials.length( 0 );
235     }
236     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
237       myListTools.length( 0 );
238   }
239   
240   // One and only one plane can be selected
241   
242   if ( getConstructorId() == 1 && 
243        myEditCurrentArgument == GroupPoints->LineEdit2 && 
244        nbSel != 1 ) {
245     myListTools.length( 0 );
246     return;
247   }
248   
249   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
250     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myListShapes, true );
251     myListMaterials.length( 0 );
252     if ( !myListShapes.length() )
253       return;
254   }
255   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
256     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myListTools, true );
257     if ( !myListTools.length() )
258       return;
259   }
260   
261   myEditCurrentArgument->setText( aString );
262 }
263
264
265 //=================================================================================
266 // function : SetEditCurrentArgument()
267 // purpose  :
268 //=================================================================================
269 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
270 {
271   QPushButton* send = (QPushButton*)sender();
272   
273   if ( send == GroupPoints->PushButton1 ) 
274     myEditCurrentArgument = GroupPoints->LineEdit1;
275   else if ( send == GroupPoints->PushButton2 ) {
276     myEditCurrentArgument = GroupPoints->LineEdit2;
277     if ( getConstructorId() == 1 )
278       globalSelection( GEOM_PLANE  );
279   }
280  
281   globalSelection( GEOM_ALLSHAPES );
282       
283   myEditCurrentArgument->setFocus();
284   SelectionIntoArgument();
285 }
286
287
288 //=================================================================================
289 // function : LineEditReturnPressed()
290 // purpose  :
291 //=================================================================================
292 void OperationGUI_PartitionDlg::LineEditReturnPressed()
293 {
294   QLineEdit* send = (QLineEdit*)sender();
295
296   if ( send == GroupPoints->LineEdit1 || 
297        send == GroupPoints->LineEdit2 ) {
298       myEditCurrentArgument = send;
299       GEOMBase_Skeleton::LineEditReturnPressed();
300   }
301 }
302
303
304 //=================================================================================
305 // function : ActivateThisDialog()
306 // purpose  :
307 //=================================================================================
308 void OperationGUI_PartitionDlg::ActivateThisDialog()
309 {
310   GEOMBase_Skeleton::ActivateThisDialog();
311   connect( myGeomGUI->getApp()->selectionMgr(), 
312            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
313
314   ConstructorsClicked( getConstructorId() ); 
315 }
316
317
318 //=================================================================================
319 // function : enterEvent()
320 // purpose  :
321 //=================================================================================
322 void OperationGUI_PartitionDlg::enterEvent( QEvent* )
323 {
324   if ( !mainFrame()->GroupConstructors->isEnabled() )
325     this->ActivateThisDialog();
326 }
327
328
329 //=================================================================================
330 // function : createOperation
331 // purpose  :
332 //=================================================================================
333 GEOM::GEOM_IOperations_ptr OperationGUI_PartitionDlg::createOperation()
334 {
335   return getGeomEngine()->GetIBooleanOperations( getStudyId() );
336 }
337
338
339 //=================================================================================
340 // function : isValid
341 // purpose  :
342 //=================================================================================
343 bool OperationGUI_PartitionDlg::isValid( QString& )
344 {
345   return ( myListShapes.length()     || myListTools.length() ||
346            myListKeepInside.length() || myListRemoveInside.length() );
347 }
348
349
350 //=================================================================================
351 // function : execute
352 // purpose  :
353 //=================================================================================
354 bool OperationGUI_PartitionDlg::execute( ObjectList& objects )
355 {
356   bool res = false;
357
358   GEOM::GEOM_Object_var anObj;
359   QString msg;
360
361   int aLimit = GetLimit();
362   int aConstructorId = getConstructorId();
363   int aKeepNonlimitShapes = 0;
364
365   if ( aConstructorId == 1 ) {
366     aLimit = GEOM::SHAPE;
367   }
368   else {
369     if ( GroupPoints->CheckButton1->isChecked() ) {
370       aKeepNonlimitShapes = 1;
371     }
372     else {
373       aKeepNonlimitShapes = 0;
374     }
375   }
376
377   if ( isValid( msg ) ) {
378     anObj = GEOM::GEOM_IBooleanOperations::_narrow( getOperation() )->
379       MakePartition( myListShapes, myListTools,
380                      myListKeepInside, myListRemoveInside,
381                      aLimit, false, myListMaterials, aKeepNonlimitShapes );
382     res = true;
383   }
384
385   if ( !anObj->_is_nil() )
386     objects.push_back( anObj._retn() );
387
388   return res;
389 }
390
391
392 //=======================================================================
393 //function : ComboTextChanged
394 //purpose  : 
395 //=======================================================================
396 void OperationGUI_PartitionDlg::ComboTextChanged()
397 {
398   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
399   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
400   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
401   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
402   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
403   //GroupPoints->PushButton3->setEnabled(IsEnabled);
404   //GroupPoints->PushButton4->setEnabled(IsEnabled);
405 }
406
407
408 //=================================================================================
409 // function : GetLimit()
410 // purpose  : 
411 //=================================================================================
412 int OperationGUI_PartitionDlg::GetLimit() const
413 {
414   int aLimit = GroupPoints->ComboBox1->currentIndex();
415
416   switch ( aLimit ) {
417   case 0:  aLimit = GEOM::SOLID ; break;
418   case 1:  aLimit = GEOM::SHELL ; break;
419   case 2:  aLimit = GEOM::FACE  ; break;
420   case 3:  aLimit = GEOM::WIRE  ; break;
421   case 4:  aLimit = GEOM::EDGE  ; break;
422   case 5:  aLimit = GEOM::VERTEX; break;
423   default: aLimit = GEOM::SHAPE ;
424   }
425
426   return aLimit;
427 }