Salome HOME
5b9d8126f56b8c087e65d4a8800b00491fac0bea
[modules/geom.git] / src / TransformationGUI / TransformationGUI_MirrorDlg.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   : TransformationGUI_MirrorDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "TransformationGUI_MirrorDlg.h"
30
31 #include <BRepBuilderAPI_Transform.hxx>
32 #include <Geom_Plane.hxx>
33 #include <BRep_Tool.hxx>
34
35 #include "utilities.h"
36
37 using namespace std;
38
39 //=================================================================================
40 // class    : TransformationGUI_MirrorDlg()
41 // purpose  : Constructs a TransformationGUI_MirrorDlg which is a child of 'parent', with the 
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 TransformationGUI_MirrorDlg::TransformationGUI_MirrorDlg(QWidget* parent,  const char* name, TransformationGUI* theTransformationGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
47     :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
48 {
49   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_MIRROR")));
50   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
51
52   setCaption(tr("GEOM_MIRROR_TITLE"));
53
54   /***************************************************************/
55   GroupConstructors->setTitle(tr("GEOM_MIRROR"));
56   RadioButton1->setPixmap(image0);
57   RadioButton2->close(TRUE);
58   RadioButton3->close(TRUE);
59
60   GroupPoints = new DlgRef_2Sel_QTD(this, "GroupPoints");
61   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
62   GroupPoints->TextLabel1->setText(tr("GEOM_OBJECT"));
63   GroupPoints->TextLabel2->setText(tr("GEOM_PLANE_MIRROR"));
64   GroupPoints->PushButton1->setPixmap(image1);
65   GroupPoints->PushButton2->setPixmap(image1);
66
67   Layout1->addWidget(GroupPoints, 1, 0);
68   /***************************************************************/
69
70   /* Initialisations */
71   myTransformationGUI = theTransformationGUI;
72   Init();
73 }
74
75
76 //=================================================================================
77 // function : ~TransformationGUI_MirrorDlg()
78 // purpose  : Destroys the object and frees any allocated resources
79 //=================================================================================
80 TransformationGUI_MirrorDlg::~TransformationGUI_MirrorDlg()
81 {  
82   /* no need to delete child widgets, Qt does it all for us */
83 }
84
85
86 //=================================================================================
87 // function : Init()
88 // purpose  :
89 //=================================================================================
90 void TransformationGUI_MirrorDlg::Init()
91 {  
92   /* init variables */
93   myEditCurrentArgument = GroupPoints->LineEdit1;
94
95   myOkShape1 = myOkShape2 = false;
96
97   /*  Vertices Filter for all arguments */
98   myFaceFilter = new GEOM_FaceFilter(StdSelect_Plane, myGeom);
99
100   /* signals and slots connections */
101   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
102   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
103
104   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
105   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
106
107   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
108   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
109
110   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
111
112   /* displays Dialog */
113   GroupPoints->show();
114   this->show();
115
116   return;
117 }
118
119
120 //=================================================================================
121 // function : ClickOnOk()
122 // purpose  :
123 //=================================================================================
124 void TransformationGUI_MirrorDlg::ClickOnOk()
125 {
126   this->ClickOnApply();
127   ClickOnCancel();
128   return;
129 }
130
131 //=================================================================================
132 // function : ClickOnApply()
133 // purpose  :
134 //=================================================================================
135 void TransformationGUI_MirrorDlg::ClickOnApply()
136 {
137   buttonApply->setFocus();
138   QAD_Application::getDesktop()->putInfo(tr(""));
139   if (mySimulationTopoDs.IsNull())
140     return;
141   myGeomBase->EraseSimulationShape();
142   mySimulationTopoDs.Nullify();
143
144   if(myOkShape1 && myOkShape2)
145     myTransformationGUI->MakeMirrorAndDisplay(myGeomShape1, myGeomShape2);
146   return;
147 }
148
149
150 //=================================================================================
151 // function : SelectionIntoArgument()
152 // purpose  : Called when selection has changed
153 //=================================================================================
154 void TransformationGUI_MirrorDlg::SelectionIntoArgument()
155 {
156   myGeomBase->EraseSimulationShape();
157   mySimulationTopoDs.Nullify();
158   myEditCurrentArgument->setText("");
159   QString aString = ""; /* name of selection */
160
161   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
162   if(nbSel != 1) {
163     if(myEditCurrentArgument == GroupPoints->LineEdit1)
164       myOkShape1 = false;
165     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
166       myOkShape2 = false;
167     return;
168   }
169
170   // nbSel == 1
171   TopoDS_Shape S;
172   Standard_Boolean testResult;
173   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
174   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
175     return;
176   
177   if(myEditCurrentArgument == GroupPoints->LineEdit1) {
178     myGeomShape1 = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
179     if(!testResult)
180       return;
181     myShape1 = S;
182     myEditCurrentArgument->setText(aString);
183     myOkShape1 = true;
184   }
185   else if(myEditCurrentArgument == GroupPoints->LineEdit2) {
186     myGeomShape2 = myGeomBase->ConvertIOinGEOMShape(IO, testResult);
187     if(!testResult)
188       return ;
189     myShape2 = S;
190     myEditCurrentArgument->setText(aString);
191     myOkShape2 = true;
192   }
193
194   if(myOkShape1 && myOkShape2)
195     MakeMirrorSimulationAndDisplay();
196   return; 
197 }
198
199
200 //=================================================================================
201 // function : SetEditCurrentArgument()
202 // purpose  :
203 //=================================================================================
204 void TransformationGUI_MirrorDlg::SetEditCurrentArgument()
205 {
206   QPushButton* send = (QPushButton*)sender();
207   mySelection->ClearFilters();
208
209   if(send == GroupPoints->PushButton1) {
210     GroupPoints->LineEdit1->setFocus();
211     myEditCurrentArgument = GroupPoints->LineEdit1;
212   }
213   else if(send == GroupPoints->PushButton2) {
214     GroupPoints->LineEdit2->setFocus();
215     myEditCurrentArgument = GroupPoints->LineEdit2;
216     mySelection->AddFilter(myFaceFilter);
217   }
218   this->SelectionIntoArgument();
219
220   return;
221 }
222
223
224 //=================================================================================
225 // function : LineEditReturnPressed()
226 // purpose  :
227 //=================================================================================
228 void TransformationGUI_MirrorDlg::LineEditReturnPressed()
229 {
230   QLineEdit* send = (QLineEdit*)sender();
231   if(send == GroupPoints->LineEdit1)
232     myEditCurrentArgument = GroupPoints->LineEdit1;
233   else if(send == GroupPoints->LineEdit2)
234     myEditCurrentArgument = GroupPoints->LineEdit2;
235   else
236     return;
237
238   GEOMBase_Skeleton::LineEditReturnPressed();
239   return;
240 }
241
242
243 //=================================================================================
244 // function : enterEvent()
245 // purpose  : when mouse enter onto the QWidget
246 //=================================================================================
247 void TransformationGUI_MirrorDlg::enterEvent(QEvent * e)
248 {
249   if(GroupConstructors->isEnabled())
250     return;
251   this->ActivateThisDialog();
252   return;
253 }
254
255
256 //=================================================================================
257 // function : ActivateThisDialog()
258 // purpose  :
259 //=================================================================================
260 void TransformationGUI_MirrorDlg::ActivateThisDialog()
261 {
262   GEOMBase_Skeleton::ActivateThisDialog();
263   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
264   GroupPoints->LineEdit1->setFocus();
265   myEditCurrentArgument = GroupPoints->LineEdit1;
266   if(!mySimulationTopoDs.IsNull())
267     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
268   return;
269 }
270
271
272 //=================================================================================
273 // function : MakeMirrorSimulationAndDisplay()
274 // purpose  : S1 is a shape and S2 a mirror.
275 //=================================================================================
276 void TransformationGUI_MirrorDlg::MakeMirrorSimulationAndDisplay()
277 {
278   myGeomBase->EraseSimulationShape();
279   mySimulationTopoDs.Nullify();
280   
281   try {
282     Handle(Geom_Surface) surf = BRep_Tool::Surface(TopoDS::Face(myShape2));
283     Handle(Geom_Plane) myPlane = Handle(Geom_Plane)::DownCast(surf);
284     const gp_Ax3 pos = myPlane->Position();
285     const gp_Pnt loc = pos.Location();  /* location of the plane */
286     const gp_Dir dir = pos.Direction(); /* Main direction of the plane (Z axis) */
287     
288     /* plane used for mirroring */
289     gp_Ax2 pln(loc, dir);
290     gp_Trsf theTransformation;
291     theTransformation.SetMirror(pln);
292     BRepBuilderAPI_Transform myBRepTransformation(myShape1, theTransformation, Standard_False);
293     
294     this->mySimulationTopoDs = myBRepTransformation.Shape();
295     if(mySimulationTopoDs.IsNull())
296       return;
297     else
298       myGeomBase->DisplaySimulationShape(mySimulationTopoDs); 
299   }
300   catch(Standard_Failure) {
301     MESSAGE("Exception catched in MakeMirrorSimulationAndDisplay");
302     return;
303   }
304   return;
305 }