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