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