Salome HOME
*** empty log message ***
[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 #include "OperationGUI_MaterialDlg.h"
28
29 #include <GEOM_DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <GEOMImpl_Types.hxx>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40
41 //=================================================================================
42 // class    : OperationGUI_PartitionDlg()
43 // purpose  : Constructs a OperationGUI_PartitionDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 OperationGUI_PartitionDlg::OperationGUI_PartitionDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
49   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
50 {
51   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
52   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_PARTITION" ) ) );
53   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_PARTITION_PLANE" ) ) );
54   QPixmap image2( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
55
56   setWindowTitle( tr( "GEOM_PARTITION_TITLE" ) );
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_PARTITION" ) );
60   mainFrame()->RadioButton1->setIcon( image0 );
61   mainFrame()->RadioButton2->setIcon( image1 );
62   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
63   mainFrame()->RadioButton3->close();
64
65   // Full partition (contains half-space partition)
66   GroupPoints = new DlgRef_2Sel1List( centralWidget() );
67   GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
68   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
69   GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECT" ) );
70   GroupPoints->TextLabel3->setText( tr( "GEOM_RECONSTRUCTION_LIMIT" ) );
71   GroupPoints->PushButton1->setIcon( image2 );
72   GroupPoints->PushButton2->setIcon( image2 );
73   GroupPoints->LineEdit1->setReadOnly( true );
74   GroupPoints->LineEdit2->setReadOnly( true );
75
76   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
77   layout->setMargin( 0 ); layout->setSpacing( 6 );
78   layout->addWidget( GroupPoints );
79
80   /***************************************************************/
81
82   setHelpFileName( "partition.htm" ); 
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   
121   /* signals and slots connections */
122   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
123   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
124
125   connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
126   
127   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
128   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
129   
130   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
131   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
132   
133   connect( GroupPoints->ComboBox1, SIGNAL( activated( int ) ), this, SLOT( ComboTextChanged() ) );
134   
135   connect( myGeomGUI->getApp()->selectionMgr(), 
136            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
137   
138   initName( tr( "GEOM_PARTITION" ) );
139
140   ConstructorsClicked( 0 );
141 }
142
143
144 //=================================================================================
145 // function : ConstructorsClicked()
146 // purpose  : Radio button management
147 //=================================================================================
148 void OperationGUI_PartitionDlg::ConstructorsClicked( int constructorId )
149 {
150   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
151   globalSelection();
152   
153   myListShapes.length( 0 );
154   myListTools.length( 0 );  
155   myListKeepInside.length( 0 );
156   myListRemoveInside.length( 0 );
157   myListMaterials.length( 0 );
158   
159   switch ( constructorId ) {
160   case 0: /*Full partition */
161     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
162     GroupPoints->TextLabel2->setText( tr( "GEOM_TOOL_OBJECT" ) );
163     GroupPoints->TextLabel3->show();
164     GroupPoints->ComboBox1->show();
165     
166     GroupPoints->ComboBox1->setCurrentIndex( 0 );
167     break;
168   case 1: /*Half-space partition */
169     GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION_HALFSPACE" ) );
170     GroupPoints->TextLabel3->hide();
171     GroupPoints->ComboBox1->hide();
172     GroupPoints->TextLabel2->setText( tr( "GEOM_PLANE" ) );
173     break;
174   } 
175
176   myEditCurrentArgument = GroupPoints->LineEdit1;
177   GroupPoints->LineEdit1->clear();
178   GroupPoints->LineEdit2->clear();
179
180   qApp->processEvents();
181   updateGeometry();
182   resize( minimumSize() );
183
184   myEditCurrentArgument->setFocus();
185   connect( myGeomGUI->getApp()->selectionMgr(), 
186            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
187 }
188
189
190 //=================================================================================
191 // function : ClickOnOk()
192 // purpose  :
193 //=================================================================================
194 void OperationGUI_PartitionDlg::ClickOnOk()
195 {
196   if ( ClickOnApply() )
197     ClickOnCancel();
198 }
199
200
201 //=================================================================================
202 // function : ClickOnApply()
203 // purpose  :
204 //=================================================================================
205 bool OperationGUI_PartitionDlg::ClickOnApply()
206 {
207   if ( !onAccept() )
208     return false;
209   
210   initName();
211   ConstructorsClicked( getConstructorId() );
212   return true;
213 }
214
215
216 //=================================================================================
217 // function : SelectionIntoArgument()
218 // purpose  : Called when selection as changed or other case
219 //=================================================================================
220 void OperationGUI_PartitionDlg::SelectionIntoArgument()
221 {
222   myEditCurrentArgument->setText( "" );
223   QString aString = "";
224   
225   int nbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aString, true );
226     
227   if ( nbSel < 1 ) {
228     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
229       myListShapes.length( 0 );
230       myListMaterials.length( 0 );
231     }
232     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
233       myListTools.length( 0 );
234   }
235   
236   // One and only one plane can be selected
237   
238   if ( getConstructorId() == 1 && 
239        myEditCurrentArgument == GroupPoints->LineEdit2 && 
240        nbSel != 1 ) {
241     myListTools.length( 0 );
242     return;
243   }
244   
245   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
246     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myListShapes, true );
247     myListMaterials.length( 0 );
248     if ( !myListShapes.length() )
249       return;
250   }
251   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
252     GEOMBase::ConvertListOfIOInListOfGO( selectedIO(), myListTools, true );
253     if ( !myListTools.length() )
254       return;
255   }
256   
257   myEditCurrentArgument->setText( aString );
258 }
259
260
261 //=================================================================================
262 // function : SetEditCurrentArgument()
263 // purpose  :
264 //=================================================================================
265 void OperationGUI_PartitionDlg::SetEditCurrentArgument()
266 {
267   QPushButton* send = (QPushButton*)sender();
268   
269   if ( send == GroupPoints->PushButton1 ) 
270     myEditCurrentArgument = GroupPoints->LineEdit1;
271   else if ( send == GroupPoints->PushButton2 ) {
272     myEditCurrentArgument = GroupPoints->LineEdit2;
273     if ( getConstructorId() == 1 )
274       globalSelection( GEOM_PLANE  );
275   }
276  
277   globalSelection( GEOM_ALLSHAPES );
278       
279   myEditCurrentArgument->setFocus();
280   SelectionIntoArgument();
281 }
282
283
284 //=================================================================================
285 // function : LineEditReturnPressed()
286 // purpose  :
287 //=================================================================================
288 void OperationGUI_PartitionDlg::LineEditReturnPressed()
289 {
290   QLineEdit* send = (QLineEdit*)sender();
291
292   if ( send == GroupPoints->LineEdit1 || 
293        send == GroupPoints->LineEdit2 ) {
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* )
319 {
320   if ( !mainFrame()->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& )
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     anObj = GEOM::GEOM_IBooleanOperations::_narrow(getOperation())->
365       MakePartition(myListShapes, myListTools,
366                     myListKeepInside, myListRemoveInside,
367                     aLimit, false, myListMaterials);
368     res = true;
369   }
370
371   if ( !anObj->_is_nil() )
372     objects.push_back( anObj._retn() );
373
374   return res;
375 }
376
377
378 //=================================================================================
379 // function : closeEvent
380 // purpose  :
381 //=================================================================================
382 void OperationGUI_PartitionDlg::closeEvent( QCloseEvent* e )
383 {
384   GEOMBase_Skeleton::closeEvent( e );
385 }
386
387
388 //=======================================================================
389 //function : ComboTextChanged
390 //purpose  : 
391 //=======================================================================
392 void OperationGUI_PartitionDlg::ComboTextChanged()
393 {
394   //bool IsEnabled = GroupPoints->ComboBox1->currentItem() < 3;
395   //GroupPoints->LineEdit3->setEnabled(IsEnabled);
396   //GroupPoints->LineEdit4->setEnabled(IsEnabled);
397   //GroupPoints->TextLabel4->setEnabled(IsEnabled);
398   //GroupPoints->TextLabel5->setEnabled(IsEnabled);
399   //GroupPoints->PushButton3->setEnabled(IsEnabled);
400   //GroupPoints->PushButton4->setEnabled(IsEnabled);
401 }
402
403
404 //=================================================================================
405 // function : GetLimit()
406 // purpose  : 
407 //=================================================================================
408 int OperationGUI_PartitionDlg::GetLimit() const
409 {
410   int aLimit = GroupPoints->ComboBox1->currentIndex();
411
412   switch ( aLimit ) {
413   case 0:  aLimit = GEOM::SOLID ; break;
414   case 1:  aLimit = GEOM::SHELL ; break;
415   case 2:  aLimit = GEOM::FACE  ; break;
416   case 3:  aLimit = GEOM::WIRE  ; break;
417   case 4:  aLimit = GEOM::EDGE  ; break;
418   case 5:  aLimit = GEOM::VERTEX; break;
419   default: aLimit = GEOM::SHAPE ;
420   }
421
422   return aLimit;
423 }