Salome HOME
e560176dc627c1b0a3387fa3ab47027fa2e5ca2c
[modules/geom.git] / src / GenerationGUI / GenerationGUI_PipeDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : GenerationGUI_PipeDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "GenerationGUI_PipeDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TColStd_MapOfInteger.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : GenerationGUI_PipeDlg()
49 // purpose  : Constructs a GenerationGUI_PipeDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 GenerationGUI_PipeDlg::GenerationGUI_PipeDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
55                                               bool modal, Qt::WindowFlags fl)
56   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
57 {
58   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_PIPE")));
59   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
60   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_PIPE_BINORMAL")));
61
62   setWindowTitle(tr("GEOM_PIPE_TITLE"));
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle(tr("GEOM_PIPE"));
66   mainFrame()->RadioButton1->setIcon(image0);
67   mainFrame()->RadioButton2->setIcon(image2);
68   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
69   mainFrame()->RadioButton3->close();
70
71   GroupPoints = new DlgRef_3Sel1Check(centralWidget());
72
73   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
74   GroupPoints->TextLabel1->setText(tr("GEOM_BASE_OBJECT"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_PATH_OBJECT"));
76   GroupPoints->TextLabel3->setText(tr("GEOM_BINORMAL"));
77   GroupPoints->PushButton1->setIcon(image1);
78   GroupPoints->PushButton2->setIcon(image1);
79   GroupPoints->PushButton3->setIcon(image1);
80   GroupPoints->CheckButton1->setText(tr("GEOM_SELECT_UNPUBLISHED_EDGES"));
81
82   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
83   layout->setMargin(0); layout->setSpacing(6);
84   layout->addWidget(GroupPoints);
85   /***************************************************************/
86
87   setHelpFileName("create_extrusion_alongpath_page.html");
88
89   // Initialisation
90   Init();
91 }
92
93 //=================================================================================
94 // function : ~GenerationGUI_PipeDlg()
95 // purpose  : Destroys the object and frees any allocated resources
96 //=================================================================================
97 GenerationGUI_PipeDlg::~GenerationGUI_PipeDlg()
98 {
99   // no need to delete child widgets, Qt does it all for us
100 }
101
102 //=================================================================================
103 // function : Init()
104 // purpose  :
105 //=================================================================================
106 void GenerationGUI_PipeDlg::Init()
107 {
108   // init variables
109   GroupPoints->LineEdit1->setReadOnly(true);
110   GroupPoints->LineEdit2->setReadOnly(true);
111   GroupPoints->LineEdit3->setReadOnly(true);
112
113   GroupPoints->LineEdit1->setText("");
114   GroupPoints->LineEdit2->setText("");
115   GroupPoints->LineEdit3->setText("");
116   myBase = myPath = myVec = GEOM::GEOM_Object::_nil();
117   myOkBase = myOkPath = myOkVec = false;
118
119   GroupPoints->CheckButton1->setEnabled(false);
120
121   // signals and slots connections
122   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
123   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
124
125   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
126
127   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
128   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129   connect(GroupPoints->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
130
131   connect(GroupPoints->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
132   connect(GroupPoints->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133   connect(GroupPoints->LineEdit3,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
134
135   connect(GroupPoints->CheckButton1,   SIGNAL(toggled(bool)), this, SLOT(SelectionTypeButtonClicked()));
136
137   initName(tr("GEOM_PIPE"));
138
139   GroupPoints->TextLabel3->hide();
140   GroupPoints->PushButton3->hide();
141   GroupPoints->LineEdit3->hide();
142   ConstructorsClicked(0);
143
144   GroupPoints->PushButton1->click();
145   SelectionIntoArgument();
146 }
147
148 //=================================================================================
149 // function : ConstructorsClicked()
150 // purpose  : Radio button management
151 //=================================================================================
152 void GenerationGUI_PipeDlg::ConstructorsClicked( int constructorId )
153 {
154   erasePreview();
155
156   switch (constructorId)
157   {
158     case 0:
159     {
160       GroupPoints->TextLabel3->hide();
161       GroupPoints->PushButton3->hide();
162       GroupPoints->LineEdit3->hide();
163       break;
164     }
165     case 1:
166     {
167       GroupPoints->TextLabel3->show();
168       GroupPoints->PushButton3->show();
169       GroupPoints->LineEdit3->show();
170       break;
171     }
172   }
173
174   displayPreview();
175 }
176
177 //=================================================================================
178 // function : SelectionBittonClicked()
179 // purpose  : Selection type Radio button management
180 //=================================================================================
181 void GenerationGUI_PipeDlg::SelectionTypeButtonClicked()
182 {
183   globalSelection();
184   if ( GroupPoints->CheckButton1->isChecked() ) {
185     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
186   } else {
187     TColStd_MapOfInteger aMap;
188     aMap.Add(GEOM_WIRE);
189     aMap.Add(GEOM_LINE);
190     globalSelection(aMap);
191   }
192   if(myEditCurrentArgument == GroupPoints->LineEdit2) {
193     myEditCurrentArgument->setText("");
194     myOkPath = false;
195   }
196   displayPreview();
197 }
198
199 //=================================================================================
200 // function : ClickOnOk()
201 // purpose  :
202 //=================================================================================
203 void GenerationGUI_PipeDlg::ClickOnOk()
204 {
205   if (ClickOnApply())
206     ClickOnCancel();
207 }
208
209 //=================================================================================
210 // function : ClickOnApply()
211 // purpose  :
212 //=================================================================================
213 bool GenerationGUI_PipeDlg::ClickOnApply()
214 {
215   if (!onAccept())
216     return false;
217
218   initName();
219   if ( getConstructorId() != 1 )
220         ConstructorsClicked( getConstructorId() );
221   // activate selection and connect selection manager
222   GroupPoints->PushButton1->click();
223   return true;
224 }
225
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose  : Called when selection is changed or on dialog initialization or activation
229 //=================================================================================
230 void GenerationGUI_PipeDlg::SelectionIntoArgument()
231 {
232   erasePreview();
233   myEditCurrentArgument->setText("");
234   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myOkBase = false;
235   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myOkPath = false;
236   else if (myEditCurrentArgument == GroupPoints->LineEdit3) myOkVec  = false;
237
238   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
239   SALOME_ListIO aSelList;
240   aSelMgr->selectedObjects(aSelList);
241
242   if (aSelList.Extent() != 1)
243     return;
244
245   // nbSel == 1
246   Standard_Boolean testResult = Standard_False;
247   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
248
249   if (!testResult || aSelectedObject->_is_nil())
250     return;
251
252   TopoDS_Shape S;
253   if (!GEOMBase::GetShape(aSelectedObject, S))
254     return;
255
256   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
257     if (S.ShapeType() == TopAbs_COMPSOLID ||
258         S.ShapeType() == TopAbs_COMPOUND ||
259         S.ShapeType() == TopAbs_SOLID ||
260         S.ShapeType() == TopAbs_SHAPE)
261       return;
262     if ( getConstructorId() == 1 && 
263          (S.ShapeType() == TopAbs_SHELL || 
264           S.ShapeType() == TopAbs_VERTEX))
265       return;
266
267     myBase = aSelectedObject;
268     myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
269     myOkBase = true;
270     if (!myOkPath)
271       GroupPoints->PushButton2->click();
272     else if (!myOkVec)
273       GroupPoints->PushButton3->click();
274   }
275   else if (myEditCurrentArgument == GroupPoints->LineEdit2 || 
276            myEditCurrentArgument == GroupPoints->LineEdit3) {
277     myEditCurrentArgument == GroupPoints->LineEdit2 ? myOkPath = false : myOkVec = false;
278     bool myOk = false;
279     QString aName = GEOMBase::GetName(aSelectedObject);
280
281     if (myEditCurrentArgument == GroupPoints->LineEdit2 && !GroupPoints->CheckButton1->isChecked() ){
282       myPath = aSelectedObject;
283       myOkPath = true;
284       myEditCurrentArgument->setText(aName);
285     }
286     else 
287       { 
288         if (aSelectedObject != myBase) {
289           TColStd_IndexedMapOfInteger aMap;
290           aSelMgr->GetIndexes(aSelList.First(), aMap);
291           if (aMap.Extent() == 1) {
292             int anIndex = aMap(1);
293             aName.append(":edge_" + QString::number(anIndex));
294             
295             //Find SubShape Object in Father
296             GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
297             
298             if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
299               GEOM::GEOM_IShapesOperations_var aShapesOp =
300                 getGeomEngine()->GetIShapesOperations(getStudyId());
301               aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
302               myOk = true;
303             }
304             else { // get Object from study
305               aSelectedObject = aFindedObject;
306               myOk = true;
307             }
308           }
309           else {
310             myOk = true;
311             if (S.ShapeType() != TopAbs_EDGE) {
312               aSelectedObject = GEOM::GEOM_Object::_nil();
313               aName = "";
314               myOk = false;
315             }
316           }
317           if (myEditCurrentArgument == GroupPoints->LineEdit2) {
318             myPath = aSelectedObject;
319             myOkPath = myOk;
320           }
321           else if (myEditCurrentArgument == GroupPoints->LineEdit3) {
322             myVec = aSelectedObject;
323             myOkVec = myOk;
324           }
325         }
326         myEditCurrentArgument->setText(aName);
327
328         if (myOkPath) {
329           if (!myOkBase)
330             GroupPoints->PushButton1->click();
331           else if (!myOkVec)
332             GroupPoints->PushButton3->click();
333         }
334         else if (myOkVec) {
335           if (!myOkBase)
336             GroupPoints->PushButton1->click();
337           else if (!myOkPath)
338             GroupPoints->PushButton2->click();
339         }
340       }
341   }
342
343   // clear selection
344   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
345   myGeomGUI->getApp()->selectionMgr()->clearSelected();
346   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
347           this, SLOT(SelectionIntoArgument()));
348
349   displayPreview();
350 }
351
352 //=================================================================================
353 // function : SetEditCurrentArgument()
354 // purpose  :
355 //=================================================================================
356 void GenerationGUI_PipeDlg::SetEditCurrentArgument()
357 {
358   QPushButton* send = (QPushButton*)sender();
359
360   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
361   globalSelection(GEOM_ALLSHAPES);
362   GroupPoints->PushButton1->setDown(false);
363   GroupPoints->PushButton2->setDown(false);
364   GroupPoints->PushButton3->setDown(false);
365   GroupPoints->LineEdit1->setEnabled(false);
366   GroupPoints->LineEdit2->setEnabled(false);
367   GroupPoints->LineEdit3->setEnabled(false);
368   if (send == GroupPoints->PushButton1) {
369     myEditCurrentArgument = GroupPoints->LineEdit1;
370     GroupPoints->CheckButton1->setEnabled(false);
371   }
372   else if (send == GroupPoints->PushButton2) {
373     myEditCurrentArgument = GroupPoints->LineEdit2;
374
375     if ( GroupPoints->CheckButton1->isChecked() ) {
376       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
377     } else {
378       TColStd_MapOfInteger aMap;
379       aMap.Add(GEOM_WIRE);
380       aMap.Add(GEOM_LINE);
381       globalSelection(aMap);
382     }
383     GroupPoints->CheckButton1->setEnabled(true);
384   }
385   else if(send == GroupPoints->PushButton3) {
386     myEditCurrentArgument = GroupPoints->LineEdit3;
387     GroupPoints->CheckButton1->setEnabled(false);
388     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
389   }
390   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
391           this, SLOT(SelectionIntoArgument()));
392
393   // enable line edit
394   myEditCurrentArgument->setEnabled(true);
395   myEditCurrentArgument->setFocus();
396   // after setFocus(), because it will be setDown(false) when loses focus
397   send->setDown(true);
398
399   // seems we need it only to avoid preview disappearing, caused by selection mode change
400   displayPreview();
401 }
402
403 //=================================================================================
404 // function : LineEditReturnPressed()
405 // purpose  :
406 //=================================================================================
407 void GenerationGUI_PipeDlg::LineEditReturnPressed()
408 {
409   QLineEdit* send = (QLineEdit*)sender();
410   if (send == GroupPoints->LineEdit1 ||
411       send == GroupPoints->LineEdit2 ||
412       send == GroupPoints->LineEdit3) {
413     myEditCurrentArgument = send;
414     GEOMBase_Skeleton::LineEditReturnPressed();
415   }
416
417   if (send == GroupPoints->LineEdit2)
418     GroupPoints->CheckButton1->setEnabled(true);
419   else
420     GroupPoints->CheckButton1->setEnabled(false);
421 }
422
423 //=================================================================================
424 // function : ActivateThisDialog()
425 // purpose  :
426 //=================================================================================
427 void GenerationGUI_PipeDlg::ActivateThisDialog()
428 {
429   GEOMBase_Skeleton::ActivateThisDialog();
430   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
431            this, SLOT( SelectionIntoArgument() ) );
432
433   ConstructorsClicked(getConstructorId());
434 }
435
436 //=================================================================================
437 // function : enterEvent()
438 // purpose  : when mouse enter onto the QWidget
439 //=================================================================================
440 void GenerationGUI_PipeDlg::enterEvent (QEvent*)
441 {
442   if (!mainFrame()->GroupConstructors->isEnabled())
443     ActivateThisDialog();
444 }
445
446 //=================================================================================
447 // function : createOperation
448 // purpose  :
449 //=================================================================================
450 GEOM::GEOM_IOperations_ptr GenerationGUI_PipeDlg::createOperation()
451 {
452   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
453 }
454
455 //=================================================================================
456 // function : isValid
457 // purpose  :
458 //=================================================================================
459 bool GenerationGUI_PipeDlg::isValid (QString&)
460 {
461   switch ( getConstructorId() ) {
462   case 0 :
463     return myOkBase && myOkPath;
464   case 1 :
465     return myOkBase && myOkPath && myOkVec;
466   }
467 }
468
469 //=================================================================================
470 // function : execute
471 // purpose  :
472 //=================================================================================
473 bool GenerationGUI_PipeDlg::execute (ObjectList& objects)
474 {
475   GEOM::GEOM_Object_var anObj;
476
477   switch ( getConstructorId() ) {
478   case 0 :
479     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->MakePipe(myBase, myPath);
480     break;
481   case 1 :
482     anObj = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation())->
483       MakePipeBiNormalAlongVector(myBase, myPath, myVec);
484     break;
485   }
486
487   if (!anObj->_is_nil())
488     objects.push_back(anObj._retn());
489
490   return true;
491 }
492
493 //=================================================================================
494 // function : addSubshapeToStudy
495 // purpose  : virtual method to add new SubObjects if local selection
496 //=================================================================================
497 void GenerationGUI_PipeDlg::addSubshapesToStudy()
498 {
499   QMap<QString, GEOM::GEOM_Object_var> objMap;
500
501   objMap[GroupPoints->LineEdit2->text()] = myPath;
502
503   addSubshapesToFather(objMap);
504 }