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