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