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