Salome HOME
fix bug 9395. set coordinates and displacements limit to interval [-1e+15, 1e+15]
[modules/geom.git] / src / GenerationGUI / GenerationGUI_PrismDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : GenerationGUI_PrismDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GenerationGUI_PrismDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <BRepPrimAPI_MakePrism.hxx>
37 #include <BRepAdaptor_Curve.hxx>
38 #include <gp_Lin.hxx>
39 #include "GEOMImpl_Types.hxx"
40
41 #include <qlabel.h>
42 #include <qcheckbox.h>
43
44 #include "utilities.h"
45
46 //=================================================================================
47 // class    : GenerationGUI_PrismDlg()
48 // purpose  : Constructs a GenerationGUI_PrismDlg which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 GenerationGUI_PrismDlg::GenerationGUI_PrismDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
54                                                const char* name, bool modal, WFlags fl)
55   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
56                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
57 {
58   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_PRISM")));
59   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
60   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_PRISM_2P")));
61
62   setCaption(tr("GEOM_PRISM_TITLE"));
63
64   /***************************************************************/
65   GroupConstructors->setTitle(tr("GEOM_PRISM"));
66   RadioButton1->setPixmap(image0);
67   RadioButton2->setPixmap(image2);
68   //RadioButton2->close(TRUE);
69   RadioButton3->close(TRUE);
70
71   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
72   //GroupPoints->CheckButton1->hide();
73   GroupPoints->GroupBox1->setTitle(tr("GEOM_PRISM_BSV"));
74   GroupPoints->TextLabel1->setText(tr("GEOM_BASE"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
76   GroupPoints->TextLabel3->setText(tr("GEOM_HEIGHT"));
77   GroupPoints->PushButton1->setPixmap(image1);
78   GroupPoints->PushButton2->setPixmap(image1);
79   GroupPoints->LineEdit1->setReadOnly( true );
80   GroupPoints->LineEdit2->setReadOnly( true );
81   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
82
83   GroupPoints2 = new DlgRef_3Sel_QTD(this, "GroupPoints2");
84   GroupPoints2->GroupBox1->setTitle(tr("GEOM_PRISM_BSV_2P"));
85   GroupPoints2->TextLabel1->setText(tr("GEOM_BASE"));
86   GroupPoints2->TextLabel2->setText(tr("GEOM_POINT_I").arg("1"));
87   GroupPoints2->TextLabel3->setText(tr("GEOM_POINT_I").arg("2"));
88   GroupPoints2->PushButton1->setPixmap(image1);
89   GroupPoints2->PushButton2->setPixmap(image1);
90   GroupPoints2->PushButton3->setPixmap(image1);
91
92   Layout1->addWidget(GroupPoints, 2, 0);
93   Layout1->addWidget(GroupPoints2, 2, 0);
94   /***************************************************************/
95
96   setHelpFileName("extrusion.htm");
97
98   /* Initialisations */
99   Init();
100 }
101
102
103 //=================================================================================
104 // function : ~GenerationGUI_PrismDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 GenerationGUI_PrismDlg::~GenerationGUI_PrismDlg()
108 {  
109   /* no need to delete child widgets, Qt does it all for us */
110 }
111
112
113 //=================================================================================
114 // function : Init()
115 // purpose  :
116 //=================================================================================
117 void GenerationGUI_PrismDlg::Init()
118 {
119   /* init variables */
120   myEditCurrentArgument = GroupPoints->LineEdit1;
121   GroupPoints->LineEdit1->setReadOnly( true );
122   GroupPoints->LineEdit2->setReadOnly( true );
123
124   GroupPoints2->LineEdit1->setReadOnly( true );
125   GroupPoints2->LineEdit2->setReadOnly( true );
126   GroupPoints2->LineEdit3->setReadOnly( true );
127
128   //myOkBase = myOkVec = false;
129   myOkBase = myOkVec = myOkPnt1 = myOkPnt2 = false;
130   
131   /* Get setting of step value from file configuration */
132   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
133   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
134
135   /* min, max, step and decimals for spin boxes & initial values */
136   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, 3);
137   GroupPoints->SpinBox_DX->SetValue(100.0);
138
139   /* signals and slots connections */
140   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
141   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
142
143   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
144
145   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
146   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
147
148   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
149   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
150
151   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
152   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
153
154   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)),      this, SLOT(onReverse()));
155
156
157   connect(GroupPoints2->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
158   connect(GroupPoints2->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
159   connect(GroupPoints2->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
160
161   connect(GroupPoints2->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
162   connect(GroupPoints2->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
163   connect(GroupPoints2->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
164
165
166   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
167           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
168
169   initName(tr("GEOM_PRISM"));
170
171   globalSelection( GEOM_ALLSHAPES );
172   ConstructorsClicked(0);
173 }
174
175
176 //=================================================================================
177 // function : ConstructorsClicked()
178 // purpose  : Radio button management
179 //=================================================================================
180 void GenerationGUI_PrismDlg::ConstructorsClicked(int constructorId)
181 {
182   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
183   
184   switch (constructorId)
185     {
186     case 0:
187       {
188         globalSelection( GEOM_POINT );
189
190         GroupPoints2->hide();
191         resize(0, 0);
192         GroupPoints->show();
193         
194         //GroupPoints->LineEdit1->setFocus();
195         //myEditCurrentArgument = GroupPoints->LineEdit1;
196         //displayPreview();
197
198         myEditCurrentArgument = GroupPoints->LineEdit1;
199         GroupPoints->LineEdit1->setText("");
200         GroupPoints->LineEdit2->setText("");
201         GroupPoints2->LineEdit1->setText("");
202         GroupPoints2->LineEdit2->setText("");
203         GroupPoints2->LineEdit3->setText("");
204         myPoint1 = myPoint2 = myBase = myVec = GEOM::GEOM_Object::_nil();
205         myOkBase = myOkVec = myOkPnt1 = myOkPnt2 = false;
206
207         break;
208       }
209     case 1:
210       {
211         GroupPoints->hide();
212         resize(0, 0);
213         GroupPoints2->show();
214         
215         myEditCurrentArgument = GroupPoints2->LineEdit1;
216         GroupPoints2->LineEdit1->setText("");
217         GroupPoints2->LineEdit2->setText("");
218         GroupPoints2->LineEdit3->setText("");
219         myPoint1 = myPoint2 = myBase = myVec = GEOM::GEOM_Object::_nil();
220         myOkBase = myOkVec = myOkPnt1 = myOkPnt2 = false;
221         GroupPoints->LineEdit1->setText("");
222         GroupPoints->LineEdit2->setText("");
223         
224         break;
225       }
226     }
227   displayPreview();
228 }
229
230
231 //=================================================================================
232 // function : ClickOnOk()
233 // purpose  :
234 //=================================================================================
235 void GenerationGUI_PrismDlg::ClickOnOk()
236 {
237   if ( ClickOnApply() )
238     ClickOnCancel();
239 }
240
241
242 //=================================================================================
243 // function : ClickOnApply()
244 // purpose  :
245 //=================================================================================
246 bool GenerationGUI_PrismDlg::ClickOnApply()
247 {
248   if ( !onAccept() )
249     return false;
250
251   initName();
252   return true;
253 }
254
255
256 //=================================================================================
257 // function : SelectionIntoArgument()
258 // purpose  : Called when selection has changed
259 //=================================================================================
260 void GenerationGUI_PrismDlg::SelectionIntoArgument()
261 {
262   erasePreview();
263   myEditCurrentArgument->setText("");
264   
265   if ( getConstructorId()==0 ) {
266
267     if (IObjectCount() != 1) {
268       if (myEditCurrentArgument == GroupPoints->LineEdit1)
269         myOkBase = false;
270       else if (myEditCurrentArgument == GroupPoints->LineEdit2)
271         myOkVec = false;
272       return;
273     }
274
275     // nbSel == 1
276     Standard_Boolean testResult = Standard_False;
277     GEOM::GEOM_Object_ptr aSelectedObject =
278       GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
279
280     if (!testResult)
281       return;
282
283     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
284       myOkBase = false;
285       TopoDS_Shape S;
286     
287       if (!GEOMBase::GetShape(aSelectedObject, S) ||
288           S.ShapeType() <= 2)
289         return;
290
291       myBase = aSelectedObject;
292       myOkBase = true;
293     }
294     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
295       myVec = aSelectedObject;
296       myOkVec = true;
297     }
298     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
299
300     displayPreview();
301   }
302
303   else { // getConstructorId()==1 - extrusion using 2 points
304
305     if (IObjectCount() != 1) {
306       if (myEditCurrentArgument == GroupPoints2->LineEdit1)
307         myOkBase = false;
308       else if (myEditCurrentArgument == GroupPoints2->LineEdit2) {
309         myPoint1 = GEOM::GEOM_Object::_nil();
310         myOkPnt1 = false;
311       }
312       else if (myEditCurrentArgument == GroupPoints2->LineEdit3) {
313         myPoint2 = GEOM::GEOM_Object::_nil();
314         myOkPnt2 = false;
315       }
316       return;
317     }
318
319     // nbSel == 1
320     Standard_Boolean testResult = Standard_False;
321     GEOM::GEOM_Object_var aSelectedObject =
322       GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
323   
324     if(!testResult || CORBA::is_nil( aSelectedObject ))
325       return;
326     
327     if(myEditCurrentArgument == GroupPoints2->LineEdit1) {
328       myBase = aSelectedObject;
329       myOkBase = true;
330     }
331     else if(myEditCurrentArgument == GroupPoints2->LineEdit2) {
332       myPoint1 = aSelectedObject;
333       myOkPnt1 = true;
334     }
335     else if(myEditCurrentArgument == GroupPoints2->LineEdit3) {
336       myPoint2 = aSelectedObject;
337       myOkPnt2 = true;
338     }
339   
340     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
341   
342     displayPreview();
343   }
344 }
345
346
347 //=================================================================================
348 // function : SetEditCurrentArgument()
349 // purpose  :
350 //=================================================================================
351 void GenerationGUI_PrismDlg::SetEditCurrentArgument()
352 {
353   QPushButton* send = (QPushButton*)sender();
354   globalSelection( GEOM_ALLSHAPES );
355
356   if(send == GroupPoints->PushButton1) {
357     GroupPoints->LineEdit1->setFocus();
358     myEditCurrentArgument = GroupPoints->LineEdit1;
359   }
360   else if(send == GroupPoints->PushButton2) {
361     GroupPoints->LineEdit2->setFocus();
362     myEditCurrentArgument = GroupPoints->LineEdit2;
363     globalSelection( GEOM_LINE );
364   }
365   else if(send == GroupPoints2->PushButton1) {
366     GroupPoints2->LineEdit1->setFocus();
367     myEditCurrentArgument = GroupPoints2->LineEdit1;
368   }
369   else if(send == GroupPoints2->PushButton2) {
370     GroupPoints2->LineEdit2->setFocus();
371     myEditCurrentArgument = GroupPoints2->LineEdit2;
372     globalSelection( GEOM_POINT );
373   }
374   else if(send == GroupPoints2->PushButton3) {
375     GroupPoints2->LineEdit3->setFocus();
376     myEditCurrentArgument = GroupPoints2->LineEdit3;
377     globalSelection( GEOM_POINT );
378   }
379   SelectionIntoArgument();
380 }
381
382
383 //=================================================================================
384 // function : LineEditReturnPressed()
385 // purpose  :
386 //=================================================================================
387 void GenerationGUI_PrismDlg::LineEditReturnPressed()
388 {
389   QLineEdit* send = (QLineEdit*)sender();
390   if(send == GroupPoints->LineEdit1 ||
391      send == GroupPoints->LineEdit2 ||
392      send == GroupPoints2->LineEdit1 ||
393      send == GroupPoints2->LineEdit2 ||
394      send == GroupPoints2->LineEdit3 )
395     {
396       myEditCurrentArgument = send;
397       GEOMBase_Skeleton::LineEditReturnPressed();
398     }
399 }
400
401 //=================================================================================
402 // function : enterEvent()
403 // purpose  : when mouse enter onto the QWidget
404 //=================================================================================
405 void GenerationGUI_PrismDlg::enterEvent(QEvent * e)
406 {
407   if ( !GroupConstructors->isEnabled() )
408     ActivateThisDialog();
409 }
410
411
412 //=================================================================================
413 // function : ActivateThisDialog()
414 // purpose  :
415 //=================================================================================
416 void GenerationGUI_PrismDlg::ActivateThisDialog()
417 {
418   GEOMBase_Skeleton::ActivateThisDialog();
419   globalSelection( GEOM_ALLSHAPES );
420   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
421           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
422   //GroupPoints->LineEdit1->setFocus();
423   //myEditCurrentArgument = GroupPoints->LineEdit1;
424   //displayPreview();
425   ConstructorsClicked( getConstructorId() );
426 }
427
428
429 //=================================================================================
430 // function : ValueChangedInSpinBox()
431 // purpose  :
432 //=================================================================================
433 void GenerationGUI_PrismDlg::ValueChangedInSpinBox()
434 {
435   displayPreview();
436 }
437
438
439 //=================================================================================
440 // function : getHeight()
441 // purpose  :
442 //=================================================================================
443 double GenerationGUI_PrismDlg::getHeight() const
444 {
445   return GroupPoints->SpinBox_DX->GetValue();
446 }
447
448 //=================================================================================
449 // function : createOperation
450 // purpose  :
451 //=================================================================================
452 GEOM::GEOM_IOperations_ptr GenerationGUI_PrismDlg::createOperation()
453 {
454   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
455 }
456
457 //=================================================================================
458 // function : isValid
459 // purpose  :
460 //=================================================================================
461 bool GenerationGUI_PrismDlg::isValid( QString& )
462 {
463   return myOkBase && ( myOkVec || (myOkPnt1 && myOkPnt2) );
464 }
465
466 //=================================================================================
467 // function : execute
468 // purpose  :
469 //=================================================================================
470 bool GenerationGUI_PrismDlg::execute( ObjectList& objects )
471 {
472   GEOM::GEOM_Object_var anObj;
473
474   switch ( getConstructorId() ) 
475   {
476   case 0 :
477     { 
478       anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
479         MakePrismVecH(myBase, myVec, getHeight());
480       break;
481     }
482   case 1 :
483     {
484       anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
485         MakePrismTwoPnt(myBase, myPoint1, myPoint2);
486       break;
487     }
488   }
489   if ( !anObj->_is_nil() )
490     objects.push_back( anObj._retn() );
491
492   return true;
493 }
494
495
496 //=================================================================================
497 // function :  onReverse()
498 // purpose  :
499 //=================================================================================
500 void GenerationGUI_PrismDlg::onReverse()
501 {
502   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
503   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
504 }