Salome HOME
a5f81be42160f834399353208733374096621796
[modules/geom.git] / src / GenerationGUI / GenerationGUI_PipeDlg.cxx
1 // Copyright (C) 2007-2013  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.nullify();
118   myVec.nullify();
119
120   GroupPoints->CheckButton1->setEnabled(false);
121
122   showOnlyPreviewControl();
123
124   // signals and slots connections
125   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
126   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
127
128   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
129
130   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
131   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
132   connect(GroupPoints->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133
134   connect(GroupPoints->CheckButton1,   SIGNAL(toggled(bool)), this, SLOT(SelectionTypeButtonClicked()));
135
136   initName(tr("GEOM_PIPE"));
137   resize(100,100);
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   case 0:
158     GroupPoints->TextLabel3->hide();
159     GroupPoints->PushButton3->hide();
160     GroupPoints->LineEdit3->hide();
161     GroupPoints->PushButton1->click();
162     break;
163   case 1:
164     GroupPoints->TextLabel3->show();
165     GroupPoints->PushButton3->show();
166     GroupPoints->LineEdit3->show();
167     break;
168   default:
169     break;
170   }
171
172   qApp->processEvents();
173   updateGeometry();
174   resize(100,100);
175
176   SelectionIntoArgument();
177   processPreview();
178 }
179
180 //=================================================================================
181 // function : SelectionBittonClicked()
182 // purpose  : Selection type Radio button management
183 //=================================================================================
184 void GenerationGUI_PipeDlg::SelectionTypeButtonClicked()
185 {
186   globalSelection();
187   if ( GroupPoints->CheckButton1->isChecked() ) {
188     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
189   } else {
190     TColStd_MapOfInteger aMap;
191     aMap.Add(GEOM_COMPOUND);
192     aMap.Add(GEOM_WIRE);
193     aMap.Add(GEOM_LINE);
194     globalSelection(aMap);
195   }
196   if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
197     myEditCurrentArgument->setText("");
198     myPath.nullify();
199   }
200   processPreview();
201 }
202
203 //=================================================================================
204 // function : ClickOnOk()
205 // purpose  :
206 //=================================================================================
207 void GenerationGUI_PipeDlg::ClickOnOk()
208 {
209   setIsApplyAndClose( true );
210   if (ClickOnApply())
211     ClickOnCancel();
212 }
213
214 //=================================================================================
215 // function : ClickOnApply()
216 // purpose  :
217 //=================================================================================
218 bool GenerationGUI_PipeDlg::ClickOnApply()
219 {
220   if (!onAccept())
221     return false;
222
223   initName();
224   if ( getConstructorId() != 1 )
225     ConstructorsClicked( getConstructorId() );
226   // activate selection and connect selection manager
227   GroupPoints->PushButton1->click();
228   return true;
229 }
230
231 //=================================================================================
232 // function : SelectionIntoArgument()
233 // purpose  : Called when selection is changed or on dialog initialization or activation
234 //=================================================================================
235 void GenerationGUI_PipeDlg::SelectionIntoArgument()
236 {
237   erasePreview();
238   myEditCurrentArgument->setText("");
239
240   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
241     myBaseObjects.clear();
242     QList<GEOM::GeomObjPtr> objects = getSelected( TopAbs_SHAPE, -1 );
243     for ( int i = 0; i < objects.count(); i++ ) {
244       GEOM::shape_type stype = objects[i]->GetMaxShapeType();
245       GEOM::shape_type maxallowed = getConstructorId() == 1 ? GEOM::FACE : GEOM::SHELL;
246       GEOM::shape_type minallowed = getConstructorId() == 1 ? GEOM::EDGE : GEOM::VERTEX;
247       if ( stype < maxallowed || stype > minallowed )
248         continue;
249       myBaseObjects << objects[i];
250     }
251     if ( !myBaseObjects.isEmpty() ) {
252       QString aName = myBaseObjects.count() > 1 ? QString( "%1_objects").arg( myBaseObjects.count() ) : GEOMBase::GetName( myBaseObjects[0].get() );
253       myEditCurrentArgument->setText( aName );
254     }
255   }
256   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
257     QList<TopAbs_ShapeEnum> types;
258     types << TopAbs_EDGE << TopAbs_WIRE;
259     myPath = getSelected( types );
260     if ( myPath ) {
261       QString aName = GEOMBase::GetName( myPath.get() );
262       myEditCurrentArgument->setText( aName );
263       if ( getConstructorId() == 1 && !myVec )
264         GroupPoints->PushButton3->click();
265       else if ( myBaseObjects.isEmpty() )
266         GroupPoints->PushButton1->click();
267     }
268   }
269   else if (myEditCurrentArgument == GroupPoints->LineEdit3) {
270     myVec = getSelected( TopAbs_EDGE );
271     if ( myVec ) {
272       QString aName = GEOMBase::GetName( myVec.get() );
273       myEditCurrentArgument->setText( aName );
274       if ( myBaseObjects.isEmpty() )
275         GroupPoints->PushButton1->click();
276       else if ( !myPath )
277         GroupPoints->PushButton2->click();
278     }
279   }
280
281   processPreview();
282 }
283
284 //=================================================================================
285 // function : SetEditCurrentArgument()
286 // purpose  :
287 //=================================================================================
288 void GenerationGUI_PipeDlg::SetEditCurrentArgument()
289 {
290   QPushButton* send = (QPushButton*)sender();
291
292   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
293   globalSelection(GEOM_ALLSHAPES);
294   GroupPoints->PushButton1->setDown(false);
295   GroupPoints->PushButton2->setDown(false);
296   GroupPoints->PushButton3->setDown(false);
297   GroupPoints->LineEdit1->setEnabled(false);
298   GroupPoints->LineEdit2->setEnabled(false);
299   GroupPoints->LineEdit3->setEnabled(false);
300   if (send == GroupPoints->PushButton1) {
301     myEditCurrentArgument = GroupPoints->LineEdit1;
302     GroupPoints->CheckButton1->setEnabled(false);
303   }
304   else if (send == GroupPoints->PushButton2) {
305     myEditCurrentArgument = GroupPoints->LineEdit2;
306
307     if ( GroupPoints->CheckButton1->isChecked() ) {
308       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
309     } else {
310       TColStd_MapOfInteger aMap;
311       aMap.Add(GEOM_COMPOUND);
312       aMap.Add(GEOM_WIRE);
313       aMap.Add(GEOM_LINE);
314       globalSelection(aMap);
315     }
316     GroupPoints->CheckButton1->setEnabled(true);
317   }
318   else if(send == GroupPoints->PushButton3) {
319     myEditCurrentArgument = GroupPoints->LineEdit3;
320     GroupPoints->CheckButton1->setEnabled(false);
321     localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
322   }
323   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
324           this, SLOT(SelectionIntoArgument()));
325
326   // enable line edit
327   myEditCurrentArgument->setEnabled(true);
328   myEditCurrentArgument->setFocus();
329   // after setFocus(), because it will be setDown(false) when loses focus
330   send->setDown(true);
331
332   // seems we need it only to avoid preview disappearing, caused by selection mode change
333   processPreview();
334 }
335
336 //=================================================================================
337 // function : ActivateThisDialog()
338 // purpose  :
339 //=================================================================================
340 void GenerationGUI_PipeDlg::ActivateThisDialog()
341 {
342   GEOMBase_Skeleton::ActivateThisDialog();
343   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
344            this, SLOT( SelectionIntoArgument() ) );
345
346   ConstructorsClicked(getConstructorId());
347 }
348
349 //=================================================================================
350 // function : enterEvent()
351 // purpose  : when mouse enter onto the QWidget
352 //=================================================================================
353 void GenerationGUI_PipeDlg::enterEvent (QEvent*)
354 {
355   if (!mainFrame()->GroupConstructors->isEnabled())
356     ActivateThisDialog();
357 }
358
359 //=================================================================================
360 // function : createOperation
361 // purpose  :
362 //=================================================================================
363 GEOM::GEOM_IOperations_ptr GenerationGUI_PipeDlg::createOperation()
364 {
365   return getGeomEngine()->GetI3DPrimOperations(getStudyId());
366 }
367
368 //=================================================================================
369 // function : isValid
370 // purpose  :
371 //=================================================================================
372 bool GenerationGUI_PipeDlg::isValid (QString&)
373 {
374   bool ok = false;
375   switch ( getConstructorId() ) {
376   case 0 :
377     ok = !myBaseObjects.isEmpty() && myPath;
378     break;
379   case 1 :
380     ok = !myBaseObjects.isEmpty() && myPath && myVec;
381     break;
382   default:
383     break;
384   }
385   return ok;
386 }
387
388 //=================================================================================
389 // function : execute
390 // purpose  :
391 //=================================================================================
392 bool GenerationGUI_PipeDlg::execute (ObjectList& objects)
393 {
394   GEOM::GEOM_Object_var anObj;
395
396   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
397
398   for (int i = 0; i < myBaseObjects.count(); i++) {
399     switch ( getConstructorId() ) {
400     case 0 :
401       anObj = anOper->MakePipe(myBaseObjects[i].get(), myPath.get());
402       break;
403     case 1 :
404       anObj = anOper->MakePipeBiNormalAlongVector(myBaseObjects[i].get(), myPath.get(), myVec.get());
405       break;
406     }
407     
408     if (!anObj->_is_nil())
409       objects.push_back(anObj._retn());
410   }
411   return true;
412 }
413
414 //=================================================================================
415 // function : addSubshapeToStudy
416 // purpose  : virtual method to add new SubObjects if local selection
417 //=================================================================================
418 void GenerationGUI_PipeDlg::addSubshapesToStudy()
419 {
420   GEOMBase::PublishSubObject( myPath.get() );
421   GEOMBase::PublishSubObject( myVec.get() );
422 }
423
424 //=================================================================================
425 // function : extractPrefix
426 // purpose  : redefined from GEOMBase_Helper class
427 //=================================================================================
428 bool GenerationGUI_PipeDlg::extractPrefix() const
429 {
430   return myBaseObjects.count() > 1;
431 }