Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : PrimitiveGUI_TorusDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "PrimitiveGUI_TorusDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 // OCCT Includes
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45
46 #include <GEOMImpl_Types.hxx>
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                                               bool modal, Qt::WindowFlags fl)
57   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
58     myInitial(true)
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   setWindowTitle(tr("GEOM_TORUS_TITLE"));
66
67   /***************************************************************/
68   mainFrame()->GroupConstructors->setTitle(tr("GEOM_TORUS"));
69   mainFrame()->RadioButton1->setIcon(image0);
70   mainFrame()->RadioButton2->setIcon(image1);
71   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
72   mainFrame()->RadioButton3->close();
73
74   GroupPoints = new DlgRef_2Sel2Spin(centralWidget());
75   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
76   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
77   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
78   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg(1));
79   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg(2));
80   GroupPoints->PushButton1->setIcon(image2);
81   GroupPoints->PushButton2->setIcon(image2);
82
83   GroupDimensions = new DlgRef_2Spin(centralWidget());
84   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
85   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg(1));
86   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg(2));
87
88   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
89   layout->setMargin(0); layout->setSpacing(6);
90   layout->addWidget(GroupPoints);
91   layout->addWidget(GroupDimensions);
92   /***************************************************************/
93
94   setHelpFileName("create_torus_page.html");
95
96   Init();
97 }
98
99 //=================================================================================
100 // function : ~PrimitiveGUI_TorusDlg()
101 // purpose  : Destroys the object and frees any allocated resources
102 //=================================================================================
103 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
104 {
105   // no need to delete child widgets, Qt does it all for us
106 }
107
108 //=================================================================================
109 // function : Init()
110 // purpose  :
111 //=================================================================================
112 void PrimitiveGUI_TorusDlg::Init()
113 {
114   // Get setting of step value from file configuration
115   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
116   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
117
118   // min, max, step and decimals for spin boxes & initial values
119   initSpinBox(GroupPoints->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
120   initSpinBox(GroupPoints->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
121   initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
122   initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
123
124   // init variables
125   GroupPoints->LineEdit1->setReadOnly(true);
126   GroupPoints->LineEdit2->setReadOnly(true);
127
128   GroupPoints->LineEdit1->setText("");
129   GroupPoints->LineEdit2->setText("");
130   myPoint = myDir = GEOM::GEOM_Object::_nil();
131
132   GroupPoints->SpinBox_DX->setValue(300.0);
133   GroupPoints->SpinBox_DY->setValue(100.0);
134   GroupDimensions->SpinBox_DX->setValue(300.0);
135   GroupDimensions->SpinBox_DY->setValue(100.0);
136
137   // signals and slots connections
138   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
139   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
140
141   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
142
143   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
144   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
145
146   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
148
149   connect(GroupPoints->SpinBox_DX,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
150   connect(GroupPoints->SpinBox_DY,     SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
151   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
152   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
153
154   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
155
156   initName(tr("GEOM_TORUS"));
157
158   setConstructorId(1); // simplest constructor
159   ConstructorsClicked(1);
160 }
161
162 //=================================================================================
163 // function : SetDoubleSpinBoxStep()
164 // purpose  : Double spin box management
165 //=================================================================================
166 void PrimitiveGUI_TorusDlg::SetDoubleSpinBoxStep (double step)
167 {
168   GroupPoints->SpinBox_DX->setSingleStep(step);
169   GroupPoints->SpinBox_DY->setSingleStep(step);
170   GroupDimensions->SpinBox_DX->setSingleStep(step);
171   GroupDimensions->SpinBox_DY->setSingleStep(step);
172 }
173
174 //=================================================================================
175 // function : ConstructorsClicked()
176 // purpose  : Radio button management
177 //=================================================================================
178 void PrimitiveGUI_TorusDlg::ConstructorsClicked (int constructorId)
179 {
180   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
181
182   switch (constructorId) {
183   case 0:
184     {
185       GroupDimensions->hide();
186       GroupPoints->show();
187
188       GroupPoints->PushButton1->click();
189       break;
190     }
191   case 1:
192     {
193       GroupPoints->hide();
194       GroupDimensions->show();
195       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
196       globalSelection(); // close local contexts, if any
197       break;
198     }
199   }
200
201   qApp->processEvents();
202   updateGeometry();
203   resize(minimumSizeHint());
204
205   if (myInitial) {
206     myInitial = false;
207     if (constructorId == 0) {
208       // on dialog initialization we init the first field with a selected object (if any)
209       SelectionIntoArgument();
210     }
211     else {
212       displayPreview();
213     }
214   }
215   else {
216     displayPreview();
217   }
218 }
219
220 //=================================================================================
221 // function : ClickOnOk()
222 // purpose  :
223 //=================================================================================
224 void PrimitiveGUI_TorusDlg::ClickOnOk()
225 {
226   if (ClickOnApply())
227     ClickOnCancel();
228 }
229
230 //=================================================================================
231 // function : ClickOnApply()
232 // purpose  :
233 //=================================================================================
234 bool PrimitiveGUI_TorusDlg::ClickOnApply()
235 {
236   if (!onAccept())
237     return false;
238
239   initName();
240   // activate selection and connect selection manager
241   ConstructorsClicked(getConstructorId());
242   return true;
243 }
244
245 //=================================================================================
246 // function : SelectionIntoArgument()
247 // purpose  : Called when selection is changed or on dialog initialization or activation
248 //=================================================================================
249 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
250 {
251   if (getConstructorId() != 0)
252     return;
253
254   erasePreview();
255   myEditCurrentArgument->setText("");
256
257   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint = GEOM::GEOM_Object::_nil();
258   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myDir   = GEOM::GEOM_Object::_nil();
259
260   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
261   SALOME_ListIO aSelList;
262   aSelMgr->selectedObjects(aSelList);
263
264   if (aSelList.Extent() != 1)
265     return;
266
267   // nbSel == 1
268   Standard_Boolean testResult = Standard_False;
269   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
270
271   if (!testResult || CORBA::is_nil(aSelectedObject))
272     return;
273
274   QString aName = GEOMBase::GetName(aSelectedObject);
275
276   // Get Selected object if selected subshape
277   TopoDS_Shape aShape;
278   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
279   {
280     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
281     if (myEditCurrentArgument == GroupPoints->LineEdit2)
282       aNeedType = TopAbs_EDGE;
283
284     TColStd_IndexedMapOfInteger aMap;
285     aSelMgr->GetIndexes(aSelList.First(), aMap);
286     if (aMap.Extent() == 1) { // Local Selection
287       int anIndex = aMap(1);
288       if (aNeedType == TopAbs_EDGE)
289         aName.append(":edge_" + QString::number(anIndex));
290       else
291         aName.append(":vertex_" + QString::number(anIndex));
292
293       //Find SubShape Object in Father
294       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
295
296       if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
297         GEOM::GEOM_IShapesOperations_var aShapesOp =
298           getGeomEngine()->GetIShapesOperations(getStudyId());
299         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
300       }
301       else {
302         aSelectedObject = aFindedObject; // get Object from study
303       }
304     }
305     else { // Global Selection
306       if (aShape.ShapeType() != aNeedType) {
307         aSelectedObject = GEOM::GEOM_Object::_nil();
308         aName = "";
309       }
310     }
311   }
312
313   myEditCurrentArgument->setText(aName);
314
315   // clear selection
316   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
317   myGeomGUI->getApp()->selectionMgr()->clearSelected();
318   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
319           this, SLOT(SelectionIntoArgument()));
320
321   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
322     myPoint = aSelectedObject;
323     if (!myPoint->_is_nil() && myDir->_is_nil())
324       GroupPoints->PushButton2->click();
325   }
326   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
327     myDir = aSelectedObject;
328     if (!myDir->_is_nil() && myPoint->_is_nil())
329       GroupPoints->PushButton1->click();
330   }
331
332   displayPreview();
333 }
334
335 //=================================================================================
336 // function : SetEditCurrentArgument()
337 // purpose  :
338 //=================================================================================
339 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
340 {
341   QPushButton* send = (QPushButton*)sender();
342
343   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
344   if (send == GroupPoints->PushButton1) {
345     myEditCurrentArgument = GroupPoints->LineEdit1;
346
347     GroupPoints->PushButton2->setDown(false);
348     GroupPoints->LineEdit2->setEnabled(false);
349
350     globalSelection(GEOM_POINT); // to break previous local selection
351     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
352   }
353   else if (send == GroupPoints->PushButton2) {
354     myEditCurrentArgument = GroupPoints->LineEdit2;
355
356     GroupPoints->PushButton1->setDown(false);
357     GroupPoints->LineEdit1->setEnabled(false);
358
359     globalSelection(GEOM_LINE);  // to break previous local selection
360     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
361   }
362   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
363           this, SLOT(SelectionIntoArgument()));
364
365   // enable line edit
366   myEditCurrentArgument->setEnabled(true);
367   myEditCurrentArgument->setFocus();
368   // after setFocus(), because it will be setDown(false) when loses focus
369   send->setDown(true);
370
371   // seems we need it only to avoid preview disappearing, caused by selection mode change
372   displayPreview();
373 }
374
375 //=================================================================================
376 // function : LineEditReturnPressed()
377 // purpose  :
378 //=================================================================================
379 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
380 {
381   QLineEdit* send = (QLineEdit*)sender();
382   if (send == GroupPoints->LineEdit1 ||
383       send == GroupPoints->LineEdit2) {
384     myEditCurrentArgument = send;
385     GEOMBase_Skeleton::LineEditReturnPressed();
386   }
387 }
388
389 //=================================================================================
390 // function : ActivateThisDialog()
391 // purpose  :
392 //=================================================================================
393 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
394 {
395   GEOMBase_Skeleton::ActivateThisDialog();
396
397   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
398            this, SLOT( SelectionIntoArgument() ) );
399   
400   ConstructorsClicked( getConstructorId() );
401 }
402
403 //=================================================================================
404 // function : enterEvent()
405 // purpose  :
406 //=================================================================================
407 void PrimitiveGUI_TorusDlg::enterEvent (QEvent*)
408 {
409   if (!mainFrame()->GroupConstructors->isEnabled())
410     ActivateThisDialog();
411 }
412
413 //=================================================================================
414 // function : ValueChangedInSpinBox()
415 // purpose  :
416 //=================================================================================
417 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
418 {
419   displayPreview();
420 }
421
422 //=================================================================================
423 // function : createOperation
424 // purpose  :
425 //=================================================================================
426 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
427 {
428   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
429 }
430
431 //=================================================================================
432 // function : isValid
433 // purpose  :
434 //=================================================================================
435 bool PrimitiveGUI_TorusDlg::isValid (QString& msg)
436 {
437   bool ok = true;
438   if( getConstructorId() == 0 )
439   {
440     ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
441     ok = GroupPoints->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
442   }
443   else if( getConstructorId() == 1 )
444   {
445     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
446     ok = GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
447   }
448   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) && ok : ok;
449 }
450
451 //=================================================================================
452 // function : execute
453 // purpose  :
454 //=================================================================================
455 bool PrimitiveGUI_TorusDlg::execute (ObjectList& objects)
456 {
457   bool res = false;
458
459   GEOM::GEOM_Object_var anObj;
460
461   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
462
463   switch (getConstructorId()) {
464   case 0:
465     if (!CORBA::is_nil(myPoint) && !CORBA::is_nil(myDir)) {
466       anObj = anOper->MakeTorusPntVecRR(myPoint, myDir, getRadius1(), getRadius2());
467       if (!anObj->_is_nil() && !IsPreview())
468       {
469         QStringList aParameters;
470         aParameters << GroupPoints->SpinBox_DX->text();
471         aParameters << GroupPoints->SpinBox_DY->text();
472         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
473       }
474       res = true;
475     }
476     break;
477   case 1:
478     anObj = anOper->MakeTorusRR(getRadius1(), getRadius2());
479     if (!anObj->_is_nil() && !IsPreview())
480     {
481       QStringList aParameters;
482       aParameters << GroupDimensions->SpinBox_DX->text();
483       aParameters << GroupDimensions->SpinBox_DY->text();
484       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
485     }
486     res = true;
487     break;
488   }
489
490   if (!anObj->_is_nil())
491     objects.push_back(anObj._retn());
492
493   return res;
494 }
495
496 //=================================================================================
497 // function : getRadius1()
498 // purpose  :
499 //=================================================================================
500 double PrimitiveGUI_TorusDlg::getRadius1() const
501 {
502   int aConstructorId = getConstructorId();
503   if (aConstructorId == 0)
504     return GroupPoints->SpinBox_DX->value();
505   else if (aConstructorId == 1)
506     return GroupDimensions->SpinBox_DX->value();
507   return 0;
508 }
509
510 //=================================================================================
511 // function : getRadius2()
512 // purpose  :
513 //=================================================================================
514 double PrimitiveGUI_TorusDlg::getRadius2() const
515 {
516   int aConstructorId = getConstructorId();
517   if (aConstructorId == 0)
518     return GroupPoints->SpinBox_DY->value();
519   else if (aConstructorId == 1)
520     return GroupDimensions->SpinBox_DY->value();
521   return 0;
522 }
523
524 //=================================================================================
525 // function : addSubshapeToStudy
526 // purpose  : virtual method to add new SubObjects if local selection
527 //=================================================================================
528 void PrimitiveGUI_TorusDlg::addSubshapesToStudy()
529 {
530   QMap<QString, GEOM::GEOM_Object_var> objMap;
531
532   switch (getConstructorId()) {
533   case 0:
534     objMap[GroupPoints->LineEdit1->text()] = myPoint;
535     objMap[GroupPoints->LineEdit2->text()] = myDir;
536     break;
537   case 1:
538     return;
539   }
540   addSubshapesToFather(objMap);
541 }