Salome HOME
SMH: Merged GEOM (NEWGUI, HEAD, POLYWORK)
[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 "SUIT_Desktop.h"
34 #include "SUIT_Session.h"
35 #include "SalomeApp_Application.h"
36 #include "SalomeApp_SelectionMgr.h"
37
38 #include <Geom_Surface.hxx>
39 #include <Geom_Plane.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopExp.hxx>
45 #include <BRep_Tool.hxx>
46 #include <gp_Pnt.hxx>
47 #include <gp_Dir.hxx>
48 #include <gp_Pln.hxx>
49 #include <V3d_View.hxx>
50
51 #include "GEOMImpl_Types.hxx"
52
53 #include <qcheckbox.h>
54 #include <qlabel.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(GeometryGUI* theGeometryGUI , QWidget* parent, const char* name, bool modal, WFlags fl)
64   :GEOMBase_Skeleton(parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu), myGeometryGUI(theGeometryGUI)
65 {
66   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
67   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_FACE")));
68   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_WPLANE_VECTOR")));
69   QPixmap image3(SUIT_Session::session()->resourceMgr()->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 = myGeometryGUI->GetWorkingPlane();
125
126   // myGeometryGUI->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(myGeometryGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
140   connect(myGeometryGUI, 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(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
157           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
158
159   initName( tr( "GEOM_WPLANE" ) );
160   ConstructorsClicked(0);
161 }
162
163 //=================================================================================
164 // function : ConstructorsClicked()
165 // purpose  : Radio button management
166 //=================================================================================
167 void BasicGUI_WorkingPlaneDlg::ConstructorsClicked(int constructorId)
168 {
169   disconnect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 0, this, 0);
170   // myGeometryGUI->SetState( 0 );
171
172   switch (constructorId)
173     {
174     case 0:
175       {
176         globalSelection( GEOM_PLANE );
177
178         Group2->hide();
179         Group3->hide();
180         resize(0, 0);
181         Group1->show();
182
183         myEditCurrentArgument = Group1->LineEdit1;
184         Group1->LineEdit1->setText("");
185         myFace = GEOM::GEOM_Object::_nil();
186
187         connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
188                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
189         break;
190       }
191     case 1:
192       {
193         globalSelection( GEOM_LINE );
194
195         Group1->hide();
196         Group3->hide();
197         resize(0, 0);
198         Group2->show();
199
200         myEditCurrentArgument = Group2->LineEdit1;
201         Group2->LineEdit1->setText("");
202          Group2->LineEdit2->setText("");
203         myVectX = GEOM::GEOM_Object::_nil();
204         myVectZ = GEOM::GEOM_Object::_nil();
205
206         connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
207                 SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
208         break;
209       }
210     case 2:
211       {
212         Group1->hide();
213         Group2->hide();
214         resize(0, 0);
215         Group3->show();
216
217         Group3->RadioButton1->setChecked(true);
218         aOriginType = 1;
219               break;
220       }
221     }
222   displayPreview();
223 }
224
225 //=================================================================================
226 // function : GroupClicked()
227 // purpose  : Radio button management
228 //=================================================================================
229 void BasicGUI_WorkingPlaneDlg::GroupClicked(int groupId)
230 {
231   aOriginType = groupId;
232 }
233
234 //=================================================================================
235 // function : ClickOnOk()
236 // purpose  :
237 //=================================================================================
238 void BasicGUI_WorkingPlaneDlg::ClickOnOk()
239 {
240   if ( ClickOnApply() )
241     ClickOnCancel();
242 }
243
244 //=================================================================================
245 // function : ClickOnApply()
246 // purpose  :
247 //=================================================================================
248 bool BasicGUI_WorkingPlaneDlg::ClickOnApply()
249 {
250   buttonApply->setFocus();
251   myGeometryGUI->application()->putInfo(tr(""));
252   const int id = getConstructorId();
253
254   if (id == 0) {
255     if ( !CORBA::is_nil( myFace ) ) {
256       TopoDS_Face aPlaneShape;
257       if ( GEOMBase::GetShape( myFace, aPlaneShape, TopAbs_FACE ) ) {
258         Handle(Geom_Surface) aGS = BRep_Tool::Surface( TopoDS::Face( aPlaneShape ) );
259         if ( !aGS.IsNull() && aGS->IsKind( STANDARD_TYPE( Geom_Plane ) ) ) {
260           Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast( aGS );
261           gp_Pln aPln = aGPlane->Pln();
262
263           myWPlane = aPln.Position();
264           myGeometryGUI->SetWorkingPlane(myWPlane);
265           myGeometryGUI->ActiveWorkingPlane();
266           return true;
267         }
268       }
269     }
270   } else if (id == 1) {
271     if ( CORBA::is_nil( myVectX ) || CORBA::is_nil( myVectZ ) ) {
272       showError( "Two vectors have to be selected" );
273       return false;
274     }
275
276     TopoDS_Edge aVectX, aVectZ;
277     TopoDS_Vertex V1, V2;
278     gp_Vec aVX, aVZ;
279     if (GEOMBase::GetShape( myVectX, aVectX, TopAbs_EDGE ) &&
280         GEOMBase::GetShape( myVectZ, aVectZ, TopAbs_EDGE )) {
281       TopExp::Vertices(aVectZ, V1, V2, Standard_True);
282       if (!V1.IsNull() && !V2.IsNull())
283         aVZ = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
284       else {
285         showError( "Bad OZ vector" );
286         return false;
287       }
288
289       TopExp::Vertices(aVectX, V1, V2, Standard_True);
290       if (!V1.IsNull() && !V2.IsNull())
291         aVX = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
292       else {
293         showError( "Bad OX vector" );
294         return false;
295       }
296
297       gp_Dir aDirZ = gp_Dir(aVZ.X(), aVZ.Y(), aVZ.Z());
298       gp_Dir aDirX = gp_Dir(aVX.X(), aVX.Y(), aVX.Z());
299
300       if (aDirX.IsParallel(aDirZ, Precision::Confusion())) {
301         showError( "Parallel vectors selected" );
302         return false;
303       }
304
305       myWPlane = gp_Ax3(BRep_Tool::Pnt(V1), aDirZ, aDirX);
306
307       myGeometryGUI->SetWorkingPlane(myWPlane);
308       myGeometryGUI->ActiveWorkingPlane();
309       return true;
310     }
311   } else if (id == 2) {
312     gp_Pnt P1 = gp_Pnt(0., 0., 0.);
313     gp_Dir aDirZ, aDirX;
314
315     if (aOriginType == 1) {
316       aDirZ = gp_Dir(0., 0., 1.);
317       aDirX = gp_Dir(1., 0., 0.);
318     }
319     else if (aOriginType == 2) {
320       aDirZ = gp_Dir(1., 0., 0.);
321       aDirX = gp_Dir(0., 1., 0.);
322     }
323     else if (aOriginType == 0) {
324       aDirZ = gp_Dir(0., 1., 0.);
325       aDirX = gp_Dir(0., 0., 1.);
326     }
327
328     myWPlane = gp_Ax3(P1, aDirZ, aDirX);
329
330     myGeometryGUI->SetWorkingPlane(myWPlane);
331     myGeometryGUI->ActiveWorkingPlane();
332     return true;
333   }
334   return false;
335 }
336
337 //=================================================================================
338 // function : SelectionIntoArgument()
339 // purpose  : Called when selection as changed or other case
340 //=================================================================================
341 void BasicGUI_WorkingPlaneDlg::SelectionIntoArgument()
342 {
343   myEditCurrentArgument->setText("");
344
345   const int id = getConstructorId();
346   if ( IObjectCount() != 1 ) {
347     if(id == 0)
348       myFace = GEOM::GEOM_Object::_nil();
349     else if(id == 1) {
350       if (myEditCurrentArgument == Group2->LineEdit1)
351         myVectX = GEOM::GEOM_Object::_nil();
352       else if (myEditCurrentArgument == Group2->LineEdit2)
353         myVectZ = GEOM::GEOM_Object::_nil();
354     }
355     return;
356   }
357
358   // nbSel == 1
359   Standard_Boolean aRes = Standard_False;
360   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aRes);
361
362   if(!aRes || CORBA::is_nil( aSelectedObject ))
363     return;
364
365   if(myEditCurrentArgument == Group1->LineEdit1)
366     myFace = aSelectedObject;
367   else if(myEditCurrentArgument == Group2->LineEdit1)
368     myVectX = aSelectedObject;
369   else if(myEditCurrentArgument == Group2->LineEdit2)
370     myVectZ = aSelectedObject;
371
372   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
373 }
374
375
376 //=================================================================================
377 // function : SetEditCurrentArgument()
378 // purpose  :
379 //=================================================================================
380 void BasicGUI_WorkingPlaneDlg::SetEditCurrentArgument()
381 {
382   QPushButton* send = (QPushButton*)sender();
383
384   if(send == Group1->PushButton1) {
385     myEditCurrentArgument = Group1->LineEdit1;
386     globalSelection( GEOM_PLANE );
387   }
388   else if(send == Group2->PushButton1) {
389     myEditCurrentArgument = Group2->LineEdit1;
390     globalSelection( GEOM_LINE );
391   }
392   else if(send == Group2->PushButton2) {
393     myEditCurrentArgument = Group2->LineEdit2;
394     globalSelection( GEOM_LINE );
395   }
396
397   myEditCurrentArgument->setFocus();
398   SelectionIntoArgument();
399 }
400
401
402 //=================================================================================
403 // function : LineEditReturnPressed()
404 // purpose  :
405 //=================================================================================
406 void BasicGUI_WorkingPlaneDlg::LineEditReturnPressed()
407 {
408   QLineEdit* send = (QLineEdit*)sender();
409   if(send == Group1->LineEdit1 || send == Group2->LineEdit1 || send == Group2->LineEdit2) {
410     myEditCurrentArgument = send;
411     GEOMBase_Skeleton::LineEditReturnPressed();
412   }
413 }
414
415
416 //=================================================================================
417 // function : ActivateThisDialog()
418 // purpose  :
419 //=================================================================================
420 void BasicGUI_WorkingPlaneDlg::ActivateThisDialog( )
421 {
422   GEOMBase_Skeleton::ActivateThisDialog();
423   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
424           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
425
426   ConstructorsClicked( getConstructorId() );
427 }
428
429
430 //=================================================================================
431 // function : DeactivateActiveDialog()
432 // purpose  : public slot to deactivate if active
433 //=================================================================================
434 void BasicGUI_WorkingPlaneDlg::DeactivateActiveDialog()
435 {
436   // myGeometryGUI->SetState( -1 );
437   GEOMBase_Skeleton::DeactivateActiveDialog();
438 }
439
440 //=======================================================================
441 // function : ClickOnCancel()
442 // purpose  :
443 //=======================================================================
444 void BasicGUI_WorkingPlaneDlg::ClickOnCancel()
445 {
446   GEOMBase_Skeleton::ClickOnCancel();
447 }
448
449 //=================================================================================
450 // function : enterEvent()
451 // purpose  :
452 //=================================================================================
453 void BasicGUI_WorkingPlaneDlg::enterEvent(QEvent* e)
454 {
455   if ( !GroupConstructors->isEnabled() )
456     ActivateThisDialog();
457 }
458
459 //=================================================================================
460 // function : closeEvent
461 // purpose  :
462 //=================================================================================
463 void BasicGUI_WorkingPlaneDlg::closeEvent( QCloseEvent* e )
464 {
465   // myGeometryGUI->SetState( -1 );
466   GEOMBase_Skeleton::closeEvent( e );
467 }