Salome HOME
Updated copyright comment
[modules/geom.git] / src / RepairGUI / RepairGUI_FuseEdgesDlg.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "RepairGUI_FuseEdgesDlg.h"
21
22 #include <DlgRef.h>
23 #include <GeometryGUI.h>
24 #include <GEOMBase.h>
25
26 #include <SUIT_Desktop.h>
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_ViewWindow.h>
30 #include <SUIT_ViewManager.h>
31 #include <SalomeApp_Application.h>
32 #include <LightApp_SelectionMgr.h>
33 #include <OCCViewer_ViewModel.h>
34
35 // OCCT Includes
36 #include <TColStd_MapOfInteger.hxx>
37 #include <TColStd_IndexedMapOfInteger.hxx>
38
39 #include <GEOMImpl_Types.hxx>
40
41 //=================================================================================
42 // class    : RepairGUI_FuseEdgesDlg()
43 // purpose  : Constructs a RepairGUI_FuseEdgesDlg which is a child of 'parent', with the
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 RepairGUI_FuseEdgesDlg::RepairGUI_FuseEdgesDlg (GeometryGUI* theGeometryGUI,
49                                                 QWidget* parent)
50   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
51 {
52   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
53   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_FUSE_EDGES")));
54   QPixmap iconSelect (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
55
56   setWindowTitle(tr("GEOM_FUSE_EDGES_TITLE"));
57
58   mainFrame()->GroupConstructors->setTitle(tr("GEOM_FUSE_EDGES"));
59   mainFrame()->RadioButton1->setIcon(image0);
60   mainFrame()->RadioButton2->close();
61   mainFrame()->RadioButton3->close();
62
63   GroupVertexes = new DlgRef_2Sel1Spin (centralWidget());
64   GroupVertexes->GroupBox1->setTitle(tr("GEOM_FUSE_EDGES"));
65   GroupVertexes->TextLabel1->setText(tr("GEOM_WIRE"));
66   GroupVertexes->TextLabel2->setText(tr("GEOM_VERTEXES"));
67   GroupVertexes->PushButton1->setIcon(iconSelect);
68   GroupVertexes->PushButton2->setIcon(iconSelect);
69   GroupVertexes->LineEdit1->setReadOnly(true);
70   GroupVertexes->LineEdit2->setReadOnly(true);
71
72   GroupVertexes->TextLabel3->setVisible(false);
73   GroupVertexes->SpinBox_DX->setVisible(false);
74
75   QVBoxLayout* layout = new QVBoxLayout (centralWidget());
76   layout->setMargin(0);
77   layout->setSpacing(6);
78   layout->addWidget(GroupVertexes);
79
80   setHelpFileName("fuse_edges_operation_page.html");
81
82   // Initialisation
83   Init();
84   resize(100,100);
85 }
86
87 //=================================================================================
88 // function : ~RepairGUI_FuseEdgesDlg()
89 // purpose  : Destroys the object and frees any allocated resources
90 //=================================================================================
91 RepairGUI_FuseEdgesDlg::~RepairGUI_FuseEdgesDlg()
92 {
93 }
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void RepairGUI_FuseEdgesDlg::Init()
100 {
101   // Clear line edits
102   GroupVertexes->LineEdit1->setText("");
103   GroupVertexes->LineEdit2->setText("");
104
105   myShape = GEOM::GEOM_Object::_nil();
106
107   myPoints.clear();
108
109   // signals and slots connections
110   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
111   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
112
113   connect(GroupVertexes->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
114   connect(GroupVertexes->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
115
116   connect(GroupVertexes->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
117   connect(GroupVertexes->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
118
119   initName(tr("FUSE_EDGES_NEW_OBJ_NAME"));
120   GroupVertexes->PushButton1->click();
121
122   SelectionIntoArgument();
123 }
124
125 //=================================================================================
126 // function : ClickOnOk()
127 // purpose  :
128 //=================================================================================
129 void RepairGUI_FuseEdgesDlg::ClickOnOk()
130 {
131   setIsApplyAndClose(true);
132   if (ClickOnApply())
133     ClickOnCancel();
134 }
135
136 //=================================================================================
137 // function : ClickOnApply()
138 // purpose  :
139 //=================================================================================
140 bool RepairGUI_FuseEdgesDlg::ClickOnApply()
141 {
142   if (!onAccept())
143     return false;
144
145   initName();
146   // Reset dialog state
147   GroupVertexes->LineEdit1->setText("");
148   GroupVertexes->LineEdit2->setText("");
149   myShape = GEOM::GEOM_Object::_nil();
150   myPoints.clear();
151   GroupVertexes->PushButton1->click();
152
153   return true;
154 }
155
156 //=================================================================================
157 // function : SelectionIntoArgument()
158 // purpose  : Called when selection is changed or on dialog initialization or activation
159 //=================================================================================
160 void RepairGUI_FuseEdgesDlg::SelectionIntoArgument()
161 {
162   myEditCurrentArgument->setText("");
163
164   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
165   SALOME_ListIO aSelList;
166   aSelMgr->selectedObjects(aSelList);
167
168   // If selection of main object is activated
169   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
170     myShape = GEOM::GEOM_Object::_nil();
171     if (aSelList.Extent() == 1) {
172       GEOM::GEOM_Object_var anObj =
173         GEOMBase::ConvertIOinGEOMObject(aSelList.First());
174
175       if (!anObj->_is_nil()) {
176         QString aName = GEOMBase::GetName(anObj);
177         TopoDS_Shape aShape;
178         if (GEOMBase::GetShape(anObj, aShape, TopAbs_SHAPE) && !aShape.IsNull()) {
179           TColStd_IndexedMapOfInteger aMap;
180           aSelMgr->GetIndexes(aSelList.First(), aMap);
181           if (aMap.Extent() == 1) { // Local Selection
182             int anIndex = aMap(1);
183             aName += QString(":wire_%1").arg(anIndex);
184
185             //Find SubShape Object in Father
186             GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(anObj, aName);
187
188             if (aFindedObject->_is_nil()) { // Object not found in study
189               GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations();
190               anObj = aShapesOp->GetSubShape(anObj, anIndex);
191             }
192             else
193               anObj = aFindedObject; // get Object from study
194           }
195           else { // Global Selection
196             if (aShape.ShapeType() != TopAbs_WIRE) {
197               anObj = GEOM::GEOM_Object::_nil();
198               aName = "";
199             }
200           }
201         }
202         myShape = anObj;
203         myEditCurrentArgument->setText(aName);
204       }
205     }
206
207     if (!myShape->_is_nil() && myPoints.isEmpty())
208       GroupVertexes->PushButton2->click();
209   }
210   else if (myEditCurrentArgument == GroupVertexes->LineEdit2) {
211     myPoints = getSelected(TopAbs_VERTEX, -1);
212     if (!myPoints.isEmpty())
213       myEditCurrentArgument->setText(QString::number(myPoints.count()) + "_" + tr("GEOM_POINT") + tr("_S_"));
214     else
215       myEditCurrentArgument->setText("");
216   }
217 }
218
219 //=================================================================================
220 // function : SetEditCurrentArgument()
221 // purpose  :
222 //=================================================================================
223 void RepairGUI_FuseEdgesDlg::SetEditCurrentArgument()
224 {
225   QPushButton* send = (QPushButton*)sender();
226
227   if (send == GroupVertexes->PushButton1) {
228     myEditCurrentArgument = GroupVertexes->LineEdit1;
229     GroupVertexes->PushButton2->setDown(false);
230     GroupVertexes->LineEdit2->setEnabled(false);
231   }
232   else if (send == GroupVertexes->PushButton2) {
233     myEditCurrentArgument = GroupVertexes->LineEdit2;
234     GroupVertexes->PushButton1->setDown(false);
235     GroupVertexes->LineEdit1->setEnabled(false);
236   }
237
238   // enable line edit
239   myEditCurrentArgument->setEnabled(true);
240   myEditCurrentArgument->setFocus();
241   // after setFocus(), because it will be setDown(false) when loses focus
242   send->setDown(true);
243
244   activateSelection();
245 }
246
247 //=================================================================================
248 // function : LineEditReturnPressed()
249 // purpose  :
250 //=================================================================================
251 void RepairGUI_FuseEdgesDlg::LineEditReturnPressed()
252 {
253   QLineEdit* send = (QLineEdit*)sender();
254
255   if (send == GroupVertexes->LineEdit1)
256     myEditCurrentArgument = GroupVertexes->LineEdit1;
257   else if (send == GroupVertexes->LineEdit2)
258     myEditCurrentArgument = GroupVertexes->LineEdit2;
259   else
260     return;
261
262   GEOMBase_Skeleton::LineEditReturnPressed();
263 }
264
265 //=================================================================================
266 // function : ActivateThisDialog()
267 // purpose  :
268 //=================================================================================
269 void RepairGUI_FuseEdgesDlg::ActivateThisDialog()
270 {
271   GEOMBase_Skeleton::ActivateThisDialog();
272 }
273
274 //=================================================================================
275 // function : enterEvent()
276 // purpose  :
277 //=================================================================================
278 void RepairGUI_FuseEdgesDlg::enterEvent (QEvent*)
279 {
280   if (!mainFrame()->GroupConstructors->isEnabled())
281     this->ActivateThisDialog();
282 }
283
284 //=================================================================================
285 // function : activateSelection
286 // purpose  : Activate selection in accordance with myEditCurrentArgument
287 //=================================================================================
288 void RepairGUI_FuseEdgesDlg::activateSelection()
289 {
290   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
291   globalSelection();
292   if (myEditCurrentArgument == GroupVertexes->LineEdit1)
293     globalSelection(GEOM_WIRE);
294   else if (!myShape->_is_nil() && myEditCurrentArgument == GroupVertexes->LineEdit2)
295     localSelection(myShape, TopAbs_VERTEX);
296
297   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
298           this, SLOT(SelectionIntoArgument()));
299 }
300
301 //=================================================================================
302 // function : createOperation
303 // purpose  :
304 //=================================================================================
305 GEOM::GEOM_IOperations_ptr RepairGUI_FuseEdgesDlg::createOperation()
306 {
307   return getGeomEngine()->GetIHealingOperations();
308 }
309
310 //=================================================================================
311 // function : isValid()
312 // purpose  : Verify validity of input data
313 //=================================================================================
314 bool RepairGUI_FuseEdgesDlg::isValid (QString& /*msg*/)
315 {
316   return (!myShape->_is_nil());
317 }
318
319 //=================================================================================
320 // function : execute
321 // purpose  :
322 //=================================================================================
323 bool RepairGUI_FuseEdgesDlg::execute (ObjectList& objects)
324 {
325   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
326
327   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
328   points->length(myPoints.count());
329   for (int i = 0; i < myPoints.count(); i++)
330     points[i] = myPoints[i].copy();
331
332   GEOM::GEOM_Object_var anObj = anOper->FuseCollinearEdgesWithinWire(myShape, points.in());
333
334   if (!anObj->_is_nil())
335     objects.push_back(anObj._retn());
336
337   return true;
338 }
339
340 //=================================================================================
341 // function : addSubshapeToStudy
342 // purpose  : virtual method to add new SubObjects if local selection
343 //=================================================================================
344 void RepairGUI_FuseEdgesDlg::addSubshapesToStudy()
345 {
346   for (int i = 0; i < myPoints.count(); i++)
347     GEOMBase::PublishSubObject(myPoints[i].get());
348 }
349
350 //=================================================================================
351 // function : getSourceObjects
352 // purpose  : virtual method to get source objects
353 //=================================================================================
354 QList<GEOM::GeomObjPtr> RepairGUI_FuseEdgesDlg::getSourceObjects()
355 {
356   QList<GEOM::GeomObjPtr> res(myPoints);
357   GEOM::GeomObjPtr aGeomObjPtr(myShape);
358   res << aGeomObjPtr;
359   return res;
360 }