Salome HOME
NPAL 18363
[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 <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <qlabel.h>
44 #include <qcheckbox.h>
45
46 #include "GEOMImpl_Types.hxx"
47
48 #include "utilities.h"
49
50 //=================================================================================
51 // class    : GenerationGUI_PrismDlg()
52 // purpose  : Constructs a GenerationGUI_PrismDlg which is a child of 'parent', with the
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 GenerationGUI_PrismDlg::GenerationGUI_PrismDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
58                                                const char* name, bool modal, WFlags fl)
59   : GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
60                       WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
61 {
62   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
63   QPixmap image0(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_PRISM")));
64   QPixmap image1(aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
65   QPixmap image2(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_PRISM_2P")));
66
67   setCaption(tr("GEOM_EXTRUSION_TITLE"));
68
69   /***************************************************************/
70   GroupConstructors->setTitle(tr("GEOM_EXTRUSION"));
71   RadioButton1->setPixmap(image0);
72   RadioButton2->setPixmap(image2);
73   RadioButton3->close(TRUE);
74
75   RadioButton1->setChecked(true);
76   myBothway = myBothway2 = false;
77
78   GroupPoints = new DlgRef_2Sel1Spin2Check(this, "GroupPoints");
79   GroupPoints->GroupBox1->setTitle(tr("GEOM_EXTRUSION_BSV"));
80   GroupPoints->TextLabel1->setText(tr("GEOM_BASE"));
81   GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
82   GroupPoints->TextLabel3->setText(tr("GEOM_HEIGHT"));
83   GroupPoints->PushButton1->setPixmap(image1);
84   GroupPoints->PushButton2->setPixmap(image1);
85   GroupPoints->LineEdit1->setReadOnly( true );
86   GroupPoints->LineEdit2->setReadOnly( true );
87   GroupPoints->CheckButton1->setText(tr("GEOM_BOTHWAY"));
88   GroupPoints->CheckButton1->setChecked(myBothway);
89   GroupPoints->CheckButton2->setText(tr("GEOM_REVERSE"));
90
91   GroupPoints2 = new DlgRef_3Sel1Check_QTD(this, "GroupPoints2");
92   GroupPoints2->GroupBox1->setTitle(tr("GEOM_EXTRUSION_BSV_2P"));
93   GroupPoints2->TextLabel1->setText(tr("GEOM_BASE"));
94   GroupPoints2->TextLabel2->setText(tr("GEOM_POINT_I").arg("1"));
95   GroupPoints2->TextLabel3->setText(tr("GEOM_POINT_I").arg("2"));
96   GroupPoints2->PushButton1->setPixmap(image1);
97   GroupPoints2->PushButton2->setPixmap(image1);
98   GroupPoints2->PushButton3->setPixmap(image1);
99   GroupPoints2->CheckButton1->setText(tr("GEOM_BOTHWAY"));
100   GroupPoints2->CheckButton1->setChecked(myBothway2);
101
102   Layout1->addWidget(GroupPoints, 2, 0);
103   Layout1->addWidget(GroupPoints2, 2, 0);
104   /***************************************************************/
105
106   setHelpFileName("extrusion.htm");
107
108   Init();
109 }
110
111
112 //=================================================================================
113 // function : ~GenerationGUI_PrismDlg()
114 // purpose  : Destroys the object and frees any allocated resources
115 //=================================================================================
116 GenerationGUI_PrismDlg::~GenerationGUI_PrismDlg()
117 {
118   // no need to delete child widgets, Qt does it all for us
119 }
120
121
122 //=================================================================================
123 // function : Init()
124 // purpose  :
125 //=================================================================================
126 void GenerationGUI_PrismDlg::Init()
127 {
128   /* init variables */
129   myEditCurrentArgument = GroupPoints->LineEdit1;
130   GroupPoints->LineEdit1->setReadOnly( true );
131   GroupPoints->LineEdit2->setReadOnly( true );
132
133   GroupPoints2->LineEdit1->setReadOnly( true );
134   GroupPoints2->LineEdit2->setReadOnly( true );
135   GroupPoints2->LineEdit3->setReadOnly( true );
136
137   myPoint1 = myPoint2 = myBase = myVec = GEOM::GEOM_Object::_nil();
138   myOkBase = myOkVec = myOkPnt1 = myOkPnt2 = false;
139
140   /* Get setting of step value from file configuration */
141   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
142   double step = resMgr->doubleValue( "Geometry", "SettingsGeomStep", 100);
143
144   /* min, max, step and decimals for spin boxes & initial values */
145   GroupPoints->SpinBox_DX->RangeStepAndValidator(COORD_MIN, COORD_MAX, step, DBL_DIGITS_DISPLAY);
146   GroupPoints->SpinBox_DX->SetValue(100.0);
147
148   /* signals and slots connections */
149   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
150   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
151
152   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
153
154   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
155   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
156
157   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
158   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
159
160   connect(GroupPoints->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
161   connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)),
162           GroupPoints->SpinBox_DX, SLOT(SetStep(double)));
163
164   connect(GroupPoints->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(onBothway()));
165   connect(GroupPoints->CheckButton2, SIGNAL(toggled(bool)), this, SLOT(onReverse()));
166
167   connect(GroupPoints2->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
168   connect(GroupPoints2->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
169   connect(GroupPoints2->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
170   connect(GroupPoints2->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(onBothway2()));
171
172   connect(GroupPoints2->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
173   connect(GroupPoints2->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
174   connect(GroupPoints2->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
175
176
177   connect(myGeomGUI->getApp()->selectionMgr(),
178           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
179
180   initName(tr("GEOM_EXTRUSION"));
181   ConstructorsClicked(0);
182 }
183
184
185 //=================================================================================
186 // function : ConstructorsClicked()
187 // purpose  : Radio button management
188 //=================================================================================
189 void GenerationGUI_PrismDlg::ConstructorsClicked(int constructorId)
190 {
191   erasePreview();
192   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
193
194   switch (constructorId)
195   {
196   case 0:
197     {
198       globalSelection( GEOM_ALLSHAPES );
199
200       GroupPoints2->hide();
201       resize(0, 0);
202       GroupPoints->show();
203
204       myEditCurrentArgument = GroupPoints->LineEdit1;
205       GroupPoints->LineEdit1->setText(GroupPoints2->LineEdit1->text()); // keep base
206       GroupPoints->LineEdit2->setText("");
207       myVec = GEOM::GEOM_Object::_nil();
208       myOkVec = false;
209
210       break;
211     }
212   case 1:
213     {
214       globalSelection( GEOM_ALLSHAPES );
215
216       GroupPoints->hide();
217       resize(0, 0);
218       GroupPoints2->show();
219
220       myEditCurrentArgument = GroupPoints2->LineEdit1;
221       GroupPoints2->LineEdit1->setText(GroupPoints->LineEdit1->text()); // keep base
222       GroupPoints2->LineEdit2->setText("");
223       GroupPoints2->LineEdit3->setText("");
224       myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
225       myOkPnt1 = myOkPnt2 = false;
226
227       break;
228     }
229   }
230
231   myEditCurrentArgument->setFocus();
232   connect(myGeomGUI->getApp()->selectionMgr(),
233           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
234
235   if (!myOkBase)
236     SelectionIntoArgument();
237 }
238
239
240 //=================================================================================
241 // function : ClickOnOk()
242 // purpose  :
243 //=================================================================================
244 void GenerationGUI_PrismDlg::ClickOnOk()
245 {
246   if ( ClickOnApply() )
247     ClickOnCancel();
248 }
249
250
251 //=================================================================================
252 // function : ClickOnApply()
253 // purpose  :
254 //=================================================================================
255 bool GenerationGUI_PrismDlg::ClickOnApply()
256 {
257   if ( !onAccept() )
258     return false;
259
260   initName();
261   return true;
262 }
263
264
265 //=================================================================================
266 // function : SelectionIntoArgument()
267 // purpose  : Called when selection has changed
268 //=================================================================================
269 void GenerationGUI_PrismDlg::SelectionIntoArgument()
270 {
271   erasePreview();
272   myEditCurrentArgument->setText("");
273
274   if (getConstructorId() == 0)
275   {
276     if (IObjectCount() != 1) {
277       if (myEditCurrentArgument == GroupPoints->LineEdit1)
278         myOkBase = false;
279       else if (myEditCurrentArgument == GroupPoints->LineEdit2)
280         myOkVec = false;
281       return;
282     }
283
284     // nbSel == 1
285     Standard_Boolean testResult = Standard_False;
286     GEOM::GEOM_Object_ptr aSelectedObject =
287     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
288
289     if (!testResult)
290       return;
291
292     bool myOk = true;
293     TopoDS_Shape aShape;
294     QString aName = GEOMBase::GetName( aSelectedObject );    
295     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
296       {
297         LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
298         TColStd_IndexedMapOfInteger aMap;
299         aSelMgr->GetIndexes( firstIObject(), aMap );
300         if ( aMap.Extent() == 1 )
301           {
302             GEOM::GEOM_IShapesOperations_var aShapesOp =
303               getGeomEngine()->GetIShapesOperations( getStudyId() );
304             int anIndex = aMap( 1 );
305             aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
306             aName.append( ":edge_" + QString::number( anIndex ) );
307             aSelMgr->clearSelected();
308           }
309         else {
310           if (aShape.ShapeType() != TopAbs_EDGE && myEditCurrentArgument == GroupPoints->LineEdit2) {
311             aSelectedObject = GEOM::GEOM_Object::_nil();
312             aName = "";
313             myOk = false;
314           }
315         }
316       }
317  
318     if (myEditCurrentArgument == GroupPoints->LineEdit1) {
319       myBase = aSelectedObject;
320       myOkBase = true;
321     }
322     else if (myEditCurrentArgument == GroupPoints->LineEdit2 && myOk) {
323       myOkVec = true;      
324       myVec = aSelectedObject;
325     }
326     myEditCurrentArgument->setText( aName );
327   }
328   else // getConstructorId()==1 - extrusion using 2 points
329   {
330     if (IObjectCount() != 1) {
331       if (myEditCurrentArgument == GroupPoints2->LineEdit1)
332         myOkBase = false;
333       else if (myEditCurrentArgument == GroupPoints2->LineEdit2) {
334         myPoint1 = GEOM::GEOM_Object::_nil();
335         myOkPnt1 = false;
336       }
337       else if (myEditCurrentArgument == GroupPoints2->LineEdit3) {
338         myPoint2 = GEOM::GEOM_Object::_nil();
339         myOkPnt2 = false;
340       }
341       return;
342     }
343
344     // nbSel == 1
345     Standard_Boolean testResult = Standard_False;
346     GEOM::GEOM_Object_var aSelectedObject =
347       GEOMBase::ConvertIOinGEOMObject(firstIObject(), testResult );
348
349     if (!testResult || CORBA::is_nil( aSelectedObject ))
350       return;
351
352     QString aName = GEOMBase::GetName( aSelectedObject );
353     TopoDS_Shape aShape;
354     bool myOk = true;
355     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
356       {
357
358         LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
359         TColStd_IndexedMapOfInteger aMap;
360         aSelMgr->GetIndexes( firstIObject(), aMap );
361         if ( aMap.Extent() == 1 )
362           {
363             GEOM::GEOM_IShapesOperations_var aShapesOp =
364               getGeomEngine()->GetIShapesOperations( getStudyId() );
365             int anIndex = aMap( 1 );
366             aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
367             aName.append( ":vertex_" + QString::number( anIndex ) );
368             aSelMgr->clearSelected();
369           }
370         else {
371           if ( (aShape.ShapeType() != TopAbs_EDGE && myEditCurrentArgument == GroupPoints2->LineEdit2 ) ||
372                (aShape.ShapeType() != TopAbs_EDGE && myEditCurrentArgument == GroupPoints2->LineEdit3 )) {
373             aSelectedObject = GEOM::GEOM_Object::_nil();
374             aName = "";
375             myOk = false;
376           }
377         }
378       }
379   
380     myEditCurrentArgument->setText( aName );
381
382     if (myEditCurrentArgument == GroupPoints2->LineEdit1) {
383       myBase = aSelectedObject;
384       myOkBase = true;
385     }
386     else if (myEditCurrentArgument == GroupPoints2->LineEdit2 && myOk) {
387       myOkPnt1 = true;
388       myPoint1 = aSelectedObject;
389     }
390     else if (myEditCurrentArgument == GroupPoints2->LineEdit3 && myOk) {
391       myOkPnt2 = true;
392       myPoint2 = aSelectedObject;
393     }
394
395   }
396
397   displayPreview();
398 }
399
400
401 //=================================================================================
402 // function : LineEditReturnPressed()
403 // purpose  :
404 //=================================================================================
405 void GenerationGUI_PrismDlg::LineEditReturnPressed()
406 {
407   QLineEdit* send = (QLineEdit*)sender();
408   if (send == GroupPoints->LineEdit1 ||
409       send == GroupPoints->LineEdit2 ||
410       send == GroupPoints2->LineEdit1 ||
411       send == GroupPoints2->LineEdit2 ||
412       send == GroupPoints2->LineEdit3 )
413   {
414     myEditCurrentArgument = send;
415     GEOMBase_Skeleton::LineEditReturnPressed();
416   }
417 }
418
419
420 //=================================================================================
421 // function : SetEditCurrentArgument()
422 // purpose  :
423 //=================================================================================
424 void GenerationGUI_PrismDlg::SetEditCurrentArgument()
425 {
426   QPushButton* send = (QPushButton*)sender();
427   globalSelection( GEOM_ALLSHAPES );
428
429   if (send == GroupPoints->PushButton1) {
430     GroupPoints->LineEdit1->setFocus();
431     myEditCurrentArgument = GroupPoints->LineEdit1;
432   }
433   else if (send == GroupPoints->PushButton2) {
434     GroupPoints->LineEdit2->setFocus();
435     myEditCurrentArgument = GroupPoints->LineEdit2;
436     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
437   }
438   else if (send == GroupPoints2->PushButton1) {
439     GroupPoints2->LineEdit1->setFocus();
440     myEditCurrentArgument = GroupPoints2->LineEdit1;
441   }
442   else if (send == GroupPoints2->PushButton2) {
443     GroupPoints2->LineEdit2->setFocus();
444     myEditCurrentArgument = GroupPoints2->LineEdit2;
445     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
446   }
447   else if (send == GroupPoints2->PushButton3) {
448     GroupPoints2->LineEdit3->setFocus();
449     myEditCurrentArgument = GroupPoints2->LineEdit3;
450     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
451   }
452
453   myEditCurrentArgument->setFocus();
454   SelectionIntoArgument();
455 }
456
457
458 //=================================================================================
459 // function : ActivateThisDialog()
460 // purpose  :
461 //=================================================================================
462 void GenerationGUI_PrismDlg::ActivateThisDialog()
463 {
464   GEOMBase_Skeleton::ActivateThisDialog();
465
466   connect(myGeomGUI->getApp()->selectionMgr(),
467           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
468
469   ConstructorsClicked( getConstructorId() );
470 }
471
472
473 //=================================================================================
474 // function : enterEvent()
475 // purpose  : when mouse enter onto the QWidget
476 //=================================================================================
477 void GenerationGUI_PrismDlg::enterEvent(QEvent * e)
478 {
479   if (!GroupConstructors->isEnabled())
480     ActivateThisDialog();
481 }
482
483
484 //=================================================================================
485 // function : ValueChangedInSpinBox()
486 // purpose  :
487 //=================================================================================
488 void GenerationGUI_PrismDlg::ValueChangedInSpinBox()
489 {
490   displayPreview();
491 }
492
493
494 //=================================================================================
495 // function : getHeight()
496 // purpose  :
497 //=================================================================================
498 double GenerationGUI_PrismDlg::getHeight() const
499 {
500     return GroupPoints->SpinBox_DX->GetValue();
501 }
502
503 //=================================================================================
504 // function : createOperation
505 // purpose  :
506 //=================================================================================
507 GEOM::GEOM_IOperations_ptr GenerationGUI_PrismDlg::createOperation()
508 {
509   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
510 }
511
512 //=================================================================================
513 // function : isValid
514 // purpose  :
515 //=================================================================================
516 bool GenerationGUI_PrismDlg::isValid( QString& )
517 {
518   if (getConstructorId() == 0)
519     return (myOkBase && myOkVec);     // by vector and height
520   else
521     return (myOkBase && myOkPnt1 && myOkPnt2);   // by two points
522 }
523
524 //=================================================================================
525 // function : execute
526 // purpose  :
527 //=================================================================================
528 bool GenerationGUI_PrismDlg::execute( ObjectList& objects )
529 {
530   GEOM::GEOM_Object_var anObj;
531
532   switch ( getConstructorId() )
533   {
534   case 0:
535     {
536       if (!myBothway)
537         anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
538           MakePrismVecH(myBase, myVec, getHeight());
539       else
540         anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
541         MakePrismVecH2Ways(myBase, myVec, getHeight() );
542       break;
543     }
544   case 1:
545     {
546       if (!myBothway2)
547         anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
548         MakePrismTwoPnt(myBase, myPoint1, myPoint2);
549       else
550         anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
551         MakePrismTwoPnt2Ways(myBase, myPoint1, myPoint2);
552       break;
553     }
554   }
555   if ( !anObj->_is_nil() )
556     objects.push_back( anObj._retn() );
557
558   return true;
559 }
560
561
562 //=================================================================================
563 // function :  onReverse()
564 // purpose  :
565 //=================================================================================
566 void GenerationGUI_PrismDlg::onReverse()
567 {
568   double anOldValue = GroupPoints->SpinBox_DX->GetValue();
569   GroupPoints->SpinBox_DX->SetValue( -anOldValue );
570 }
571
572 //=================================================================================
573 // function :  onBothway()
574 // purpose  :  
575 //=================================================================================
576 void GenerationGUI_PrismDlg::onBothway()
577 {
578   bool anOldValue = myBothway;
579   myBothway = !anOldValue;
580   GroupPoints->CheckButton2->setEnabled(!myBothway);
581   displayPreview();
582 }
583
584 void GenerationGUI_PrismDlg::onBothway2()
585 {
586   bool anOldValue = myBothway2;
587   myBothway2 = !anOldValue;
588   displayPreview();
589 }