Salome HOME
38ccb0933932eef53d25cad3cf4e369e452cf6b5
[modules/geom.git] / src / GenerationGUI / GenerationGUI_FillingDlg.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   : GenerationGUI_FillingDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "GenerationGUI_FillingDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <TopoDS_Iterator.hxx>
38 #include <GEOMImpl_Types.hxx>
39
40 //=================================================================================
41 // class    : GenerationGUI_FillingDlg()
42 // purpose  : Constructs a GenerationGUI_FillingDlg which is a child of 'parent', with the
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 GenerationGUI_FillingDlg::GenerationGUI_FillingDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
48                                                     bool modal, Qt::WindowFlags fl )
49   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
50 {
51   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_FILLING" ) ) );
52   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
53
54   setWindowTitle( tr( "GEOM_FILLING_TITLE" ) );
55
56   /***************************************************************/
57   mainFrame()->GroupConstructors->setTitle( tr ( "GEOM_FILLING" ) );
58   mainFrame()->RadioButton1->setIcon( image0 );
59   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
60   mainFrame()->RadioButton2->close();
61   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton3->close();
63
64   GroupPoints = new DlgRef_1Sel5Spin1Check( centralWidget() );
65   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
66   GroupPoints->TextLabel1->setText( tr( "GEOM_FILLING_COMPOUND" ) );
67   GroupPoints->TextLabel2->setText( tr( "GEOM_FILLING_MIN_DEG" ) );
68   GroupPoints->TextLabel3->setText( tr( "GEOM_FILLING_TOL_2D" ) );
69   GroupPoints->TextLabel4->setText( tr( "GEOM_FILLING_NB_ITER" ) );
70   GroupPoints->TextLabel5->setText( tr( "GEOM_FILLING_MAX_DEG" ) );
71   GroupPoints->TextLabel6->setText( tr( "GEOM_FILLING_TOL_3D" ) );
72   GroupPoints->CheckBox1->setText( tr( "GEOM_FILLING_APPROX" ) );
73   GroupPoints->TextLabel7->setText( tr( "GEOM_FILLING_METHOD" ) );
74   GroupPoints->PushButton1->setIcon( image1 );
75   GroupPoints->LineEdit1->setReadOnly( true );
76
77   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
78   layout->setMargin( 0 ); layout->setSpacing( 6 );
79   layout->addWidget( GroupPoints  );
80   /***************************************************************/
81
82   setHelpFileName( "create_filling_page.html" );
83
84   /* Initialisations */
85   Init();
86 }
87
88 //=================================================================================
89 // function : ~GenerationGUI_FillingDlg()
90 // purpose  : Destroys the object and frees any allocated resources
91 //=================================================================================
92 GenerationGUI_FillingDlg::~GenerationGUI_FillingDlg()
93 {
94   // no need to delete child widgets, Qt does it all for us
95 }
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void GenerationGUI_FillingDlg::Init()
102 {
103   /* init variables */
104   myEditCurrentArgument = GroupPoints->LineEdit1;
105   GroupPoints->LineEdit1->setReadOnly( true );
106
107   myMinDeg = 2;
108   myMaxDeg = 5;
109   myTol3D = 0.0001;
110   myTol2D = 0.0001;
111   myNbIter = 0;
112   myMethod = 0;
113   myIsApprox = false;
114   myOkCompound = false;
115
116   globalSelection( GEOM_COMPOUND );
117
118   double SpecificStep1 = 1;
119   double SpecificStep2 = 0.0001;
120   /* min, max, step and decimals for spin boxes & initial values */
121   initSpinBox( GroupPoints->SpinBox1, 2.0, MAX_NUMBER,  SpecificStep1, 3 );
122   initSpinBox( GroupPoints->SpinBox2, 0.00001, 10000.0, SpecificStep2, 5 );
123   initSpinBox( GroupPoints->SpinBox3, 0.0, MAX_NUMBER,  SpecificStep1, 3 );
124   initSpinBox( GroupPoints->SpinBox4, 1.0, MAX_NUMBER,  SpecificStep1, 3 );
125   initSpinBox( GroupPoints->SpinBox5, 0.00001, 10000.0, SpecificStep2, 5 );
126
127   GroupPoints->SpinBox1->setValue( myMinDeg );
128   GroupPoints->SpinBox2->setValue( myTol2D );
129   GroupPoints->SpinBox3->setValue( myNbIter );
130   GroupPoints->SpinBox4->setValue( myMaxDeg );
131   GroupPoints->SpinBox5->setValue( myTol3D );
132
133   GroupPoints->ComboBox1->addItem(tr("GEOM_FILLING_DEFAULT"));
134   GroupPoints->ComboBox1->addItem(tr("GEOM_FILLING_USEORI"));
135   GroupPoints->ComboBox1->addItem(tr("GEOM_FILLING_AUTO"));
136
137   /* signals and slots connections */
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
144   connect( GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(MethodChanged()));
145
146   connect( GroupPoints->SpinBox1, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
147   connect( GroupPoints->SpinBox2, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
148   connect( GroupPoints->SpinBox3, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
149   connect( GroupPoints->SpinBox4, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
150   connect( GroupPoints->SpinBox5, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
151
152   connect( GroupPoints->CheckBox1, SIGNAL( stateChanged( int ) ),
153            this, SLOT( ApproxChanged() ) );
154
155   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
156
157   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
158            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
159
160   initName( tr( "GEOM_FILLING" ) );
161   resize(100,100);
162 }
163
164 //=================================================================================
165 // function : SetDoubleSpinBoxStep()
166 // purpose  : Double spin box management
167 //=================================================================================
168 void GenerationGUI_FillingDlg::SetDoubleSpinBoxStep( double step )
169 {
170   GroupPoints->SpinBox1->setSingleStep(step);
171   GroupPoints->SpinBox2->setSingleStep(step);
172   GroupPoints->SpinBox3->setSingleStep(step);
173   GroupPoints->SpinBox4->setSingleStep(step);
174   GroupPoints->SpinBox5->setSingleStep(step);
175 }
176
177
178 //=================================================================================
179 // function : ClickOnOk()
180 // purpose  :
181 //=================================================================================
182 void GenerationGUI_FillingDlg::ClickOnOk()
183 {
184   if ( ClickOnApply() )
185     ClickOnCancel();
186 }
187
188
189 //=================================================================================
190 // function : ClickOnApply()
191 // purpose  :
192 //=================================================================================
193 bool GenerationGUI_FillingDlg::ClickOnApply()
194 {
195   if ( !onAccept() )
196     return false;
197
198   initName();
199   return true;
200 }
201
202
203 //=================================================================================
204 // function : SelectionIntoArgument()
205 // purpose  : Called when selection as changed or other case
206 //=================================================================================
207 void GenerationGUI_FillingDlg::SelectionIntoArgument()
208 {
209   erasePreview();
210   myEditCurrentArgument->setText("");
211
212   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
213   SALOME_ListIO aSelList;
214   aSelMgr->selectedObjects(aSelList);
215
216   if (aSelList.Extent() != 1) {
217     if (myEditCurrentArgument == GroupPoints->LineEdit1)
218       myOkCompound = false;
219     return;
220   }
221
222   // nbSel == 1
223   Standard_Boolean testResult = Standard_False;
224   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
225
226   if (!testResult)
227     return;
228
229   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
230     TopoDS_Shape S;
231     myOkCompound = false;
232
233     if (GEOMBase::GetShape(aSelectedObject, S) && S.ShapeType() == TopAbs_COMPOUND) {
234       // myCompound should be a compound of edges
235       for (TopoDS_Iterator it (S); it.More(); it.Next())
236         if ( it.Value().ShapeType() != TopAbs_EDGE && 
237              it.Value().ShapeType() != TopAbs_WIRE )
238           return;
239       myCompound = aSelectedObject;
240       myOkCompound = true;
241     }
242   }
243
244   myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
245   displayPreview();
246 }
247
248 //=================================================================================
249 // function : SetEditCurrentArgument()
250 // purpose  :
251 //=================================================================================
252 void GenerationGUI_FillingDlg::SetEditCurrentArgument()
253 {
254   QPushButton* send = (QPushButton*)sender();
255   globalSelection( GEOM_ALLSHAPES );
256
257   if ( send == GroupPoints->PushButton1 ) {
258     GroupPoints->LineEdit1->setFocus();
259     myEditCurrentArgument = GroupPoints->LineEdit1;
260     globalSelection( GEOM_COMPOUND );
261     this->SelectionIntoArgument();
262   }
263 }
264
265
266 //=================================================================================
267 // function : LineEditReturnPressed()
268 // purpose  :
269 //=================================================================================
270 void GenerationGUI_FillingDlg::LineEditReturnPressed()
271 {
272   QLineEdit* send = (QLineEdit*)sender();
273   if ( send == GroupPoints->LineEdit1 ) {
274     myEditCurrentArgument = send;
275     GEOMBase_Skeleton::LineEditReturnPressed();
276   }
277 }
278
279
280 //=================================================================================
281 // function : ActivateThisDialog()
282 // purpose  :
283 //=================================================================================
284 void GenerationGUI_FillingDlg::ActivateThisDialog()
285 {
286   GEOMBase_Skeleton::ActivateThisDialog();
287   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
288            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
289   globalSelection( GEOM_COMPOUND );
290   displayPreview();
291 }
292
293
294 //=================================================================================
295 // function : enterEvent()
296 // purpose  :
297 //=================================================================================
298 void GenerationGUI_FillingDlg::enterEvent( QEvent* )
299 {
300   if ( !mainFrame()->GroupConstructors->isEnabled() )
301     ActivateThisDialog();
302 }
303
304
305 //=================================================================================
306 // function : ValueChangedInSpinBox()
307 // purpose  :
308 //=================================================================================
309 void GenerationGUI_FillingDlg::ValueChangedInSpinBox( double newValue )
310 {
311   QObject* send = (QObject*)sender();
312
313   if ( send == GroupPoints->SpinBox1 )
314     myMinDeg = int( newValue );
315   else if ( send == GroupPoints->SpinBox2 )
316     myTol2D = newValue;
317   else if ( send == GroupPoints->SpinBox3 )
318     myNbIter = int(newValue);
319   else if ( send == GroupPoints->SpinBox4 )
320     myMaxDeg = int(newValue);
321   else if ( send == GroupPoints->SpinBox5 )
322     myTol3D = newValue;
323
324   displayPreview();
325 }
326
327 //=================================================================================
328 // function : MethodChanged
329 // purpose  :
330 //=================================================================================
331 void GenerationGUI_FillingDlg::MethodChanged()
332 {
333   myMethod = GroupPoints->ComboBox1->currentIndex();
334   displayPreview();
335 }
336
337 //=================================================================================
338 // function : ApproxChanged()
339 // purpose  :
340 //=================================================================================
341 void GenerationGUI_FillingDlg::ApproxChanged()
342 {
343   myIsApprox = GroupPoints->CheckBox1->isChecked();
344   displayPreview();
345 }
346
347
348 //=================================================================================
349 // function : createOperation
350 // purpose  :
351 //=================================================================================
352 GEOM::GEOM_IOperations_ptr GenerationGUI_FillingDlg::createOperation()
353 {
354   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
355 }
356
357 //=================================================================================
358 // function : isValid
359 // purpose  :
360 //=================================================================================
361 bool GenerationGUI_FillingDlg::isValid( QString& msg )
362 {
363   bool ok = true;
364   ok = GroupPoints->SpinBox1->isValid( msg, !IsPreview() ) && ok;
365   ok = GroupPoints->SpinBox2->isValid( msg, !IsPreview() ) && ok;
366   ok = GroupPoints->SpinBox3->isValid( msg, !IsPreview() ) && ok;
367   ok = GroupPoints->SpinBox4->isValid( msg, !IsPreview() ) && ok;
368   ok = GroupPoints->SpinBox5->isValid( msg, !IsPreview() ) && ok;
369
370   return myOkCompound > 0 && ok;
371 }
372
373 //=================================================================================
374 // function : execute
375 // purpose  :
376 //=================================================================================
377 bool GenerationGUI_FillingDlg::execute( ObjectList& objects )
378 {
379   GEOM::GEOM_I3DPrimOperations_var anOper =
380     GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
381
382   GEOM::filling_oper_method aMethod;
383   switch (GroupPoints->ComboBox1->currentIndex())
384   {
385     case 0:  aMethod = GEOM::FOM_Default; break;
386     case 1:  aMethod = GEOM::FOM_UseOri; break;
387     case 2:  aMethod = GEOM::FOM_AutoCorrect; break;
388     default: break;
389   }
390
391   GEOM::GEOM_Object_var anObj =
392     anOper->MakeFilling( myCompound, myMinDeg, myMaxDeg, myTol2D, myTol3D,
393                          myNbIter, aMethod, myIsApprox );
394   if ( !anObj->_is_nil() )
395   {
396     if ( !IsPreview() )
397     {
398       QStringList aParameters;
399       aParameters << GroupPoints->SpinBox1->text();
400       aParameters << GroupPoints->SpinBox2->text();
401       aParameters << GroupPoints->SpinBox3->text();
402       aParameters << GroupPoints->SpinBox4->text();
403       aParameters << GroupPoints->SpinBox5->text();
404       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
405     }
406     objects.push_back( anObj._retn() );
407   }
408
409   return true;
410 }