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