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