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