Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / BasicGUI / BasicGUI_WorkingPlaneDlg.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   : BasicGUI_WorkingPlaneDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27
28 using namespace std;
29 #include "BasicGUI_WorkingPlaneDlg.h"
30
31 #include "GEOMBase.h"
32
33 #include "OCCViewer_Viewer3d.h"
34 #include "OCCViewer_ViewPort3d.h"
35 #include "QAD_StudyFrame.h"
36 #include "QAD_RightFrame.h"
37 #include "QAD_Desktop.h"
38
39 #include <Geom_Surface.hxx>
40 #include <Geom_Plane.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopExp.hxx>
46 #include <BRep_Tool.hxx>
47 #include <gp_Pnt.hxx>
48 #include <gp_Dir.hxx>
49 #include <gp_Pln.hxx>
50 #include <V3d_View.hxx>
51
52 #include "GEOMImpl_Types.hxx"
53
54 #include <qcheckbox.h>
55
56 //=================================================================================
57 // class    : BasicGUI_WorkingPlaneDlg()
58 // purpose  : Constructs a BasicGUI_WorkingPlaneDlg which is a child of 'parent', with the
59 //            name 'name' and widget flags set to 'f'.
60 //            The dialog will by default be modeless, unless you set 'modal' to
61 //            TRUE to construct a modal dialog.
62 //=================================================================================
63 BasicGUI_WorkingPlaneDlg::BasicGUI_WorkingPlaneDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
64   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
65 {
66   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
67   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_FACE")));
68   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_VECTOR")));
69   QPixmap image3(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_ORIGIN")));
70
71   setCaption(tr("GEOM_WPLANE_TITLE"));
72
73   /***************************************************************/
74   GroupConstructors->setTitle(tr("GEOM_WPLANE"));
75   RadioButton1->setPixmap(image1);
76   RadioButton2->setPixmap(image2);
77   RadioButton3->setPixmap(image3);
78
79   Group1 = new DlgRef_1Sel_QTD(this, "Group1");
80   Group1->GroupBox1->setTitle(tr("GEOM_WPLANE_FACE"));
81   Group1->TextLabel1->setText(tr("GEOM_SELECTION"));
82   Group1->PushButton1->setPixmap(image0);
83   Group1->LineEdit1->setReadOnly( true );
84
85   Group2 = new DlgRef_2Sel_QTD(this, "Group2");
86   Group2->GroupBox1->setTitle(tr("GEOM_WPLANE_VECTOR"));
87   Group2->TextLabel1->setText(tr("GEOM_WPLANE_VX"));
88   Group2->TextLabel2->setText(tr("GEOM_WPLANE_VZ"));
89   Group2->PushButton1->setPixmap(image0);
90   Group2->PushButton2->setPixmap(image0);
91   Group2->LineEdit1->setReadOnly( true );
92   Group2->LineEdit2->setReadOnly( true );
93
94   Group3 = new DlgRef_3Check_QTD(this, "Group3");
95   Group3->GroupBox1->setTitle(tr("GEOM_WPLANE_ORIGIN"));
96   Group3->RadioButton1->setText(tr("GEOM_WPLANE_OXY"));
97   Group3->RadioButton2->setText(tr("GEOM_WPLANE_OYZ"));
98   Group3->RadioButton3->setText(tr("GEOM_WPLANE_OZX"));
99
100   Layout1->addWidget(Group1, 1, 0);
101   Layout1->addWidget(Group2, 1, 0);
102   Layout1->addWidget(Group3, 1, 0);
103   /***************************************************************/
104
105   Init();
106 }
107
108 //=================================================================================
109 // function : ~BasicGUI_WorkingPlaneDlg()
110 // purpose  : Destroys the object and frees any allocated resources
111 //=================================================================================
112 BasicGUI_WorkingPlaneDlg::~BasicGUI_WorkingPlaneDlg()
113 {
114 }
115
116 //=================================================================================
117 // function : Init()
118 // purpose  :
119 //=================================================================================
120 void BasicGUI_WorkingPlaneDlg::Init()
121 {
122   /* init variables */
123   myEditCurrentArgument = Group1->LineEdit1;
124   myWPlane = GeometryGUI::GetGeomGUI()->GetWorkingPlane();
125
126   myGeomGUI->SetState( 0 );
127
128   myFace = GEOM::GEOM_Object::_nil();
129   myVectX = GEOM::GEOM_Object::_nil();
130   myVectZ = GEOM::GEOM_Object::_nil();
131
132   aOriginType = 1;
133
134   /* Filter definition */
135   globalSelection( GEOM_PLANE );
136
137    /* signals and slots connections */
138   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
139   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
140   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
141   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
142
143   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
144   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
145
146   connect(Group1->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
147   connect(Group1->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
148
149   connect(Group2->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
150   connect(Group2->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
151   connect(Group2->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
152   connect(Group2->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
153
154   connect(Group3->GroupBox1, SIGNAL(clicked(int)), this, SLOT(GroupClicked(int)));
155
156   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
157
158   initName( tr( "GEOM_WPLANE" ) );
159   ConstructorsClicked(0);
160 }
161
162 //=================================================================================
163 // function : ConstructorsClicked()
164 // purpose  : Radio button management
165 //=================================================================================
166 void BasicGUI_WorkingPlaneDlg::ConstructorsClicked(int constructorId)
167 {
168   disconnect(mySelection, 0, this, 0);
169   myGeomGUI->SetState( 0 );
170
171   switch (constructorId)
172     {
173     case 0:
174       {
175         globalSelection( GEOM_PLANE );
176
177         Group2->hide();
178         Group3->hide();
179         resize(0, 0);
180         Group1->show();
181
182         myEditCurrentArgument = Group1->LineEdit1;
183         Group1->LineEdit1->setText("");
184         myFace = GEOM::GEOM_Object::_nil();
185
186         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
187         break;
188       }
189     case 1:
190       {
191         globalSelection( GEOM_LINE );
192
193         Group1->hide();
194         Group3->hide();
195         resize(0, 0);
196         Group2->show();
197
198         myEditCurrentArgument = Group2->LineEdit1;
199         Group2->LineEdit1->setText("");
200          Group2->LineEdit2->setText("");
201         myVectX = GEOM::GEOM_Object::_nil();
202         myVectZ = GEOM::GEOM_Object::_nil();
203
204         connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
205         break;
206       }
207     case 2:
208       {
209         Group1->hide();
210         Group2->hide();
211         resize(0, 0);
212         Group3->show();
213
214         Group3->RadioButton1->setChecked(true);
215         aOriginType = 1;
216               break;
217       }
218     }
219   displayPreview();
220 }
221
222 //=================================================================================
223 // function : GroupClicked()
224 // purpose  : Radio button management
225 //=================================================================================
226 void BasicGUI_WorkingPlaneDlg::GroupClicked(int groupId)
227 {
228   aOriginType = groupId;
229 }
230
231 //=================================================================================
232 // function : ClickOnOk()
233 // purpose  :
234 //=================================================================================
235 void BasicGUI_WorkingPlaneDlg::ClickOnOk()
236 {
237   if ( ClickOnApply() )
238     ClickOnCancel();
239 }
240
241 //=================================================================================
242 // function : ClickOnApply()
243 // purpose  :
244 //=================================================================================
245 bool BasicGUI_WorkingPlaneDlg::ClickOnApply()
246 {
247   buttonApply->setFocus();
248   QAD_Application::getDesktop()->putInfo(tr(""));
249   const int id = getConstructorId();
250
251   if (id == 0) {
252     if ( !CORBA::is_nil( myFace ) ) {
253       TopoDS_Face aPlaneShape;
254       if ( GEOMBase::GetShape( myFace, aPlaneShape, TopAbs_FACE ) ) {
255         Handle(Geom_Surface) aGS = BRep_Tool::Surface( TopoDS::Face( aPlaneShape ) );
256         if ( !aGS.IsNull() && aGS->IsKind( STANDARD_TYPE( Geom_Plane ) ) ) {
257           Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast( aGS );
258           gp_Pln aPln = aGPlane->Pln();
259
260           myWPlane = aPln.Position();
261           GeometryGUI::GetGeomGUI()->SetWorkingPlane(myWPlane);
262           GeometryGUI::GetGeomGUI()->ActiveWorkingPlane();
263           return true;
264         }
265       }
266     }
267   } else if (id == 1) {
268     if ( CORBA::is_nil( myVectX ) || CORBA::is_nil( myVectZ ) ) {
269       showError( "Two vectors have to be selected" );
270       return false;
271     }
272
273     TopoDS_Edge aVectX, aVectZ;
274     TopoDS_Vertex V1, V2;
275     gp_Vec aVX, aVZ;
276     if (GEOMBase::GetShape( myVectX, aVectX, TopAbs_EDGE ) &&
277         GEOMBase::GetShape( myVectZ, aVectZ, TopAbs_EDGE )) {
278       TopExp::Vertices(aVectZ, V1, V2, Standard_True);
279       if (!V1.IsNull() && !V2.IsNull())
280         aVZ = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
281       else {
282         showError( "Bad OZ vector" );
283         return false;
284       }
285
286       TopExp::Vertices(aVectX, V1, V2, Standard_True);
287       if (!V1.IsNull() && !V2.IsNull())
288         aVX = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
289       else {
290         showError( "Bad OX vector" );
291         return false;
292       }
293
294       gp_Dir aDirZ = gp_Dir(aVZ.X(), aVZ.Y(), aVZ.Z());
295       gp_Dir aDirX = gp_Dir(aVX.X(), aVX.Y(), aVX.Z());
296
297       if (aDirX.IsParallel(aDirZ, Precision::Confusion())) {
298         showError( "Parallel vectors selected" );
299         return false;
300       }
301
302       myWPlane = gp_Ax3(BRep_Tool::Pnt(V1), aDirZ, aDirX);
303
304       GeometryGUI::GetGeomGUI()->SetWorkingPlane(myWPlane);
305       GeometryGUI::GetGeomGUI()->ActiveWorkingPlane();
306       return true;
307     }
308   } else if (id == 2) {
309     gp_Pnt P1 = gp_Pnt(0., 0., 0.);
310     gp_Dir aDirZ, aDirX;
311
312     if (aOriginType == 1) {
313       aDirZ = gp_Dir(0., 0., 1.);
314       aDirX = gp_Dir(1., 0., 0.);
315     }
316     else if (aOriginType == 2) {
317       aDirZ = gp_Dir(1., 0., 0.);
318       aDirX = gp_Dir(0., 1., 0.);
319     }
320     else if (aOriginType == 0) {
321       aDirZ = gp_Dir(0., 1., 0.);
322       aDirX = gp_Dir(0., 0., 1.);
323     }
324
325     myWPlane = gp_Ax3(P1, aDirZ, aDirX);
326
327     GeometryGUI::GetGeomGUI()->SetWorkingPlane(myWPlane);
328     GeometryGUI::GetGeomGUI()->ActiveWorkingPlane();
329     return true;
330   }
331   return false;
332 }
333
334 //=================================================================================
335 // function : SelectionIntoArgument()
336 // purpose  : Called when selection as changed or other case
337 //=================================================================================
338 void BasicGUI_WorkingPlaneDlg::SelectionIntoArgument()
339 {
340   myEditCurrentArgument->setText("");
341
342   const int id = getConstructorId();
343   if ( mySelection->IObjectCount() != 1 ) {
344     if(id == 0)
345       myFace = GEOM::GEOM_Object::_nil();
346     else if(id == 1) {
347       if (myEditCurrentArgument == Group2->LineEdit1)
348         myVectX = GEOM::GEOM_Object::_nil();
349       else if (myEditCurrentArgument == Group2->LineEdit2)
350         myVectZ = GEOM::GEOM_Object::_nil();
351     }
352     return;
353   }
354
355   // nbSel == 1
356   Standard_Boolean aRes = Standard_False;
357   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), aRes);
358
359   if(!aRes || CORBA::is_nil( aSelectedObject ))
360     return;
361
362   if(myEditCurrentArgument == Group1->LineEdit1)
363     myFace = aSelectedObject;
364   else if(myEditCurrentArgument == Group2->LineEdit1)
365     myVectX = aSelectedObject;
366   else if(myEditCurrentArgument == Group2->LineEdit2)
367     myVectZ = aSelectedObject;
368
369   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
370 }
371
372
373 //=================================================================================
374 // function : SetEditCurrentArgument()
375 // purpose  :
376 //=================================================================================
377 void BasicGUI_WorkingPlaneDlg::SetEditCurrentArgument()
378 {
379   QPushButton* send = (QPushButton*)sender();
380
381   if(send == Group1->PushButton1) {
382     myEditCurrentArgument = Group1->LineEdit1;
383     globalSelection( GEOM_PLANE );
384   }
385   else if(send == Group2->PushButton1) {
386     myEditCurrentArgument = Group2->LineEdit1;
387     globalSelection( GEOM_LINE );
388   }
389   else if(send == Group2->PushButton2) {
390     myEditCurrentArgument = Group2->LineEdit2;
391     globalSelection( GEOM_LINE );
392   }
393
394   myEditCurrentArgument->setFocus();
395   SelectionIntoArgument();
396 }
397
398
399 //=================================================================================
400 // function : LineEditReturnPressed()
401 // purpose  :
402 //=================================================================================
403 void BasicGUI_WorkingPlaneDlg::LineEditReturnPressed()
404 {
405   QLineEdit* send = (QLineEdit*)sender();
406   if(send == Group1->LineEdit1 || send == Group2->LineEdit1 || send == Group2->LineEdit2) {
407     myEditCurrentArgument = send;
408     GEOMBase_Skeleton::LineEditReturnPressed();
409   }
410 }
411
412
413 //=================================================================================
414 // function : ActivateThisDialog()
415 // purpose  :
416 //=================================================================================
417 void BasicGUI_WorkingPlaneDlg::ActivateThisDialog( )
418 {
419   GEOMBase_Skeleton::ActivateThisDialog();
420   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
421
422   ConstructorsClicked( getConstructorId() );
423 }
424
425
426 //=================================================================================
427 // function : DeactivateActiveDialog()
428 // purpose  : public slot to deactivate if active
429 //=================================================================================
430 void BasicGUI_WorkingPlaneDlg::DeactivateActiveDialog()
431 {
432   myGeomGUI->SetState( -1 );
433   GEOMBase_Skeleton::DeactivateActiveDialog();
434 }
435
436 //=======================================================================
437 // function : ClickOnCancel()
438 // purpose  :
439 //=======================================================================
440 void BasicGUI_WorkingPlaneDlg::ClickOnCancel()
441 {
442   GEOMBase_Skeleton::ClickOnCancel();
443 }
444
445 //=================================================================================
446 // function : enterEvent()
447 // purpose  :
448 //=================================================================================
449 void BasicGUI_WorkingPlaneDlg::enterEvent(QEvent* e)
450 {
451   if ( !GroupConstructors->isEnabled() )
452     ActivateThisDialog();
453 }
454
455 //=================================================================================
456 // function : closeEvent
457 // purpose  :
458 //=================================================================================
459 void BasicGUI_WorkingPlaneDlg::closeEvent( QCloseEvent* e )
460 {
461   myGeomGUI->SetState( -1 );
462   GEOMBase_Skeleton::closeEvent( e );
463 }