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