Salome HOME
Build with native prerequsites
[modules/geom.git] / src / RepairGUI / RepairGUI_CloseContourDlg.cxx
1 // Copyright (C) 2007-2020  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   : RepairGUI_CloseContourDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_CloseContourDlg.h"
28
29 #include "DlgRef.h"
30 #include "GeometryGUI.h"
31 #include "GEOMBase.h"
32 #include "RepairGUI.h"
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38
39 // OCCT Includes
40 #include <TopAbs.hxx>
41 #include <TColStd_MapOfInteger.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43
44 #include <QButtonGroup>
45
46 #include <GEOMImpl_Types.hxx>
47
48 //=================================================================================
49 // class    : RepairGUI_CloseContourDlg()
50 // purpose  : Constructs a RepairGUI_CloseContourDlg  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 RepairGUI_CloseContourDlg::RepairGUI_CloseContourDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
56                                                       bool modal)
57   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
58 {
59   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CLOSECONTOUR")));
60   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
61
62   setWindowTitle(tr("GEOM_CLOSECONTOUR_TITLE"));
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle(tr("GEOM_CLOSECONTOUR_TITLE"));
66   mainFrame()->RadioButton1->setIcon(image0);
67   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
68   mainFrame()->RadioButton2->close();
69   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
70   mainFrame()->RadioButton3->close();
71
72   GroupPoints = new DlgRef_2SelExt(centralWidget());
73   GroupPoints->GroupBox1->setTitle(tr("Contour to close"));
74   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
75   GroupPoints->PushButton1->setIcon(image1);
76   GroupPoints->LineEdit1->setReadOnly(true);
77
78   GroupPoints->TextLabel2->setText(tr("Contour to close"));
79   GroupPoints->PushButton2->setIcon(image1);
80   GroupPoints->LineEdit2->setReadOnly(true);
81
82   QRadioButton* rb1 = new QRadioButton(tr("Close by common vertex"), GroupPoints->Box);
83   QRadioButton* rb2 = new QRadioButton(tr("Close by new edge"),      GroupPoints->Box);
84
85   myIsVertexGr = new QButtonGroup(GroupPoints->Box);
86   myIsVertexGr->addButton(rb1, 0);
87   myIsVertexGr->addButton(rb2, 1);
88   rb1->setChecked(true);
89
90   QVBoxLayout* l = new QVBoxLayout(GroupPoints->Box);
91   l->setMargin(0); l->setSpacing(6);
92   l->addWidget(rb1);
93   l->addWidget(rb2);
94
95   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
96   layout->setMargin(0); layout->setSpacing(6);
97   layout->addWidget(GroupPoints);
98   /***************************************************************/
99
100   setHelpFileName("close_contour_operation_page.html");
101
102   Init();
103 }
104
105 //=================================================================================
106 // function : ~RepairGUI_CloseContourDlg()
107 // purpose  : Destroys the object and frees any allocated resources
108 //=================================================================================
109 RepairGUI_CloseContourDlg::~RepairGUI_CloseContourDlg()
110 {
111 }
112
113 //=================================================================================
114 // function : Init()
115 // purpose  :
116 //=================================================================================
117 void RepairGUI_CloseContourDlg::Init()
118 {
119   // init variables
120   GroupPoints->LineEdit1->clear();
121   GroupPoints->LineEdit2->clear();
122   myObject = GEOM::GEOM_Object::_nil();
123   myWiresInd = new GEOM::short_array();
124   myWiresInd->length(0);
125
126   // signals and slots connections
127   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
128   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129
130   connect(GroupPoints->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
131   connect(GroupPoints->PushButton2, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
132
133   connect(GroupPoints->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
134   connect(GroupPoints->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
135
136   initName(tr("CLOSE_CONTOUR_NEW_OBJ_NAME"));
137
138   GroupPoints->PushButton1->click();
139   SelectionIntoArgument();
140   resize(100,100);
141 }
142
143 //=================================================================================
144 // function : ClickOnOk()
145 // purpose  : Same than click on apply but close this dialog.
146 //=================================================================================
147 void RepairGUI_CloseContourDlg::ClickOnOk()
148 {
149   setIsApplyAndClose( true );
150   if (ClickOnApply())
151     ClickOnCancel();
152 }
153
154 //=================================================================================
155 // function : ClickOnApply()
156 // purpose  :
157 //=================================================================================
158 bool RepairGUI_CloseContourDlg::ClickOnApply()
159 {
160   if (!onAccept())
161     return false;
162
163   initName();
164   // activate first line edit
165   GroupPoints->PushButton1->click();
166   return true;
167 }
168
169 //=================================================================================
170 // function : SelectionIntoArgument()
171 // purpose  : Called when selection is changed or on dialog initialization or activation
172 //=================================================================================
173 void RepairGUI_CloseContourDlg::SelectionIntoArgument()
174 {
175   myEditCurrentArgument->setText("");
176   // the second argument depends on the first one
177   GroupPoints->LineEdit2->setText("");
178   myWiresInd->length(0);
179
180   if (myEditCurrentArgument == GroupPoints->LineEdit1)
181     myObject = GEOM::GEOM_Object::_nil();
182
183   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
184   SALOME_ListIO aSelList;
185   aSelMgr->selectedObjects(aSelList);
186
187   if (aSelList.Extent() == 1) {
188     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
189
190     if (myEditCurrentArgument == GroupPoints->LineEdit1) { // face selection
191       myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
192       if ( GEOMBase::IsShape(myObject) ) {
193         myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
194         TopoDS_Shape aShape;
195         if (GEOMBase::GetShape(myObject, aShape, TopAbs_WIRE))
196           GroupPoints->LineEdit2->setText(myEditCurrentArgument->text());
197
198         // clear selection
199         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
200         myGeomGUI->getApp()->selectionMgr()->clearSelected();
201         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
202                 this, SLOT(SelectionIntoArgument()));
203
204         GroupPoints->PushButton2->click();
205       }
206       else
207         myObject = GEOM::GEOM_Object::_nil();
208     }
209     else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
210       TColStd_IndexedMapOfInteger aMap;
211       aSelMgr->GetIndexes(anIO, aMap);
212       const int n = aMap.Extent();
213       myWiresInd->length(n);
214       for (int i = 1; i <= n; i++)
215         myWiresInd[i-1] = aMap(i);
216       if (n)
217         myEditCurrentArgument->setText(QString::number(n) + "_" + tr("GEOM_WIRE") + tr("_S_"));
218     }
219   }
220 }
221
222 //=================================================================================
223 // function : SetEditCurrentArgument()
224 // purpose  :
225 //=================================================================================
226 void RepairGUI_CloseContourDlg::SetEditCurrentArgument()
227 {
228   QPushButton* send = (QPushButton*)sender();
229
230   bool isEffective = false;
231
232   if (send == GroupPoints->PushButton1) {
233     isEffective = true;
234     myEditCurrentArgument = GroupPoints->LineEdit1;
235
236     GroupPoints->PushButton2->setDown(false);
237     GroupPoints->LineEdit2->setEnabled(false);
238   }
239   else if (send == GroupPoints->PushButton2 && !myObject->_is_nil()) {
240     isEffective = true;
241     myEditCurrentArgument = GroupPoints->LineEdit2;
242
243     GroupPoints->PushButton1->setDown(false);
244     GroupPoints->LineEdit1->setEnabled(false);
245   }
246
247   if (isEffective) {
248     initSelection();
249
250     // enable line edit
251     myEditCurrentArgument->setEnabled(true);
252     myEditCurrentArgument->setFocus();
253     // after setFocus(), because it will be setDown(false) when loses focus
254     send->setDown(true);
255   }
256 }
257
258 //=================================================================================
259 // function : LineEditReturnPressed()
260 // purpose  :
261 //=================================================================================
262 void RepairGUI_CloseContourDlg::LineEditReturnPressed()
263 {
264   const QObject* send = sender();
265   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
266     myEditCurrentArgument = (QLineEdit*)send;
267     GEOMBase_Skeleton::LineEditReturnPressed();
268   }
269 }
270
271 //=================================================================================
272 // function : ActivateThisDialog()
273 // purpose  :
274 //=================================================================================
275 void RepairGUI_CloseContourDlg::ActivateThisDialog()
276 {
277   GEOMBase_Skeleton::ActivateThisDialog();
278   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
279            this, SLOT( SelectionIntoArgument() ) );
280
281   myEditCurrentArgument = GroupPoints->LineEdit1;
282   myEditCurrentArgument->setText( "" );
283   GroupPoints->LineEdit2->setText( "" );
284   myObject = GEOM::GEOM_Object::_nil();
285   myWiresInd->length( 0 );
286
287   initSelection();
288 }
289
290 //=================================================================================
291 // function : enterEvent()
292 // purpose  : Mouse enter onto the dialog to activate it
293 //=================================================================================
294 void RepairGUI_CloseContourDlg::enterEvent (QEvent*)
295 {
296   if (!mainFrame()->GroupConstructors->isEnabled())
297     ActivateThisDialog();
298 }
299
300 //=================================================================================
301 // function : createOperation
302 // purpose  :
303 //=================================================================================
304 GEOM::GEOM_IOperations_ptr RepairGUI_CloseContourDlg::createOperation()
305 {
306   return getGeomEngine()->GetIHealingOperations();
307 }
308
309 //=================================================================================
310 // function : isValid
311 // purpose  :
312 //=================================================================================
313 bool RepairGUI_CloseContourDlg::isValid (QString&)
314 {
315   TopoDS_Shape aTmpShape;
316   return !myObject->_is_nil() && (myWiresInd->length() ||
317                                   GEOMBase::GetShape(myObject, aTmpShape, TopAbs_WIRE));
318 }
319
320 //=================================================================================
321 // function : execute
322 // purpose  :
323 //=================================================================================
324 bool RepairGUI_CloseContourDlg::execute (ObjectList& objects)
325 {
326   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
327   GEOM::GEOM_Object_var anObj = anOper->CloseContour(myObject, myWiresInd, getIsByVertex());
328
329   bool aResult = !anObj->_is_nil();
330   if (aResult)
331   {
332     if ( !IsPreview() )
333       RepairGUI::ShowStatistics( anOper, this );
334     objects.push_back(anObj._retn());
335   }
336   return aResult;
337 }
338
339 //=================================================================================
340 // function : getIsByVertex
341 // purpose  :
342 //=================================================================================
343 bool RepairGUI_CloseContourDlg::getIsByVertex() const
344 {
345   return myIsVertexGr->button(0)->isChecked();
346 }
347
348 //=================================================================================
349 // function : initSelection
350 // purpose  :
351 //=================================================================================
352 void RepairGUI_CloseContourDlg::initSelection()
353 {
354   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
355
356   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
357     TColStd_MapOfInteger aTypes;
358     aTypes.Add(GEOM_COMPOUND);
359     aTypes.Add(GEOM_SOLID);
360     aTypes.Add(GEOM_SHELL);
361     aTypes.Add(GEOM_FACE);
362     aTypes.Add(GEOM_WIRE);
363
364     globalSelection(aTypes);
365   }
366   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
367     localSelection(myObject, TopAbs_EDGE);
368     localSelection(myObject, TopAbs_WIRE);
369   }
370
371   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
372           this, SLOT(SelectionIntoArgument()));
373 }
374
375 //=================================================================================
376 // function : getSourceObjects
377 // purpose  : virtual method to get source objects
378 //=================================================================================
379 QList<GEOM::GeomObjPtr> RepairGUI_CloseContourDlg::getSourceObjects()
380 {
381   QList<GEOM::GeomObjPtr> res;
382   GEOM::GeomObjPtr aGeomObjPtr(myObject);
383   res << aGeomObjPtr;
384   return res;
385 }