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