Salome HOME
cdcdcc8f1cad9a5284eaa72fb0eec2270238390c
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_StdHypothesisCreator.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 // File   : StdMeshersGUI_StdHypothesisCreator.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
24 // SMESH includes
25 //
26 #include "StdMeshersGUI_StdHypothesisCreator.h"
27
28 #include <SMESHGUI.h>
29 #include <SMESHGUI_SpinBox.h>
30 #include <SMESHGUI_HypothesesUtils.h>
31 #include <SMESHGUI_Utils.h>
32 #include <SMESH_TypeFilter.hxx>
33 #include <SMESH_NumberFilter.hxx>
34 #include "StdMeshersGUI_ObjectReferenceParamWdg.h"
35 #include "StdMeshersGUI_LayerDistributionParamWdg.h"
36 #include <SALOMEDSClient_Study.hxx>
37
38 // SALOME GUI includes
39 #include <SUIT_ResourceMgr.h>
40
41 // IDL includes
42 #include <SALOMEconfig.h>
43 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
44 #include CORBA_SERVER_HEADER(SMESH_Mesh)
45
46 // Qt includes
47 #include <QHBoxLayout>
48 #include <QSlider>
49 #include <QLabel>
50 #include <QCheckBox>
51
52 const double VALUE_MAX = 1.0e+15, // COORD_MAX
53              VALUE_MAX_2  = VALUE_MAX * VALUE_MAX,
54              VALUE_MAX_3  = VALUE_MAX_2 * VALUE_MAX,
55              VALUE_SMALL = 1.0e-15,
56              VALUE_SMALL_2 = VALUE_SMALL * VALUE_SMALL,
57              VALUE_SMALL_3 = VALUE_SMALL_2 * VALUE_SMALL;
58
59 //================================================================================
60 /*!
61  * \brief Constructor
62   * \param type - hypothesis type
63  */
64 //================================================================================
65
66 StdMeshersGUI_StdHypothesisCreator::StdMeshersGUI_StdHypothesisCreator( const QString& type )
67 : SMESHGUI_GenericHypothesisCreator( type )
68 {
69 }
70
71 //================================================================================
72 /*!
73  * \brief Destructor
74  */
75 //================================================================================
76
77 StdMeshersGUI_StdHypothesisCreator::~StdMeshersGUI_StdHypothesisCreator()
78 {
79 }
80
81 //================================================================================
82 /*!
83  * \brief Return widget for i-th hypothesis parameter (got from myParamWidgets)
84   * \param i - index of hypothesis parameter
85   * \retval QWidget* - found widget
86  */
87 //================================================================================
88
89 QWidget* StdMeshersGUI_StdHypothesisCreator::getWidgetForParam( int i ) const
90 {
91   QWidget* w = 0;
92   if ( isCreation() ) ++i; // skip widget of 'name' parameter
93
94   if ( i < myCustomWidgets.count() ) {
95     QList<QWidget*>::const_iterator anIt  = myCustomWidgets.begin();
96     QList<QWidget*>::const_iterator aLast = myCustomWidgets.end();
97     for ( int j = 0 ; !w && anIt != aLast; ++anIt, ++j )
98       if ( i == j )
99         w = *anIt;
100   }
101   if ( !w ) {
102     // list has no at() const, so we iterate
103     QList<QWidget*>::const_iterator anIt  = widgets().begin();
104     QList<QWidget*>::const_iterator aLast = widgets().end();
105     for( int j = 0; !w && anIt!=aLast; anIt++, ++j ) {
106       if ( i == j )
107         w = *anIt;
108     }
109   }
110   return w;
111 }
112
113 //================================================================================
114 /*!
115  * \brief Allow modifing myCustomWidgets in const methods
116   * \retval ListOfWidgets* - non-const pointer to myCustomWidgets
117  */
118 //================================================================================
119
120 StdMeshersGUI_StdHypothesisCreator::ListOfWidgets*
121 StdMeshersGUI_StdHypothesisCreator::customWidgets() const
122 {
123   return const_cast< ListOfWidgets* >( & myCustomWidgets );
124 }
125
126 //================================================================================
127 /*!
128  * \brief Builds dlg layout
129   * \retval QFrame* - the built widget
130  */
131 //================================================================================
132
133 QFrame* StdMeshersGUI_StdHypothesisCreator::buildFrame()
134 {
135   return buildStdFrame();
136 }
137
138 //================================================================================
139 /*!
140  * \brief Initialise parameter values in controls
141  */
142 //================================================================================
143
144 void StdMeshersGUI_StdHypothesisCreator::retrieveParams() const
145 {
146   // buildStdFrame() sets values itself calling stdParams()
147
148   if ( hypType().startsWith("ProjectionSource" ))
149   {
150     // we use this method to connect depending custom widgets
151     StdMeshersGUI_ObjectReferenceParamWdg* widgetToActivate = 0;
152     ListOfWidgets::const_iterator anIt = myCustomWidgets.begin();
153     for ( ; anIt != myCustomWidgets.end(); anIt++)
154     {
155       if ( *anIt && (*anIt)->inherits("StdMeshersGUI_ObjectReferenceParamWdg"))
156       {
157         StdMeshersGUI_ObjectReferenceParamWdg * w1 =
158           ( StdMeshersGUI_ObjectReferenceParamWdg* ) ( *anIt );
159         ListOfWidgets::const_iterator anIt2 = anIt;
160         for ( ++anIt2; anIt2 != myCustomWidgets.end(); anIt2++)
161           if ( *anIt2 && (*anIt2)->inherits("StdMeshersGUI_ObjectReferenceParamWdg"))
162           {
163             StdMeshersGUI_ObjectReferenceParamWdg * w2 =
164               ( StdMeshersGUI_ObjectReferenceParamWdg* ) ( *anIt2 );
165             w1->AvoidSimultaneousSelection( w2 );
166           }
167         if ( !widgetToActivate )
168           widgetToActivate = w1;
169       }
170     }
171     if ( widgetToActivate )
172       widgetToActivate->activateSelection();
173   }
174
175   if ( dlg() )
176     dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
177 }
178
179 namespace {
180
181   //================================================================================
182   /*!
183    * \brief Widget: slider with left and right labels
184    */
185   //================================================================================
186
187   class TDoubleSliderWith2Lables: public QWidget
188   {
189   public:
190     TDoubleSliderWith2Lables( const QString& leftLabel, const QString& rightLabel,
191                               const double   initValue, const double   bottom,
192                               const double   top      , const double   precision,
193                               QWidget *      parent=0 , const char *   name=0 )
194       :QWidget(parent), _bottom(bottom), _precision(precision)
195     {
196       setObjectName(name);
197
198       QHBoxLayout* aHBoxL = new QHBoxLayout(this);
199       
200       if ( !leftLabel.isEmpty() ) {
201         QLabel* aLeftLabel = new QLabel( this );
202         aLeftLabel->setText( leftLabel );
203         aHBoxL->addWidget( aLeftLabel );
204       }
205
206       _slider = new QSlider( Qt::Horizontal, this );
207       _slider->setRange( 0, toInt( top ));
208       _slider->setValue( toInt( initValue ));
209       aHBoxL->addWidget( _slider );
210
211       if ( !rightLabel.isEmpty() ) {
212         QLabel* aRightLabel = new QLabel( this );
213         aRightLabel->setText( rightLabel );
214         aHBoxL->addWidget( aRightLabel );
215       }
216
217       setLayout( aHBoxL );
218     }
219     double value() const { return _bottom + _slider->value() * _precision; }
220     QSlider * getSlider() const { return _slider; }
221     int toInt( double val ) const { return (int) ceil(( val - _bottom ) / _precision ); }
222   private:
223     double _bottom, _precision;
224     QSlider * _slider;
225   };
226
227   //================================================================================
228   /*!
229    * \brief Retrieve GEOM_Object held by widget
230    */
231   //================================================================================
232
233   inline GEOM::GEOM_Object_var geomFromWdg(const QWidget* wdg)
234   {
235     const StdMeshersGUI_ObjectReferenceParamWdg * objRefWdg =
236       dynamic_cast<const StdMeshersGUI_ObjectReferenceParamWdg*>( wdg );
237     if ( objRefWdg )
238       return objRefWdg->GetObject< GEOM::GEOM_Object >();
239
240     return GEOM::GEOM_Object::_nil();
241   }
242   //================================================================================
243   /*!
244    * \brief Retrieve SMESH_Mesh held by widget
245    */
246   //================================================================================
247
248   inline SMESH::SMESH_Mesh_var meshFromWdg(const QWidget* wdg)
249   {
250     const StdMeshersGUI_ObjectReferenceParamWdg * objRefWdg =
251       dynamic_cast<const StdMeshersGUI_ObjectReferenceParamWdg*>( wdg );
252     if ( objRefWdg )
253       return objRefWdg->GetObject< SMESH::SMESH_Mesh >();
254
255     return SMESH::SMESH_Mesh::_nil();
256   }
257   //================================================================================
258   /*!
259    * \brief creates a filter for selection of shapes of given dimension
260     * \param dim - dimension
261     * \param subShapeType - required type of subshapes, number of which must be \a nbSubShapes
262     * \param nbSubShapes - number of subshapes of given type
263     * \param closed - required closeness flag of a shape
264     * \retval SUIT_SelectionFilter* - created filter
265    */
266   //================================================================================
267
268   SUIT_SelectionFilter* filterForShapeOfDim(const int        dim,
269                                             TopAbs_ShapeEnum subShapeType = TopAbs_SHAPE,
270                                             const int        nbSubShapes = 0,
271                                             bool             closed = false)
272   {
273     TColStd_MapOfInteger shapeTypes;
274     switch ( dim ) {
275     case 0: shapeTypes.Add( TopAbs_VERTEX ); break;
276     case 1:
277       if ( subShapeType == TopAbs_SHAPE ) subShapeType = TopAbs_EDGE;
278       shapeTypes.Add( TopAbs_EDGE );
279       shapeTypes.Add( TopAbs_COMPOUND ); // for a group
280       break;
281     case 2:
282       if ( subShapeType == TopAbs_SHAPE ) subShapeType = TopAbs_FACE;
283       shapeTypes.Add( TopAbs_FACE );
284       shapeTypes.Add( TopAbs_COMPOUND ); // for a group
285       break;
286     case 3:
287       shapeTypes.Add( TopAbs_SHELL );
288       shapeTypes.Add( TopAbs_SOLID );
289       shapeTypes.Add( TopAbs_COMPSOLID );
290       shapeTypes.Add( TopAbs_COMPOUND );
291       break;
292     }
293     return new SMESH_NumberFilter("GEOM", subShapeType, nbSubShapes,
294                                   shapeTypes, GEOM::GEOM_Object::_nil(), closed);
295   }
296
297   //================================================================================
298   /*!
299    * \brief Create a widget for object selection
300     * \param object - initial object
301     * \param filter - selection filter
302     * \retval QWidget* - created widget
303    */
304   //================================================================================
305
306   QWidget* newObjRefParamWdg( SUIT_SelectionFilter* filter,
307                               CORBA::Object_var     object)
308   {
309     StdMeshersGUI_ObjectReferenceParamWdg* w =
310       new StdMeshersGUI_ObjectReferenceParamWdg( filter, 0);
311     w->SetObject( object.in() );
312     return w;
313   }
314
315   //================================================================================
316   /*!
317    * \brief calls deactivateSelection() for StdMeshersGUI_ObjectReferenceParamWdg
318     * \param widgetList - list of widgets
319    */
320   //================================================================================
321
322   void deactivateObjRefParamWdg( QList<QWidget*>* widgetList )
323   {
324     StdMeshersGUI_ObjectReferenceParamWdg* w = 0;
325     QList<QWidget*>::iterator anIt  = widgetList->begin();
326     QList<QWidget*>::iterator aLast = widgetList->end();
327     for ( ; anIt != aLast; anIt++ ) {
328       if ( (*anIt) && (*anIt)->inherits( "StdMeshersGUI_ObjectReferenceParamWdg" ))
329       {
330         w = (StdMeshersGUI_ObjectReferenceParamWdg* )( *anIt );
331         w->deactivateSelection();
332       }
333     }
334   }
335 }
336
337 //================================================================================
338 /*!
339  * \brief Check parameter values before accept()
340   * \retval bool - true if OK
341  */
342 //================================================================================
343
344 bool StdMeshersGUI_StdHypothesisCreator::checkParams( QString& msg ) const
345 {
346   if( !SMESHGUI_GenericHypothesisCreator::checkParams( msg ) )
347     return false;
348
349   // check if object reference parameter is set, as it has no default value
350   bool ok = true;
351   if ( hypType().startsWith("ProjectionSource" ))
352   {
353     StdMeshersGUI_ObjectReferenceParamWdg* w =
354       widget< StdMeshersGUI_ObjectReferenceParamWdg >( 0 );
355     ok = ( w->IsObjectSelected() );
356     if ( !ok ) w->SetObject( CORBA::Object::_nil() );
357     int nbAssocVert = ( hypType() == "ProjectionSource1D" ? 1 : 2 );
358     for ( int i = 0; ok && i < nbAssocVert; i += 2)
359     {
360       QString srcV, tgtV;
361       StdMeshersGUI_ObjectReferenceParamWdg* w1 =
362         widget< StdMeshersGUI_ObjectReferenceParamWdg >( i+2 );
363       StdMeshersGUI_ObjectReferenceParamWdg* w2 =
364         widget< StdMeshersGUI_ObjectReferenceParamWdg >( i+3 );
365       srcV = w1->GetValue();
366       tgtV = w2->GetValue();
367       ok = (( srcV.isEmpty()  && tgtV.isEmpty() ) ||
368             ( !srcV.isEmpty() && !tgtV.isEmpty() && srcV != tgtV ));
369       if ( !ok ) {
370         w1->SetObject( CORBA::Object::_nil() );
371         w2->SetObject( CORBA::Object::_nil() );
372       }
373     }
374
375     // Uninstall filters of StdMeshersGUI_ObjectReferenceParamWdg
376     if ( ok )
377       deactivateObjRefParamWdg( customWidgets() );
378   }
379   else if ( hypType() == "LayerDistribution" )
380   {
381     StdMeshersGUI_LayerDistributionParamWdg* w = 
382       widget< StdMeshersGUI_LayerDistributionParamWdg >( 0 );
383     ok = ( w && w->IsOk() );
384   }
385   return ok;
386 }
387
388 //================================================================================
389 /*!
390  * \brief Store params from GUI controls to a hypothesis
391   * \retval QString - text representation of parameters
392  */
393 //================================================================================
394
395 QString StdMeshersGUI_StdHypothesisCreator::storeParams() const
396 {
397   ListOfStdParams params;
398   bool res = getStdParamFromDlg( params );
399   if( isCreation() )
400   {
401     SMESH::SetName( SMESH::FindSObject( hypothesis() ), params[0].myValue.toString().toLatin1().data() );
402     params.erase( params.begin() );
403   }
404
405   QString valueStr = stdParamValues( params );
406   QStringList aVariablesList = getVariablesFromDlg();
407
408   if( res && !params.isEmpty() )
409   {
410     if( hypType()=="LocalLength" )
411     {
412       StdMeshers::StdMeshers_LocalLength_var h =
413         StdMeshers::StdMeshers_LocalLength::_narrow( hypothesis() );
414
415       h->SetLength( params[0].myValue.toDouble() );
416       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
417       h->SetPrecision( params[1].myValue.toDouble() );
418       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
419     }
420     else if( hypType()=="MaxLength" )
421     {
422       StdMeshers::StdMeshers_MaxLength_var h =
423         StdMeshers::StdMeshers_MaxLength::_narrow( hypothesis() );
424
425       h->SetLength( params[0].myValue.toDouble() );
426       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
427       h->SetUsePreestimatedLength( widget< QCheckBox >( 1 )->isChecked() );
428       if ( !h->HavePreestimatedLength() && !h->_is_equivalent( initParamsHypothesis() )) {
429         StdMeshers::StdMeshers_MaxLength_var hInit =
430           StdMeshers::StdMeshers_MaxLength::_narrow( initParamsHypothesis() );
431         h->SetPreestimatedLength( hInit->GetPreestimatedLength() );
432       }
433     }
434     else if( hypType()=="SegmentLengthAroundVertex" )
435     {
436       StdMeshers::StdMeshers_SegmentLengthAroundVertex_var h =
437         StdMeshers::StdMeshers_SegmentLengthAroundVertex::_narrow( hypothesis() );
438
439       h->SetLength( params[0].myValue.toDouble() );
440       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
441     }
442     else if( hypType()=="Arithmetic1D" )
443     {
444       StdMeshers::StdMeshers_Arithmetic1D_var h =
445         StdMeshers::StdMeshers_Arithmetic1D::_narrow( hypothesis() );
446
447       h->SetLength( params[0].myValue.toDouble(), true );
448       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
449       h->SetLength( params[1].myValue.toDouble(), false );
450       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
451     }
452     else if( hypType()=="MaxElementArea" )
453     {
454       StdMeshers::StdMeshers_MaxElementArea_var h =
455         StdMeshers::StdMeshers_MaxElementArea::_narrow( hypothesis() );
456       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
457       h->SetMaxElementArea( params[0].myValue.toDouble() );
458     }
459     else if( hypType()=="MaxElementVolume" )
460     {
461       StdMeshers::StdMeshers_MaxElementVolume_var h =
462         StdMeshers::StdMeshers_MaxElementVolume::_narrow( hypothesis() );
463
464       h->SetMaxElementVolume( params[0].myValue.toDouble() );
465       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
466     }
467     else if( hypType()=="StartEndLength" )
468     {
469       StdMeshers::StdMeshers_StartEndLength_var h =
470         StdMeshers::StdMeshers_StartEndLength::_narrow( hypothesis() );
471
472       h->SetLength( params[0].myValue.toDouble(), true );
473       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
474       h->SetLength( params[1].myValue.toDouble(), false );
475       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
476     }
477     else if( hypType()=="Deflection1D" )
478     {
479       StdMeshers::StdMeshers_Deflection1D_var h =
480         StdMeshers::StdMeshers_Deflection1D::_narrow( hypothesis() );
481       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
482       h->SetDeflection( params[0].myValue.toDouble() );
483     }
484     else if( hypType()=="AutomaticLength" )
485     {
486       StdMeshers::StdMeshers_AutomaticLength_var h =
487         StdMeshers::StdMeshers_AutomaticLength::_narrow( hypothesis() );
488
489       h->SetFineness( params[0].myValue.toDouble() );
490     }
491     else if( hypType()=="NumberOfLayers" )
492     {
493       StdMeshers::StdMeshers_NumberOfLayers_var h =
494         StdMeshers::StdMeshers_NumberOfLayers::_narrow( hypothesis() );
495
496       h->SetNumberOfLayers( params[0].myValue.toInt() );
497       h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
498     }
499     else if( hypType()=="LayerDistribution" )
500     {
501       StdMeshers::StdMeshers_LayerDistribution_var h =
502         StdMeshers::StdMeshers_LayerDistribution::_narrow( hypothesis() );
503       StdMeshersGUI_LayerDistributionParamWdg* w = 
504         widget< StdMeshersGUI_LayerDistributionParamWdg >( 0 );
505       
506       h->SetLayerDistribution( w->GetHypothesis() );
507       h->SetParameters(w->GetHypothesis()->GetParameters());
508       w->GetHypothesis()->ClearParameters();
509     }
510     else if( hypType()=="ProjectionSource1D" )
511     {
512       StdMeshers::StdMeshers_ProjectionSource1D_var h =
513         StdMeshers::StdMeshers_ProjectionSource1D::_narrow( hypothesis() );
514
515       h->SetSourceEdge       ( geomFromWdg ( getWidgetForParam( 0 )));
516       h->SetSourceMesh       ( meshFromWdg ( getWidgetForParam( 1 )));
517       h->SetVertexAssociation( geomFromWdg ( getWidgetForParam( 2 )),
518                                geomFromWdg ( getWidgetForParam( 3 )));
519     }
520     else if( hypType()=="ProjectionSource2D" )
521     {
522       StdMeshers::StdMeshers_ProjectionSource2D_var h =
523         StdMeshers::StdMeshers_ProjectionSource2D::_narrow( hypothesis() );
524
525       h->SetSourceFace       ( geomFromWdg ( getWidgetForParam( 0 )));
526       h->SetSourceMesh       ( meshFromWdg ( getWidgetForParam( 1 )));
527       h->SetVertexAssociation( geomFromWdg ( getWidgetForParam( 2 )), // src1
528                                geomFromWdg ( getWidgetForParam( 4 )), // src2
529                                geomFromWdg ( getWidgetForParam( 3 )), // tgt1
530                                geomFromWdg ( getWidgetForParam( 5 ))); // tgt2
531     }
532     else if( hypType()=="ProjectionSource3D" )
533     {
534       StdMeshers::StdMeshers_ProjectionSource3D_var h =
535         StdMeshers::StdMeshers_ProjectionSource3D::_narrow( hypothesis() );
536
537       h->SetSource3DShape    ( geomFromWdg ( getWidgetForParam( 0 )));
538       h->SetSourceMesh       ( meshFromWdg ( getWidgetForParam( 1 )));
539       h->SetVertexAssociation( geomFromWdg ( getWidgetForParam( 2 )), // src1
540                                geomFromWdg ( getWidgetForParam( 4 )), // src2
541                                geomFromWdg ( getWidgetForParam( 3 )), // tgt1
542                                geomFromWdg ( getWidgetForParam( 5 ))); // tgt2
543     }
544   }
545   return valueStr;
546 }
547
548 //================================================================================
549 /*!
550  * \brief Return parameter values as SMESHGUI_GenericHypothesisCreator::StdParam
551   * \param p - list of parameters
552   * \retval bool - success flag
553   *
554   * Is called from SMESHGUI_GenericHypothesisCreator::buildStdFrame().
555   * Parameters will be shown using "standard" controls:
556   *   Int by QtxIntSpinBox
557   *   Double by SMESHGUI_SpinBox
558   *   String by QLineEdit
559   * getCustomWidget() allows to redefine control for a parameter
560  */
561 //================================================================================
562
563 bool StdMeshersGUI_StdHypothesisCreator::stdParams( ListOfStdParams& p ) const
564 {
565   bool res = true;
566   SMESHGUI_GenericHypothesisCreator::StdParam item;
567
568   p.clear();
569   customWidgets()->clear();
570   if( isCreation() )
571   {
572     HypothesisData* data = SMESH::GetHypothesisData( hypType() );
573     item.myName = tr( "SMESH_NAME" );
574     item.myValue = data ? hypName() : QString();
575     p.append( item );
576     customWidgets()->append(0);
577   }
578   
579   SMESH::SMESH_Hypothesis_var hyp = initParamsHypothesis();
580   SMESH::ListOfParameters_var aParameters = hyp->GetLastParameters();
581
582   if( hypType()=="LocalLength" )
583   {
584     StdMeshers::StdMeshers_LocalLength_var h =
585       StdMeshers::StdMeshers_LocalLength::_narrow( hyp );
586     
587     item.myName = tr("SMESH_LOCAL_LENGTH_PARAM");
588     if(!initVariableName(aParameters,item,0))
589       item.myValue = h->GetLength();
590     p.append( item );     
591     
592     item.myName = tr("SMESH_LOCAL_LENGTH_PRECISION");
593     if(!initVariableName(aParameters,item,1))
594       item.myValue = h->GetPrecision(); 
595     p.append( item );
596     
597   }
598   else if( hypType()=="MaxLength" )
599   {
600     StdMeshers::StdMeshers_MaxLength_var h =
601       StdMeshers::StdMeshers_MaxLength::_narrow( hyp );
602     // try to set a right preestimated length to edited hypothesis
603     bool noPreestimatedAtEdition = false;
604     if ( !isCreation() ) {
605       StdMeshers::StdMeshers_MaxLength_var initHyp =
606         StdMeshers::StdMeshers_MaxLength::_narrow( initParamsHypothesis(true) );
607       noPreestimatedAtEdition =
608         ( initHyp->_is_nil() || !initHyp->HavePreestimatedLength() );
609       if ( !noPreestimatedAtEdition )
610         h->SetPreestimatedLength( initHyp->GetPreestimatedLength() );
611     }
612
613     item.myName = tr("SMESH_LOCAL_LENGTH_PARAM");
614     if(!initVariableName(aParameters,item,0))
615       item.myValue = h->GetLength();
616     p.append( item );
617     customWidgets()->append(0);
618
619     item.myName = tr("SMESH_USE_PREESTIMATED_LENGTH");
620     p.append( item );
621     QCheckBox* aQCheckBox = new QCheckBox(dlg());
622     if ( !noPreestimatedAtEdition && h->HavePreestimatedLength() ) {
623       aQCheckBox->setChecked( h->GetUsePreestimatedLength() );
624       connect( aQCheckBox, SIGNAL(  stateChanged(int) ), this, SLOT( onValueChanged() ) );
625     }
626     else {
627       aQCheckBox->setChecked( false );
628       aQCheckBox->setEnabled( false );
629     }
630     customWidgets()->append( aQCheckBox );
631   }
632   else if( hypType()=="SegmentLengthAroundVertex" )
633   {
634     StdMeshers::StdMeshers_SegmentLengthAroundVertex_var h =
635       StdMeshers::StdMeshers_SegmentLengthAroundVertex::_narrow( hyp );
636
637     item.myName = tr("SMESH_LOCAL_LENGTH_PARAM");
638     if(!initVariableName(aParameters,item,0))
639       item.myValue = h->GetLength();
640     
641     p.append( item );
642   }
643   else if( hypType()=="Arithmetic1D" )
644   {
645     StdMeshers::StdMeshers_Arithmetic1D_var h =
646       StdMeshers::StdMeshers_Arithmetic1D::_narrow( hyp );
647
648     item.myName = tr( "SMESH_START_LENGTH_PARAM" );
649     if(!initVariableName(aParameters,item,0))
650       item.myValue = h->GetLength( true );
651     p.append( item );
652
653     item.myName = tr( "SMESH_END_LENGTH_PARAM" );
654     if(!initVariableName(aParameters,item,1))
655       item.myValue = h->GetLength( false );
656     p.append( item );
657   }
658   else if( hypType()=="MaxElementArea" )
659   {
660     StdMeshers::StdMeshers_MaxElementArea_var h =
661       StdMeshers::StdMeshers_MaxElementArea::_narrow( hyp );
662
663     item.myName = tr( "SMESH_MAX_ELEMENT_AREA_PARAM" );
664     if(!initVariableName(aParameters,item,0))
665       item.myValue = h->GetMaxElementArea();
666     p.append( item );
667     
668   }
669   else if( hypType()=="MaxElementVolume" )
670   {
671     StdMeshers::StdMeshers_MaxElementVolume_var h =
672       StdMeshers::StdMeshers_MaxElementVolume::_narrow( hyp );
673
674     item.myName = tr( "SMESH_MAX_ELEMENT_VOLUME_PARAM" );
675     if(!initVariableName(aParameters,item,0))
676       item.myValue = h->GetMaxElementVolume();
677     p.append( item );
678   }
679   else if( hypType()=="StartEndLength" )
680   {
681     StdMeshers::StdMeshers_StartEndLength_var h =
682       StdMeshers::StdMeshers_StartEndLength::_narrow( hyp );
683
684     item.myName = tr( "SMESH_START_LENGTH_PARAM" );
685
686     if(!initVariableName(aParameters,item,0)) 
687       item.myValue = h->GetLength( true );
688     p.append( item );
689
690     item.myName = tr( "SMESH_END_LENGTH_PARAM" );
691     if(!initVariableName(aParameters,item,1)) 
692       item.myValue = h->GetLength( false );
693     p.append( item );
694     
695   }
696   else if( hypType()=="Deflection1D" )
697   {
698     StdMeshers::StdMeshers_Deflection1D_var h =
699       StdMeshers::StdMeshers_Deflection1D::_narrow( hyp );
700     
701     item.myName = tr( "SMESH_DEFLECTION1D_PARAM" );
702     if(!initVariableName(aParameters,item,0)) 
703       item.myValue = h->GetDeflection();
704     p.append( item );
705   }
706   else if( hypType()=="AutomaticLength" )
707   {
708     StdMeshers::StdMeshers_AutomaticLength_var h =
709       StdMeshers::StdMeshers_AutomaticLength::_narrow( hyp );
710
711     item.myName = tr( "SMESH_FINENESS_PARAM" );
712     //item.myValue = h->GetFineness();
713     p.append( item );
714     customWidgets()->append
715       ( new TDoubleSliderWith2Lables( "0 ", " 1", h->GetFineness(), 0, 1, 0.01, 0 ));
716   }
717   else if( hypType()=="NumberOfLayers" )
718   {
719     StdMeshers::StdMeshers_NumberOfLayers_var h =
720       StdMeshers::StdMeshers_NumberOfLayers::_narrow( hyp );
721
722     item.myName = tr( "SMESH_NUMBER_OF_LAYERS" );
723     if(!initVariableName(aParameters,item,0))     
724       item.myValue = (int) h->GetNumberOfLayers();
725     p.append( item );
726   }
727   else if( hypType()=="LayerDistribution" )
728     {
729       StdMeshers::StdMeshers_LayerDistribution_var h =
730       StdMeshers::StdMeshers_LayerDistribution::_narrow( hyp );
731
732     item.myName = tr( "SMESH_LAYERS_DISTRIBUTION" ); p.append( item );
733     
734     //Set into not published hypo last variables
735     QStringList aLastVarsList;
736     for(int i = 0;i<aParameters->length();i++) 
737       aLastVarsList.append(QString(aParameters[i].in()));
738
739     if(!aLastVarsList.isEmpty())
740       h->GetLayerDistribution()->SetLastParameters(SMESHGUI::JoinObjectParameters(aLastVarsList));
741     
742     customWidgets()->append
743       ( new StdMeshersGUI_LayerDistributionParamWdg( h->GetLayerDistribution(), hypName(), dlg()));
744   }
745   else if( hypType()=="ProjectionSource1D" )
746   {
747     StdMeshers::StdMeshers_ProjectionSource1D_var h =
748       StdMeshers::StdMeshers_ProjectionSource1D::_narrow( hyp );
749
750     item.myName = tr( "SMESH_SOURCE_EDGE" ); p.append( item );
751     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 1 ),
752                                                h->GetSourceEdge()));
753     item.myName = tr( "SMESH_SOURCE_MESH" ); p.append( item );
754     customWidgets()->append( newObjRefParamWdg( new SMESH_TypeFilter( MESH ),
755                                                h->GetSourceMesh()));
756     item.myName = tr( "SMESH_SOURCE_VERTEX" ); p.append( item );
757     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
758                                                h->GetSourceVertex()));
759     item.myName = tr( "SMESH_TARGET_VERTEX" ); p.append( item );
760     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
761                                                h->GetTargetVertex()));
762   }
763   else if( hypType()=="ProjectionSource2D" )
764   {
765     StdMeshers::StdMeshers_ProjectionSource2D_var h =
766       StdMeshers::StdMeshers_ProjectionSource2D::_narrow( hyp );
767
768     item.myName = tr( "SMESH_SOURCE_FACE" ); p.append( item );
769     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 2 ),
770                                                h->GetSourceFace()));
771     item.myName = tr( "SMESH_SOURCE_MESH" ); p.append( item );
772     customWidgets()->append( newObjRefParamWdg( new SMESH_TypeFilter( MESH ),
773                                                h->GetSourceMesh()));
774     item.myName = tr( "SMESH_SOURCE_VERTEX1" ); p.append( item );
775     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
776                                                h->GetSourceVertex( 1 )));
777     item.myName = tr( "SMESH_TARGET_VERTEX1" ); p.append( item );
778     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
779                                                h->GetTargetVertex( 1 )));
780     item.myName = tr( "SMESH_SOURCE_VERTEX2" ); p.append( item );
781     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
782                                                h->GetSourceVertex( 2 )));
783     item.myName = tr( "SMESH_TARGET_VERTEX2" ); p.append( item );
784     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
785                                                h->GetTargetVertex( 2 )));
786   }
787   else if( hypType()=="ProjectionSource3D" )
788   {
789     StdMeshers::StdMeshers_ProjectionSource3D_var h =
790       StdMeshers::StdMeshers_ProjectionSource3D::_narrow( hyp );
791
792     item.myName = tr( "SMESH_SOURCE_3DSHAPE" ); p.append( item );
793     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 3, TopAbs_FACE, 6, true ),
794                                                h->GetSource3DShape()));
795     item.myName = tr( "SMESH_SOURCE_MESH" ); p.append( item );
796     customWidgets()->append( newObjRefParamWdg( new SMESH_TypeFilter( MESH ),
797                                                h->GetSourceMesh()));
798     item.myName = tr( "SMESH_SOURCE_VERTEX1" ); p.append( item );
799     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
800                                                h->GetSourceVertex( 1 )));
801     item.myName = tr( "SMESH_TARGET_VERTEX1" ); p.append( item );
802     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
803                                                h->GetTargetVertex( 1 )));
804     item.myName = tr( "SMESH_SOURCE_VERTEX2" ); p.append( item );
805     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
806                                                h->GetSourceVertex( 2 )));
807     item.myName = tr( "SMESH_TARGET_VERTEX2" ); p.append( item );
808     customWidgets()->append( newObjRefParamWdg( filterForShapeOfDim( 0 ),
809                                                h->GetTargetVertex( 2 )));
810   }
811   else
812     res = false;
813   return res;
814 }
815
816 //================================================================================
817 /*!
818  * \brief tune "standard" control
819   * \param w - control widget
820   * \param int - parameter index
821  */
822 //================================================================================
823
824 void StdMeshersGUI_StdHypothesisCreator::attuneStdWidget (QWidget* w, const int) const
825 {
826   SMESHGUI_SpinBox* sb = w->inherits( "SMESHGUI_SpinBox" ) ? ( SMESHGUI_SpinBox* )w : 0;
827   if( hypType()=="LocalLength" &&  sb )
828   {
829     if (sb->objectName() == tr("SMESH_LOCAL_LENGTH_PARAM"))
830       sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
831     else if (sb->objectName() == tr("SMESH_LOCAL_LENGTH_PRECISION"))
832       sb->RangeStepAndValidator( 0.0, 1.0, 0.05, 7 );
833   }
834   else if( hypType()=="Arithmetic1D" && sb )
835   {
836     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
837   }
838   else if( hypType()=="MaxLength" && sb )
839   {
840     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
841     sb->setEnabled( !widget< QCheckBox >( 1 )->isChecked() );
842   }
843   else if( hypType()=="MaxElementArea" && sb )
844   {
845     sb->RangeStepAndValidator( VALUE_SMALL_2, VALUE_MAX_2, 1.0, 6 );
846   }
847   else if( hypType()=="MaxElementVolume" && sb )
848   {
849     sb->RangeStepAndValidator( VALUE_SMALL_3, VALUE_MAX_3, 1.0, 6 );
850   }
851   else if( hypType()=="StartEndLength" && sb )
852   {
853     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
854   }
855   else if( hypType()=="Deflection1D" && sb )
856   {
857     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
858   }
859   else if ( sb ) // default validator for possible ancestors
860   {
861     sb->RangeStepAndValidator( VALUE_SMALL, VALUE_MAX, 1.0, 6 );
862   }
863 }
864
865 //================================================================================
866 /*!
867  * \brief Return dlg title
868   * \retval QString - title string
869  */
870 //================================================================================
871
872 QString StdMeshersGUI_StdHypothesisCreator::caption() const
873 {
874   return tr( QString( "SMESH_%1_TITLE" ).arg( hypTypeName( hypType() ) ).toLatin1().data() );
875 }
876
877 //================================================================================
878 /*!
879  * \brief return pixmap for dlg icon
880   * \retval QPixmap - 
881  */
882 //================================================================================
883
884 QPixmap StdMeshersGUI_StdHypothesisCreator::icon() const
885 {
886   QString hypIconName = tr( QString( "ICON_DLG_%1" ).arg( hypTypeName( hypType() ) ).toLatin1().data() );
887   return SMESHGUI::resourceMgr()->loadPixmap( "SMESH", hypIconName );
888 }
889
890 //================================================================================
891 /*!
892  * \brief Return hypothesis type name to show in dlg
893   * \retval QString - 
894  */
895 //================================================================================
896
897 QString StdMeshersGUI_StdHypothesisCreator::type() const
898 {
899   return tr( QString( "SMESH_%1_HYPOTHESIS" ).arg( hypTypeName( hypType() ) ).toLatin1().data() );
900 }
901
902 //================================================================================
903 /*!
904  * \brief String to insert in "SMESH_%1_HYPOTHESIS" to get hypothesis type name
905  * from message resouce file
906   * \param t - hypothesis type
907   * \retval QString - result string
908  */
909 //================================================================================
910
911 QString StdMeshersGUI_StdHypothesisCreator::hypTypeName( const QString& t ) const
912 {
913   static QMap<QString,QString>  types;
914   if( types.isEmpty() )
915   {
916     types.insert( "LocalLength", "LOCAL_LENGTH" );
917     types.insert( "NumberOfSegments", "NB_SEGMENTS" );
918     types.insert( "MaxElementArea", "MAX_ELEMENT_AREA" );
919     types.insert( "MaxElementVolume", "MAX_ELEMENT_VOLUME" );
920     types.insert( "StartEndLength", "START_END_LENGTH" );
921     types.insert( "Deflection1D", "DEFLECTION1D" );
922     types.insert( "Arithmetic1D", "ARITHMETIC_1D" );
923     types.insert( "AutomaticLength", "AUTOMATIC_LENGTH" );
924     types.insert( "ProjectionSource1D", "PROJECTION_SOURCE_1D" );
925     types.insert( "ProjectionSource2D", "PROJECTION_SOURCE_2D" );
926     types.insert( "ProjectionSource3D", "PROJECTION_SOURCE_3D" );
927     types.insert( "NumberOfLayers", "NUMBER_OF_LAYERS" );
928     types.insert( "LayerDistribution", "LAYER_DISTRIBUTION" );
929     types.insert( "SegmentLengthAroundVertex", "SEGMENT_LENGTH_AROUND_VERTEX" );
930     types.insert( "MaxLength", "MAX_LENGTH" );
931   }
932
933   QString res;
934   if( types.contains( t ) )
935     res = types[ t ];
936
937   return res;
938 }
939
940
941 //=======================================================================
942 //function : getCustomWidget
943 //purpose  : is called from buildStdFrame()
944 //=======================================================================
945
946 QWidget* StdMeshersGUI_StdHypothesisCreator::getCustomWidget( const StdParam & param,
947                                                               QWidget*         parent,
948                                                               const int        index) const
949 {
950   QWidget* w = 0;
951   if ( index < customWidgets()->count() ) {
952     w = customWidgets()->at( index );
953     if ( w ) {
954       w->setParent( parent );
955       w->move( QPoint( 0, 0 ) );
956     }
957   }
958   return w;
959 }
960
961 //================================================================================
962 /*!
963  * \brief Set param value taken from a custom widget
964   * \param param - SMESHGUI_GenericHypothesisCreator::StdParam structure
965   * \param widget - widget presenting param
966   * \retval bool - success flag
967  * 
968  * this method is called from getStdParamFromDlg()
969  */
970 //================================================================================
971
972 bool StdMeshersGUI_StdHypothesisCreator::getParamFromCustomWidget( StdParam & param,
973                                                                    QWidget*   widget) const
974 {
975   if ( hypType()=="AutomaticLength" ) {
976     TDoubleSliderWith2Lables* w = dynamic_cast<TDoubleSliderWith2Lables*>( widget );
977     if ( w ) {
978       param.myValue = w->value();
979       return true;
980     }
981   }
982   if ( hypType() == "MaxLength" ) {
983     param.myValue = "";
984     return true;
985   }
986   if ( widget->inherits( "StdMeshersGUI_ObjectReferenceParamWdg" ))
987   {
988     // show only 1st reference value
989     if ( true /*widget == getWidgetForParam( 0 )*/) {
990       const StdMeshersGUI_ObjectReferenceParamWdg * w =
991         static_cast<const StdMeshersGUI_ObjectReferenceParamWdg*>( widget );
992       param.myValue = w->GetValue();
993     }
994     return true;
995   }
996   if ( widget->inherits( "StdMeshersGUI_LayerDistributionParamWdg" ))
997   {
998     const StdMeshersGUI_LayerDistributionParamWdg * w =
999       static_cast<const StdMeshersGUI_LayerDistributionParamWdg*>( widget );
1000     param.myValue = w->GetValue();
1001     return true;
1002   }
1003   return false;
1004 }
1005
1006 //================================================================================
1007 /*!
1008  * \brief called when operation cancelled
1009  */
1010 //================================================================================
1011
1012 void StdMeshersGUI_StdHypothesisCreator::onReject()
1013 {
1014   if ( hypType().startsWith("ProjectionSource" ))
1015   {
1016     // Uninstall filters of StdMeshersGUI_ObjectReferenceParamWdg
1017     deactivateObjRefParamWdg( customWidgets() );
1018   }
1019 }
1020
1021 //================================================================================
1022 /*!
1023  * \brief 
1024  */
1025 //================================================================================
1026
1027 void StdMeshersGUI_StdHypothesisCreator::valueChanged( QWidget* paramWidget)
1028 {
1029   if ( hypType() == "MaxLength" && paramWidget == getWidgetForParam(1) ) {
1030     getWidgetForParam(0)->setEnabled( !widget< QCheckBox >( 1 )->isChecked() );
1031     if ( !getWidgetForParam(0)->isEnabled() ) {
1032       StdMeshers::StdMeshers_MaxLength_var h =
1033         StdMeshers::StdMeshers_MaxLength::_narrow( initParamsHypothesis() );
1034       widget< QtxDoubleSpinBox >( 0 )->setValue( h->GetPreestimatedLength() );
1035     }
1036   }
1037 }
1038
1039 //================================================================================
1040 /*!
1041  *
1042  */
1043 //================================================================================
1044
1045 bool StdMeshersGUI_StdHypothesisCreator::initVariableName(SMESH::ListOfParameters_var theParameters, 
1046                                                           StdParam &theParams, 
1047                                                           int order) const
1048 {
1049   QString aVaribaleName = (theParameters->length() > order) ? QString(theParameters[order].in()) : QString("");
1050   theParams.isVariable = !aVaribaleName.isEmpty();
1051   if(theParams.isVariable) 
1052     theParams.myValue = aVaribaleName;
1053
1054   return theParams.isVariable;
1055 }