Salome HOME
Fix for PAL8309(now, if the ctrl key is pressed, the input coordinates function(by...
[modules/geom.git] / src / PrimitiveGUI / PrimitiveGUI_TorusDlg.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : PrimitiveGUI_TorusDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "PrimitiveGUI_TorusDlg.h"
30
31 #include "QAD_Config.h"
32 #include "QAD_Desktop.h"
33
34 #include "GEOMImpl_Types.hxx"
35
36 #include "utilities.h"
37
38 using namespace std;
39
40 //=================================================================================
41 // class    : PrimitiveGUI_TorusDlg()
42 // purpose  : Constructs a PrimitiveGUI_TorusDlg which is a child of 'parent', with the 
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 PrimitiveGUI_TorusDlg::PrimitiveGUI_TorusDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
48   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
49 {
50   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_TORUS_PV")));
51   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_TORUS_DXYZ")));
52   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
53     
54   setCaption(tr("GEOM_TORUS_TITLE"));
55
56   /***************************************************************/
57   GroupConstructors->setTitle(tr("GEOM_TORUS"));
58   RadioButton1->setPixmap(image0);
59   RadioButton2->setPixmap(image1);
60   RadioButton3->close(TRUE);
61
62   GroupPoints = new DlgRef_2Sel2Spin(this, "GroupPoints");
63   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
64   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_POINT"));
65   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
66   GroupPoints->TextLabel3->setText(tr("GEOM_RADIUS_I").arg("1"));
67   GroupPoints->TextLabel4->setText(tr("GEOM_RADIUS_I").arg("2"));
68   GroupPoints->PushButton1->setPixmap(image2);
69   GroupPoints->PushButton2->setPixmap(image2);
70
71   GroupDimensions = new DlgRef_2Spin(this, "GroupDimensions");
72   GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
73   GroupDimensions->TextLabel1->setText(tr("GEOM_RADIUS_I").arg("1"));
74   GroupDimensions->TextLabel2->setText(tr("GEOM_RADIUS_I").arg("2"));
75
76   Layout1->addWidget(GroupPoints, 2, 0);
77   Layout1->addWidget(GroupDimensions, 2, 0);
78   /***************************************************************/
79   
80   Init();
81 }
82
83
84 //=================================================================================
85 // function : ~PrimitiveGUI_TorusDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 PrimitiveGUI_TorusDlg::~PrimitiveGUI_TorusDlg()
89 {
90     // no need to delete child widgets, Qt does it all for us
91 }
92
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void PrimitiveGUI_TorusDlg::Init()
99 {
100   /* init variables */
101   myEditCurrentArgument = GroupPoints->LineEdit1;
102   GroupPoints->LineEdit1->setReadOnly( true );
103   GroupPoints->LineEdit2->setReadOnly( true );
104  
105   myPoint = myDir = GEOM::GEOM_Object::_nil();
106
107   /* Get setting of step value from file configuration */
108   QString St = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
109   double step = St.toDouble();
110
111   /* min, max, step and decimals for spin boxes & initial values */
112   GroupPoints->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
113   GroupPoints->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
114   GroupDimensions->SpinBox_DX->RangeStepAndValidator(0.001, 999.999, step, 3);
115   GroupDimensions->SpinBox_DY->RangeStepAndValidator(0.001, 999.999, step, 3);
116
117   GroupPoints->SpinBox_DX->SetValue(300.0);
118   GroupPoints->SpinBox_DY->SetValue(100.0);
119   GroupDimensions->SpinBox_DX->SetValue(300.0);
120   GroupDimensions->SpinBox_DY->SetValue(100.0);
121
122   /* signals and slots connections */
123   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
124   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
125   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
126
127   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
128   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129
130   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
131   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
132
133   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
134   connect(GroupPoints->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
135   connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
136   connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
137
138   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
139   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupPoints->SpinBox_DY, SLOT(SetStep(double)));
140   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DX, SLOT(SetStep(double)));
141   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), GroupDimensions->SpinBox_DY, SLOT(SetStep(double)));
142   
143   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
144   
145   initName( tr( "GEOM_TORUS" ) );
146   ConstructorsClicked(0);
147 }
148
149
150
151 //=================================================================================
152 // function : ConstructorsClicked()
153 // purpose  : Radio button management
154 //=================================================================================
155 void PrimitiveGUI_TorusDlg::ConstructorsClicked(int constructorId)
156 {
157   disconnect(mySelection, 0, this, 0);
158   
159   switch(constructorId)
160     { 
161     case 0 :
162       {
163         globalSelection( GEOM_LINE );
164
165         GroupDimensions->hide();
166         resize(0, 0);
167         GroupPoints->show();
168         
169         myEditCurrentArgument = GroupPoints->LineEdit1;
170         GroupPoints->LineEdit1->setText(tr(""));
171         GroupPoints->LineEdit2->setText(tr(""));
172         myPoint = myDir = GEOM::GEOM_Object::_nil();
173         
174         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
175         break ;
176       }
177     case 1 :
178       {
179         GroupPoints->hide();
180         resize( 0, 0 );
181         GroupDimensions->show();
182         
183         break ;
184       }
185     }
186   displayPreview();
187 }
188
189
190 //=================================================================================
191 // function : ClickOnOk()
192 // purpose  :
193 //=================================================================================
194 void PrimitiveGUI_TorusDlg::ClickOnOk()
195 {
196   if ( ClickOnApply() )
197     ClickOnCancel();
198 }
199
200
201 //=================================================================================
202 // function : ClickOnApply()
203 // purpose  :
204 //=================================================================================
205 bool PrimitiveGUI_TorusDlg::ClickOnApply()
206 {
207   if ( !onAccept() )
208     return false;
209
210   initName();
211   ConstructorsClicked( getConstructorId() );
212   return true;
213 }
214
215
216 //=======================================================================
217 // function : ClickOnCancel()
218 // purpose  :
219 //=======================================================================
220 void PrimitiveGUI_TorusDlg::ClickOnCancel()
221 {
222   GEOMBase_Skeleton::ClickOnCancel();
223 }
224
225
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose  : Called when selection as changed or other case
229 //=================================================================================
230 void PrimitiveGUI_TorusDlg::SelectionIntoArgument()
231 {
232   if ( getConstructorId() != 0 )
233     return;
234   
235   myEditCurrentArgument->setText("");
236   
237   if(mySelection->IObjectCount() != 1)
238     {
239       if(myEditCurrentArgument == GroupPoints->LineEdit1)
240         myPoint = GEOM::GEOM_Object::_nil();
241       else if(myEditCurrentArgument == GroupPoints->LineEdit2)
242         myDir = GEOM::GEOM_Object::_nil();
243       return;
244     }
245   
246   
247   /* nbSel == 1 */
248   Standard_Boolean testResult = Standard_False;
249   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), testResult );
250   
251   if(!testResult || CORBA::is_nil( aSelectedObject ))
252     return;
253   
254   if(myEditCurrentArgument == GroupPoints->LineEdit1)
255     myPoint = aSelectedObject;
256   else if(myEditCurrentArgument == GroupPoints->LineEdit2)
257     myDir = aSelectedObject;
258     
259   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
260   displayPreview();
261 }
262
263
264 //=================================================================================
265 // function : SetEditCurrentArgument()
266 // purpose  :
267 //=================================================================================
268 void PrimitiveGUI_TorusDlg::SetEditCurrentArgument()
269 {
270   QPushButton* send = (QPushButton*)sender();
271   
272   if(send == GroupPoints->PushButton1) {
273     myEditCurrentArgument = GroupPoints->LineEdit1;
274     globalSelection( GEOM_POINT );
275   }
276   else if(send == GroupPoints->PushButton2) {
277     myEditCurrentArgument = GroupPoints->LineEdit2;
278     globalSelection( GEOM_LINE );
279   }
280   
281   myEditCurrentArgument->setFocus();
282   SelectionIntoArgument();
283 }
284
285
286 //=================================================================================
287 // function : LineEditReturnPressed()
288 // purpose  :
289 //=================================================================================
290 void PrimitiveGUI_TorusDlg::LineEditReturnPressed()
291 {  
292   QLineEdit* send = (QLineEdit*)sender();
293   if(send == GroupPoints->LineEdit1 ||
294      send == GroupPoints->LineEdit2)
295     {
296       myEditCurrentArgument = send;
297       GEOMBase_Skeleton::LineEditReturnPressed();
298     }
299 }
300
301
302 //=================================================================================
303 // function : ActivateThisDialog()
304 // purpose  :
305 //=================================================================================
306 void PrimitiveGUI_TorusDlg::ActivateThisDialog()
307 {
308   GEOMBase_Skeleton::ActivateThisDialog();
309   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
310  
311   ConstructorsClicked( getConstructorId() );
312 }
313
314
315 //=================================================================================
316 // function : enterEvent()
317 // purpose  :
318 //=================================================================================
319 void PrimitiveGUI_TorusDlg::enterEvent(QEvent* e)
320 {
321   if ( !GroupConstructors->isEnabled() )
322     ActivateThisDialog();
323 }
324
325
326 //=================================================================================
327 // function : DeactivateActiveDialog()
328 // purpose  : public slot to deactivate if active
329 //=================================================================================
330 void PrimitiveGUI_TorusDlg::DeactivateActiveDialog()
331 {
332   GEOMBase_Skeleton::DeactivateActiveDialog();
333 }
334
335
336 //=================================================================================
337 // function : ValueChangedInSpinBox
338 // purpose  :
339 //=================================================================================
340 void PrimitiveGUI_TorusDlg::ValueChangedInSpinBox()
341 {
342   displayPreview();
343 }
344
345
346 //=================================================================================
347 // function : createOperation
348 // purpose  :
349 //=================================================================================
350 GEOM::GEOM_IOperations_ptr PrimitiveGUI_TorusDlg::createOperation()
351 {
352   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
353 }
354
355
356 //=================================================================================
357 // function : isValid
358 // purpose  :
359 //=================================================================================
360 bool PrimitiveGUI_TorusDlg::isValid( QString& msg )
361 {
362   return getConstructorId() == 0 ? !(myPoint->_is_nil() || myDir->_is_nil()) : true;
363 }
364
365
366 //=================================================================================
367 // function : execute
368 // purpose  :
369 //=================================================================================
370 bool PrimitiveGUI_TorusDlg::execute( ObjectList& objects )
371 {
372   bool res = false;
373   
374   GEOM::GEOM_Object_var anObj;
375
376   switch ( getConstructorId() ) 
377   {
378   case 0 :
379   {
380     if (!CORBA::is_nil( myPoint ) && !CORBA::is_nil( myDir )){
381       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeTorusPntVecRR(myPoint, myDir, getRadius1(), getRadius2());
382       res = true;
383     }
384     break;
385   }
386   case 1 :
387     {
388       anObj = GEOM::GEOM_I3DPrimOperations::_narrow( getOperation() )->MakeTorusRR(getRadius1(), getRadius2());
389       res = true;
390       break;
391     }
392   }
393
394   if ( !anObj->_is_nil() )
395     objects.push_back( anObj._retn() );
396
397   return res;
398 }
399
400
401 //=================================================================================
402 // function : closeEvent
403 // purpose  :
404 //=================================================================================
405 void PrimitiveGUI_TorusDlg::closeEvent( QCloseEvent* e )
406 {
407   GEOMBase_Skeleton::closeEvent( e );
408 }
409
410
411 //=================================================================================
412 // function : getRadius1()
413 // purpose  :
414 //=================================================================================
415 double PrimitiveGUI_TorusDlg::getRadius1() const
416 {
417   int aConstructorId = getConstructorId();
418   if (aConstructorId == 0)
419     return GroupPoints->SpinBox_DX->GetValue();
420   else if (aConstructorId == 1)
421     return GroupDimensions->SpinBox_DX->GetValue();
422   return 0;
423 }
424
425
426 //=================================================================================
427 // function : getRadius2()
428 // purpose  :
429 //=================================================================================
430 double PrimitiveGUI_TorusDlg::getRadius2() const
431 {
432   int aConstructorId = getConstructorId();
433   if (aConstructorId == 0)
434     return GroupPoints->SpinBox_DY->GetValue();
435   else if (aConstructorId == 1)
436     return GroupDimensions->SpinBox_DY->GetValue();
437   return 0;
438 }