Salome HOME
Update copyright information
[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
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "RepairGUI_SewingDlg.h"
29
30 #include "SalomeApp_Application.h"
31 #include "LightApp_SelectionMgr.h"
32 #include "SUIT_Session.h"
33 #include "SUIT_MessageBox.h"
34 #include "SALOME_ListIteratorOfListIO.hxx"
35
36 #include "GEOMImpl_Types.hxx"
37
38 #include <TopAbs.hxx>
39 #include <TColStd_MapOfInteger.hxx>
40
41 #define DEFAULT_TOLERANCE_VALUE 1e-07
42
43 using namespace std;
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                                          const char* name, bool modal, WFlags fl)
54   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
55                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
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   setCaption(tr("GEOM_SEWING_TITLE"));
61
62   /***************************************************************/
63   GroupConstructors->setTitle(tr("GEOM_SEWING_TITLE"));
64   RadioButton1->setPixmap(image0);
65   RadioButton2->close(TRUE);
66   RadioButton3->close(TRUE);
67
68   GroupPoints = new DlgRef_1Sel_Ext(this, "GroupPoints");
69   GroupPoints->GroupBox1->setTitle(tr("GEOM_SEWING"));
70   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
71   GroupPoints->PushButton1->setPixmap(image1);
72   GroupPoints->LineEdit1->setReadOnly( true );
73
74   Layout1->addWidget(GroupPoints, 2, 0);
75
76   QGridLayout* aLay = new QGridLayout( 0, 2, 2, 0, 6, "aLay" );
77   myTolEdt = new QtxDblSpinBox( 0, 100, 1e-7, GroupPoints->GroupBox1 );//QAD_SpinBoxDbl( GroupPoints->GroupBox1, 0, 100, 1e-7, 10, 1e-10 );
78   myTolEdt->setPrecision( 10 );
79   myTolEdt->setValue( DEFAULT_TOLERANCE_VALUE );
80   QLabel* aLbl1 = new QLabel( tr( "GEOM_TOLERANCE" ), GroupPoints->GroupBox1 );
81         myFreeBoundBtn = new QPushButton( tr( "GEOM_DETECT" ), GroupPoints->GroupBox1 );
82   QLabel* aLbl2 = new QLabel( tr( "GEOM_FREE_BOUNDARIES" ), GroupPoints->GroupBox1 );
83   aLay->addWidget( aLbl1, 0, 0 );
84   aLay->addWidget( myTolEdt, 0, 1 );
85   aLay->addWidget( aLbl2, 1, 0 );
86   aLay->addWidget( myFreeBoundBtn, 1, 1 );
87
88   GroupPoints->getGroupBoxLayout()->addLayout( aLay, 3, 0 );
89   /***************************************************************/
90
91   setHelpFileName("sewing_operation_page.html");
92
93   Init();
94 }
95
96
97 //=================================================================================
98 // function : ~RepairGUI_SewingDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 RepairGUI_SewingDlg::~RepairGUI_SewingDlg()
102 {
103 }
104
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void RepairGUI_SewingDlg::Init()
111 {
112   /* init variables */
113   myEditCurrentArgument = GroupPoints->LineEdit1;
114
115   myObject = GEOM::GEOM_Object::_nil();
116
117   //myGeomGUI->SetState( 0 );
118   initSelection();
119
120   myClosed = -1;
121   myOpen = -1;
122         
123   /* signals and slots connections */
124   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
125   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
126
127   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
128   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
129
130   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
131           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
132
133   connect(myFreeBoundBtn, SIGNAL(clicked()), this, SLOT(onDetect()));
134
135   initName( tr( "SEWING_NEW_OBJ_NAME" ) );
136 }
137
138
139 //=================================================================================
140 // function : ClickOnOk()
141 // purpose  : Same than click on apply but close this dialog.
142 //=================================================================================
143 void RepairGUI_SewingDlg::ClickOnOk()
144 {
145   if ( ClickOnApply() )
146     ClickOnCancel();
147 }
148
149 //=================================================================================
150 // function : ClickOnApply()
151 // purpose  :
152 //=================================================================================
153 bool RepairGUI_SewingDlg::ClickOnApply()
154 {
155   if ( !onAccept() )
156         return false;
157
158   initName();
159
160   GroupPoints->LineEdit1->setText("");
161   myObject = GEOM::GEOM_Object::_nil();
162
163   initSelection();
164
165   return true;
166 }
167
168
169 //=================================================================================
170 // function : SelectionIntoArgument()
171 // purpose  : Called when selection
172 //=================================================================================
173 void RepairGUI_SewingDlg::SelectionIntoArgument()
174 {
175   erasePreview();
176   myEditCurrentArgument->setText("");
177   myObject = GEOM::GEOM_Object::_nil();
178
179   if ( IObjectCount() == 1 )
180   {
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   {
198     myEditCurrentArgument->setFocus();
199     SelectionIntoArgument();
200   }
201 }
202
203
204 //=================================================================================
205 // function : LineEditReturnPressed()
206 // purpose  :
207 //=================================================================================
208 void RepairGUI_SewingDlg::LineEditReturnPressed()
209 {
210   const QObject* send = sender();
211   if( send == GroupPoints->LineEdit1 )
212   {
213     myEditCurrentArgument = GroupPoints->LineEdit1;
214     GEOMBase_Skeleton::LineEditReturnPressed();
215   }
216 }
217
218
219 //=================================================================================
220 // function : ActivateThisDialog()
221 // purpose  :
222 //=================================================================================
223 void RepairGUI_SewingDlg::ActivateThisDialog()
224 {
225   GEOMBase_Skeleton::ActivateThisDialog();
226   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
227           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
228
229   GroupPoints->LineEdit1->setText("");
230   myObject = GEOM::GEOM_Object::_nil();
231
232   myClosed = -1;
233   myOpen = -1;
234
235   //myGeomGUI->SetState( 0 );
236   initSelection();
237 }
238
239
240 //=================================================================================
241 // function : enterEvent()
242 // purpose  : Mouse enter onto the dialog to activate it
243 //=================================================================================
244 void RepairGUI_SewingDlg::enterEvent(QEvent* e)
245 {
246   if ( !GroupConstructors->isEnabled() )
247     ActivateThisDialog();
248 }
249
250
251 //=================================================================================
252 // function : closeEvent()
253 // purpose  :
254 //=================================================================================
255 void RepairGUI_SewingDlg::closeEvent(QCloseEvent* e)
256 {
257   //myGeomGUI->SetState( -1 );
258   GEOMBase_Skeleton::closeEvent( e );
259 }
260
261 //=================================================================================
262 // function : createOperation
263 // purpose  :
264 //=================================================================================
265 GEOM::GEOM_IOperations_ptr RepairGUI_SewingDlg::createOperation()
266 {
267   return getGeomEngine()->GetIHealingOperations( getStudyId() );
268 }
269
270 //=================================================================================
271 // function : isValid
272 // purpose  :
273 //=================================================================================
274 bool RepairGUI_SewingDlg::isValid( QString& msg )
275 {
276   myClosed = -1;
277   return !myObject->_is_nil() && ( IsPreview() || myTolEdt->value() > 0. );
278 }
279
280 //=================================================================================
281 // function : execute
282 // purpose  :
283 //=================================================================================
284 bool RepairGUI_SewingDlg::execute( ObjectList& objects )
285 {
286   bool aResult = false;
287   if ( IsPreview() ) // called from onDetect(): detect free boundary edges, highlight them (add to objects), display message dialog
288   {
289     GEOM::ListOfGO_var aClosed, anOpen;
290
291     aResult = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->GetFreeBoundary( myObject, aClosed, anOpen );
292
293     if ( aResult )
294     {
295       myClosed = aClosed->length();
296       myOpen = anOpen->length();
297       int i;
298       for ( i = 0; i < myClosed; i++ )
299         objects.push_back( aClosed[i]._retn() );
300       for ( i = 0; i < myOpen; i++ )
301         objects.push_back( anOpen[i]._retn() );
302     }
303     else
304       myClosed = -1;
305   }
306   else
307   {
308     GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->Sew( myObject, myTolEdt->value() );
309     aResult = !anObj->_is_nil();
310     if ( aResult )
311       objects.push_back( anObj._retn() );
312   }
313
314   return aResult;
315 }
316
317 //=================================================================================
318 // function : initSelection
319 // purpose  :
320 //=================================================================================
321 void RepairGUI_SewingDlg::initSelection()
322 {
323   TColStd_MapOfInteger aTypes;
324   aTypes.Add( GEOM_SHELL );
325   aTypes.Add( GEOM_SOLID );
326   aTypes.Add( GEOM_COMPOUND );
327   globalSelection( aTypes );
328 }
329
330 //=================================================================================
331 // function : onDetect
332 // purpose  :
333 //=================================================================================
334 void RepairGUI_SewingDlg::onDetect()
335 {
336   displayPreview( false, true, true, 3 );
337
338   // field myClosed,myOpen is initialized in execute() method, called by displayPreview().
339   QString msg;
340   if ( myClosed != -1 )
341     msg = tr( "GEOM_FREE_BOUNDS_MSG" ).arg( myClosed + myOpen ).arg( myClosed ).arg( myOpen );
342   else
343     msg = tr( "GEOM_FREE_BOUNDS_ERROR" );
344   SUIT_MessageBox::info1( this, tr( "GEOM_FREE_BOUNDS_TLT" ), msg, "Close" );
345 }