Salome HOME
Merging with BR_WPdev_For_5_0
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_BoxDlg.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   : PrimitiveGUI_BoxDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "PrimitiveGUI_BoxDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44 #include <ostream>
45
46 //=================================================================================
47 // class    : PrimitiveGUI_BoxDlg()
48 // purpose  : Constructs a PrimitiveGUI_BoxDlg which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 PrimitiveGUI_BoxDlg::PrimitiveGUI_BoxDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                           bool modal, Qt::WindowFlags fl )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
56 {
57   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BOX_2P" ) ) );
58   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BOX_DXYZ" ) ));
59   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
60
61   setWindowTitle( tr( "GEOM_BOX_TITLE" ) );
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BOX" ) );
65   mainFrame()->RadioButton1->setIcon( image0 );
66   mainFrame()->RadioButton2->setIcon( image1 );
67   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
68   mainFrame()->RadioButton3->close();
69
70   GroupPoints = new DlgRef_2Sel( centralWidget() );
71   GroupPoints->GroupBox1->setTitle( tr( "GEOM_DIAGONAL_POINTS" ) );
72   GroupPoints->TextLabel1->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
73   GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
74   GroupPoints->PushButton1->setIcon( image2 );
75   GroupPoints->PushButton2->setIcon( image2 );
76
77   GroupDimensions = new DlgRef_3Spin( centralWidget() );
78   GroupDimensions->GroupBox1->setTitle( tr( "GEOM_BOX_OBJ" ) );
79   GroupDimensions->TextLabel1->setText( tr( "GEOM_DX" ) );
80   GroupDimensions->TextLabel2->setText( tr( "GEOM_DY" ) );
81   GroupDimensions->TextLabel3->setText( tr( "GEOM_DZ" ) );
82
83   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
84   layout->setMargin( 0 ); layout->setSpacing( 6 );
85   layout->addWidget( GroupPoints );
86   layout->addWidget( GroupDimensions );
87   /***************************************************************/
88
89   setHelpFileName( "create_box_page.html" );
90   
91   Init();
92 }
93
94
95 //=================================================================================
96 // function : ~DialogBox_Box()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 PrimitiveGUI_BoxDlg::~PrimitiveGUI_BoxDlg()
100 {
101   // no need to delete child widgets, Qt does it all for us
102 }
103
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void PrimitiveGUI_BoxDlg::Init()
110 {
111   /* init variables */
112   myEditCurrentArgument = GroupPoints->LineEdit1;
113   GroupPoints->LineEdit1->setReadOnly( true );
114   GroupPoints->LineEdit2->setReadOnly( true );
115   
116   myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
117     
118   /* Get setting of step value from file configuration */
119   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
120   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100 );
121   
122   /* min, max, step and decimals for spin boxes */
123   initSpinBox( GroupDimensions->SpinBox_DX, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
124   initSpinBox( GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
125   initSpinBox( GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3 ); // VSR: TODO: DBL_DIGITS_DISPLAY
126
127   double initValue = 200.0;
128   GroupDimensions->SpinBox_DX->setValue( initValue );
129   GroupDimensions->SpinBox_DY->setValue( initValue );
130   GroupDimensions->SpinBox_DZ->setValue( initValue );
131
132   /* signals and slots connections */
133   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
134   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
135
136   connect( this,          SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
137
138   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
139   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
140
141   connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
142   connect( GroupPoints->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
143
144   connect( GroupDimensions->SpinBox_DX, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
145   connect( GroupDimensions->SpinBox_DY, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
146   connect( GroupDimensions->SpinBox_DZ, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
147   
148   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
149
150   connect( myGeomGUI->getApp()->selectionMgr(), 
151            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
152
153   initName( tr( "GEOM_BOX" ) );
154
155   ConstructorsClicked( 0 );
156 }
157
158 //=================================================================================
159 // function : SetDoubleSpinBoxStep()
160 // purpose  : Double spin box management
161 //=================================================================================
162 void PrimitiveGUI_BoxDlg::SetDoubleSpinBoxStep( double step )
163 {
164   GroupDimensions->SpinBox_DX->setSingleStep(step);
165   GroupDimensions->SpinBox_DY->setSingleStep(step);
166   GroupDimensions->SpinBox_DZ->setSingleStep(step);
167 }
168
169
170 //=================================================================================
171 // function : ConstructorsClicked()
172 // purpose  : Radio button management
173 //=================================================================================
174 void PrimitiveGUI_BoxDlg::ConstructorsClicked( int constructorId )
175 {
176   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
177   
178   switch (  constructorId ) {
179   case 0:
180     {
181       //globalSelection( GEOM_POINT );
182       globalSelection(); // close local contexts, if any
183       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
184       
185       GroupDimensions->hide();
186       GroupPoints->show();
187       
188       myEditCurrentArgument = GroupPoints->LineEdit1;
189       GroupPoints->LineEdit1->setText( "" );
190       GroupPoints->LineEdit2->setText( "" );
191       myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
192       
193       connect( myGeomGUI->getApp()->selectionMgr(),
194                SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
195       break;
196     }
197   case 1:
198     {
199       GroupPoints->hide();
200       GroupDimensions->show();
201       globalSelection(); // close local contexts, if any
202       break;
203     }
204   }
205  
206   qApp->processEvents();
207   updateGeometry();
208   resize( minimumSize() );
209
210   displayPreview();
211 }
212
213
214 //=================================================================================
215 // function : ClickOnOk()
216 // purpose  :
217 //=================================================================================
218 void PrimitiveGUI_BoxDlg::ClickOnOk()
219 {
220   if ( ClickOnApply() )
221     ClickOnCancel();
222 }
223
224
225 //=================================================================================
226 // function : ClickOnApply()
227 // purpose  :
228 //=================================================================================
229 bool PrimitiveGUI_BoxDlg::ClickOnApply()
230 {
231   if ( !onAccept() )
232     return false;
233   
234   initName();
235   ConstructorsClicked( getConstructorId() );
236   return true;
237 }
238
239
240 //=================================================================================
241 // function : SelectionIntoArgument()
242 // purpose  : Called when selection as changed
243 //=================================================================================
244 void PrimitiveGUI_BoxDlg::SelectionIntoArgument()
245 {
246   if ( getConstructorId() != 0 )
247     return;
248   
249   myEditCurrentArgument->setText( "" );
250   
251   if ( IObjectCount() != 1 ) {
252     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
253       myPoint1 = GEOM::GEOM_Object::_nil();
254     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
255       myPoint2 = GEOM::GEOM_Object::_nil();
256     return;
257   }
258
259   // nbSel == 1
260   Standard_Boolean testResult = Standard_False;
261   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
262   
263   if ( !testResult || CORBA::is_nil( aSelectedObject ) )
264     return;
265   
266   TopoDS_Shape aShape;
267   QString aName = GEOMBase::GetName( aSelectedObject );
268   if (GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
269     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
270     TColStd_IndexedMapOfInteger aMap;
271     aSelMgr->GetIndexes( firstIObject(), aMap );
272     if ( aMap.Extent() == 1) { // Local Selection
273       int anIndex = aMap( 1 );
274       aName.append( ":vertex_" + QString::number( anIndex ) );    
275
276       //Find SubShape Object in Father
277       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
278       
279       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
280         GEOM::GEOM_IShapesOperations_var aShapesOp =
281           getGeomEngine()->GetIShapesOperations( getStudyId() );
282         aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
283       }
284       else {
285         aSelectedObject = aFindedObject; // get Object from study       
286       }
287     }
288     else { // Global Selection
289       if ( aShape.ShapeType() != TopAbs_VERTEX ) {
290         aSelectedObject = GEOM::GEOM_Object::_nil();
291         aName = "";
292       }
293     }
294   }
295
296   myEditCurrentArgument->setText( aName );
297
298   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
299     myPoint1 = aSelectedObject;
300   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
301     myPoint2 = aSelectedObject;
302   
303   displayPreview();
304 }
305
306
307 //=================================================================================
308 // function : SetEditCurrentArgument()
309 // purpose  :
310 //=================================================================================
311 void PrimitiveGUI_BoxDlg::SetEditCurrentArgument()
312 {
313   QPushButton* send = (QPushButton*)sender();
314   
315   if ( send == GroupPoints->PushButton1 )
316     myEditCurrentArgument = GroupPoints->LineEdit1;
317   else if ( send == GroupPoints->PushButton2 )
318     myEditCurrentArgument = GroupPoints->LineEdit2;
319   
320   //  globalSelection( GEOM_POINT );
321   globalSelection(); // close local contexts, if any
322   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
323   
324   myEditCurrentArgument->setFocus();
325   SelectionIntoArgument();
326 }
327
328
329 //=================================================================================
330 // function : LineEditReturnPressed()
331 // purpose  :
332 //=================================================================================
333 void PrimitiveGUI_BoxDlg::LineEditReturnPressed()
334 {
335   QLineEdit* send = (QLineEdit*)sender();
336   if ( send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2 ) {
337     myEditCurrentArgument = send;
338     GEOMBase_Skeleton::LineEditReturnPressed();
339   }
340 }
341
342
343 //=================================================================================
344 // function : ActivateThisDialog()
345 // purpose  :
346 //=================================================================================
347 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
348 {
349   GEOMBase_Skeleton::ActivateThisDialog();
350   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
351            this, SLOT( SelectionIntoArgument() ) );
352   
353   ConstructorsClicked( getConstructorId() );
354 }
355
356
357 //=================================================================================
358 // function : enterEvent [REDEFINED]
359 // purpose  :
360 //=================================================================================
361 void PrimitiveGUI_BoxDlg::enterEvent( QEvent* )
362 {
363   if ( !mainFrame()->GroupConstructors->isEnabled() )
364     ActivateThisDialog();
365 }
366
367
368 //=================================================================================
369 // function : ValueChangedInSpinBox()
370 // purpose  :
371 //=================================================================================
372 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
373 {
374   displayPreview();
375 }
376
377
378 //=================================================================================
379 // function : createOperation
380 // purpose  :
381 //=================================================================================
382 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
383 {
384   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
385 }
386
387
388 //=================================================================================
389 // function : isValid
390 // purpose  :
391 //=================================================================================
392 bool PrimitiveGUI_BoxDlg::isValid( QString& msg )
393 {
394   return getConstructorId() == 0 ? !( myPoint1->_is_nil() || myPoint2->_is_nil() ) : true;
395 }
396
397
398 //=================================================================================
399 // function : execute
400 // purpose  :
401 //=================================================================================
402 bool PrimitiveGUI_BoxDlg::execute( ObjectList& objects )
403 {
404   bool res = false;
405   
406   GEOM::GEOM_Object_var anObj;
407   
408   switch ( getConstructorId() ) {
409   case 0 :
410     { 
411       if ( !CORBA::is_nil( myPoint1 ) &&  !CORBA::is_nil( myPoint2 ) ) {
412         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeBoxTwoPnt( myPoint1, myPoint2 );
413         res = true;
414       }
415       
416       break;
417     }
418   case 1 :
419     {
420       double x = GroupDimensions->SpinBox_DX->value();
421       double y = GroupDimensions->SpinBox_DY->value();
422       double z = GroupDimensions->SpinBox_DZ->value();
423       
424       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeBoxDXDYDZ( x, y, z );
425       res = true;
426       break;
427     }
428   }
429   
430   if ( !anObj->_is_nil() )
431   objects.push_back( anObj._retn() );
432   
433   return res;
434 }
435
436 //=================================================================================
437 // function : addSubshapeToStudy
438 // purpose  : virtual method to add new SubObjects if local selection
439 //=================================================================================
440 void PrimitiveGUI_BoxDlg::addSubshapesToStudy()
441 {
442   QMap<QString, GEOM::GEOM_Object_var> objMap;
443   if ( getConstructorId() == 0 ) {
444     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
445     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
446     addSubshapesToFather( objMap );
447   }
448 }