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