Salome HOME
Changing version to 6.3.1
[modules/geom.git] / src / BasicGUI / BasicGUI_CurveDlg.cxx
1 // Copyright (C) 2007-2011  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BasicGUI_CurveDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26
27 #include "BasicGUI_CurveDlg.h"
28 #include "BasicGUI_ParamCurveWidget.h"
29
30 #include <DlgRef.h>
31 #include <GeometryGUI.h>
32 #include <GEOMBase.h>
33
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
38 #include <LightApp_SelectionMgr.h>
39
40 #include <SALOME_ListIteratorOfListIO.hxx>
41 #include <SALOME_ListIO.hxx>
42
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS.hxx>
45 #include <TopExp.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48
49 #include <GEOMImpl_Types.hxx>
50
51 //=================================================================================
52 // class    : BasicGUI_CurveDlg()
53 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the
54 //            name 'name' and widget flags set to 'f'.
55 //            The dialog will by default be modeless, unless you set 'modal' to
56 //            TRUE to construct a modal dialog.
57 //=================================================================================
58 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
59                                       bool modal, Qt::WindowFlags fl )
60   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
61 {
62   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
63   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
64   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
65   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
66
67   setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
68
69   /***************************************************************/
70   mainFrame()->RadioButton1->setIcon( image0 );
71   mainFrame()->RadioButton2->setIcon( image3 );
72   mainFrame()->RadioButton3->setIcon( image2 );
73
74   QGroupBox* creationModeCroup = new QGroupBox(this);
75   QButtonGroup* bg = new QButtonGroup(this);
76
77   creationModeCroup->setTitle( tr( "GEOM_CURVE_CRMODE" ) );  
78   QHBoxLayout * creationModeLayout = new QHBoxLayout(creationModeCroup);
79   myBySelectionBtn = new QRadioButton(  tr( "GEOM_CURVE_SELECTION" ) ,creationModeCroup );
80   myAnaliticalBtn = new QRadioButton(  tr( "GEOM_CURVE_ANALITICAL" ) ,creationModeCroup );
81
82   bg->addButton(myBySelectionBtn);
83   bg->addButton(myAnaliticalBtn);
84   
85   creationModeLayout->addWidget(myBySelectionBtn);
86   creationModeLayout->addWidget(myAnaliticalBtn);
87
88   GroupPoints = new DlgRef_1Sel3Check( centralWidget() );
89
90   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
91   GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
92   GroupPoints->PushButton1->setIcon( image1 );
93   GroupPoints->PushButton1->setDown( true );
94
95   GroupPoints->LineEdit1->setReadOnly( true );
96
97   GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
98   GroupPoints->CheckButton1->setChecked(false);
99   //GroupPoints->CheckButton1->hide();
100
101   GroupPoints->CheckButton2->setText( tr( "GEOM_IS_REORDER" ) );
102   GroupPoints->CheckButton2->setChecked(false);
103   GroupPoints->CheckButton2->hide();
104
105   GroupPoints->CheckButton3->hide();
106
107   myParams = new BasicGUI_ParamCurveWidget( centralWidget() );
108
109   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
110   layout->setMargin( 0 ); layout->setSpacing( 6 );
111   layout->addWidget( creationModeCroup );
112   layout->addWidget( GroupPoints );
113   layout->addWidget( myParams );
114   /***************************************************************/
115
116   setHelpFileName( "create_curve_page.html" );
117
118   Init();
119 }
120
121
122 //=================================================================================
123 // function : ~BasicGUI_CurveDlg()
124 // purpose  : Destroys the object and frees any allocated resources
125 //=================================================================================
126 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
127 {
128 }
129
130
131 //=================================================================================
132 // function : Init()
133 // purpose  :
134 //=================================================================================
135 void BasicGUI_CurveDlg::Init()
136 {
137   /* init variables */
138   myEditCurrentArgument = GroupPoints->LineEdit1;
139
140   myPoints.clear();
141
142   globalSelection(); // close local contexts, if any
143   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
144
145   showOnlyPreviewControl();
146   myBySelectionBtn->setChecked(true);
147
148   /* Get setting of step value from file configuration */
149   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
150   double step = resMgr ? resMgr->doubleValue( "Geometry", "SettingsGeomStep", 10. ) : 10.;
151
152   double aMax( 100. ), aMin( 0.0 );
153
154   /* min, max, step and decimals for spin boxes & initial values */
155   initSpinBox( myParams->myPMin, COORD_MIN, COORD_MAX, step, "length_precision" );
156   initSpinBox( myParams->myPMax, COORD_MIN, COORD_MAX, step, "length_precision" );
157   initSpinBox( myParams->myPStep, COORD_MIN, COORD_MAX, step, "length_precision" );
158   myParams->myPMin->setValue( aMin );
159   myParams->myPMax->setValue( aMax );
160   myParams->myPStep->setValue( step );
161   myParams->myXExpr->setText("t");
162   myParams->myYExpr->setText("t");
163   myParams->myZExpr->setText("t");
164   
165   myParams->hide();
166
167   /* signals and slots connections */
168   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
169   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
170
171   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
172   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
173
174   connect( this,          SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
175
176   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
177
178   connect( GroupPoints->CheckButton1, SIGNAL( toggled(bool) ),  this, SLOT( CheckButtonToggled() ) );
179   connect( GroupPoints->CheckButton2, SIGNAL( toggled(bool) ),  this, SLOT( CheckButtonToggled() ) );
180
181   connect( myGeomGUI->getApp()->selectionMgr(),
182            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
183
184   connect( myBySelectionBtn, SIGNAL( clicked() ), this, SLOT( CreationModeChanged() ) );
185   connect( myAnaliticalBtn, SIGNAL( clicked() ), this, SLOT( CreationModeChanged() ) );
186
187   connect(myParams->myPMin, SIGNAL(valueChanged(double)),    this, SLOT(ValueChangedInSpinBox(double)));
188   connect(myParams->myPMax, SIGNAL(valueChanged(double)),    this, SLOT(ValueChangedInSpinBox(double)));
189   connect(myParams->myPStep, SIGNAL(valueChanged(double)),    this, SLOT(ValueChangedInSpinBox(double)));
190
191   connect(myParams->myXExpr, SIGNAL(editingFinished()), this, SLOT(OnEditingFinished()));
192   connect(myParams->myYExpr, SIGNAL(editingFinished()), this, SLOT(OnEditingFinished()));
193   connect(myParams->myZExpr, SIGNAL(editingFinished()), this, SLOT(OnEditingFinished()));
194
195   initName( tr( "GEOM_CURVE" ) );
196   resize(100,100);
197   ConstructorsClicked( 0 );  
198 }
199
200 //=================================================================================
201 // function : ConstructorsClicked()
202 // purpose  :
203 //=================================================================================
204 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
205 {
206   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
207   mainFrame()->GroupConstructors->setTitle( aTitle );
208
209   if (id == 0) { // polyline (wire)
210     //GroupPoints->CheckButton1->hide();
211     GroupPoints->CheckButton1->setText( tr( "GEOM_BUILD_CLOSED_WIRE" ) );
212     GroupPoints->CheckButton2->hide();
213   }
214   else if (id == 1) { // bezier
215     //GroupPoints->CheckButton1->hide();
216     GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
217     GroupPoints->CheckButton2->hide();
218   }
219   else { // b-spline
220     //GroupPoints->CheckButton1->show();
221     GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
222     GroupPoints->CheckButton2->show();
223   }
224
225   myPoints.clear();
226
227   myEditCurrentArgument->setText( "" );
228   qApp->processEvents();
229   updateGeometry();
230   resize( minimumSizeHint() );
231   SelectionIntoArgument();
232 }
233
234
235 //=================================================================================
236 // function : SetEditCurrentArgument()
237 // purpose  :
238 //=================================================================================
239 void BasicGUI_CurveDlg::SetEditCurrentArgument()
240 {
241   if ( sender() == GroupPoints->PushButton1 )
242     myEditCurrentArgument = GroupPoints->LineEdit1;
243   myEditCurrentArgument->setFocus();
244   SelectionIntoArgument();
245 }
246
247 //=================================================================================
248 // function : CheckButtonToggled()
249 // purpose  :
250 //=================================================================================
251 void BasicGUI_CurveDlg::CheckButtonToggled()
252 {
253   processPreview();
254 }
255
256 //=================================================================================
257 // function : ClickOnOk()
258 // purpose  :
259 //=================================================================================
260 void BasicGUI_CurveDlg::ClickOnOk()
261 {
262   setIsApplyAndClose( true );
263   if ( ClickOnApply() )
264     ClickOnCancel();
265 }
266
267 //=================================================================================
268 // function : ClickOnApply()
269 // purpose  :
270 //=================================================================================
271 bool BasicGUI_CurveDlg::ClickOnApply()
272 {
273   if ( !onAccept() )
274     return false;
275
276   initName();
277   globalSelection(); // close local contexts, if any
278   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
279   return true;
280 }
281
282 static void synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
283 {
284   // 1. remove items from the "left" list that are not in the "right" list
285   QMutableListIterator<GEOM::GeomObjPtr> it1( left );
286   while ( it1.hasNext() ) {
287     GEOM::GeomObjPtr o1 = it1.next();
288     bool found = false;
289     QMutableListIterator<GEOM::GeomObjPtr> it2( right );
290     while ( it2.hasNext() && !found )
291       found = o1 == it2.next();
292     if ( !found )
293       it1.remove();
294   }
295   // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
296   it1 = right;
297   while ( it1.hasNext() ) {
298     GEOM::GeomObjPtr o1 = it1.next();
299     bool found = false;
300     QMutableListIterator<GEOM::GeomObjPtr> it2( left );
301     while ( it2.hasNext() && !found )
302       found = o1 == it2.next();
303     if ( !found )
304       left << o1;
305   }
306 }
307
308 //=================================================================================
309 // function : SelectionIntoArgument()
310 // purpose  : Called when selection as changed or other case
311 //=================================================================================
312 void BasicGUI_CurveDlg::SelectionIntoArgument()
313 {
314   QList<GEOM::GeomObjPtr> points = getSelected( TopAbs_VERTEX, -1 );
315   synchronize( myPoints, points );
316   if ( !myPoints.isEmpty()  )
317     GroupPoints->LineEdit1->setText( QString::number( myPoints.count() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
318   else
319     GroupPoints->LineEdit1->setText( "" );
320   processPreview();
321 }
322
323
324 //=================================================================================
325 // function : ActivateThisDialog()
326 // purpose  :
327 //=================================================================================
328 void BasicGUI_CurveDlg::ActivateThisDialog()
329 {
330   GEOMBase_Skeleton::ActivateThisDialog();
331   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
332            this, SLOT( SelectionIntoArgument() ) );
333
334   globalSelection(); // close local contexts, if any
335   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
336
337   ConstructorsClicked( getConstructorId() );
338 }
339
340 //=================================================================================
341 // function : DeactivateActiveDialog()
342 // purpose  : public slot to deactivate if active
343 //=================================================================================
344 void BasicGUI_CurveDlg::DeactivateActiveDialog()
345 {
346   // myGeomGUI->SetState( -1 );
347   GEOMBase_Skeleton::DeactivateActiveDialog();
348 }
349
350 //=================================================================================
351 // function : enterEvent()
352 // purpose  :
353 //=================================================================================
354 void BasicGUI_CurveDlg::enterEvent( QEvent* )
355 {
356   if ( !mainFrame()->GroupConstructors->isEnabled() )
357     ActivateThisDialog();
358 }
359
360 //=================================================================================
361 // function : createOperation
362 // purpose  :
363 //=================================================================================
364 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
365 {
366   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
367 }
368
369 //=================================================================================
370 // function : isValid
371 // purpose  :
372 //=================================================================================
373 bool BasicGUI_CurveDlg::isValid( QString& msg )
374 {
375   if( myBySelectionBtn->isChecked() )
376     return myPoints.count() > 1;
377   else {
378     bool ok = myParams->myPMin->isValid( msg, !IsPreview() ) &&
379               myParams->myPMax->isValid( msg, !IsPreview() ) &&
380               myParams->myPStep->isValid( msg, !IsPreview() );
381     ok &= !myParams->myXExpr->text().isEmpty();
382     ok &= !myParams->myYExpr->text().isEmpty();
383     ok &= !myParams->myZExpr->text().isEmpty();
384     return ok;
385   }
386 }
387
388 //=================================================================================
389 // function : execute
390 // purpose  :
391 //=================================================================================
392 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
393 {
394   bool res = false;
395
396   GEOM::GEOM_Object_var anObj;
397
398   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
399
400   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
401   points->length( myPoints.count() );
402   for ( int i = 0; i < myPoints.count(); i++ )
403     points[i] = myPoints[i].copy();
404
405   switch ( getConstructorId() ) {
406   case 0 :
407     if( myBySelectionBtn->isChecked() )
408       anObj = anOper->MakePolyline( points.in(), GroupPoints->CheckButton1->isChecked() );
409     else
410       anObj = anOper->MakeCurveParametric(qPrintable(myParams->myXExpr->text()),
411                                           qPrintable(myParams->myYExpr->text()),
412                                           qPrintable(myParams->myZExpr->text()),
413                                           myParams->myPMin->value(),
414                                           myParams->myPMax->value(),
415                                           myParams->myPStep->value(),
416                                           GEOM::Polyline);
417     res = true;
418     break;
419   case 1 :
420     if( myBySelectionBtn->isChecked() )
421       anObj = anOper->MakeSplineBezier( points.in(), GroupPoints->CheckButton1->isChecked() );
422     else
423       anObj = anOper->MakeCurveParametric(qPrintable(myParams->myXExpr->text()),
424                                           qPrintable(myParams->myYExpr->text()),
425                                           qPrintable(myParams->myZExpr->text()),
426                                           myParams->myPMin->value(),
427                                           myParams->myPMax->value(),
428                                           myParams->myPStep->value(),
429                                           GEOM::Bezier);
430
431     res = true;
432     break;
433   case 2 :
434     if( myBySelectionBtn->isChecked() )
435       anObj = anOper->MakeSplineInterpolation( points.in(), GroupPoints->CheckButton1->isChecked(),
436                                                GroupPoints->CheckButton2->isChecked() );
437     else
438       anObj = anOper->MakeCurveParametric(qPrintable(myParams->myXExpr->text()),
439                                           qPrintable(myParams->myYExpr->text()),
440                                           qPrintable(myParams->myZExpr->text()),
441                                           myParams->myPMin->value(),
442                                           myParams->myPMax->value(),
443                                           myParams->myPStep->value(),
444                                           GEOM::Interpolation);
445     res = true;
446     break;
447   }
448
449   if ( !anObj->_is_nil() ) {
450     if(myAnaliticalBtn->isChecked() && !IsPreview()) {
451       QStringList aParameters;
452       aParameters<<myParams->myPMin->text();
453       aParameters<<myParams->myPMax->text();
454       aParameters<<myParams->myPStep->text();
455       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
456     }
457     objects.push_back( anObj._retn() );
458   }
459   
460   return res;
461 }
462
463 //=================================================================================
464 // function : addSubshapeToStudy
465 // purpose  : virtual method to add new SubObjects if local selection
466 //=================================================================================
467 void BasicGUI_CurveDlg::addSubshapesToStudy()
468 {
469   for ( int i = 0; i < myPoints.count(); i++ )
470     GEOMBase::PublishSubObject( myPoints[i].get() );
471 }
472
473 //=================================================================================
474 // function : CreationModeChanged
475 // purpose  :
476 //=================================================================================
477 void BasicGUI_CurveDlg::CreationModeChanged() {
478   const QObject* s = sender();
479   GroupPoints->setVisible(myBySelectionBtn == s);
480   myParams->setVisible(myBySelectionBtn != s);
481   
482   ConstructorsClicked( getConstructorId() );
483 }
484
485 //=================================================================================
486 // function : ValueChangedInSpinBox()
487 // purpose  :
488 //=================================================================================
489 void BasicGUI_CurveDlg::ValueChangedInSpinBox(double/*theValue*/)
490 {
491   processPreview();
492 }
493
494 //=================================================================================
495 // function : ValueChangedInSpinBox()
496 // purpose  :
497 //=================================================================================
498 void BasicGUI_CurveDlg::OnEditingFinished() {
499   processPreview();
500 }