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