Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/geom.git] / src / RepairGUI / RepairGUI_CloseContourDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : RepairGUI_CloseContourDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "RepairGUI_CloseContourDlg.h"
30
31 #include "SalomeApp_Application.h"
32 #include "LightApp_SelectionMgr.h"
33 #include "SUIT_Session.h"
34 #include "SALOME_ListIteratorOfListIO.hxx"
35
36 #include "GEOMImpl_Types.hxx"
37
38 #include <TopAbs.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TColStd_MapOfInteger.hxx>
41
42 #include <qlabel.h>
43
44 using namespace std;
45
46 //=================================================================================
47 // class    : RepairGUI_CloseContourDlg()
48 // purpose  : Constructs a RepairGUI_CloseContourDlg  which is a child of 'parent', with the
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 RepairGUI_CloseContourDlg::RepairGUI_CloseContourDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
54                                                      const char* name, bool modal, WFlags fl)
55   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
56                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
57 {
58   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_CLOSECONTOUR")));
59   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_CLOSECONTOUR_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_CLOSECONTOUR_TITLE"));
65   RadioButton1->setPixmap(image0);
66   RadioButton2->close(TRUE);
67   RadioButton3->close(TRUE);
68
69   GroupPoints = new DlgRef_1Sel_Ext(this, "GroupPoints");
70   GroupPoints->GroupBox1->setTitle(tr("Contour to close"));
71   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
72   GroupPoints->PushButton1->setPixmap(image1);
73   GroupPoints->LineEdit1->setReadOnly( true );
74
75   QGridLayout* aSelectWiresLay = new QGridLayout( 0, 1, 1, 0, 6, "aSelectWiresLay");
76
77   mySelectWiresBtn = new QPushButton( GroupPoints->GroupBox1, "mySelectWiresBtn" );
78   mySelectWiresBtn->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, mySelectWiresBtn->sizePolicy().hasHeightForWidth() ) );
79   mySelectWiresBtn->setText( trUtf8( "" ) );
80   mySelectWiresBtn->setPixmap(image1);
81
82   mySelectWiresEdt = new QLineEdit( GroupPoints->GroupBox1, "mySelectWiresEdt" );
83   mySelectWiresEdt->setReadOnly( true );
84
85   mySelectWiresLbl = new QLabel( tr( "Contour to close" ), GroupPoints->GroupBox1, "ContourLbl" );
86   mySelectWiresLbl->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, 0, 0, mySelectWiresLbl->sizePolicy().hasHeightForWidth() ) );
87
88   aSelectWiresLay->addWidget( mySelectWiresLbl, 0, 0 );
89   aSelectWiresLay->addWidget( mySelectWiresBtn, 0, 1 );
90   aSelectWiresLay->addWidget( mySelectWiresEdt, 0, 2 );
91   QSpacerItem* spacer = new QSpacerItem( 0, 16, QSizePolicy::Minimum, QSizePolicy::Expanding );
92   aSelectWiresLay->addItem( spacer, 1, 2 );
93
94   myIsVertexGr = new QButtonGroup( 2, Qt::Vertical, GroupPoints );
95   myIsVertexGr->setFrameStyle( QFrame::NoFrame );
96   myIsVertexGr->setRadioButtonExclusive( true );
97   myIsVertexGr->insert( new QRadioButton( tr( "Close by common vertex" ), myIsVertexGr ), 0 );
98   myIsVertexGr->insert( new QRadioButton( tr( "Close by new edge" ), myIsVertexGr ), 1 );
99   myIsVertexGr->find( 0 )->toggle();
100
101   Layout1->addWidget(GroupPoints, 2, 0);
102   GroupPoints->getGroupBoxLayout()->addLayout( aSelectWiresLay, 1, 0 );
103   GroupPoints->getGroupBoxLayout()->addMultiCellWidget(myIsVertexGr, 2, 2, 0, 2);
104   /***************************************************************/
105
106   setHelpFileName("close_contour.htm");
107
108   Init();
109 }
110
111
112 //=================================================================================
113 // function : ~RepairGUI_CloseContourDlg()
114 // purpose  : Destroys the object and frees any allocated resources
115 //=================================================================================
116 RepairGUI_CloseContourDlg::~RepairGUI_CloseContourDlg()
117 {
118 }
119
120
121 //=================================================================================
122 // function : Init()
123 // purpose  :
124 //=================================================================================
125 void RepairGUI_CloseContourDlg::Init()
126 {
127   /* init variables */
128   myEditCurrentArgument = GroupPoints->LineEdit1;
129
130   myObject = GEOM::GEOM_Object::_nil();
131   myWiresInd = new GEOM::short_array();
132   myWiresInd->length( 0 );
133   
134   //myGeomGUI->SetState( 0 );
135   initSelection();
136
137   /* signals and slots connections */
138   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
139   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
140
141   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
143   connect(mySelectWiresBtn, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
144   connect(mySelectWiresEdt, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145
146   connect(myGeomGUI->getApp()->selectionMgr(), 
147           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
148
149   initName( tr( "CLOSE_CONTOUR_NEW_OBJ_NAME" ) );
150 }
151
152
153 //=================================================================================
154 // function : ClickOnOk()
155 // purpose  : Same than click on apply but close this dialog.
156 //=================================================================================
157 void RepairGUI_CloseContourDlg::ClickOnOk()
158 {
159   if ( ClickOnApply() )
160     ClickOnCancel();
161 }
162
163
164
165 //=================================================================================
166 // function : ClickOnApply()
167 // purpose  :
168 //=================================================================================
169 bool RepairGUI_CloseContourDlg::ClickOnApply()
170 {
171   if ( !onAccept() )
172     return false;
173
174   initName();
175
176   myEditCurrentArgument = GroupPoints->LineEdit1;
177   myEditCurrentArgument->setText("");
178   myObject = GEOM::GEOM_Object::_nil();
179   myWiresInd->length( 0 );
180
181   initSelection();
182
183   return true;
184 }
185
186 //=================================================================================
187 // function : SelectionIntoArgument()
188 // purpose  : Called when selection as changed or other case
189 //          : used only by SelectButtonC1A1 (LineEditC1A1)
190 //=================================================================================
191 void RepairGUI_CloseContourDlg::SelectionIntoArgument()
192 {
193   erasePreview();
194   myEditCurrentArgument->setText( "" );
195   mySelectWiresEdt->setText( "" );
196   
197   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
198     myObject = GEOM::GEOM_Object::_nil();
199   else if ( myEditCurrentArgument == mySelectWiresEdt )
200     myWiresInd->length( 0 );
201
202   if ( IObjectCount() == 1 )
203   {
204     Handle(SALOME_InteractiveObject) anIO = firstIObject();
205
206     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )  // face selection
207     {
208       Standard_Boolean aRes;
209       myObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
210       if ( aRes && GEOMBase::IsShape( myObject ) )
211       {
212         myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
213         TopoDS_Shape aShape;
214         if ( GEOMBase::GetShape( myObject, aShape, TopAbs_WIRE ) )
215           mySelectWiresEdt->setText( myEditCurrentArgument->text() );
216       }
217       else
218         myObject = GEOM::GEOM_Object::_nil();
219     }
220     else if ( myEditCurrentArgument == mySelectWiresEdt )
221     {
222       TColStd_IndexedMapOfInteger aMap;
223       myGeomGUI->getApp()->selectionMgr()->GetIndexes( anIO, aMap );
224       const int n = aMap.Extent();
225       myWiresInd->length( n );
226       for ( int i = 1; i <= n; i++ )
227         myWiresInd[ i-1 ] = aMap( i );
228       if ( n )
229         myEditCurrentArgument->setText( QString::number( n ) + "_" + tr( "GEOM_WIRE" ) + tr( "_S_" ) );
230     }
231   }
232 }
233
234 //=================================================================================
235 // function : SetEditCurrentArgument()
236 // purpose  :
237 //=================================================================================
238 void RepairGUI_CloseContourDlg::SetEditCurrentArgument()
239 {
240   const QObject* send = sender();
241   if ( send == GroupPoints->PushButton1 )
242     myEditCurrentArgument = GroupPoints->LineEdit1; 
243   else if ( send == mySelectWiresBtn && !myObject->_is_nil() )
244     myEditCurrentArgument = mySelectWiresEdt;
245   if ( myEditCurrentArgument )
246   {
247     initSelection();
248     myEditCurrentArgument->setFocus();
249     SelectionIntoArgument();
250   }
251 }
252
253
254 //=================================================================================
255 // function : LineEditReturnPressed()
256 // purpose  :
257 //=================================================================================
258 void RepairGUI_CloseContourDlg::LineEditReturnPressed()
259 {
260   const QObject* send = sender();
261   if( send == GroupPoints->LineEdit1 || send == mySelectWiresEdt )
262   {
263     myEditCurrentArgument = (QLineEdit*)send;
264     GEOMBase_Skeleton::LineEditReturnPressed();
265   }
266 }
267
268
269 //=================================================================================
270 // function : ActivateThisDialog()
271 // purpose  :
272 //=================================================================================
273 void RepairGUI_CloseContourDlg::ActivateThisDialog()
274 {
275   GEOMBase_Skeleton::ActivateThisDialog();
276   connect(myGeomGUI->getApp()->selectionMgr(), 
277           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
278
279   myEditCurrentArgument = GroupPoints->LineEdit1;
280   myEditCurrentArgument->setText("");
281   mySelectWiresEdt->setText("");
282   myObject = GEOM::GEOM_Object::_nil();
283   myWiresInd->length( 0 );
284
285   //myGeomGUI->SetState( 0 );
286   initSelection();
287 }
288
289
290 //=================================================================================
291 // function : enterEvent()
292 // purpose  : Mouse enter onto the dialog to activate it
293 //=================================================================================
294 void RepairGUI_CloseContourDlg::enterEvent(QEvent* e)
295 {
296   if ( !GroupConstructors->isEnabled() )
297     ActivateThisDialog();
298 }
299
300
301 //=================================================================================
302 // function : closeEvent()
303 // purpose  :
304 //=================================================================================
305 void RepairGUI_CloseContourDlg::closeEvent(QCloseEvent* e)
306 {
307   //myGeomGUI->SetState( -1 );
308   GEOMBase_Skeleton::closeEvent( e );
309 }
310
311 //=================================================================================
312 // function : createOperation
313 // purpose  :
314 //=================================================================================
315 GEOM::GEOM_IOperations_ptr RepairGUI_CloseContourDlg::createOperation()
316 {
317   return getGeomEngine()->GetIHealingOperations( getStudyId() );
318 }
319
320 //=================================================================================
321 // function : isValid
322 // purpose  :
323 //=================================================================================
324 bool RepairGUI_CloseContourDlg::isValid( QString& msg )
325 {
326   TopoDS_Shape aTmpShape;
327   return !myObject->_is_nil() && ( myWiresInd->length() || GEOMBase::GetShape( myObject, aTmpShape, TopAbs_WIRE ) );
328 }
329
330 //=================================================================================
331 // function : execute
332 // purpose  :
333 //=================================================================================
334 bool RepairGUI_CloseContourDlg::execute( ObjectList& objects )
335 {
336   GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->CloseContour( myObject, myWiresInd, getIsByVertex() );
337   bool aResult = !anObj->_is_nil();
338   if ( aResult )
339     objects.push_back( anObj._retn() );
340     
341   return aResult;
342 }
343
344 //=================================================================================
345 // function : getIsByVertex
346 // purpose  :
347 //=================================================================================
348 bool RepairGUI_CloseContourDlg::getIsByVertex() const
349 {
350   return myIsVertexGr->find( 0 )->isOn();
351 }
352
353 //=================================================================================
354 // function : initSelection
355 // purpose  :
356 //=================================================================================
357 void RepairGUI_CloseContourDlg::initSelection()
358 {
359   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
360   {
361     TColStd_MapOfInteger aTypes;
362     aTypes.Add( GEOM_COMPOUND );
363     aTypes.Add( GEOM_SOLID );
364     aTypes.Add( GEOM_SHELL );
365     aTypes.Add( GEOM_FACE );
366     aTypes.Add( GEOM_WIRE );
367     
368     globalSelection( aTypes );
369   }
370   else if ( myEditCurrentArgument == mySelectWiresEdt )
371   {
372     localSelection( myObject, TopAbs_EDGE );
373     localSelection( myObject, TopAbs_WIRE );
374   }
375 }