Salome HOME
d0aab81089b82dfb28914ba9ea9e6d9196baf19d
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_BoxDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
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 {
58   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_2P")));
59   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_DXYZ")));
60   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
61
62   setWindowTitle(tr("GEOM_BOX_TITLE"));
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle(tr("GEOM_BOX"));
66   mainFrame()->RadioButton1->setIcon(image0);
67   mainFrame()->RadioButton2->setIcon(image1);
68   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
69   mainFrame()->RadioButton3->close();
70
71   GroupPoints = new DlgRef_2Sel(centralWidget());
72   GroupPoints->GroupBox1->setTitle(tr("GEOM_DIAGONAL_POINTS"));
73   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg(1));
74   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(2));
75   GroupPoints->PushButton1->setIcon(image2);
76   GroupPoints->PushButton2->setIcon(image2);
77
78   GroupDimensions = new DlgRef_3Spin(centralWidget());
79   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
80   GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
81   GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
82   GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
83
84   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
85   layout->setMargin(0); layout->setSpacing(6);
86   layout->addWidget(GroupPoints);
87   layout->addWidget(GroupDimensions);
88   /***************************************************************/
89
90   setHelpFileName("create_box_page.html");
91
92   Init();
93 }
94
95 //=================================================================================
96 // function : ~DialogBox_Box()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 PrimitiveGUI_BoxDlg::~PrimitiveGUI_BoxDlg()
100 {
101   // no need to delete child widgets, Qt does it all for us
102 }
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void PrimitiveGUI_BoxDlg::Init()
109 {
110   // Get setting of step value from file configuration
111   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
112   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
113
114   // min, max, step and decimals for spin boxes
115   initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
116   initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
117   initSpinBox(GroupDimensions->SpinBox_DZ, 0.000001, COORD_MAX, step, "length_precision" );
118
119   // init variables
120   GroupPoints->LineEdit1->setReadOnly(true);
121   GroupPoints->LineEdit2->setReadOnly(true);
122
123   GroupPoints->LineEdit1->setText("");
124   GroupPoints->LineEdit2->setText("");
125   myPoint1.nullify();
126   myPoint2.nullify();
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(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
143   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
144   connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
145
146   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
147
148   initName(tr("GEOM_BOX"));
149
150   setConstructorId(1); // simplest constructor
151   ConstructorsClicked(1);
152 }
153
154 //=================================================================================
155 // function : SetDoubleSpinBoxStep()
156 // purpose  : Double spin box management
157 //=================================================================================
158 void PrimitiveGUI_BoxDlg::SetDoubleSpinBoxStep (double step)
159 {
160   GroupDimensions->SpinBox_DX->setSingleStep(step);
161   GroupDimensions->SpinBox_DY->setSingleStep(step);
162   GroupDimensions->SpinBox_DZ->setSingleStep(step);
163 }
164
165 //=================================================================================
166 // function : ConstructorsClicked()
167 // purpose  : Radio button management
168 //=================================================================================
169 void PrimitiveGUI_BoxDlg::ConstructorsClicked (int constructorId)
170 {
171   switch (constructorId) {
172   case 0:
173     {
174       GroupDimensions->hide();
175       GroupPoints->show();
176
177       GroupPoints->PushButton1->click();
178       break;
179     }
180   case 1:
181     {
182       GroupPoints->hide();
183       GroupDimensions->show();
184       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
185       globalSelection(); // close local contexts, if any
186
187       displayPreview(true);
188       break;
189     }
190   }
191
192   qApp->processEvents();
193   updateGeometry();
194   resize(minimumSizeHint());
195   SelectionIntoArgument();
196
197   displayPreview(true);
198 }
199
200 //=================================================================================
201 // function : ClickOnOk()
202 // purpose  :
203 //=================================================================================
204 void PrimitiveGUI_BoxDlg::ClickOnOk()
205 {
206   setIsApplyAndClose( true );
207   if (ClickOnApply())
208     ClickOnCancel();
209 }
210
211 //=================================================================================
212 // function : ClickOnApply()
213 // purpose  :
214 //=================================================================================
215 bool PrimitiveGUI_BoxDlg::ClickOnApply()
216 {
217   if (!onAccept())
218     return false;
219
220   initName();
221   // activate selection and connect selection manager
222   ConstructorsClicked(getConstructorId());
223   return true;
224 }
225
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose  : Called when selection is changed or on dialog initialization or activation
229 //=================================================================================
230 void PrimitiveGUI_BoxDlg::SelectionIntoArgument()
231 {
232   if (getConstructorId() != 0)
233     return;
234
235   erasePreview();
236   myEditCurrentArgument->setText("");
237
238   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
239   SALOME_ListIO aSelList;
240   aSelMgr->selectedObjects(aSelList);
241
242   if (aSelList.Extent() != 1) {
243     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1.nullify();
244     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2.nullify();
245     return;
246   }
247
248   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_VERTEX );
249   TopoDS_Shape aShape;
250   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
251     QString aName = GEOMBase::GetName( aSelectedObject.get() );
252
253     myEditCurrentArgument->setText(aName);
254  
255     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
256       myPoint1 = aSelectedObject;
257       if (myPoint1 && !myPoint2)
258         GroupPoints->PushButton2->click();
259     }
260     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
261       myPoint2 = aSelectedObject;
262       if (myPoint2 && !myPoint1)
263         GroupPoints->PushButton1->click();
264     }
265     if ((myPoint1 && !myPoint2) || (myPoint2 && !myPoint1)) {
266       // clear selection
267       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
268       myGeomGUI->getApp()->selectionMgr()->clearSelected();
269       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
270         this, SLOT(SelectionIntoArgument()));
271     }
272   }
273   displayPreview(true);
274 }
275
276 //=================================================================================
277 // function : SetEditCurrentArgument()
278 // purpose  :
279 //=================================================================================
280 void PrimitiveGUI_BoxDlg::SetEditCurrentArgument()
281 {
282   QPushButton* send = (QPushButton*)sender();
283
284   // ?? Commented, because we need this flag in ConstructorsClicked, because
285   // SelectionIntoArgument must be called only on dialog creation, and must not be called on
286   // simple switch between constructors (as we need to keep old values in fields in this case)
287
288   // clear selection
289   //disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
290
291   if (send == GroupPoints->PushButton1) {
292     myEditCurrentArgument = GroupPoints->LineEdit1;
293     GroupPoints->PushButton2->setDown(false);
294     GroupPoints->LineEdit2->setEnabled(false);
295   }
296   else if (send == GroupPoints->PushButton2) {
297     myEditCurrentArgument = GroupPoints->LineEdit2;
298     GroupPoints->PushButton1->setDown(false);
299     GroupPoints->LineEdit1->setEnabled(false);
300   }
301
302   // enable line edit
303   myEditCurrentArgument->setEnabled(true);
304   myEditCurrentArgument->setFocus();
305   // after setFocus(), because it will be setDown(false) when loses focus
306   send->setDown(true);
307
308   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
309   globalSelection(); // close local contexts, if any
310   localSelection(TopAbs_VERTEX);
311   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
312           this, SLOT(SelectionIntoArgument()));
313
314   // seems we need it only to avoid preview disappearing, caused by selection mode change
315   displayPreview(true);
316 }
317
318 //=================================================================================
319 // function : ActivateThisDialog()
320 // purpose  :
321 //=================================================================================
322 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
323 {
324   GEOMBase_Skeleton::ActivateThisDialog();
325   if (getConstructorId() == 0) {
326     localSelection(TopAbs_VERTEX);
327     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
328             this, SLOT(SelectionIntoArgument()));
329   }
330   displayPreview(true);
331 }
332
333 //=================================================================================
334 // function : enterEvent [REDEFINED]
335 // purpose  :
336 //=================================================================================
337 void PrimitiveGUI_BoxDlg::enterEvent (QEvent*)
338 {
339   if (!mainFrame()->GroupConstructors->isEnabled())
340     ActivateThisDialog();
341 }
342
343 //=================================================================================
344 // function : ValueChangedInSpinBox()
345 // purpose  :
346 //=================================================================================
347 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
348 {
349   displayPreview(true);
350 }
351
352 //=================================================================================
353 // function : createOperation
354 // purpose  :
355 //=================================================================================
356 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
357 {
358   return getGeomEngine()->GetI3DPrimOperations();
359 }
360
361 //=================================================================================
362 // function : isValid
363 // purpose  :
364 //=================================================================================
365 bool PrimitiveGUI_BoxDlg::isValid (QString& msg)
366 {
367   bool ok = false;
368   if( getConstructorId() == 1 )
369   {
370     ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) &&
371          GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) &&
372          GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() ) &&
373          qAbs( GroupDimensions->SpinBox_DX->value() ) > Precision::Confusion() &&
374          qAbs( GroupDimensions->SpinBox_DY->value() ) > Precision::Confusion() &&
375          qAbs( GroupDimensions->SpinBox_DZ->value() ) > Precision::Confusion();
376   }
377   else
378   {
379     ok = myPoint1 && myPoint2 && myPoint1 != myPoint2;
380   }
381   return ok;
382 }
383
384 //=================================================================================
385 // function : execute
386 // purpose  :
387 //=================================================================================
388 bool PrimitiveGUI_BoxDlg::execute (ObjectList& objects)
389 {
390   bool res = false;
391
392   GEOM::GEOM_Object_var anObj;
393
394   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
395   
396   switch (getConstructorId()) {
397   case 0:
398     {
399       if ( myPoint1 && myPoint2 ) {
400         anObj = anOper->MakeBoxTwoPnt(myPoint1.get(), myPoint2.get());
401         res = true;
402       }
403     }
404     break;
405   case 1:
406     {
407       double x = GroupDimensions->SpinBox_DX->value();
408       double y = GroupDimensions->SpinBox_DY->value();
409       double z = GroupDimensions->SpinBox_DZ->value();
410
411       anObj = anOper->MakeBoxDXDYDZ(x, y, z);
412       if (!anObj->_is_nil() && !IsPreview())
413       {
414         QStringList aParameters;
415         aParameters << GroupDimensions->SpinBox_DX->text();
416         aParameters << GroupDimensions->SpinBox_DY->text();
417         aParameters << GroupDimensions->SpinBox_DZ->text();
418         anObj->SetParameters(aParameters.join(":").toUtf8().constData());
419       }
420       res = true;
421     }
422     break;
423   }
424
425   if (!anObj->_is_nil())
426     objects.push_back(anObj._retn());
427
428   return res;
429 }
430
431 //=================================================================================
432 // function : addSubshapesToStudy
433 // purpose  : virtual method to add new SubObjects if local selection
434 //=================================================================================
435 void PrimitiveGUI_BoxDlg::addSubshapesToStudy()
436 {
437   if (getConstructorId() == 0) {
438     GEOMBase::PublishSubObject( myPoint1.get() );
439     GEOMBase::PublishSubObject( myPoint2.get() );
440   }
441 }
442
443 //=================================================================================
444 // function : getSourceObjects
445 // purpose  : virtual method to get source objects
446 //=================================================================================
447 QList<GEOM::GeomObjPtr> PrimitiveGUI_BoxDlg::getSourceObjects()
448 {
449   QList<GEOM::GeomObjPtr> res;
450   res << myPoint1 << myPoint2;
451   return res;
452 }