Salome HOME
updated copyright message
[modules/geom.git] / src / GenerationGUI / GenerationGUI_PipePathDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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_PipePathDlg.cxx
25
26 #include "GenerationGUI_PipePathDlg.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_PipePathDlg()
49 // purpose  : Constructs a GenerationGUI_PipePathDlg 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_PipePathDlg::GenerationGUI_PipePathDlg (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_PATH")));
59   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_PIPE_PATH_TITLE"));
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle(tr("GEOM_PIPE_PATH"));
65   mainFrame()->RadioButton1->setIcon(image0);
66   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
67   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
68   mainFrame()->RadioButton2->close();
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_PIPE_LIKE_SHAPE"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_PIPE_BASE1_OBJECT"));
76   GroupPoints->TextLabel3->setText(tr("GEOM_PIPE_BASE2_OBJECT"));
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);
84   layout->setSpacing(6);
85   layout->addWidget(GroupPoints);
86   /***************************************************************/
87
88   setHelpFileName("create_pipe_path_page.html");
89
90   // Initialisation
91   Init();
92 }
93
94 //=================================================================================
95 // function : ~GenerationGUI_PipePathDlg()
96 // purpose  : Destroys the object and frees any allocated resources
97 //=================================================================================
98 GenerationGUI_PipePathDlg::~GenerationGUI_PipePathDlg()
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_PipePathDlg::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
118   myShape.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(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->CheckButton1,   SIGNAL(toggled(bool)), this, SLOT(SelectionTypeButtonClicked()));
133
134   initName(tr("GEOM_PIPE_PATH"));
135
136   updateGeometry();
137   resize(100,100);
138
139   GroupPoints->PushButton1->click();
140   SelectionIntoArgument();
141 }
142
143 //=================================================================================
144 // function : SelectionBittonClicked()
145 // purpose  : Selection type Radio button management
146 //=================================================================================
147 void GenerationGUI_PipePathDlg::SelectionTypeButtonClicked()
148 {
149   globalSelection();
150   if (GroupPoints->CheckButton1->isChecked()) {
151     localSelection(TopAbs_EDGE);
152   }
153   else {
154     TColStd_MapOfInteger aMap;
155     aMap.Add(GEOM_FACE);
156     aMap.Add(GEOM_WIRE);
157     aMap.Add(GEOM_EDGE);
158     globalSelection(aMap);
159   }
160   processPreview();
161 }
162
163 //=================================================================================
164 // function : ClickOnOk()
165 // purpose  :
166 //=================================================================================
167 void GenerationGUI_PipePathDlg::ClickOnOk()
168 {
169   setIsApplyAndClose(true);
170   if (ClickOnApply())
171     ClickOnCancel();
172 }
173
174 //=================================================================================
175 // function : ClickOnApply()
176 // purpose  :
177 //=================================================================================
178 bool GenerationGUI_PipePathDlg::ClickOnApply()
179 {
180   if (!onAccept())
181     return false;
182
183   initName();
184   // activate selection and connect selection manager
185   GroupPoints->PushButton1->click();
186   return true;
187 }
188
189 //=================================================================================
190 // function : SelectionIntoArgument()
191 // purpose  : Called when selection is changed or on dialog initialization or activation
192 //=================================================================================
193 void GenerationGUI_PipePathDlg::SelectionIntoArgument()
194 {
195   erasePreview();
196   myEditCurrentArgument->setText("");
197
198   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
199     QList<TopAbs_ShapeEnum> types;
200     types << TopAbs_SOLID << TopAbs_SHELL << TopAbs_FACE;
201     myShape = getSelected(types);
202     if (myShape) {
203       QString aName = GEOMBase::GetName(myShape.get());
204       myEditCurrentArgument->setText(aName);
205       if (myBase1Objects.isEmpty())
206         GroupPoints->PushButton2->click();
207       else if (myBase2Objects.isEmpty())
208         GroupPoints->PushButton3->click();
209     }
210   }
211   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
212     myBase1Objects.clear();
213     QList<GEOM::GeomObjPtr> objects = getSelected(TopAbs_SHAPE, -1);
214     for (int i = 0; i < objects.count(); i++) {
215       GEOM::shape_type stype = objects[i]->GetMaxShapeType();
216      if (GEOM::FACE <= stype && stype <= GEOM::EDGE)
217         myBase1Objects << objects[i];
218     }
219     int nbObj = myBase1Objects.count();
220     if (nbObj) {
221       QString aName =
222         nbObj > 1 ? QString("%1_objects").arg(nbObj) : GEOMBase::GetName(myBase1Objects[0].get());
223       myEditCurrentArgument->setText(aName);
224     }
225   }
226   else if (myEditCurrentArgument == GroupPoints->LineEdit3) {
227     myBase2Objects.clear();
228     QList<GEOM::GeomObjPtr> objects = getSelected(TopAbs_SHAPE, -1);
229     for (int i = 0; i < objects.count(); i++) {
230       GEOM::shape_type stype = objects[i]->GetMaxShapeType();
231       if (GEOM::FACE <= stype && stype <= GEOM::EDGE)
232         myBase2Objects << objects[i];
233     }
234     int nbObj = myBase2Objects.count();
235     if (nbObj) {
236       QString aName =
237         nbObj > 1 ? QString("%1_objects").arg(nbObj) : GEOMBase::GetName(myBase2Objects[0].get());
238       myEditCurrentArgument->setText(aName);
239     }
240   }
241
242   processPreview();
243 }
244
245 //=================================================================================
246 // function : SetEditCurrentArgument()
247 // purpose  :
248 //=================================================================================
249 void GenerationGUI_PipePathDlg::SetEditCurrentArgument()
250 {
251   QPushButton* send = (QPushButton*)sender();
252
253   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
254
255   globalSelection(GEOM_ALLSHAPES);
256
257   GroupPoints->PushButton1->setDown(false);
258   GroupPoints->PushButton2->setDown(false);
259   GroupPoints->PushButton3->setDown(false);
260   GroupPoints->LineEdit1->setEnabled(false);
261   GroupPoints->LineEdit2->setEnabled(false);
262   GroupPoints->LineEdit3->setEnabled(false);
263
264   if (send == GroupPoints->PushButton1) {
265     myEditCurrentArgument = GroupPoints->LineEdit1;
266     GroupPoints->CheckButton1->setEnabled(false);
267   }
268   else if (send == GroupPoints->PushButton2) {
269     myEditCurrentArgument = GroupPoints->LineEdit2;
270
271     if (GroupPoints->CheckButton1->isChecked()) {
272       localSelection(TopAbs_EDGE);
273     }
274     else {
275       TColStd_MapOfInteger aMap;
276       aMap.Add(GEOM_FACE);
277       aMap.Add(GEOM_WIRE);
278       aMap.Add(GEOM_EDGE);
279       globalSelection(aMap);
280     }
281     GroupPoints->CheckButton1->setEnabled(true);
282   }
283   else if (send == GroupPoints->PushButton3) {
284     myEditCurrentArgument = GroupPoints->LineEdit3;
285
286     if (GroupPoints->CheckButton1->isChecked()) {
287       localSelection(TopAbs_EDGE);
288     }
289     else {
290       TColStd_MapOfInteger aMap;
291       aMap.Add(GEOM_FACE);
292       aMap.Add(GEOM_WIRE);
293       aMap.Add(GEOM_EDGE);
294       globalSelection(aMap);
295     }
296     GroupPoints->CheckButton1->setEnabled(true);
297   }
298   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
299           this, SLOT(SelectionIntoArgument()));
300
301   // enable line edit
302   myEditCurrentArgument->setEnabled(true);
303   myEditCurrentArgument->setFocus();
304   // after setFocus(), because it will be setDown(false) when loses focus
305   send->setDown(true);
306
307   // seems we need it only to avoid preview disappearing, caused by selection mode change
308   processPreview();
309 }
310
311 //=================================================================================
312 // function : ActivateThisDialog()
313 // purpose  :
314 //=================================================================================
315 void GenerationGUI_PipePathDlg::ActivateThisDialog()
316 {
317   GEOMBase_Skeleton::ActivateThisDialog();
318
319   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
320           this, SLOT(SelectionIntoArgument()));
321
322   GroupPoints->PushButton1->click();
323   SelectionIntoArgument();
324 }
325
326 //=================================================================================
327 // function : enterEvent()
328 // purpose  : when mouse enter onto the QWidget
329 //=================================================================================
330 void GenerationGUI_PipePathDlg::enterEvent (QEvent*)
331 {
332   if (!mainFrame()->GroupConstructors->isEnabled())
333     ActivateThisDialog();
334 }
335
336 //=================================================================================
337 // function : createOperation
338 // purpose  :
339 //=================================================================================
340 GEOM::GEOM_IOperations_ptr GenerationGUI_PipePathDlg::createOperation()
341 {
342   return getGeomEngine()->GetI3DPrimOperations();
343 }
344
345 //=================================================================================
346 // function : isValid
347 // purpose  :
348 //=================================================================================
349 bool GenerationGUI_PipePathDlg::isValid (QString&)
350 {
351   bool ok = myShape && !myBase1Objects.isEmpty() && !myBase2Objects.isEmpty();
352   return ok;
353 }
354
355 //=================================================================================
356 // function : execute
357 // purpose  :
358 //=================================================================================
359 bool GenerationGUI_PipePathDlg::execute (ObjectList& objects)
360 {
361   GEOM::ListOfGO_var aBase1 = new GEOM::ListOfGO();
362   GEOM::ListOfGO_var aBase2 = new GEOM::ListOfGO();
363
364   aBase1->length(myBase1Objects.count());
365   aBase2->length(myBase2Objects.count());
366
367   int i;
368   for (i = 0; i < myBase1Objects.count(); i++)
369     aBase1[i] = myBase1Objects[i].copy();
370   for (i = 0; i < myBase2Objects.count(); i++)
371     aBase2[i] = myBase2Objects[i].copy();
372
373   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
374   GEOM::GEOM_Object_var anObj = anOper->RestorePathEdges(myShape.get(), aBase1.in(), aBase2.in());
375     
376   if (!anObj->_is_nil())
377     objects.push_back(anObj._retn());
378
379   return true;
380 }
381
382 //=================================================================================
383 // function : addSubshapeToStudy
384 // purpose  : virtual method to add new SubObjects if local selection
385 //=================================================================================
386 void GenerationGUI_PipePathDlg::addSubshapesToStudy()
387 {
388   int i;
389   for (i = 0; i < myBase1Objects.count(); i++)
390     GEOMBase::PublishSubObject(myBase1Objects[i].get());
391   for (i = 0; i < myBase2Objects.count(); i++)
392     GEOMBase::PublishSubObject(myBase2Objects[i].get());
393 }
394
395 //=================================================================================
396 // function : getSourceObjects
397 // purpose  : virtual method to get source objects
398 //=================================================================================
399 QList<GEOM::GeomObjPtr> GenerationGUI_PipePathDlg::getSourceObjects()
400 {
401   QList<GEOM::GeomObjPtr> res(myBase1Objects);
402   res << myShape ;
403   for (int i = 0; i < myBase2Objects.count(); i++)
404     res << myBase2Objects[i];
405   return res;
406 }