Salome HOME
15648d3e1b0843fa514c5b7c2ac11a5c7e0c8e33
[modules/geom.git] / src / RepairGUI / RepairGUI_SewingDlg.cxx
1 // Copyright (C) 2007-2012  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : RepairGUI_SewingDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_SewingDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32 #include <SalomeApp_DoubleSpinBox.h>
33
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <GEOMImpl_Types.hxx>
41
42 #include <TopAbs.hxx>
43 #include <TColStd_MapOfInteger.hxx>
44
45 #define DEFAULT_TOLERANCE_VALUE 1e-07
46
47 //=================================================================================
48 // class    : RepairGUI_SewingDlg()
49 // purpose  : Constructs a RepairGUI_SewingDlg  which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 RepairGUI_SewingDlg::RepairGUI_SewingDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
55                                           bool modal )
56   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
57 {
58   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SEWING" ) ) );
59   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
60
61   setWindowTitle( tr( "GEOM_SEWING_TITLE" ) );
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SEWING_TITLE" ) );
65   mainFrame()->RadioButton1->setIcon( image0 );
66   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
67   mainFrame()->RadioButton2->close();
68   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
69   mainFrame()->RadioButton3->close();
70
71   GroupPoints = new DlgRef_1SelExt( centralWidget() );
72   GroupPoints->GroupBox1->setTitle( tr( "GEOM_SEWING" ) );
73   GroupPoints->TextLabel1->setText( tr( "GEOM_SELECTED_SHAPE" ) );
74   GroupPoints->PushButton1->setIcon( image1 );
75   GroupPoints->LineEdit1->setReadOnly( true );
76
77   QGridLayout* aLay = new QGridLayout( GroupPoints->Box );
78   aLay->setMargin( 0 ); aLay->setSpacing( 6 );
79   myTolEdt = new SalomeApp_DoubleSpinBox( GroupPoints->Box );
80   initSpinBox( myTolEdt, 0.0, 100.0, DEFAULT_TOLERANCE_VALUE, "len_tol_precision" );
81   myTolEdt->setValue( DEFAULT_TOLERANCE_VALUE );
82   QLabel* aLbl1 = new QLabel( tr( "GEOM_TOLERANCE" ), GroupPoints->Box );
83   myFreeBoundBtn = new QPushButton( tr( "GEOM_DETECT" ) + QString( " [%1]" ).arg( tr( "GEOM_FREE_BOUNDARIES" ) ), 
84                                     GroupPoints->Box );
85   aLay->addWidget( aLbl1,          0, 0 );
86   aLay->addWidget( myTolEdt,       0, 1 );
87   aLay->addWidget( myFreeBoundBtn, 1, 0, 1, 2 );
88
89   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
90   layout->setMargin( 0 ); layout->setSpacing( 6 );
91   layout->addWidget( GroupPoints );
92   /***************************************************************/
93
94   setHelpFileName( "sewing_operation_page.html" );
95
96   Init();
97 }
98
99
100 //=================================================================================
101 // function : ~RepairGUI_SewingDlg()
102 // purpose  : Destroys the object and frees any allocated resources
103 //=================================================================================
104 RepairGUI_SewingDlg::~RepairGUI_SewingDlg()
105 {
106 }
107
108
109 //=================================================================================
110 // function : Init()
111 // purpose  :
112 //=================================================================================
113 void RepairGUI_SewingDlg::Init()
114 {
115   /* init variables */
116   myEditCurrentArgument = GroupPoints->LineEdit1;
117
118   myObject = GEOM::GEOM_Object::_nil();
119
120   //myGeomGUI->SetState( 0 );
121   initSelection();
122
123   myClosed = -1;
124   myOpen = -1;
125         
126   /* signals and slots connections */
127   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
128   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
129
130   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
131   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
132
133   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
134            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
135
136   connect( myFreeBoundBtn, SIGNAL( clicked() ), this, SLOT( onDetect() ) );
137
138   initName( tr( "SEWING_NEW_OBJ_NAME" ) );
139   resize(100,100);
140   SelectionIntoArgument();
141 }
142
143
144 //=================================================================================
145 // function : ClickOnOk()
146 // purpose  : Same than click on apply but close this dialog.
147 //=================================================================================
148 void RepairGUI_SewingDlg::ClickOnOk()
149 {
150   setIsApplyAndClose( true );
151   if ( ClickOnApply() )
152     ClickOnCancel();
153 }
154
155 //=================================================================================
156 // function : ClickOnApply()
157 // purpose  :
158 //=================================================================================
159 bool RepairGUI_SewingDlg::ClickOnApply()
160 {
161   if ( !onAccept() )
162         return false;
163
164   initName();
165
166   GroupPoints->LineEdit1->setText( "" );
167   myObject = GEOM::GEOM_Object::_nil();
168
169   initSelection();
170
171   return true;
172 }
173
174
175 //=================================================================================
176 // function : SelectionIntoArgument()
177 // purpose  : Called when selection
178 //=================================================================================
179 void RepairGUI_SewingDlg::SelectionIntoArgument()
180 {
181   erasePreview();
182   myEditCurrentArgument->setText( "" );
183   myObject = GEOM::GEOM_Object::_nil();
184
185   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
186   SALOME_ListIO aSelList;
187   aSelMgr->selectedObjects(aSelList);
188
189   if ( aSelList.Extent() == 1 ) {
190     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
191     myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
192     if ( !CORBA::is_nil( myObject ) )
193       myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
194   }
195 }
196
197 //=================================================================================
198 // function : SetEditCurrentArgument()
199 // purpose  :
200 //=================================================================================
201 void RepairGUI_SewingDlg::SetEditCurrentArgument()
202 {
203   const QObject* send = sender();
204   if ( send == GroupPoints->PushButton1 ) {
205     myEditCurrentArgument->setFocus();
206     SelectionIntoArgument();
207   }
208 }
209
210
211 //=================================================================================
212 // function : LineEditReturnPressed()
213 // purpose  :
214 //=================================================================================
215 void RepairGUI_SewingDlg::LineEditReturnPressed()
216 {
217   const QObject* send = sender();
218   if( send == GroupPoints->LineEdit1 ) {
219     myEditCurrentArgument = GroupPoints->LineEdit1;
220     GEOMBase_Skeleton::LineEditReturnPressed();
221   }
222 }
223
224
225 //=================================================================================
226 // function : ActivateThisDialog()
227 // purpose  :
228 //=================================================================================
229 void RepairGUI_SewingDlg::ActivateThisDialog()
230 {
231   GEOMBase_Skeleton::ActivateThisDialog();
232   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
233            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
234
235   GroupPoints->LineEdit1->setText( "" );
236   myObject = GEOM::GEOM_Object::_nil();
237
238   myClosed = -1;
239   myOpen = -1;
240
241   //myGeomGUI->SetState( 0 );
242   initSelection();
243 }
244
245
246 //=================================================================================
247 // function : enterEvent()
248 // purpose  : Mouse enter onto the dialog to activate it
249 //=================================================================================
250 void RepairGUI_SewingDlg::enterEvent( QEvent* )
251 {
252   if ( !mainFrame()->GroupConstructors->isEnabled() )
253     ActivateThisDialog();
254 }
255
256
257 //=================================================================================
258 // function : createOperation
259 // purpose  :
260 //=================================================================================
261 GEOM::GEOM_IOperations_ptr RepairGUI_SewingDlg::createOperation()
262 {
263   return getGeomEngine()->GetIHealingOperations( getStudyId() );
264 }
265
266 //=================================================================================
267 // function : isValid
268 // purpose  :
269 //=================================================================================
270 bool RepairGUI_SewingDlg::isValid( QString& msg )
271 {
272   myClosed = -1;
273   bool ok = myTolEdt->isValid( msg, !IsPreview() );
274   return !myObject->_is_nil() && ( IsPreview() || myTolEdt->value() > 0. ) && ok;
275 }
276
277 //=================================================================================
278 // function : execute
279 // purpose  :
280 //=================================================================================
281 bool RepairGUI_SewingDlg::execute( ObjectList& objects )
282 {
283   bool aResult = false;
284   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
285
286   if ( IsPreview() ) { // called from onDetect(): detect free boundary edges, highlight them (add to objects), display message dialog
287     GEOM::ListOfGO_var aClosed, anOpen;
288
289     aResult = anOper->GetFreeBoundary( myObject, aClosed, anOpen );
290
291     if ( aResult ) {
292       myClosed = aClosed->length();
293       myOpen = anOpen->length();
294       int i;
295       for ( i = 0; i < myClosed; i++ )
296         objects.push_back( aClosed[i]._retn() );
297       for ( i = 0; i < myOpen; i++ )
298         objects.push_back( anOpen[i]._retn() );
299     }
300     else
301       myClosed = -1;
302   }
303   else {
304     GEOM::GEOM_Object_var anObj = anOper->Sew( myObject, myTolEdt->value() );
305     aResult = !anObj->_is_nil();
306     if ( aResult )
307     {
308       if ( !IsPreview() )
309       {
310         QStringList aParameters;
311         aParameters << myTolEdt->text();
312         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
313       }
314       objects.push_back( anObj._retn() );
315     }
316   }
317
318   return aResult;
319 }
320
321 //=================================================================================
322 // function : initSelection
323 // purpose  :
324 //=================================================================================
325 void RepairGUI_SewingDlg::initSelection()
326 {
327   TColStd_MapOfInteger aTypes;
328   aTypes.Add( GEOM_SHELL );
329   aTypes.Add( GEOM_SOLID );
330   aTypes.Add( GEOM_COMPOUND );
331   globalSelection( aTypes );
332 }
333
334 //=================================================================================
335 // function : onDetect
336 // purpose  :
337 //=================================================================================
338 void RepairGUI_SewingDlg::onDetect()
339 {
340   displayPreview( true, false, true, true, 3 );
341
342   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
343   QString msg;
344   if ( myClosed != -1 )
345     msg = tr( "GEOM_FREE_BOUNDS_MSG" ).arg( myClosed + myOpen ).arg( myClosed ).arg( myOpen );
346   else
347     msg = tr( "GEOM_FREE_BOUNDS_ERROR" );
348   SUIT_MessageBox::information( this, tr( "GEOM_FREE_BOUNDS_TLT" ), msg );
349 }