Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / OperationGUI / OperationGUI_FilletDlg.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_FilletDlg.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "OperationGUI_FilletDlg.h"
30 #include "DlgRef_1Sel1Spin.h"
31 #include "DlgRef_2Sel1Spin.h"
32
33 #include "SUIT_Desktop.h"
34 #include "SUIT_Session.h"
35 #include "SalomeApp_Application.h"
36 #include "LightApp_SelectionMgr.h"
37 #include "OCCViewer_ViewModel.h"
38
39 #include "qlabel.h"
40 #include "qpixmap.h"
41 #include <TColStd_MapOfInteger.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <list>
44
45 #include "GEOMImpl_Types.hxx"
46
47 //=================================================================================
48 // class    : OperationGUI_FilletDlg()
49 // purpose  : Constructs a OperationGUI_FilletDlg which is a child of 'parent', with the 
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 OperationGUI_FilletDlg::OperationGUI_FilletDlg(GeometryGUI* theGeometryGUI, QWidget* parent)
55   :GEOMBase_Skeleton(theGeometryGUI, parent, "OperationGUI_FilletDlg", false,
56                      WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
57 {
58   myConstructorId = -1;
59
60   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
61   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_FILLET_ALL")));
62   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_FILLET_EDGE")));
63   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_FILLET_FACE")));
64
65   QPixmap iconSelect(aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
66
67   setCaption(tr("GEOM_FILLET_TITLE"));
68
69   /***************************************************************/
70   GroupConstructors->setTitle( tr( "GEOM_FILLET" ) );
71   RadioButton1->setPixmap( image0 );
72   RadioButton2->setPixmap( image1 );
73   RadioButton3->setPixmap( image2 );
74
75   Group1 = new DlgRef_1Sel1Spin( this, "Group1" );
76   Group1->GroupBox1->setTitle( tr( "GEOM_FILLET_ALL" ) );
77   Group1->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
78   Group1->TextLabel2->setText( tr( "GEOM_RADIUS" ) );
79   Group1->PushButton1->setPixmap( iconSelect );
80   Group1->LineEdit1->setReadOnly( true );
81
82   Group2 = new DlgRef_2Sel1Spin( this, "Group2" );
83   Group2->GroupBox1->setTitle( tr( "GEOM_FILLET_EDGES" ) );
84   Group2->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
85   Group2->TextLabel2->setText( tr( "SELECTED_EDGES" ) );
86   Group2->TextLabel3->setText( tr( "GEOM_RADIUS" ) );
87   Group2->PushButton1->setPixmap( iconSelect );
88   Group2->PushButton2->setPixmap( iconSelect );
89   Group2->LineEdit1->setReadOnly( true );
90   Group2->LineEdit2->setReadOnly( true );
91
92   Group3 = new DlgRef_2Sel1Spin( this, "Group3" );
93   Group3->GroupBox1->setTitle(tr( "GEOM_FILLET_FACES" ) );
94   Group3->TextLabel1->setText(tr( "GEOM_MAIN_OBJECT" ) );
95   Group3->TextLabel2->setText(tr( "SELECTED_FACES" ) );
96   Group3->TextLabel3->setText(tr( "GEOM_RADIUS" ) );
97   Group3->PushButton1->setPixmap( iconSelect );
98   Group3->PushButton2->setPixmap( iconSelect );
99   Group3->LineEdit1->setReadOnly( true );
100   Group3->LineEdit2->setReadOnly( true );
101
102   Layout1->addWidget( Group1, 2, 0 );
103   Layout1->addWidget( Group2, 2, 0 );
104   Layout1->addWidget( Group3, 2, 0 );
105   /***************************************************************/
106
107   double SpecificStep = 10.0;
108   Group1->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, SpecificStep, 3);
109   Group2->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, SpecificStep, 3);
110   Group3->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, SpecificStep, 3);
111
112   setHelpFileName("fillet.htm");
113
114   /* Initialisations */
115   Init();
116 }
117
118
119 //=================================================================================
120 // function : ~OperationGUI_FilletDlg()
121 // purpose  : Destroys the object and frees any allocated resources
122 //=================================================================================
123 OperationGUI_FilletDlg::~OperationGUI_FilletDlg()
124 {  
125 }
126
127
128 //=================================================================================
129 // function : Init()
130 // purpose  :
131 //=================================================================================
132 void OperationGUI_FilletDlg::Init()
133 {
134   myConstructorId = -1;
135   reset();
136   RadioButton1->setChecked( true );
137   myEditCurrentArgument = Group1->LineEdit1;
138
139   // main buttons
140   connect( buttonOk   , SIGNAL( clicked() ), this, SLOT( ClickOnOk()    ) );
141   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
142
143   // group box
144   connect( GroupConstructors, SIGNAL( clicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
145
146   // push buttons
147   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
148   connect(Group2->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
149   connect(Group3->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
150   connect(Group2->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
151   connect(Group3->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
152
153   // line edits
154   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
155   connect(Group2->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
156   connect(Group3->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
157
158   // spin boxes  
159   connect(Group1->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
160   connect(Group2->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
161   connect(Group3->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
162
163     // selection
164   connect(myGeomGUI->getApp()->selectionMgr(), 
165           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
166
167   initName( tr( "GEOM_FILLET" ) );
168
169   Group2->hide();
170   Group3->hide();
171   Group1->show();
172 }
173
174
175 //=================================================================================
176 // function : ConstructorsClicked()
177 // purpose  : Radio button management
178 //=================================================================================
179 void OperationGUI_FilletDlg::ConstructorsClicked( int constructorId )
180 {
181   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
182        != OCCViewer_Viewer::Type() )
183   {
184     RadioButton1->setChecked( true );
185     return;
186   }
187
188   if ( myConstructorId == constructorId )
189     return;
190
191   // Get radius from previous widget
192   double R = 5;
193   if      ( myConstructorId == 0 ) R = Group1->SpinBox_DX->GetValue();
194   else if ( myConstructorId == 1 ) R = Group2->SpinBox_DX->GetValue();
195   else                             R = Group3->SpinBox_DX->GetValue();
196
197   myConstructorId = constructorId;
198
199   switch ( constructorId )
200   {
201     case 0:
202         Group1->show();
203         Group2->hide();
204         Group3->hide();
205         Group1->SpinBox_DX->SetValue( R );
206     break;
207     case 1:
208         Group1->hide();
209         Group2->show();
210         Group3->hide();
211         Group2->SpinBox_DX->SetValue( R );
212     break;
213     case 2:
214         Group1->hide();
215         Group2->hide();
216         Group3->show();
217         Group3->SpinBox_DX->SetValue( R );
218     break;
219     default:
220     break;
221   }
222
223   if      ( constructorId == 0 ) myEditCurrentArgument = Group1->LineEdit1;
224   else if ( constructorId == 1 ) myEditCurrentArgument = Group2->LineEdit1;
225   else                           myEditCurrentArgument = Group3->LineEdit1;
226
227   activateSelection();
228   enableWidgets();
229
230   if ( !myShape->_is_nil() )
231   {
232     myEditCurrentArgument->setText( GEOMBase::GetName( myShape ) );
233     GEOMBase_Skeleton::LineEditReturnPressed();
234   }
235   else
236     myEditCurrentArgument->setText( "" );
237
238   displayPreview();  
239 }
240
241
242 //=================================================================================
243 // function : ClickOnOk()
244 // purpose  :
245 //=================================================================================
246 void OperationGUI_FilletDlg::ClickOnOk()
247 {
248   if ( ClickOnApply() )
249     ClickOnCancel();
250 }
251
252
253 //=================================================================================
254 // function : ClickOnApply()
255 // purpose  :
256 //=================================================================================
257 bool OperationGUI_FilletDlg::ClickOnApply()
258 {
259     if ( !onAccept() )
260     return false;
261
262   initName();
263   return true;
264 }
265
266 //=================================================================================
267 // function : SelectionIntoArgument()
268 // purpose  : Called when selection has changed
269 //=================================================================================
270 void OperationGUI_FilletDlg::SelectionIntoArgument()
271 {
272   erasePreview();
273   myEditCurrentArgument->setText( "" );
274
275   // If selection of main object is activated
276   if ( myEditCurrentArgument == Group1->LineEdit1 ||
277        myEditCurrentArgument == Group2->LineEdit1 ||
278        myEditCurrentArgument == Group3->LineEdit1 )
279   {
280     if ( IObjectCount() == 1 )
281     {
282       Standard_Boolean aResult = Standard_False;
283       GEOM::GEOM_Object_var anObj =
284         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
285
286       if ( aResult && !anObj->_is_nil() )
287       {
288         myShape = anObj;
289         myEditCurrentArgument->setText( GEOMBase::GetName( anObj ) );
290         displayPreview();
291         enableWidgets();
292         return;
293       }
294     }
295
296     myShape = GEOM::GEOM_Object::_nil();
297     enableWidgets();
298   }
299   // If face or edge selection is activated
300   else if ( myEditCurrentArgument == Group2->LineEdit2 ||
301             myEditCurrentArgument == Group3->LineEdit2 )
302   {
303     if ( IObjectCount() == 1 )
304     {
305       Standard_Boolean aResult = Standard_False;
306       GEOM::GEOM_Object_var anObj =
307         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
308
309       if ( aResult && !anObj->_is_nil() )
310       {
311         TColStd_IndexedMapOfInteger anIndexes;
312         myGeomGUI->getApp()->selectionMgr()->GetIndexes( firstIObject(), anIndexes );
313
314         if ( anIndexes.Extent() > 0 )
315         {
316           QString aName;
317           if ( anIndexes.Extent() == 1 )
318           {
319             int anIndex = anIndexes( 1 );
320
321             aName = QString( GEOMBase::GetName( anObj ) ) + QString( ":%1" ).arg( anIndex );
322           }
323           else
324             aName = tr( "GEOM_MEN_POPUP_NAME" ).arg( anIndexes.Extent() );
325
326           myEditCurrentArgument->setText( aName );
327
328           if ( myConstructorId == 1 )
329             myEdges = anIndexes;
330           else
331             myFaces = anIndexes;
332
333           displayPreview();
334           return;
335         }
336       }
337     }
338     myFaces.Clear();
339   }
340 }
341
342
343 //=================================================================================
344 // function : LineEditReturnPressed()
345 // purpose  :
346 //=================================================================================
347 void OperationGUI_FilletDlg::LineEditReturnPressed()
348 {
349   QLineEdit* send = ( QLineEdit* )sender();
350   
351   if ( send == Group1->LineEdit1 )
352     myEditCurrentArgument = Group1->LineEdit1;
353   else if ( send == Group2->LineEdit1 )
354     myEditCurrentArgument = Group2->LineEdit1;
355   else if ( send == Group3->LineEdit1 )
356     myEditCurrentArgument = Group3->LineEdit1;
357   else
358     return;
359
360   GEOMBase_Skeleton::LineEditReturnPressed();
361   return;
362 }
363
364
365 //=================================================================================
366 // function : SetEditCurrentArgument()
367 // purpose  :
368 //=================================================================================
369 void OperationGUI_FilletDlg::SetEditCurrentArgument()
370 {
371   QPushButton* send = (QPushButton*)sender();
372
373   if ( send == Group1->PushButton1 )
374   {
375     Group1->LineEdit1->setFocus();
376     myEditCurrentArgument = Group1->LineEdit1;
377   }
378   else if( send == Group2->PushButton1 )
379   {
380     Group2->LineEdit1->setFocus();
381     myEditCurrentArgument = Group2->LineEdit1;
382   }
383   else if( send == Group2->PushButton2 )
384   {
385     Group2->LineEdit2->setFocus();
386     myEditCurrentArgument = Group2->LineEdit2;
387   }
388   else if ( send == Group3->PushButton1 )
389   {
390     Group3->LineEdit1->setFocus();
391     myEditCurrentArgument = Group3->LineEdit1;
392   }
393   else if( send == Group3->PushButton2 )
394   {
395     Group3->LineEdit1->setFocus();
396     myEditCurrentArgument = Group3->LineEdit2;
397   }
398
399   activateSelection();
400 }
401
402
403 //=================================================================================
404 // function : ActivateThisDialog()
405 // purpose  :
406 //=================================================================================
407 void OperationGUI_FilletDlg::ActivateThisDialog()
408 {
409   GEOMBase_Skeleton::ActivateThisDialog();
410
411   connect(myGeomGUI->getApp()->selectionMgr(), 
412           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
413
414   activateSelection();
415   displayPreview();
416 }
417
418
419 //=================================================================================
420 // function : enterEvent()
421 // purpose  :
422 //=================================================================================
423 void OperationGUI_FilletDlg::enterEvent( QEvent* e )
424 {
425   if ( !GroupConstructors->isEnabled() )
426     this->ActivateThisDialog();
427 }
428
429 //=================================================================================
430 // function : ValueChangedInSpinBox()
431 // purpose  :
432 //=================================================================================
433 void OperationGUI_FilletDlg::ValueChangedInSpinBox( double newValue )
434 {
435   displayPreview();
436 }
437
438 //=================================================================================
439 // function : reset()
440 // purpose  :
441 //=================================================================================
442 void OperationGUI_FilletDlg::reset()
443 {
444   // Set Initial values of spinboxes
445   Group1->SpinBox_DX->SetValue( 5 );
446   Group2->SpinBox_DX->SetValue( 5 );
447   Group3->SpinBox_DX->SetValue( 5 );
448
449   Group1->LineEdit1->setText( "" );
450   Group2->LineEdit1->setText( "" );
451   Group2->LineEdit2->setText( "" );
452   Group3->LineEdit1->setText( "" );
453   Group3->LineEdit2->setText( "" );
454
455   // constructor id
456   int aConstructorId = getConstructorId();
457
458   if      ( aConstructorId == 0 ) myEditCurrentArgument = Group1->LineEdit1;
459   else if ( aConstructorId == 1 ) myEditCurrentArgument = Group2->LineEdit1;
460   else                            myEditCurrentArgument = Group3->LineEdit1;
461
462   myShape = GEOM::GEOM_Object::_nil();
463
464   myFaces.Clear();
465   myEdges.Clear();
466
467   erasePreview( true );
468
469   activateSelection();
470
471   enableWidgets();
472 }
473
474 //=================================================================================
475 // function : getConstructorId()
476 // purpose  :
477 //=================================================================================
478 int OperationGUI_FilletDlg::getConstructorId() const
479 {
480   return GroupConstructors->id( GroupConstructors->selected() );
481 }
482
483 //=================================================================================
484 // function : activateSelection
485 // purpose  : Activate selection in accordance with myEditCurrentArgument
486 //=================================================================================
487 void OperationGUI_FilletDlg::activateSelection()
488 {
489
490   if ( !myShape->_is_nil() && myEditCurrentArgument == Group2->LineEdit2 )
491     localSelection( myShape, TopAbs_EDGE );
492   else if ( !myShape->_is_nil() && myEditCurrentArgument == Group3->LineEdit2 )
493     localSelection( myShape, TopAbs_FACE );
494   else
495   {
496     TColStd_MapOfInteger aMap;
497     aMap.Add( GEOM_SHELL );
498     aMap.Add( GEOM_SOLID );
499     aMap.Add( GEOM_COMPOUND );
500     globalSelection( aMap );
501   }
502
503   SelectionIntoArgument();
504 }
505
506 //=================================================================================
507 // function : enableWidgets
508 // purpose  : Enable widgets of faces in accordance with value of main object
509 //=================================================================================
510 void OperationGUI_FilletDlg::enableWidgets()
511 {
512   int anId = getConstructorId();
513
514   bool toEnable = !myShape->_is_nil();
515
516   if ( anId == 1 )
517   {
518     Group2->LineEdit2->setEnabled( toEnable );
519     Group2->PushButton2->setEnabled( toEnable );
520     
521     if ( !toEnable )
522     {
523       Group2->LineEdit2->setText( "" );
524       myEdges.Clear();
525     }
526   }
527   else if ( anId == 2 )
528   {
529     Group3->LineEdit2->setEnabled( toEnable );
530     Group3->PushButton2->setEnabled( toEnable );
531
532     if ( !toEnable )
533     {
534       Group3->LineEdit2->setText( "" );
535       myFaces.Clear();
536     }
537   }
538 }
539
540 //=================================================================================
541 // function : createOperation
542 // purpose  :
543 //=================================================================================
544 GEOM::GEOM_IOperations_ptr OperationGUI_FilletDlg::createOperation()
545 {
546   return getGeomEngine()->GetILocalOperations( getStudyId() );
547 }
548
549 //=================================================================================
550 // function : ClickOnApply()
551 // purpose  : Verify validity of input data
552 //=================================================================================
553 bool OperationGUI_FilletDlg::isValid( QString& )
554 {
555   switch ( getConstructorId() )
556   {
557     case 0: return !myShape->_is_nil();
558     case 1: return !myShape->_is_nil() && myEdges.Extent() > 0;
559     case 2: return !myShape->_is_nil() && myFaces.Extent() > 0;
560     default: return false;
561   }
562 }
563
564 //=================================================================================
565 // function : execute
566 // purpose  :
567 //=================================================================================
568 bool OperationGUI_FilletDlg::execute( ObjectList& objects )
569 {
570   GEOM::GEOM_Object_var anObj;
571
572   int anId = getConstructorId();
573   if ( anId == 0 )
574     anObj = GEOM::GEOM_ILocalOperations::_narrow(
575       getOperation() )->MakeFilletAll( myShape,
576                                        getRadius() );
577   else if ( anId == 1 )
578   {
579     GEOM::ListOfLong_var aList = new GEOM::ListOfLong;
580     aList->length( myEdges.Extent() );
581
582     for ( int i = 1, n = myEdges.Extent(); i <= n; i++ )
583       aList[ i - 1 ] = myEdges( i );
584
585     anObj = GEOM::GEOM_ILocalOperations::_narrow(
586       getOperation() )->MakeFilletEdges( myShape, getRadius(), aList );
587   }
588   else if ( anId == 2 )
589   {
590     int i = 0;
591     GEOM::ListOfLong_var aList = new GEOM::ListOfLong;
592     aList->length( myFaces.Extent() );
593
594     for ( int i = 1, n = myFaces.Extent(); i <= n; i++ )
595       aList[ i - 1 ] = myFaces( i );
596
597     anObj = GEOM::GEOM_ILocalOperations::_narrow(
598       getOperation() )->MakeFilletFaces( myShape, getRadius(), aList );
599   }
600
601   if ( !anObj->_is_nil() )
602     objects.push_back( anObj._retn() );
603
604   return true;
605 }
606
607 //=================================================================================
608 // function : getRadius
609 // purpose  : Get radius     
610 //=================================================================================
611 double OperationGUI_FilletDlg::getRadius() const
612 {
613   int anId = getConstructorId();
614   if      ( anId == 0 ) return Group1->SpinBox_DX->GetValue();
615   else if ( anId == 1 ) return Group2->SpinBox_DX->GetValue();
616   else                  return Group3->SpinBox_DX->GetValue();
617 }