]> SALOME platform Git repositories - modules/geom.git/blob - src/PrimitiveGUI/PrimitiveGUI_TorusDlg.cxx
Salome HOME
Bug 0020413: Dump file has many GetMainShape instructions.
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.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_TorusDlg.cxx
24 //  Author : Lucien PIGNOLONI
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "PrimitiveGUI_TorusDlg.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_Edge.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 //=================================================================================
49 // class    : PrimitiveGUI_TorusDlg()
50 // purpose  : Constructs a PrimitiveGUI_TorusDlg which is a child of 'parent', with the
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            TRUE to construct a modal dialog.
54 //=================================================================================
55 PrimitiveGUI_TorusDlg::PrimitiveGUI_TorusDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
56                                              const char* name, bool modal, WFlags fl)
57   : GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
58                       WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
59 {
60   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
61   QPixmap image0(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_PV")));
62   QPixmap image1(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TORUS_DXYZ")));
63   QPixmap image2(aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
64
65   setCaption(tr("GEOM_TORUS_TITLE"));
66
67   /***************************************************************/
68   GroupConstructors->setTitle(tr("GEOM_TORUS"));
69   RadioButton1->setPixmap(image0);
70   RadioButton2->setPixmap(image1);
71   RadioButton3->close(TRUE);
72
73   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
74   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
75   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
76   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
77   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg("1"));
78   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg("2"));
79   GroupPoints->PushButton1->setPixmap(image2);
80   GroupPoints->PushButton2->setPixmap(image2);
81
82   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
83   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
84   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg("1"));
85   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg("2"));
86
87   Layout1->addWidget(GroupPoints, 2, 0);
88   Layout1->addWidget(GroupDimensions, 2, 0);
89   /***************************************************************/
90
91   setHelpFileName("create_torus_page.html");
92
93   Init();
94 }
95
96
97 //=================================================================================
98 // function : ~PrimitiveGUI_TorusDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
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_TorusDlg::Init()
112 {
113   /* init variables */
114   myEditCurrentArgument = GroupPoints->LineEdit1;
115   GroupPoints->LineEdit1->setReadOnly( true );
116   GroupPoints->LineEdit2->setReadOnly( true );
117
118   myPoint = myDir = 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 & initial values */
125   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
126   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
127   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
128   GroupDimensions->SpinBox_DY->RangeStepAndValidator(0.001, COORD_MAX, step, DBL_DIGITS_DISPLAY);
129
130   GroupPoints->SpinBox_DX->SetValue(300.0);
131   GroupPoints->SpinBox_DY->SetValue(100.0);
132   GroupDimensions->SpinBox_DX->SetValue(300.0);
133   GroupDimensions->SpinBox_DY->SetValue(100.0);
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(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
148   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
149   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
150   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
151
152   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
153           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
154   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
155           GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
156   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
157           GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
158   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
159           GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
160
161   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
162           this, SLOT(SelectionIntoArgument()));
163
164   initName( tr( "GEOM_TORUS" ) );
165   ConstructorsClicked(0);
166 }
167
168 //=================================================================================
169 // function : ConstructorsClicked()
170 // purpose  : Radio button management
171 //=================================================================================
172 void PrimitiveGUI_TorusDlg::ConstructorsClicked (int constructorId)
173 {
174   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
175
176   switch (constructorId)
177   {
178   case 0:
179     {
180       globalSelection(); // close local contexts, if any
181       localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
182
183       GroupDimensions->hide();
184       resize(0, 0);
185       GroupPoints->show();
186
187       myEditCurrentArgument = GroupPoints->LineEdit1;
188       GroupPoints->LineEdit1->setText(tr(""));
189       GroupPoints->LineEdit2->setText(tr(""));
190       myPoint = myDir = GEOM::GEOM_Object::_nil();
191
192       connect(myGeomGUI->getApp()->selectionMgr(),
193               SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
194       SelectionIntoArgument();
195
196       break;
197     }
198   case 1:
199     {
200       GroupPoints->hide();
201       globalSelection(); // close local contexts, if any
202       resize(0, 0);
203       GroupDimensions->show();
204
205       break;
206     }
207   }
208
209   displayPreview();
210 }
211
212
213 //=================================================================================
214 // function : ClickOnOk()
215 // purpose  :
216 //=================================================================================
217 void PrimitiveGUI_TorusDlg::ClickOnOk()
218 {
219   if (ClickOnApply())
220     ClickOnCancel();
221 }
222
223
224 //=================================================================================
225 // function : ClickOnApply()
226 // purpose  :
227 //=================================================================================
228 bool PrimitiveGUI_TorusDlg::ClickOnApply()
229 {
230   if (!onAccept())
231     return false;
232
233   initName();
234   return true;
235 }
236
237
238 //=================================================================================
239 // function : SelectionIntoArgument()
240 // purpose  : Called when selection as changed or other case
241 //=================================================================================
242 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
243 {
244   if (getConstructorId() != 0)
245     return;
246
247   myEditCurrentArgument->setText("");
248
249   if (IObjectCount() != 1)
250   {
251     if (myEditCurrentArgument == GroupPoints->LineEdit1)
252       myPoint = GEOM::GEOM_Object::_nil();
253     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
254       myDir = GEOM::GEOM_Object::_nil();
255     return;
256   }
257
258   /* nbSel == 1 */
259   Standard_Boolean testResult = Standard_False;
260   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult);
261
262   if (!testResult || CORBA::is_nil(aSelectedObject))
263     return;
264
265   QString aName = GEOMBase::GetName(aSelectedObject);
266   TopoDS_Shape aShape;
267   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
268   {
269     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
270     if (myEditCurrentArgument == GroupPoints->LineEdit2)
271       aNeedType = TopAbs_EDGE;
272
273     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
274     TColStd_IndexedMapOfInteger aMap;
275     aSelMgr->GetIndexes(firstIObject(), aMap);
276     if (aMap.Extent() == 1) // Local Selection
277     {
278       int anIndex = aMap(1);
279       if (aNeedType == TopAbs_EDGE)
280         aName.append(":edge_" + QString::number(anIndex));
281       else
282         aName.append(":vertex_" + QString::number(anIndex));
283
284       //Find SubShape Object in Father
285       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
286
287       if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
288         GEOM::GEOM_IShapesOperations_var aShapesOp =
289           getGeomEngine()->GetIShapesOperations( getStudyId() );
290         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
291       }
292       else
293         aSelectedObject = aFindedObject; // get Object from study
294     }
295     else // Global Selection
296     {
297       if (aShape.ShapeType() != aNeedType) {
298         aSelectedObject = GEOM::GEOM_Object::_nil();
299         aName = "";
300       }
301     }
302   }
303
304   myEditCurrentArgument->setText(aName);
305
306   if (myEditCurrentArgument == GroupPoints->LineEdit1)
307     myPoint = aSelectedObject;
308   else if (myEditCurrentArgument == GroupPoints->LineEdit2)
309     myDir = aSelectedObject;
310
311   displayPreview();
312 }
313
314 //=================================================================================
315 // function : LineEditReturnPressed()
316 // purpose  :
317 //=================================================================================
318 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
319 {
320   QLineEdit* send = (QLineEdit*)sender();
321   if (send == GroupPoints->LineEdit1 ||
322       send == GroupPoints->LineEdit2)
323   {
324     myEditCurrentArgument = send;
325     GEOMBase_Skeleton::LineEditReturnPressed();
326   }
327 }
328
329
330 //=================================================================================
331 // function : SetEditCurrentArgument()
332 // purpose  :
333 //=================================================================================
334 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
335 {
336   QPushButton* send = (QPushButton*)sender();
337   globalSelection( GEOM_POINT ); // to break previous local selection
338
339   if (send == GroupPoints->PushButton1) {
340     myEditCurrentArgument = GroupPoints->LineEdit1;
341     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
342   }
343   else if (send == GroupPoints->PushButton2) {
344     myEditCurrentArgument = GroupPoints->LineEdit2;
345     GEOM::GEOM_Object_var anObj;
346     localSelection( anObj, TopAbs_EDGE );
347   }
348
349   myEditCurrentArgument->setFocus();
350   SelectionIntoArgument();
351 }
352
353
354 //=================================================================================
355 // function : ActivateThisDialog()
356 // purpose  :
357 //=================================================================================
358 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
359 {
360   GEOMBase_Skeleton::ActivateThisDialog();
361
362   connect(myGeomGUI->getApp()->selectionMgr(),
363           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
364
365   ConstructorsClicked( getConstructorId() );
366 }
367
368
369 //=================================================================================
370 // function : enterEvent()
371 // purpose  :
372 //=================================================================================
373 void PrimitiveGUI_TorusDlg::enterEvent(QEvent* e)
374 {
375   if ( !GroupConstructors->isEnabled() )
376     ActivateThisDialog();
377 }
378
379
380 //=================================================================================
381 // function : ValueChangedInSpinBox
382 // purpose  :
383 //=================================================================================
384 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
385 {
386   displayPreview();
387 }
388
389
390 //=================================================================================
391 // function : createOperation
392 // purpose  :
393 //=================================================================================
394 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
395 {
396   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
397 }
398
399 //=================================================================================
400 // function : isValid
401 // purpose  :
402 //=================================================================================
403 bool PrimitiveGUI_TorusDlg::isValid( QString& msg )
404 {
405   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
406 }
407
408 //=================================================================================
409 // function : execute
410 // purpose  :
411 //=================================================================================
412 bool PrimitiveGUI_TorusDlg::execute( ObjectList& objects )
413 {
414   bool res = false;
415
416   GEOM::GEOM_Object_var anObj;
417
418   switch ( getConstructorId() )
419   {
420   case 0:
421     {
422       if (!CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir ))
423       {
424         anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->
425           MakeTorusPntVecRR(myPoint, myDir, getRadius1(), getRadius2());
426         res = true;
427       }
428       break;
429     }
430   case 1:
431     {
432       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->
433         MakeTorusRR(getRadius1(), getRadius2());
434       res = true;
435       break;
436     }
437   }
438
439   if ( !anObj->_is_nil() )
440     objects.push_back( anObj._retn() );
441
442   return res;
443 }
444
445 //=================================================================================
446 // function : getRadius1()
447 // purpose  :
448 //=================================================================================
449 double PrimitiveGUI_TorusDlg::getRadius1() const
450 {
451   int aConstructorId = getConstructorId();
452   if (aConstructorId == 0)
453     return GroupPoints->SpinBox_DX->GetValue();
454   else if (aConstructorId == 1)
455     return GroupDimensions->SpinBox_DX->GetValue();
456   return 0;
457 }
458
459 //=================================================================================
460 // function : getRadius2()
461 // purpose  :
462 //=================================================================================
463 double PrimitiveGUI_TorusDlg::getRadius2() const
464 {
465   int aConstructorId = getConstructorId();
466   if (aConstructorId == 0)
467     return GroupPoints->SpinBox_DY->GetValue();
468   else if (aConstructorId == 1)
469     return GroupDimensions->SpinBox_DY->GetValue();
470   return 0;
471 }
472
473 //=================================================================================
474 // function : addSubshapeToStudy
475 // purpose  : virtual method to add new SubObjects if local selection
476 //=================================================================================
477 void PrimitiveGUI_TorusDlg::addSubshapesToStudy()
478 {
479   QMap<QString, GEOM::GEOM_Object_var> objMap;
480
481 switch (getConstructorId())
482   {
483   case 0:
484     objMap[GroupPoints->LineEdit1->text()] = myPoint;
485     objMap[GroupPoints->LineEdit2->text()] = myDir;
486     break;
487   case 1:
488     return;
489   }
490  addSubshapesToFather( objMap );
491 }