Salome HOME
untabify
[modules/geom.git] / src / RepairGUI / RepairGUI_SewingDlg.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : RepairGUI_SewingDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "RepairGUI_SewingDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31 #include <QtxDoubleSpinBox.h>
32
33 #include <SalomeApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_ResourceMgr.h>
38
39 #include <GEOMImpl_Types.hxx>
40
41 #include <TopAbs.hxx>
42 #include <TColStd_MapOfInteger.hxx>
43
44 #define DEFAULT_TOLERANCE_VALUE 1e-07
45
46 //=================================================================================
47 // class    : RepairGUI_SewingDlg()
48 // purpose  : Constructs a RepairGUI_SewingDlg  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_SewingDlg::RepairGUI_SewingDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                           bool modal )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
56 {
57   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SEWING" ) ) );
58   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
59
60   setWindowTitle( tr( "GEOM_SEWING_TITLE" ) );
61
62   /***************************************************************/
63   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SEWING_TITLE" ) );
64   mainFrame()->RadioButton1->setIcon( image0 );
65   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
66   mainFrame()->RadioButton2->close();
67   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
68   mainFrame()->RadioButton3->close();
69
70   GroupPoints = new DlgRef_1SelExt( centralWidget() );
71   GroupPoints->GroupBox1->setTitle( tr( "GEOM_SEWING" ) );
72   GroupPoints->TextLabel1->setText( tr( "GEOM_SELECTED_SHAPE" ) );
73   GroupPoints->PushButton1->setIcon( image1 );
74   GroupPoints->LineEdit1->setReadOnly( true );
75
76   QGridLayout* aLay = new QGridLayout( GroupPoints->Box );
77   aLay->setMargin( 0 ); aLay->setSpacing( 6 );
78   myTolEdt = new SalomeApp_DoubleSpinBox( 0.0, 100.0, DEFAULT_TOLERANCE_VALUE, 7, 10, GroupPoints->Box );
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   resize(100,100);
138 }
139
140
141 //=================================================================================
142 // function : ClickOnOk()
143 // purpose  : Same than click on apply but close this dialog.
144 //=================================================================================
145 void RepairGUI_SewingDlg::ClickOnOk()
146 {
147   if ( ClickOnApply() )
148     ClickOnCancel();
149 }
150
151 //=================================================================================
152 // function : ClickOnApply()
153 // purpose  :
154 //=================================================================================
155 bool RepairGUI_SewingDlg::ClickOnApply()
156 {
157   if ( !onAccept() )
158         return false;
159
160   initName();
161
162   GroupPoints->LineEdit1->setText( "" );
163   myObject = GEOM::GEOM_Object::_nil();
164
165   initSelection();
166
167   return true;
168 }
169
170
171 //=================================================================================
172 // function : SelectionIntoArgument()
173 // purpose  : Called when selection
174 //=================================================================================
175 void RepairGUI_SewingDlg::SelectionIntoArgument()
176 {
177   erasePreview();
178   myEditCurrentArgument->setText( "" );
179   myObject = GEOM::GEOM_Object::_nil();
180
181   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
182   SALOME_ListIO aSelList;
183   aSelMgr->selectedObjects(aSelList);
184
185   if ( aSelList.Extent() == 1 ) {
186     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
187     Standard_Boolean aRes;
188     myObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
189     if ( aRes )
190       myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
191   }
192 }
193
194 //=================================================================================
195 // function : SetEditCurrentArgument()
196 // purpose  :
197 //=================================================================================
198 void RepairGUI_SewingDlg::SetEditCurrentArgument()
199 {
200   const QObject* send = sender();
201   if ( send == GroupPoints->PushButton1 ) {
202     myEditCurrentArgument->setFocus();
203     SelectionIntoArgument();
204   }
205 }
206
207
208 //=================================================================================
209 // function : LineEditReturnPressed()
210 // purpose  :
211 //=================================================================================
212 void RepairGUI_SewingDlg::LineEditReturnPressed()
213 {
214   const QObject* send = sender();
215   if( send == GroupPoints->LineEdit1 ) {
216     myEditCurrentArgument = GroupPoints->LineEdit1;
217     GEOMBase_Skeleton::LineEditReturnPressed();
218   }
219 }
220
221
222 //=================================================================================
223 // function : ActivateThisDialog()
224 // purpose  :
225 //=================================================================================
226 void RepairGUI_SewingDlg::ActivateThisDialog()
227 {
228   GEOMBase_Skeleton::ActivateThisDialog();
229   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
230            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
231
232   GroupPoints->LineEdit1->setText( "" );
233   myObject = GEOM::GEOM_Object::_nil();
234
235   myClosed = -1;
236   myOpen = -1;
237
238   //myGeomGUI->SetState( 0 );
239   initSelection();
240 }
241
242
243 //=================================================================================
244 // function : enterEvent()
245 // purpose  : Mouse enter onto the dialog to activate it
246 //=================================================================================
247 void RepairGUI_SewingDlg::enterEvent( QEvent* )
248 {
249   if ( !mainFrame()->GroupConstructors->isEnabled() )
250     ActivateThisDialog();
251 }
252
253
254 //=================================================================================
255 // function : createOperation
256 // purpose  :
257 //=================================================================================
258 GEOM::GEOM_IOperations_ptr RepairGUI_SewingDlg::createOperation()
259 {
260   return getGeomEngine()->GetIHealingOperations( getStudyId() );
261 }
262
263 //=================================================================================
264 // function : isValid
265 // purpose  :
266 //=================================================================================
267 bool RepairGUI_SewingDlg::isValid( QString& msg )
268 {
269   myClosed = -1;
270   bool ok = myTolEdt->isValid( msg, !IsPreview() );
271   return !myObject->_is_nil() && ( IsPreview() || myTolEdt->value() > 0. ) && ok;
272 }
273
274 //=================================================================================
275 // function : execute
276 // purpose  :
277 //=================================================================================
278 bool RepairGUI_SewingDlg::execute( ObjectList& objects )
279 {
280   bool aResult = false;
281   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
282
283   if ( IsPreview() ) { // called from onDetect(): detect free boundary edges, highlight them (add to objects), display message dialog
284     GEOM::ListOfGO_var aClosed, anOpen;
285
286     aResult = anOper->GetFreeBoundary( myObject, aClosed, anOpen );
287
288     if ( aResult ) {
289       myClosed = aClosed->length();
290       myOpen = anOpen->length();
291       int i;
292       for ( i = 0; i < myClosed; i++ )
293         objects.push_back( aClosed[i]._retn() );
294       for ( i = 0; i < myOpen; i++ )
295         objects.push_back( anOpen[i]._retn() );
296     }
297     else
298       myClosed = -1;
299   }
300   else {
301     GEOM::GEOM_Object_var anObj = anOper->Sew( myObject, myTolEdt->value() );
302     aResult = !anObj->_is_nil();
303     if ( aResult )
304     {
305       if ( !IsPreview() )
306       {
307         QStringList aParameters;
308         aParameters << myTolEdt->text();
309         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
310       }
311       objects.push_back( anObj._retn() );
312     }
313   }
314
315   return aResult;
316 }
317
318 //=================================================================================
319 // function : initSelection
320 // purpose  :
321 //=================================================================================
322 void RepairGUI_SewingDlg::initSelection()
323 {
324   TColStd_MapOfInteger aTypes;
325   aTypes.Add( GEOM_SHELL );
326   aTypes.Add( GEOM_SOLID );
327   aTypes.Add( GEOM_COMPOUND );
328   globalSelection( aTypes );
329 }
330
331 //=================================================================================
332 // function : onDetect
333 // purpose  :
334 //=================================================================================
335 void RepairGUI_SewingDlg::onDetect()
336 {
337   displayPreview( false, true, true, 3 );
338
339   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
340   QString msg;
341   if ( myClosed != -1 )
342     msg = tr( "GEOM_FREE_BOUNDS_MSG" ).arg( myClosed + myOpen ).arg( myClosed ).arg( myOpen );
343   else
344     msg = tr( "GEOM_FREE_BOUNDS_ERROR" );
345   SUIT_MessageBox::information( this, tr( "GEOM_FREE_BOUNDS_TLT" ), msg );
346 }