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