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