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