Salome HOME
untabify
[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   resize(100,100);
139
140   GroupPoints->TextLabel3->hide();
141   GroupPoints->PushButton3->hide();
142   GroupPoints->LineEdit3->hide();
143   ConstructorsClicked(0);
144
145   GroupPoints->PushButton1->click();
146   SelectionIntoArgument();
147 }
148
149 //=================================================================================
150 // function : ConstructorsClicked()
151 // purpose  : Radio button management
152 //=================================================================================
153 void GenerationGUI_PipeDlg::ConstructorsClicked( int constructorId )
154 {
155   erasePreview();
156
157   switch (constructorId)
158   {
159     case 0:
160     {
161       GroupPoints->TextLabel3->hide();
162       GroupPoints->PushButton3->hide();
163       GroupPoints->LineEdit3->hide();
164       break;
165     }
166     case 1:
167     {
168       GroupPoints->TextLabel3->show();
169       GroupPoints->PushButton3->show();
170       GroupPoints->LineEdit3->show();
171       break;
172     }
173   }
174
175   displayPreview();
176 }
177
178 //=================================================================================
179 // function : SelectionBittonClicked()
180 // purpose  : Selection type Radio button management
181 //=================================================================================
182 void GenerationGUI_PipeDlg::SelectionTypeButtonClicked()
183 {
184   globalSelection();
185   if ( GroupPoints->CheckButton1->isChecked() ) {
186     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
187   } else {
188     TColStd_MapOfInteger aMap;
189     aMap.Add(GEOM_WIRE);
190     aMap.Add(GEOM_LINE);
191     globalSelection(aMap);
192   }
193   if(myEditCurrentArgument == GroupPoints->LineEdit2) {
194     myEditCurrentArgument->setText("");
195     myOkPath = false;
196   }
197   displayPreview();
198 }
199
200 //=================================================================================
201 // function : ClickOnOk()
202 // purpose  :
203 //=================================================================================
204 void GenerationGUI_PipeDlg::ClickOnOk()
205 {
206   if (ClickOnApply())
207     ClickOnCancel();
208 }
209
210 //=================================================================================
211 // function : ClickOnApply()
212 // purpose  :
213 //=================================================================================
214 bool GenerationGUI_PipeDlg::ClickOnApply()
215 {
216   if (!onAccept())
217     return false;
218
219   initName();
220   if ( getConstructorId() != 1 )
221         ConstructorsClicked( getConstructorId() );
222   // activate selection and connect selection manager
223   GroupPoints->PushButton1->click();
224   return true;
225 }
226
227 //=================================================================================
228 // function : SelectionIntoArgument()
229 // purpose  : Called when selection is changed or on dialog initialization or activation
230 //=================================================================================
231 void GenerationGUI_PipeDlg::SelectionIntoArgument()
232 {
233   erasePreview();
234   myEditCurrentArgument->setText("");
235   if      (myEditCurrentArgument == GroupPoints->LineEdit1) myOkBase = false;
236   else if (myEditCurrentArgument == GroupPoints->LineEdit2) myOkPath = false;
237   else if (myEditCurrentArgument == GroupPoints->LineEdit3) myOkVec  = false;
238
239   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
240   SALOME_ListIO aSelList;
241   aSelMgr->selectedObjects(aSelList);
242
243   if (aSelList.Extent() != 1)
244     return;
245
246   // nbSel == 1
247   Standard_Boolean testResult = Standard_False;
248   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
249
250   if (!testResult || aSelectedObject->_is_nil())
251     return;
252
253   TopoDS_Shape S;
254   if (!GEOMBase::GetShape(aSelectedObject, S))
255     return;
256
257   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
258     if (S.ShapeType() == TopAbs_COMPSOLID ||
259         S.ShapeType() == TopAbs_COMPOUND ||
260         S.ShapeType() == TopAbs_SOLID ||
261         S.ShapeType() == TopAbs_SHAPE)
262       return;
263     if ( getConstructorId() == 1 && 
264          (S.ShapeType() == TopAbs_SHELL || 
265           S.ShapeType() == TopAbs_VERTEX))
266       return;
267
268     myBase = aSelectedObject;
269     myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
270     myOkBase = true;
271     if (!myOkPath)
272       GroupPoints->PushButton2->click();
273     else if (!myOkVec)
274       GroupPoints->PushButton3->click();
275   }
276   else if (myEditCurrentArgument == GroupPoints->LineEdit2 || 
277            myEditCurrentArgument == GroupPoints->LineEdit3) {
278     myEditCurrentArgument == GroupPoints->LineEdit2 ? myOkPath = false : myOkVec = false;
279     bool myOk = false;
280     QString aName = GEOMBase::GetName(aSelectedObject);
281
282     if (myEditCurrentArgument == GroupPoints->LineEdit2 && !GroupPoints->CheckButton1->isChecked() ){
283       myPath = aSelectedObject;
284       myOkPath = true;
285       myEditCurrentArgument->setText(aName);
286     }
287     else 
288       { 
289         if (aSelectedObject != myBase) {
290           TColStd_IndexedMapOfInteger aMap;
291           aSelMgr->GetIndexes(aSelList.First(), aMap);
292           if (aMap.Extent() == 1) {
293             int anIndex = aMap(1);
294             aName.append(":edge_" + QString::number(anIndex));
295             
296             //Find SubShape Object in Father
297             GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
298             
299             if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
300               GEOM::GEOM_IShapesOperations_var aShapesOp =
301                 getGeomEngine()->GetIShapesOperations(getStudyId());
302               aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
303               myOk = true;
304             }
305             else { // get Object from study
306               aSelectedObject = aFindedObject;
307               myOk = true;
308             }
309           }
310           else {
311             myOk = true;
312             if (S.ShapeType() != TopAbs_EDGE) {
313               aSelectedObject = GEOM::GEOM_Object::_nil();
314               aName = "";
315               myOk = false;
316             }
317           }
318           if (myEditCurrentArgument == GroupPoints->LineEdit2) {
319             myPath = aSelectedObject;
320             myOkPath = myOk;
321           }
322           else if (myEditCurrentArgument == GroupPoints->LineEdit3) {
323             myVec = aSelectedObject;
324             myOkVec = myOk;
325           }
326         }
327         myEditCurrentArgument->setText(aName);
328
329         if (myOkPath) {
330           if (!myOkBase)
331             GroupPoints->PushButton1->click();
332           else if (!myOkVec)
333             GroupPoints->PushButton3->click();
334         }
335         else if (myOkVec) {
336           if (!myOkBase)
337             GroupPoints->PushButton1->click();
338           else if (!myOkPath)
339             GroupPoints->PushButton2->click();
340         }
341       }
342   }
343
344   // clear selection
345   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
346   myGeomGUI->getApp()->selectionMgr()->clearSelected();
347   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
348           this, SLOT(SelectionIntoArgument()));
349
350   displayPreview();
351 }
352
353 //=================================================================================
354 // function : SetEditCurrentArgument()
355 // purpose  :
356 //=================================================================================
357 void GenerationGUI_PipeDlg::SetEditCurrentArgument()
358 {
359   QPushButton* send = (QPushButton*)sender();
360
361   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
362   globalSelection(GEOM_ALLSHAPES);
363   GroupPoints->PushButton1->setDown(false);
364   GroupPoints->PushButton2->setDown(false);
365   GroupPoints->PushButton3->setDown(false);
366   GroupPoints->LineEdit1->setEnabled(false);
367   GroupPoints->LineEdit2->setEnabled(false);
368   GroupPoints->LineEdit3->setEnabled(false);
369   if (send == GroupPoints->PushButton1) {
370     myEditCurrentArgument = GroupPoints->LineEdit1;
371     GroupPoints->CheckButton1->setEnabled(false);
372   }
373   else if (send == GroupPoints->PushButton2) {
374     myEditCurrentArgument = GroupPoints->LineEdit2;
375
376     if ( GroupPoints->CheckButton1->isChecked() ) {
377       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
378     } else {
379       TColStd_MapOfInteger aMap;
380       aMap.Add(GEOM_WIRE);
381       aMap.Add(GEOM_LINE);
382       globalSelection(aMap);
383     }
384     GroupPoints->CheckButton1->setEnabled(true);
385   }
386   else if(send == GroupPoints->PushButton3) {
387     myEditCurrentArgument = GroupPoints->LineEdit3;
388     GroupPoints->CheckButton1->setEnabled(false);
389     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
390   }
391   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
392           this, SLOT(SelectionIntoArgument()));
393
394   // enable line edit
395   myEditCurrentArgument->setEnabled(true);
396   myEditCurrentArgument->setFocus();
397   // after setFocus(), because it will be setDown(false) when loses focus
398   send->setDown(true);
399
400   // seems we need it only to avoid preview disappearing, caused by selection mode change
401   displayPreview();
402 }
403
404 //=================================================================================
405 // function : LineEditReturnPressed()
406 // purpose  :
407 //=================================================================================
408 void GenerationGUI_PipeDlg::LineEditReturnPressed()
409 {
410   QLineEdit* send = (QLineEdit*)sender();
411   if (send == GroupPoints->LineEdit1 ||
412       send == GroupPoints->LineEdit2 ||
413       send == GroupPoints->LineEdit3) {
414     myEditCurrentArgument = send;
415     GEOMBase_Skeleton::LineEditReturnPressed();
416   }
417
418   if (send == GroupPoints->LineEdit2)
419     GroupPoints->CheckButton1->setEnabled(true);
420   else
421     GroupPoints->CheckButton1->setEnabled(false);
422 }
423
424 //=================================================================================
425 // function : ActivateThisDialog()
426 // purpose  :
427 //=================================================================================
428 void GenerationGUI_PipeDlg::ActivateThisDialog()
429 {
430   GEOMBase_Skeleton::ActivateThisDialog();
431   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
432            this, SLOT( SelectionIntoArgument() ) );
433
434   ConstructorsClicked(getConstructorId());
435 }
436
437 //=================================================================================
438 // function : enterEvent()
439 // purpose  : when mouse enter onto the QWidget
440 //=================================================================================
441 void GenerationGUI_PipeDlg::enterEvent (QEvent*)
442 {
443   if (!mainFrame()->GroupConstructors->isEnabled())
444     ActivateThisDialog();
445 }
446
447 //=================================================================================
448 // function : createOperation
449 // purpose  :
450 //=================================================================================
451 GEOM::GEOM_IOperations_ptr GenerationGUI_PipeDlg::createOperation()
452 {
453   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
454 }
455
456 //=================================================================================
457 // function : isValid
458 // purpose  :
459 //=================================================================================
460 bool GenerationGUI_PipeDlg::isValid (QString&)
461 {
462   switch ( getConstructorId() ) {
463   case 0 :
464     return myOkBase && myOkPath;
465   case 1 :
466     return myOkBase && myOkPath && myOkVec;
467   }
468 }
469
470 //=================================================================================
471 // function : execute
472 // purpose  :
473 //=================================================================================
474 bool GenerationGUI_PipeDlg::execute (ObjectList& objects)
475 {
476   GEOM::GEOM_Object_var anObj;
477
478   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
479
480   switch ( getConstructorId() ) {
481   case 0 :
482     anObj = anOper->MakePipe(myBase, myPath);
483     break;
484   case 1 :
485     anObj = anOper->MakePipeBiNormalAlongVector(myBase, myPath, myVec);
486     break;
487   }
488
489   if (!anObj->_is_nil())
490     objects.push_back(anObj._retn());
491
492   return true;
493 }
494
495 //=================================================================================
496 // function : addSubshapeToStudy
497 // purpose  : virtual method to add new SubObjects if local selection
498 //=================================================================================
499 void GenerationGUI_PipeDlg::addSubshapesToStudy()
500 {
501   QMap<QString, GEOM::GEOM_Object_var> objMap;
502
503   objMap[GroupPoints->LineEdit2->text()] = myPath;
504
505   addSubshapesToFather(objMap);
506 }