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