Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / BasicGUI / BasicGUI_CurveDlg.cxx
1 // Copyright (C) 2007-2012  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   myParams->myPStep->setValue( 10 );
158   myParams->myPStep->setMaximum( 999 );
159   myParams->myPStep->setSingleStep( 10 );
160   myParams->myPMin->setValue( aMin );
161   myParams->myPMax->setValue( aMax );
162   myParams->myPStep->setValue( step );
163   myParams->myXExpr->setText("t");
164   myParams->myYExpr->setText("t");
165   myParams->myZExpr->setText("t");
166   
167   myParams->hide();
168
169   /* signals and slots connections */
170   connect( myGeomGUI,        SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
171   connect( myGeomGUI,        SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
172
173   connect( buttonOk(),       SIGNAL( clicked() ),                      this, SLOT( ClickOnOk() ) );
174   connect( buttonApply(),    SIGNAL( clicked() ),                      this, SLOT( ClickOnApply() ) );
175
176   connect( this,             SIGNAL( constructorsClicked( int ) ),     this, SLOT( ConstructorsClicked( int ) ) );
177
178   connect( GroupPoints->PushButton1,  SIGNAL( clicked() ),             this, SLOT( SetEditCurrentArgument() ) );
179
180   connect( GroupPoints->CheckButton1, SIGNAL( toggled(bool) ),         this, SLOT( CheckButtonToggled() ) );
181   connect( GroupPoints->CheckButton2, SIGNAL( toggled(bool) ),         this, SLOT( CheckButtonToggled() ) );
182
183   connect( myGeomGUI->getApp()->selectionMgr(),
184            SIGNAL( currentSelectionChanged() ),                        this, SLOT( SelectionIntoArgument() ) );
185
186   connect( myBySelectionBtn, SIGNAL( clicked() ),                      this, SLOT( CreationModeChanged() ) );
187   connect( myAnaliticalBtn,  SIGNAL( clicked() ),                      this, SLOT( CreationModeChanged() ) );
188
189   connect(myParams->myPMin,  SIGNAL(valueChanged(double)),             this, SLOT(ValueChangedInSpinBox(double)));
190   connect(myParams->myPMax,  SIGNAL(valueChanged(double)),             this, SLOT(ValueChangedInSpinBox(double)));
191   connect(myParams->myPStep, SIGNAL(valueChanged(int)),                this, SLOT(ValueChangedInSpinBox(int)));
192
193   connect(myParams->myXExpr, SIGNAL(editingFinished()),                this, SLOT(OnEditingFinished()));
194   connect(myParams->myYExpr, SIGNAL(editingFinished()),                this, SLOT(OnEditingFinished()));
195   connect(myParams->myZExpr, SIGNAL(editingFinished()),                this, SLOT(OnEditingFinished()));
196
197   initName( tr( "GEOM_CURVE" ) );
198   resize(100,100);
199   ConstructorsClicked( 0 );  
200 }
201
202 //=================================================================================
203 // function : ConstructorsClicked()
204 // purpose  :
205 //=================================================================================
206 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
207 {
208   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
209   mainFrame()->GroupConstructors->setTitle( aTitle );
210
211   if (id == 0) { // polyline (wire)
212     //GroupPoints->CheckButton1->hide();
213     GroupPoints->CheckButton1->setText( tr( "GEOM_BUILD_CLOSED_WIRE" ) );
214     GroupPoints->CheckButton2->hide();
215   }
216   else if (id == 1) { // bezier
217     //GroupPoints->CheckButton1->hide();
218     GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
219     GroupPoints->CheckButton2->hide();
220   }
221   else { // b-spline
222     //GroupPoints->CheckButton1->show();
223     GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
224     GroupPoints->CheckButton2->show();
225   }
226
227   myPoints.clear();
228
229   myEditCurrentArgument->setText( "" );
230   qApp->processEvents();
231   updateGeometry();
232   resize( minimumSizeHint() );
233   SelectionIntoArgument();
234 }
235
236
237 //=================================================================================
238 // function : SetEditCurrentArgument()
239 // purpose  :
240 //=================================================================================
241 void BasicGUI_CurveDlg::SetEditCurrentArgument()
242 {
243   if ( sender() == GroupPoints->PushButton1 )
244     myEditCurrentArgument = GroupPoints->LineEdit1;
245   myEditCurrentArgument->setFocus();
246   SelectionIntoArgument();
247 }
248
249 //=================================================================================
250 // function : CheckButtonToggled()
251 // purpose  :
252 //=================================================================================
253 void BasicGUI_CurveDlg::CheckButtonToggled()
254 {
255   processPreview();
256 }
257
258 //=================================================================================
259 // function : ClickOnOk()
260 // purpose  :
261 //=================================================================================
262 void BasicGUI_CurveDlg::ClickOnOk()
263 {
264   setIsApplyAndClose( true );
265   if ( ClickOnApply() )
266     ClickOnCancel();
267 }
268
269 //=================================================================================
270 // function : ClickOnApply()
271 // purpose  :
272 //=================================================================================
273 bool BasicGUI_CurveDlg::ClickOnApply()
274 {
275   if ( !onAccept() )
276     return false;
277
278   initName();
279   globalSelection(); // close local contexts, if any
280   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
281   return true;
282 }
283
284 static void synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
285 {
286   // 1. remove items from the "left" list that are not in the "right" list
287   QMutableListIterator<GEOM::GeomObjPtr> it1( left );
288   while ( it1.hasNext() ) {
289     GEOM::GeomObjPtr o1 = it1.next();
290     bool found = false;
291     QMutableListIterator<GEOM::GeomObjPtr> it2( right );
292     while ( it2.hasNext() && !found )
293       found = o1 == it2.next();
294     if ( !found )
295       it1.remove();
296   }
297   // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
298   it1 = right;
299   while ( it1.hasNext() ) {
300     GEOM::GeomObjPtr o1 = it1.next();
301     bool found = false;
302     QMutableListIterator<GEOM::GeomObjPtr> it2( left );
303     while ( it2.hasNext() && !found )
304       found = o1 == it2.next();
305     if ( !found )
306       left << o1;
307   }
308 }
309
310 //=================================================================================
311 // function : SelectionIntoArgument()
312 // purpose  : Called when selection as changed or other case
313 //=================================================================================
314 void BasicGUI_CurveDlg::SelectionIntoArgument()
315 {
316   QList<GEOM::GeomObjPtr> points = getSelected( TopAbs_VERTEX, -1 );
317   synchronize( myPoints, points );
318   if ( !myPoints.isEmpty()  )
319     GroupPoints->LineEdit1->setText( QString::number( myPoints.count() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
320   else
321     GroupPoints->LineEdit1->setText( "" );
322   processPreview();
323 }
324
325
326 //=================================================================================
327 // function : ActivateThisDialog()
328 // purpose  :
329 //=================================================================================
330 void BasicGUI_CurveDlg::ActivateThisDialog()
331 {
332   GEOMBase_Skeleton::ActivateThisDialog();
333   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
334            this, SLOT( SelectionIntoArgument() ) );
335
336   globalSelection(); // close local contexts, if any
337   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
338
339   ConstructorsClicked( getConstructorId() );
340 }
341
342 //=================================================================================
343 // function : DeactivateActiveDialog()
344 // purpose  : public slot to deactivate if active
345 //=================================================================================
346 void BasicGUI_CurveDlg::DeactivateActiveDialog()
347 {
348   // myGeomGUI->SetState( -1 );
349   GEOMBase_Skeleton::DeactivateActiveDialog();
350 }
351
352 //=================================================================================
353 // function : enterEvent()
354 // purpose  :
355 //=================================================================================
356 void BasicGUI_CurveDlg::enterEvent( QEvent* )
357 {
358   if ( !mainFrame()->GroupConstructors->isEnabled() )
359     ActivateThisDialog();
360 }
361
362 //=================================================================================
363 // function : createOperation
364 // purpose  :
365 //=================================================================================
366 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
367 {
368   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
369 }
370
371 //=================================================================================
372 // function : isValid
373 // purpose  :
374 //=================================================================================
375 bool BasicGUI_CurveDlg::isValid( QString& msg )
376 {
377   if( myBySelectionBtn->isChecked() )
378     return myPoints.count() > 1;
379   else {
380     bool ok = myParams->myPMin->isValid( msg, !IsPreview() ) &&
381               myParams->myPMax->isValid( msg, !IsPreview() ) &&
382               myParams->myPStep->isValid( msg, !IsPreview() );
383     ok &= !myParams->myXExpr->text().isEmpty();
384     ok &= !myParams->myYExpr->text().isEmpty();
385     ok &= !myParams->myZExpr->text().isEmpty();
386     return ok;
387   }
388 }
389
390 //=================================================================================
391 // function : execute
392 // purpose  :
393 //=================================================================================
394 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
395 {
396   bool res = false;
397
398   GEOM::GEOM_Object_var anObj;
399
400   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
401
402   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
403   points->length( myPoints.count() );
404   for ( int i = 0; i < myPoints.count(); i++ )
405     points[i] = myPoints[i].copy();
406
407   switch ( getConstructorId() ) {
408   case 0 :
409     if( myBySelectionBtn->isChecked() )
410       anObj = anOper->MakePolyline( points.in(), GroupPoints->CheckButton1->isChecked() );
411     else
412       anObj = anOper->MakeCurveParametricNew(qPrintable(myParams->myXExpr->text()),
413                                           qPrintable(myParams->myYExpr->text()),
414                                           qPrintable(myParams->myZExpr->text()),
415                                           myParams->myPMin->value(),
416                                           myParams->myPMax->value(),
417                                           myParams->myPStep->value(),
418                                           GEOM::Polyline);
419     res = true;
420     break;
421   case 1 :
422     if( myBySelectionBtn->isChecked() )
423       anObj = anOper->MakeSplineBezier( points.in(), GroupPoints->CheckButton1->isChecked() );
424     else
425       anObj = anOper->MakeCurveParametricNew(qPrintable(myParams->myXExpr->text()),
426                                           qPrintable(myParams->myYExpr->text()),
427                                           qPrintable(myParams->myZExpr->text()),
428                                           myParams->myPMin->value(),
429                                           myParams->myPMax->value(),
430                                           myParams->myPStep->value(),
431                                           GEOM::Bezier);
432
433     res = true;
434     break;
435   case 2 :
436     if( myBySelectionBtn->isChecked() )
437       anObj = anOper->MakeSplineInterpolation( points.in(), GroupPoints->CheckButton1->isChecked(),
438                                                GroupPoints->CheckButton2->isChecked() );
439     else
440       anObj = anOper->MakeCurveParametricNew(qPrintable(myParams->myXExpr->text()),
441                                           qPrintable(myParams->myYExpr->text()),
442                                           qPrintable(myParams->myZExpr->text()),
443                                           myParams->myPMin->value(),
444                                           myParams->myPMax->value(),
445                                           myParams->myPStep->value(),
446                                           GEOM::Interpolation);
447     res = true;
448     break;
449   }
450
451   if ( !anObj->_is_nil() ) {
452     if(myAnaliticalBtn->isChecked() && !IsPreview()) {
453       QStringList aParameters;
454       aParameters<<myParams->myPMin->text();
455       aParameters<<myParams->myPMax->text();
456       aParameters<<myParams->myPStep->text();
457       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
458     }
459     objects.push_back( anObj._retn() );
460   }
461   
462   return res;
463 }
464
465 //=================================================================================
466 // function : addSubshapeToStudy
467 // purpose  : virtual method to add new SubObjects if local selection
468 //=================================================================================
469 void BasicGUI_CurveDlg::addSubshapesToStudy()
470 {
471   for ( int i = 0; i < myPoints.count(); i++ )
472     GEOMBase::PublishSubObject( myPoints[i].get() );
473 }
474
475 //=================================================================================
476 // function : CreationModeChanged
477 // purpose  :
478 //=================================================================================
479 void BasicGUI_CurveDlg::CreationModeChanged() {
480   const QObject* s = sender();
481   GroupPoints->setVisible(myBySelectionBtn == s);
482   myParams->setVisible(myBySelectionBtn != s);
483   
484   ConstructorsClicked( getConstructorId() );
485 }
486
487 //=================================================================================
488 // function : ValueChangedInSpinBox()
489 // purpose  :
490 //=================================================================================
491 void BasicGUI_CurveDlg::ValueChangedInSpinBox(double/*theValue*/)
492 {
493   processPreview();
494 }
495
496 //=================================================================================
497 // function : ValueChangedInSpinBox()
498 // purpose  :
499 //=================================================================================
500 void BasicGUI_CurveDlg::ValueChangedInSpinBox(int/*theValue*/)
501 {
502   processPreview();
503 }
504
505 //=================================================================================
506 // function : ValueChangedInSpinBox()
507 // purpose  :
508 //=================================================================================
509 void BasicGUI_CurveDlg::OnEditingFinished() {
510   processPreview();
511 }