Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/geom.git] / src / BasicGUI / BasicGUI_ArcDlg.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_ArcDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "BasicGUI_ArcDlg.h"
30
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <GC_MakeArcOfCircle.hxx>
33 #include <Geom_TrimmedCurve.hxx>
34 #include <Precision.hxx>
35
36 #include "utilities.h"
37
38 using namespace std;
39
40 //=================================================================================
41 // class    : BasicGUI_ArcDlg()
42 // purpose  : Constructs a BasicGUI_ArcDlg which is a child of 'parent', with the 
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 BasicGUI_ArcDlg::BasicGUI_ArcDlg(QWidget* parent, const char* name, BasicGUI* theBasicGUI, SALOME_Selection* Sel, bool modal, WFlags fl)
48   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
49 {
50   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_ARC")));
51   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
52
53   setCaption(tr("GEOM_ARC_TITLE"));
54
55   /***************************************************************/
56   GroupConstructors->setTitle(tr("GEOM_ARC"));
57   RadioButton1->setPixmap(image0);
58   RadioButton2->close(TRUE);
59   RadioButton3->close(TRUE);
60
61   GroupPoints = new DlgRef_3Sel_QTD(this, "GroupPoints");
62   GroupPoints->GroupBox1->setTitle(tr("GEOM_POINTS"));
63   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg("1"));
64   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg("2"));
65   GroupPoints->TextLabel3->setText(tr("GEOM_POINT_I").arg("3"));
66   GroupPoints->PushButton1->setPixmap(image1);
67   GroupPoints->PushButton2->setPixmap(image1);
68   GroupPoints->PushButton3->setPixmap(image1);
69
70   Layout1->addWidget(GroupPoints, 1, 0);
71   /***************************************************************/
72
73   /* Initialisations */
74   myBasicGUI = theBasicGUI;
75   Init();
76 }
77
78
79 //=================================================================================
80 // function : ~BasicGUI_ArcDlg()
81 // purpose  : Destroys the object and frees any allocated resources
82 //=================================================================================
83 BasicGUI_ArcDlg::~BasicGUI_ArcDlg()
84 {
85     // no need to delete child widgets, Qt does it all for us
86 }
87
88
89 //=================================================================================
90 // function : Init()
91 // purpose  :
92 //=================================================================================
93 void BasicGUI_ArcDlg::Init()
94 {
95   /* init variables */
96   myEditCurrentArgument = GroupPoints->LineEdit1;
97   myOkPoint1 = myOkPoint2 = myOkPoint3 = false;
98
99   /*  Vertices Filter for all arguments */
100   myVertexFilter = new GEOM_ShapeTypeFilter(TopAbs_VERTEX, myGeom);
101   mySelection->AddFilter(myVertexFilter);
102
103   /* signals and slots connections */
104   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
105   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
106
107   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
108   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
109   connect(GroupPoints->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
110
111   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
112   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
113   connect(GroupPoints->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
114
115   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
116
117   /* displays Dialog */
118   GroupPoints->show();
119   this->show();
120
121   return;
122 }
123
124
125 //=================================================================================
126 // function : ClickOnOk()
127 // purpose  :
128 //=================================================================================
129 void BasicGUI_ArcDlg::ClickOnOk()
130 {
131   this->ClickOnApply();
132   ClickOnCancel();
133   return;
134 }
135
136
137 //=================================================================================
138 // function : ClickOnApply()
139 // purpose  :
140 //=================================================================================
141 void BasicGUI_ArcDlg::ClickOnApply()
142 {
143   buttonApply->setFocus();
144   QAD_Application::getDesktop()->putInfo(tr(""));
145   if (mySimulationTopoDs.IsNull())
146     return;
147   myGeomBase->EraseSimulationShape();
148   mySimulationTopoDs.Nullify();
149
150   if(myOkPoint1 && myOkPoint2 && myOkPoint3) 
151     myBasicGUI->MakeArcAndDisplay(myPoint1, myPoint2, myPoint3);
152   return;
153 }
154
155
156 //=================================================================================
157 // function : SelectionIntoArgument()
158 // purpose  : Called when selection as changed or other case
159 //=================================================================================
160 void BasicGUI_ArcDlg::SelectionIntoArgument()
161 {
162   myGeomBase->EraseSimulationShape();
163   mySimulationTopoDs.Nullify();
164   myEditCurrentArgument->setText("");
165   QString aString = ""; /* name of selection */
166
167   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
168   if (nbSel != 1) {
169     if (myEditCurrentArgument == GroupPoints->LineEdit1)
170       myOkPoint1 = false;
171     else if(myEditCurrentArgument == GroupPoints->LineEdit2)
172       myOkPoint2 = false;
173     else if(myEditCurrentArgument == GroupPoints->LineEdit3)
174       myOkPoint3 = false;
175     return;
176   }
177
178   // nbSel == 1
179   TopoDS_Shape S; 
180   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
181     return;
182   
183   if(myEditCurrentArgument == GroupPoints->LineEdit1 && myGeomBase->VertexToPoint(S, myPoint1)) {
184     myEditCurrentArgument->setText(aString);
185     myOkPoint1 = true;
186   }
187   else if(myEditCurrentArgument == GroupPoints->LineEdit2 && myGeomBase->VertexToPoint(S, myPoint2)) {
188     myEditCurrentArgument->setText(aString);
189     myOkPoint2 = true;
190   }
191   else if(myEditCurrentArgument == GroupPoints->LineEdit3 && myGeomBase->VertexToPoint(S, myPoint3)) {
192     myEditCurrentArgument->setText(aString);
193     myOkPoint3 = true;
194   }
195
196   if(myOkPoint1 && myOkPoint2 && myOkPoint3)
197     this->MakeArcSimulationAndDisplay();
198   return;
199 }
200
201
202 //=================================================================================
203 // function : SetEditCurrentArgument()
204 // purpose  :
205 //=================================================================================
206 void BasicGUI_ArcDlg::SetEditCurrentArgument()
207 {
208   QPushButton* send = (QPushButton*)sender();
209   mySelection->ClearFilters();
210
211   if(send == GroupPoints->PushButton1) {
212     GroupPoints->LineEdit1->setFocus();
213     myEditCurrentArgument = GroupPoints->LineEdit1;
214   }
215   else if(send == GroupPoints->PushButton2) {
216     GroupPoints->LineEdit2->setFocus();
217     myEditCurrentArgument = GroupPoints->LineEdit2;
218   }
219   else if(send == GroupPoints->PushButton3) {
220     GroupPoints->LineEdit3->setFocus();
221     myEditCurrentArgument = GroupPoints->LineEdit3;
222   }
223   mySelection->AddFilter(myVertexFilter);
224   this->SelectionIntoArgument();
225
226   return;
227 }
228
229
230 //=================================================================================
231 // function : LineEditReturnPressed()
232 // purpose  :
233 //=================================================================================
234 void BasicGUI_ArcDlg::LineEditReturnPressed()
235
236   QLineEdit* send = (QLineEdit*)sender();
237   if(send == GroupPoints->LineEdit1)
238     myEditCurrentArgument = GroupPoints->LineEdit1;
239   else if(send == GroupPoints->LineEdit2)
240     myEditCurrentArgument = GroupPoints->LineEdit2;
241   else if(send == GroupPoints->LineEdit3)
242     myEditCurrentArgument = GroupPoints->LineEdit3;
243   else
244     return;
245
246   GEOMBase_Skeleton::LineEditReturnPressed();
247   return;
248 }
249
250
251 //=================================================================================
252 // function : ActivateThisDialog()
253 // purpose  :
254 //=================================================================================
255 void BasicGUI_ArcDlg::ActivateThisDialog()
256 {
257   GEOMBase_Skeleton::ActivateThisDialog();
258   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
259   mySelection->AddFilter(myVertexFilter);
260   if(!mySimulationTopoDs.IsNull())
261     myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
262   return;
263 }
264
265
266 //=================================================================================
267 // function : enterEvent()
268 // purpose  :
269 //=================================================================================
270 void BasicGUI_ArcDlg::enterEvent(QEvent* e)
271 {
272   if(GroupConstructors->isEnabled())
273     return;
274   this->ActivateThisDialog();
275   return;
276 }
277
278
279 //=================================================================================
280 // function : MakeArcSimulationAndDisplay()
281 // purpose  :
282 //=================================================================================
283 void BasicGUI_ArcDlg::MakeArcSimulationAndDisplay() 
284 {
285   myGeomBase->EraseSimulationShape();
286   mySimulationTopoDs.Nullify();
287
288   try {
289     if(myPoint2.IsEqual(myPoint1, Precision::Confusion()) || 
290        myPoint2.IsEqual(myPoint3, Precision::Confusion())) {
291       myEditCurrentArgument->setText("");
292       return;
293     }
294
295     gp_Vec v1(myPoint2, myPoint1);
296     gp_Vec v2(myPoint2, myPoint3);
297     if(v1.IsParallel(v2, Precision::Angular())) {
298       myEditCurrentArgument->setText("");
299       return;
300     }
301     
302     GC_MakeArcOfCircle Arc(myPoint1, myPoint2, myPoint3);
303     if(Arc.IsDone()) {
304       Handle(Geom_TrimmedCurve) curve = Arc.Value();
305       mySimulationTopoDs = BRepBuilderAPI_MakeEdge(curve).Shape();
306       myGeomBase->DisplaySimulationShape(mySimulationTopoDs);
307     }
308   }
309   catch(Standard_Failure) {
310     MESSAGE("Exception catched in MakeArcSimulationAndDisplay");
311   }
312   return;
313 }