Salome HOME
Preparation of intermediate revision
[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 <SalomeApp_Notebook.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <TopoDS_Iterator.hxx>
39 #include <GEOMImpl_Types.hxx>
40
41 //=================================================================================
42 // class    : GenerationGUI_FillingDlg()
43 // purpose  : Constructs a GenerationGUI_FillingDlg which is a child of 'parent', with the
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 GenerationGUI_FillingDlg::GenerationGUI_FillingDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
49                                                     bool modal, Qt::WindowFlags fl )
50   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
51 {
52   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_FILLING" ) ) );
53   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
54
55   setWindowTitle( tr( "GEOM_FILLING_TITLE" ) );
56
57   /***************************************************************/
58   mainFrame()->GroupConstructors->setTitle( tr ( "GEOM_FILLING" ) );
59   mainFrame()->RadioButton1->setIcon( image0 );
60   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton2->close();
62   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
63   mainFrame()->RadioButton3->close();
64
65   GroupPoints = new DlgRef_1Sel5Spin1Check( centralWidget() );
66   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
67   GroupPoints->TextLabel1->setText( tr( "GEOM_FILLING_COMPOUND" ) );
68   GroupPoints->TextLabel2->setText( tr( "GEOM_FILLING_MIN_DEG" ) );
69   GroupPoints->TextLabel3->setText( tr( "GEOM_FILLING_TOL_2D" ) );
70   GroupPoints->TextLabel4->setText( tr( "GEOM_FILLING_NB_ITER" ) );
71   GroupPoints->TextLabel5->setText( tr( "GEOM_FILLING_MAX_DEG" ) );
72   GroupPoints->TextLabel6->setText( tr( "GEOM_FILLING_TOL_3D" ) );
73   GroupPoints->CheckBox1->setText( tr( "GEOM_FILLING_APPROX" ) );
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   myIsApprox = false;
113   myOkCompound = false;
114
115   globalSelection( GEOM_COMPOUND );
116
117   double SpecificStep1 = 1;
118   double SpecificStep2 = 0.0001;
119   /* min, max, step and decimals for spin boxes & initial values */
120   initSpinBox( GroupPoints->SpinBox1, 2.0, MAX_NUMBER,  SpecificStep1, 3 );
121   initSpinBox( GroupPoints->SpinBox2, 0.00001, 10000.0, SpecificStep2, 5 );
122   initSpinBox( GroupPoints->SpinBox3, 0.0, MAX_NUMBER,  SpecificStep1, 3 );
123   initSpinBox( GroupPoints->SpinBox4, 1.0, MAX_NUMBER,  SpecificStep1, 3 );
124   initSpinBox( GroupPoints->SpinBox5, 0.00001, 10000.0, SpecificStep2, 5 );
125
126   GroupPoints->SpinBox1->setValue( myMinDeg );
127   GroupPoints->SpinBox2->setValue( myTol2D );
128   GroupPoints->SpinBox3->setValue( myNbIter );
129   GroupPoints->SpinBox4->setValue( myMaxDeg );
130   GroupPoints->SpinBox5->setValue( myTol3D );
131
132   /* signals and slots connections */
133   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
134   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
135
136   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
137   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
138
139   connect( GroupPoints->SpinBox1, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
140   connect( GroupPoints->SpinBox2, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
141   connect( GroupPoints->SpinBox3, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
142   connect( GroupPoints->SpinBox4, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
143   connect( GroupPoints->SpinBox5, SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox( double ) ) );
144
145   connect( GroupPoints->CheckBox1, SIGNAL( stateChanged( int ) ), this, SLOT( ApproxChanged() ) );
146
147   connect( myGeomGUI, SIGNAL( SignalDefaultStepValueChanged( double ) ), this, SLOT( SetDoubleSpinBoxStep( double ) ) );
148
149   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
150            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
151
152   initName( tr( "GEOM_FILLING" ) );
153   resize(100,100);
154 }
155
156 //=================================================================================
157 // function : SetDoubleSpinBoxStep()
158 // purpose  : Double spin box management
159 //=================================================================================
160 void GenerationGUI_FillingDlg::SetDoubleSpinBoxStep( double step )
161 {
162   GroupPoints->SpinBox1->setSingleStep(step);
163   GroupPoints->SpinBox2->setSingleStep(step);
164   GroupPoints->SpinBox3->setSingleStep(step);
165   GroupPoints->SpinBox4->setSingleStep(step);
166   GroupPoints->SpinBox5->setSingleStep(step);
167 }
168
169
170 //=================================================================================
171 // function : ClickOnOk()
172 // purpose  :
173 //=================================================================================
174 void GenerationGUI_FillingDlg::ClickOnOk()
175 {
176   if ( ClickOnApply() )
177     ClickOnCancel();
178 }
179
180
181 //=================================================================================
182 // function : ClickOnApply()
183 // purpose  :
184 //=================================================================================
185 bool GenerationGUI_FillingDlg::ClickOnApply()
186 {
187   if ( !onAccept() )
188     return false;
189
190   initName();
191   return true;
192 }
193
194
195 //=================================================================================
196 // function : SelectionIntoArgument()
197 // purpose  : Called when selection as changed or other case
198 //=================================================================================
199 void GenerationGUI_FillingDlg::SelectionIntoArgument()
200 {
201   erasePreview();
202   myEditCurrentArgument->setText("");
203
204   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
205   SALOME_ListIO aSelList;
206   aSelMgr->selectedObjects(aSelList);
207
208   if (aSelList.Extent() != 1) {
209     if (myEditCurrentArgument == GroupPoints->LineEdit1)
210       myOkCompound = false;
211     return;
212   }
213
214   // nbSel == 1
215   Standard_Boolean testResult = Standard_False;
216   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
217
218   if (!testResult)
219     return;
220
221   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
222     TopoDS_Shape S;
223     myOkCompound = false;
224
225     if (GEOMBase::GetShape(aSelectedObject, S) && S.ShapeType() == TopAbs_COMPOUND) {
226       // myCompound should be a compound of edges
227       for (TopoDS_Iterator it (S); it.More(); it.Next())
228         if (it.Value().ShapeType() != TopAbs_EDGE)
229           return;
230       myCompound = aSelectedObject;
231       myOkCompound = true;
232     }
233   }
234
235   myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
236   displayPreview();
237 }
238
239 //=================================================================================
240 // function : SetEditCurrentArgument()
241 // purpose  :
242 //=================================================================================
243 void GenerationGUI_FillingDlg::SetEditCurrentArgument()
244 {
245   QPushButton* send = (QPushButton*)sender();
246   globalSelection( GEOM_ALLSHAPES );
247
248   if ( send == GroupPoints->PushButton1 ) {
249     GroupPoints->LineEdit1->setFocus();
250     myEditCurrentArgument = GroupPoints->LineEdit1;
251     globalSelection( GEOM_COMPOUND );
252     this->SelectionIntoArgument();
253   }
254 }
255
256
257 //=================================================================================
258 // function : LineEditReturnPressed()
259 // purpose  :
260 //=================================================================================
261 void GenerationGUI_FillingDlg::LineEditReturnPressed()
262 {
263   QLineEdit* send = (QLineEdit*)sender();
264   if ( send == GroupPoints->LineEdit1 ) {
265     myEditCurrentArgument = send;
266     GEOMBase_Skeleton::LineEditReturnPressed();
267   }
268 }
269
270
271 //=================================================================================
272 // function : ActivateThisDialog()
273 // purpose  :
274 //=================================================================================
275 void GenerationGUI_FillingDlg::ActivateThisDialog()
276 {
277   GEOMBase_Skeleton::ActivateThisDialog();
278   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
279            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
280   globalSelection( GEOM_COMPOUND );
281   displayPreview();
282 }
283
284
285 //=================================================================================
286 // function : enterEvent()
287 // purpose  :
288 //=================================================================================
289 void GenerationGUI_FillingDlg::enterEvent( QEvent* )
290 {
291   if ( !mainFrame()->GroupConstructors->isEnabled() )
292     ActivateThisDialog();
293 }
294
295
296 //=================================================================================
297 // function : ValueChangedInSpinBox()
298 // purpose  :
299 //=================================================================================
300 void GenerationGUI_FillingDlg::ValueChangedInSpinBox( double newValue )
301 {
302   QObject* send = (QObject*)sender();
303
304   if ( send == GroupPoints->SpinBox1 )
305     myMinDeg = int( newValue );
306   else if ( send == GroupPoints->SpinBox2 )
307     myTol2D = newValue;
308   else if ( send == GroupPoints->SpinBox3 )
309     myNbIter = int(newValue);
310   else if ( send == GroupPoints->SpinBox4 )
311     myMaxDeg = int(newValue);
312   else if ( send == GroupPoints->SpinBox5 )
313     myTol3D = newValue;
314
315   displayPreview();
316 }
317
318 //=================================================================================
319 // function : ApproxChanged()
320 // purpose  :
321 //=================================================================================
322 void GenerationGUI_FillingDlg::ApproxChanged()
323 {
324   myIsApprox = GroupPoints->CheckBox1->isChecked();
325   displayPreview();
326 }
327
328 //=================================================================================
329 // function : createOperation
330 // purpose  :
331 //=================================================================================
332 GEOM::GEOM_IOperations_ptr GenerationGUI_FillingDlg::createOperation()
333 {
334   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
335 }
336
337 //=================================================================================
338 // function : isValid
339 // purpose  :
340 //=================================================================================
341 bool GenerationGUI_FillingDlg::isValid( QString& msg )
342 {
343   bool ok = true;
344   ok = GroupPoints->SpinBox1->isValid( msg, !IsPreview() ) && ok;
345   ok = GroupPoints->SpinBox2->isValid( msg, !IsPreview() ) && ok;
346   ok = GroupPoints->SpinBox3->isValid( msg, !IsPreview() ) && ok;
347   ok = GroupPoints->SpinBox4->isValid( msg, !IsPreview() ) && ok;
348   ok = GroupPoints->SpinBox5->isValid( msg, !IsPreview() ) && ok;
349
350   return myOkCompound > 0 && ok;
351 }
352
353 //=================================================================================
354 // function : execute
355 // purpose  :
356 //=================================================================================
357 bool GenerationGUI_FillingDlg::execute( ObjectList& objects )
358 {
359   GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
360   GEOM::GEOM_Object_var anObj = anOper->MakeFilling( myCompound, myMinDeg, myMaxDeg, 
361                                                      myTol2D, myTol3D, myNbIter, myIsApprox );
362   if ( !anObj->_is_nil() )
363   {
364     if ( !IsPreview() )
365       myNoteBook->setParameters(anObj, 5,
366                                 GroupPoints->SpinBox1,
367                                 GroupPoints->SpinBox2,
368                                 GroupPoints->SpinBox3,
369                                 GroupPoints->SpinBox4,
370                                 GroupPoints->SpinBox5);
371     objects.push_back( anObj._retn() );
372   }
373
374   return true;
375 }