Salome HOME
12a538083d1cab609b63a240adc741d4672dbe46
[modules/geom.git] / src / AdvancedGUI / AdvancedGUI_DividedDiskDlg.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 #include "AdvancedGUI_DividedDiskDlg.h"
24
25 #include <DlgRef.h>
26 #include <GeometryGUI.h>
27 #include <GEOMBase.h>
28
29 #include <SUIT_Session.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <SalomeApp_Application.h>
32 #include <LightApp_SelectionMgr.h>
33
34 // OCCT Includes
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_IndexedMapOfInteger.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include <GEOMImpl_Types.hxx>
42
43 // enum
44 // {
45 //  SQUARE,
46 //  HEXAGON
47 // };
48
49 //=================================================================================
50 // Constructor
51 //=================================================================================
52 AdvancedGUI_DividedDiskDlg::AdvancedGUI_DividedDiskDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
53   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
54 {
55 //   QPixmap imageOp  (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_DIVIDEDDISK_R_RATIO")));
56   QPixmap imageOp1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_DISK_R")));
57   QPixmap imageOp2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_DISK_PNT_VEC_R")));
58   QPixmap imageSel (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
59
60   setWindowTitle(tr("GEOM_DIVIDEDDISK_TITLE"));
61
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle(tr("GEOM_DIVIDEDDISK"));
64   mainFrame()->RadioButton1->setIcon(imageOp1);
65   mainFrame()->RadioButton2->setIcon(imageOp2);
66 //   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
67 //   mainFrame()->RadioButton2->close();
68   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
69   mainFrame()->RadioButton3->close();
70
71   GroupParams      = new DlgRef_1Spin(centralWidget());
72   GroupParams->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
73   GroupParams->TextLabel1->setText(tr("GEOM_RADIUS"));
74   
75   GroupOrientation = new DlgRef_3Radio(centralWidget());
76   GroupOrientation->GroupBox1->setTitle(tr("GEOM_ORIENTATION"));
77   GroupOrientation->RadioButton1->setText(tr("GEOM_WPLANE_OXY"));
78   GroupOrientation->RadioButton2->setText(tr("GEOM_WPLANE_OYZ"));
79   GroupOrientation->RadioButton3->setText(tr("GEOM_WPLANE_OZX"));
80   
81   GroupPattern = new DlgRef_3Radio(centralWidget());
82   GroupPattern->GroupBox1->setTitle(tr("GEOM_PATTERN"));
83   GroupPattern->RadioButton1->setText(tr("GEOM_SQUARE"));
84   GroupPattern->RadioButton2->setText(tr("GEOM_HEXAGON"));
85   GroupPattern->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
86   GroupPattern->RadioButton3->close();
87   
88   GroupPntVecR = new DlgRef_2Sel1Spin(centralWidget());
89   GroupPntVecR->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
90   GroupPntVecR->TextLabel1->setText(tr("GEOM_CENTER_POINT"));
91   GroupPntVecR->TextLabel2->setText(tr("GEOM_VECTOR"));
92   GroupPntVecR->TextLabel3->setText(tr("GEOM_RADIUS"));
93   GroupPntVecR->PushButton1->setIcon(imageSel);
94   GroupPntVecR->PushButton2->setIcon(imageSel);
95   GroupPntVecR->LineEdit1->setReadOnly(true);
96   GroupPntVecR->LineEdit2->setReadOnly(true);
97   //@@ setup dialog box layout here @@//
98
99   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
100   layout->setMargin(0); layout->setSpacing(6);
101   layout->addWidget(GroupParams);
102   layout->addWidget(GroupOrientation);
103   layout->addWidget(GroupPntVecR);
104   layout->addWidget(GroupPattern);
105   /***************************************************************/
106
107   setHelpFileName("create_divideddisk_page.html");
108
109   Init();
110 }
111
112 //=================================================================================
113 // Destructor
114 //=================================================================================
115 AdvancedGUI_DividedDiskDlg::~AdvancedGUI_DividedDiskDlg()
116 {
117   // no need to delete child widgets, Qt does it all for us
118 }
119
120 //=================================================================================
121 // function : Init()
122 // purpose  :
123 //=================================================================================
124 void AdvancedGUI_DividedDiskDlg::Init()
125 {
126   // Get setting of step value from file configuration
127   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
128   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
129
130   // min, max, step and decimals for spin boxes & initial values
131   initSpinBox(GroupParams->SpinBox_DX, 0.00001, COORD_MAX, step, "length_precision" );
132   initSpinBox(GroupPntVecR->SpinBox_DX, 0.00001, COORD_MAX, step, "length_precision" );
133   GroupParams ->SpinBox_DX->setValue(100);
134   GroupPntVecR->SpinBox_DX->setValue(100);
135   
136   GroupOrientation->RadioButton1->setChecked(true);
137   myOrientation = 1;
138   
139   GroupPattern->RadioButton1->setChecked(true);
140   myPattern = GEOM::SQUARE;
141
142   // Signal/slot connections
143   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
144   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
145   connect(myGeomGUI,     SIGNAL(SignalDefaultStepValueChanged(double)),
146           this,          SLOT(SetDoubleSpinBoxStep(double)));
147   
148   connect(this,                           SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
149
150   connect(GroupPntVecR->PushButton1,      SIGNAL(clicked()),                this, SLOT(SetEditCurrentArgument()));
151   connect(GroupPntVecR->PushButton2,      SIGNAL(clicked()),                this, SLOT(SetEditCurrentArgument()));
152
153   connect(GroupParams->SpinBox_DX,        SIGNAL(valueChanged(double)),     this, SLOT(ValueChangedInSpinBox()));  
154   connect(GroupPntVecR->SpinBox_DX,       SIGNAL(valueChanged(double)),     this, SLOT(ValueChangedInSpinBox()));
155   
156   connect(GroupOrientation->RadioButton1, SIGNAL(clicked()),                this, SLOT(RadioButtonClicked()));
157   connect(GroupOrientation->RadioButton2, SIGNAL(clicked()),                this, SLOT(RadioButtonClicked()));
158   connect(GroupOrientation->RadioButton3, SIGNAL(clicked()),                this, SLOT(RadioButtonClicked()));
159   
160   connect(GroupPattern->RadioButton1,     SIGNAL(clicked()),                this, SLOT(RadioButtonClicked()));
161   connect(GroupPattern->RadioButton2,     SIGNAL(clicked()),                this, SLOT(RadioButtonClicked()));
162
163   initName(tr("GEOM_DIVIDEDDISK"));
164   
165   ConstructorsClicked(0);
166 }
167
168 //=================================================================================
169 // function : SetDoubleSpinBoxStep()
170 // purpose  : Double spin box management
171 //=================================================================================
172 void AdvancedGUI_DividedDiskDlg::SetDoubleSpinBoxStep (double /*step*/)
173 {
174   //@@ set double spin box step for all spin boxes here @@//
175 }
176
177 //=================================================================================
178 // function : ConstructorsClicked()
179 // purpose  : Radio button management
180 //=================================================================================
181 void AdvancedGUI_DividedDiskDlg::ConstructorsClicked (int constructorId)
182 {
183   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
184
185   switch (constructorId) {
186   case 0:
187     {
188       GroupPntVecR->hide();
189       GroupParams->show();
190       GroupOrientation->show();
191
192       disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
193       globalSelection(); // close local contexts, if any
194       break;
195     }
196   case 1:
197     {
198       GroupParams->hide();
199       GroupOrientation->hide();
200       GroupPntVecR->show();
201
202       GroupPntVecR->PushButton1->click();
203       break;
204     }
205   }
206
207   qApp->processEvents();
208   updateGeometry();
209   resize(minimumSizeHint());
210   SelectionIntoArgument();
211
212   displayPreview(true);
213 }
214
215 //=================================================================================
216 // function : RadioButtonClicked()
217 // purpose  : Radio button management
218 //=================================================================================
219 void AdvancedGUI_DividedDiskDlg::RadioButtonClicked()
220
221   QRadioButton* send = (QRadioButton*)sender();
222   
223   // Orientation
224   if (send == GroupOrientation->RadioButton1)
225     myOrientation = 1;
226   else if ( send == GroupOrientation->RadioButton2)
227     myOrientation = 2;
228   else if ( send == GroupOrientation->RadioButton3)
229     myOrientation = 3;
230   
231   // Division pattern
232   else if (send == GroupPattern->RadioButton1)
233     myPattern = GEOM::SQUARE;
234   else if (send == GroupPattern->RadioButton2)
235     myPattern = GEOM::HEXAGON;
236   
237   displayPreview(true);
238 }
239
240 //=================================================================================
241 // function : ClickOnOk()
242 // purpose  :
243 //=================================================================================
244 void AdvancedGUI_DividedDiskDlg::ClickOnOk()
245 {
246   if (ClickOnApply())
247     ClickOnCancel();
248 }
249
250 //=================================================================================
251 // function : ClickOnApply()
252 // purpose  :
253 //=================================================================================
254 bool AdvancedGUI_DividedDiskDlg::ClickOnApply()
255 {
256   if (!onAccept())
257     return false;
258
259   initName();
260
261   return true;
262 }
263
264 //=================================================================================
265 // function : SelectionIntoArgument()
266 // purpose  : Called when selection is changed or on dialog initialization or activation
267 //=================================================================================
268 void AdvancedGUI_DividedDiskDlg::SelectionIntoArgument()
269 {
270   if (getConstructorId() == 0)
271     return;
272
273   erasePreview();
274   myEditCurrentArgument->setText("");
275
276   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
277   SALOME_ListIO aSelList;
278   aSelMgr->selectedObjects(aSelList);
279
280   if (aSelList.Extent() != 1) {
281     if      (myEditCurrentArgument == GroupPntVecR->LineEdit1) myPoint.nullify();
282     else if (myEditCurrentArgument == GroupPntVecR->LineEdit2) myDir.nullify();
283     return;
284   }
285
286   TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupPntVecR->LineEdit2 ? TopAbs_EDGE : TopAbs_VERTEX;
287   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
288   TopoDS_Shape aShape;
289   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ){
290     QString aName = GEOMBase::GetName( aSelectedObject.get() );
291
292     myEditCurrentArgument->setText(aName);
293     
294     // clear selection
295     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
296     myGeomGUI->getApp()->selectionMgr()->clearSelected();
297     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
298         this, SLOT(SelectionIntoArgument()));
299     
300     if (myEditCurrentArgument == GroupPntVecR->LineEdit1) {
301       myPoint = aSelectedObject;
302       if (myPoint && !myDir)
303     GroupPntVecR->PushButton2->click();
304     }
305     else if (myEditCurrentArgument == GroupPntVecR->LineEdit2) {
306       myDir = aSelectedObject;
307       if (myDir && !myPoint)
308     GroupPntVecR->PushButton1->click();
309     }
310   }
311   displayPreview(true);
312 }
313
314 //=================================================================================
315 // function : SetEditCurrentArgument()
316 // purpose  :
317 //=================================================================================
318 void AdvancedGUI_DividedDiskDlg::SetEditCurrentArgument()
319 {
320   QPushButton* send = (QPushButton*)sender();
321
322   if (send == GroupPntVecR->PushButton1) {
323     myEditCurrentArgument = GroupPntVecR->LineEdit1;
324
325     GroupPntVecR->PushButton2->setDown(false);
326     GroupPntVecR->LineEdit2->setEnabled(false);
327   }
328   else if (send == GroupPntVecR->PushButton2) {
329     myEditCurrentArgument = GroupPntVecR->LineEdit2;
330
331     GroupPntVecR->PushButton1->setDown(false);
332     GroupPntVecR->LineEdit1->setEnabled(false);
333   }
334
335   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
336   if (myEditCurrentArgument == GroupPntVecR->LineEdit2) {
337     globalSelection(); // close local contexts, if any
338     localSelection(TopAbs_EDGE);
339   }
340   else {
341     globalSelection(); // close local contexts, if any
342     localSelection(TopAbs_VERTEX);
343   }
344   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
345           this, SLOT(SelectionIntoArgument()));
346
347   // enable line edit
348   myEditCurrentArgument->setEnabled(true);
349   myEditCurrentArgument->setFocus();
350   // after setFocus(), because it will be setDown(false) when loses focus
351   send->setDown(true);
352
353   // seems we need it only to avoid preview disappearing, caused by selection mode change
354   displayPreview(true);
355 }
356
357 //=================================================================================
358 // function : ActivateThisDialog()
359 // purpose  :
360 //=================================================================================
361 void AdvancedGUI_DividedDiskDlg::ActivateThisDialog()
362 {
363   GEOMBase_Skeleton::ActivateThisDialog();
364   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
365            this, SLOT( SelectionIntoArgument() ) );
366   
367   ConstructorsClicked( getConstructorId() );
368 }
369
370 //=================================================================================
371 // function : enterEvent [REDEFINED]
372 // purpose  :
373 //=================================================================================
374 void AdvancedGUI_DividedDiskDlg::enterEvent (QEvent*)
375 {
376   if (!mainFrame()->GroupConstructors->isEnabled())
377     ActivateThisDialog();
378 }
379
380 //=================================================================================
381 // function : ValueChangedInSpinBox()
382 // purpose  :
383 //=================================================================================
384 void AdvancedGUI_DividedDiskDlg::ValueChangedInSpinBox()
385 {
386   displayPreview(true);
387 }
388
389 //=================================================================================
390 // function : createOperation
391 // purpose  :
392 //=================================================================================
393 GEOM::GEOM_IOperations_ptr AdvancedGUI_DividedDiskDlg::createOperation()
394 {
395   return getGeomEngine()->GetPluginOperations("AdvancedEngine");
396 }
397
398 //=================================================================================
399 // function : isValid
400 // purpose  :
401 //=================================================================================
402 bool AdvancedGUI_DividedDiskDlg::isValid (QString& /*msg*/)
403 {
404   bool ok = true;
405
406   //@@ add custom validation actions here @@//
407
408   return ok;
409 }
410
411 //=================================================================================
412 // function : execute
413 // purpose  :
414 //=================================================================================
415 bool AdvancedGUI_DividedDiskDlg::execute (ObjectList& objects)
416 {
417   bool res = false;
418
419   GEOM::GEOM_Object_var anObj;
420
421   GEOM::IAdvancedOperations_var anOper = GEOM::IAdvancedOperations::_narrow(getOperation());
422
423   CORBA::Double theRatio = 67; // About  2/3  
424   CORBA::Double theR = 0;
425   
426   switch (getConstructorId()) {
427   case 0:
428     theR = GroupParams->SpinBox_DX->value(); // init parameter value from dialog box ;
429
430     // call engine function
431     anObj = anOper->MakeDividedDisk(theR, theRatio, myOrientation, myPattern);
432     res = !anObj->_is_nil();
433     if (res && !IsPreview())
434     {
435       QStringList aParameters;
436       aParameters << GroupParams->SpinBox_DX->text();
437       if ( aParameters.count() > 0 ) anObj->SetParameters(aParameters.join(":").toUtf8().constData());
438     }
439     break;
440   case 1:
441     theR = GroupPntVecR->SpinBox_DX->value(); 
442     
443     // call engine function
444     anObj = anOper->MakeDividedDiskPntVecR(myPoint.get(), myDir.get(), theR, theRatio, myPattern);
445     res = !anObj->_is_nil();
446     if (res && !IsPreview())
447     {
448       QStringList aParameters;
449       aParameters << GroupPntVecR->SpinBox_DX->text();
450       if ( aParameters.count() > 0 ) anObj->SetParameters(aParameters.join(":").toUtf8().constData());
451     }
452     break;
453   }
454     
455   
456   if (res)
457     objects.push_back(anObj._retn());
458
459   return res;
460 }
461
462 //=================================================================================
463 // function : getSourceObjects
464 // purpose  : virtual method to get source objects
465 //=================================================================================
466 QList<GEOM::GeomObjPtr> AdvancedGUI_DividedDiskDlg::getSourceObjects()
467 {
468   QList<GEOM::GeomObjPtr> res;
469   res << myPoint << myDir;
470   return res;
471 }