Salome HOME
Dump Python extension
[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_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 // OCCT Includes
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS.hxx>
40 #include <TopExp.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43
44 #include <GEOMImpl_Types.hxx>
45 //#include <ostream>
46
47 //=================================================================================
48 // class    : PrimitiveGUI_BoxDlg()
49 // purpose  : Constructs a PrimitiveGUI_BoxDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 PrimitiveGUI_BoxDlg::PrimitiveGUI_BoxDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                           bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
57     myInitial(true)
58 {
59   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_2P")));
60   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_DXYZ")));
61   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
62
63   setWindowTitle(tr("GEOM_BOX_TITLE"));
64
65   /***************************************************************/
66   mainFrame()->GroupConstructors->setTitle(tr("GEOM_BOX"));
67   mainFrame()->RadioButton1->setIcon(image0);
68   mainFrame()->RadioButton2->setIcon(image1);
69   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
70   mainFrame()->RadioButton3->close();
71
72   GroupPoints = new DlgRef_2Sel(centralWidget());
73   GroupPoints->GroupBox1->setTitle(tr("GEOM_DIAGONAL_POINTS"));
74   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg(1));
75   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(2));
76   GroupPoints->PushButton1->setIcon(image2);
77   GroupPoints->PushButton2->setIcon(image2);
78
79   GroupDimensions = new DlgRef_3Spin(centralWidget());
80   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
81   GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
82   GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
83   GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
84
85   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
86   layout->setMargin(0); layout->setSpacing(6);
87   layout->addWidget(GroupPoints);
88   layout->addWidget(GroupDimensions);
89   /***************************************************************/
90
91   setHelpFileName("create_box_page.html");
92
93   Init();
94 }
95
96 //=================================================================================
97 // function : ~DialogBox_Box()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 PrimitiveGUI_BoxDlg::~PrimitiveGUI_BoxDlg()
101 {
102   // no need to delete child widgets, Qt does it all for us
103 }
104
105 //=================================================================================
106 // function : Init()
107 // purpose  :
108 //=================================================================================
109 void PrimitiveGUI_BoxDlg::Init()
110 {
111   // Get setting of step value from file configuration
112   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
113   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
114
115   // min, max, step and decimals for spin boxes
116   initSpinBox(GroupDimensions->SpinBox_DX, COORD_MIN, COORD_MAX, step, 3); // VSR: TODO: DBL_DIGITS_DISPLAY
117   initSpinBox(GroupDimensions->SpinBox_DY, COORD_MIN, COORD_MAX, step, 3); // VSR: TODO: DBL_DIGITS_DISPLAY
118   initSpinBox(GroupDimensions->SpinBox_DZ, COORD_MIN, COORD_MAX, step, 3); // VSR: TODO: DBL_DIGITS_DISPLAY
119
120   // init variables
121   GroupPoints->LineEdit1->setReadOnly(true);
122   GroupPoints->LineEdit2->setReadOnly(true);
123
124   GroupPoints->LineEdit1->setText("");
125   GroupPoints->LineEdit2->setText("");
126   myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
127
128   double initValue = 200.0;
129   GroupDimensions->SpinBox_DX->setValue(initValue);
130   GroupDimensions->SpinBox_DY->setValue(initValue);
131   GroupDimensions->SpinBox_DZ->setValue(initValue);
132
133   // signals and slots connections
134   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
135   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
136
137   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
138
139   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141
142   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
143   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144
145   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
146   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
147   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
148
149   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
150
151   initName(tr("GEOM_BOX"));
152
153   setConstructorId(1); // simplest constructor
154   ConstructorsClicked(1);
155 }
156
157 //=================================================================================
158 // function : SetDoubleSpinBoxStep()
159 // purpose  : Double spin box management
160 //=================================================================================
161 void PrimitiveGUI_BoxDlg::SetDoubleSpinBoxStep (double step)
162 {
163   GroupDimensions->SpinBox_DX->setSingleStep(step);
164   GroupDimensions->SpinBox_DY->setSingleStep(step);
165   GroupDimensions->SpinBox_DZ->setSingleStep(step);
166 }
167
168 //=================================================================================
169 // function : ConstructorsClicked()
170 // purpose  : Radio button management
171 //=================================================================================
172 void PrimitiveGUI_BoxDlg::ConstructorsClicked (int constructorId)
173 {
174   switch (constructorId) {
175   case 0:
176     {
177       GroupDimensions->hide();
178       GroupPoints->show();
179
180       GroupPoints->PushButton1->click();
181       break;
182     }
183   case 1:
184     {
185       GroupPoints->hide();
186       GroupDimensions->show();
187       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
188       globalSelection(); // close local contexts, if any
189
190       displayPreview();
191       break;
192     }
193   }
194
195   qApp->processEvents();
196   updateGeometry();
197   resize(minimumSize());
198
199   if (myInitial) {
200     // on dialog initialization we init the first field with a selected object (if any)
201     SelectionIntoArgument();
202     myInitial = false;
203   }
204   else {
205     displayPreview();
206   }
207 }
208
209 //=================================================================================
210 // function : ClickOnOk()
211 // purpose  :
212 //=================================================================================
213 void PrimitiveGUI_BoxDlg::ClickOnOk()
214 {
215   if (ClickOnApply())
216     ClickOnCancel();
217 }
218
219 //=================================================================================
220 // function : ClickOnApply()
221 // purpose  :
222 //=================================================================================
223 bool PrimitiveGUI_BoxDlg::ClickOnApply()
224 {
225   if (!onAccept())
226     return false;
227
228   initName();
229   // activate selection and connect selection manager
230   ConstructorsClicked(getConstructorId());
231   return true;
232 }
233
234 //=================================================================================
235 // function : SelectionIntoArgument()
236 // purpose  : Called when selection is changed or on dialog initialization or activation
237 //=================================================================================
238 void PrimitiveGUI_BoxDlg::SelectionIntoArgument()
239 {
240   if (getConstructorId() != 0)
241     return;
242
243   erasePreview();
244   myEditCurrentArgument->setText("");
245
246   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
247   SALOME_ListIO aSelList;
248   aSelMgr->selectedObjects(aSelList);
249
250   if (aSelList.Extent() != 1) {
251     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1 = GEOM::GEOM_Object::_nil();
252     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2 = GEOM::GEOM_Object::_nil();
253     return;
254   }
255
256   // nbSel == 1
257   Standard_Boolean testResult = Standard_False;
258   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
259
260   if (!testResult || CORBA::is_nil(aSelectedObject))
261     return;
262
263   QString aName = GEOMBase::GetName(aSelectedObject);
264
265   // Get Selected object if selected subshape
266   TopoDS_Shape aShape;
267   if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
268   {
269     TColStd_IndexedMapOfInteger aMap;
270     aSelMgr->GetIndexes(aSelList.First(), aMap);
271     if (aMap.Extent() == 1) // Local Selection
272     {
273       int anIndex = aMap(1);
274       aName.append(":vertex_" + QString::number(anIndex));        
275
276       //Find SubShape Object in Father
277       GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
278
279       if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
280         GEOM::GEOM_IShapesOperations_var aShapesOp =
281           getGeomEngine()->GetIShapesOperations(getStudyId());
282         aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
283       }
284       else {
285         aSelectedObject = aFindedObject; // get Object from study        
286       }
287     }
288     else // Global Selection
289     {
290       if (aShape.ShapeType() != TopAbs_VERTEX) {
291         aSelectedObject = GEOM::GEOM_Object::_nil();
292         aName = "";
293       }
294     }
295   }
296
297   myEditCurrentArgument->setText(aName);
298
299   // clear selection
300   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
301   myGeomGUI->getApp()->selectionMgr()->clearSelected();
302   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
303           this, SLOT(SelectionIntoArgument()));
304
305   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
306     myPoint1 = aSelectedObject;
307     if (!myPoint1->_is_nil() && myPoint2->_is_nil())
308       GroupPoints->PushButton2->click();
309   }
310   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
311     myPoint2 = aSelectedObject;
312     if (!myPoint2->_is_nil() && myPoint1->_is_nil())
313       GroupPoints->PushButton1->click();
314   }
315
316   displayPreview();
317 }
318
319 //=================================================================================
320 // function : SetEditCurrentArgument()
321 // purpose  :
322 //=================================================================================
323 void PrimitiveGUI_BoxDlg::SetEditCurrentArgument()
324 {
325   QPushButton* send = (QPushButton*)sender();
326
327   // ?? Commented, because we need this flag in ConstructorsClicked, because
328   // SelectionIntoArgument must be called only on dialog creation, and must not be called on
329   // simple switch between constructors (as we need to keep old values in fields in this case)
330
331   // clear selection
332   //disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
333   //if (myInitial)
334   //  myInitial = false;
335   //else
336   //  myGeomGUI->getApp()->selectionMgr()->clearSelected();
337
338   if (send == GroupPoints->PushButton1) {
339     myEditCurrentArgument = GroupPoints->LineEdit1;
340     GroupPoints->PushButton2->setDown(false);
341     GroupPoints->LineEdit2->setEnabled(false);
342   }
343   else if (send == GroupPoints->PushButton2) {
344     myEditCurrentArgument = GroupPoints->LineEdit2;
345     GroupPoints->PushButton1->setDown(false);
346     GroupPoints->LineEdit1->setEnabled(false);
347   }
348
349   // enable line edit
350   myEditCurrentArgument->setEnabled(true);
351   myEditCurrentArgument->setFocus();
352   // after setFocus(), because it will be setDown(false) when loses focus
353   send->setDown(true);
354
355   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
356   globalSelection(); // close local contexts, if any
357   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
358   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
359           this, SLOT(SelectionIntoArgument()));
360
361   // seems we need it only to avoid preview disappearing, caused by selection mode change
362   displayPreview();
363 }
364
365 //=================================================================================
366 // function : LineEditReturnPressed()
367 // purpose  :
368 //=================================================================================
369 void PrimitiveGUI_BoxDlg::LineEditReturnPressed()
370 {
371   QLineEdit* send = (QLineEdit*)sender();
372   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
373     myEditCurrentArgument = send;
374     GEOMBase_Skeleton::LineEditReturnPressed();
375   }
376 }
377
378 //=================================================================================
379 // function : ActivateThisDialog()
380 // purpose  :
381 //=================================================================================
382 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
383 {
384   GEOMBase_Skeleton::ActivateThisDialog();
385   //if (getConstructorId() == 0) {
386   //  localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
387   //  connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
388   //          this, SLOT(SelectionIntoArgument()));
389   //}
390   //displayPreview();
391
392   // reinit, because some selected objects could be removed
393   Init();
394 }
395
396 //=================================================================================
397 // function : enterEvent [REDEFINED]
398 // purpose  :
399 //=================================================================================
400 void PrimitiveGUI_BoxDlg::enterEvent (QEvent*)
401 {
402   if (!mainFrame()->GroupConstructors->isEnabled())
403     ActivateThisDialog();
404 }
405
406 //=================================================================================
407 // function : ValueChangedInSpinBox()
408 // purpose  :
409 //=================================================================================
410 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
411 {
412   displayPreview();
413 }
414
415 //=================================================================================
416 // function : createOperation
417 // purpose  :
418 //=================================================================================
419 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
420 {
421   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
422 }
423
424 //=================================================================================
425 // function : isValid
426 // purpose  :
427 //=================================================================================
428 bool PrimitiveGUI_BoxDlg::isValid (QString& msg)
429 {
430   bool ok = true;
431   if( getConstructorId() == 1 )
432   {
433     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
434     ok = GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
435     ok = GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() ) && ok;
436   }
437   return getConstructorId() == 0 ? !(myPoint1->_is_nil() || myPoint2->_is_nil()) : ok;
438 }
439
440 //=================================================================================
441 // function : execute
442 // purpose  :
443 //=================================================================================
444 bool PrimitiveGUI_BoxDlg::execute (ObjectList& objects)
445 {
446   bool res = false;
447
448   GEOM::GEOM_Object_var anObj;
449
450   switch (getConstructorId()) {
451   case 0:
452     {
453       if (!CORBA::is_nil(myPoint1) && !CORBA::is_nil(myPoint2)) {
454         anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->MakeBoxTwoPnt(myPoint1, myPoint2);
455         res = true;
456       }
457     }
458     break;
459   case 1:
460     {
461       double x = GroupDimensions->SpinBox_DX->value();
462       double y = GroupDimensions->SpinBox_DY->value();
463       double z = GroupDimensions->SpinBox_DZ->value();
464
465       QStringList aParameters;
466       aParameters << GroupDimensions->SpinBox_DX->text();
467       aParameters << GroupDimensions->SpinBox_DY->text();
468       aParameters << GroupDimensions->SpinBox_DZ->text();
469       getOperation()->SetParameters( aParameters.join( ":" ).toLatin1().constData() );
470
471       anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->MakeBoxDXDYDZ(x, y, z);
472       res = true;
473     }
474     break;
475   }
476
477   if (!anObj->_is_nil())
478     objects.push_back(anObj._retn());
479
480   return res;
481 }
482
483 //=================================================================================
484 // function : addSubshapesToStudy
485 // purpose  : virtual method to add new SubObjects if local selection
486 //=================================================================================
487 void PrimitiveGUI_BoxDlg::addSubshapesToStudy()
488 {
489   if (getConstructorId() == 0) {
490     QMap<QString, GEOM::GEOM_Object_var> objMap;
491     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
492     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
493     addSubshapesToFather(objMap);
494   }
495 }