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