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