Salome HOME
PAL11736. fix reading/writting quadratic elements
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Hypotheses.cxx
1
2 #include "SMESHGUI_Hypotheses.h"
3 #include "SMESHGUI.h"
4 #include "SMESHGUI_HypothesesUtils.h"
5 #include "SMESHGUI_Utils.h"
6 #include "SMESHGUI_SpinBox.h"
7
8 #include <SALOMEDSClient_Study.hxx>
9 #include <utilities.h>
10
11 #include <SMESHGUI.h>
12
13 #include <QtxIntSpinBox.h>
14
15 #include <SUIT_Session.h>
16 #include <SUIT_MessageBox.h>
17
18 #include <LightApp_Application.h>
19
20 #include <qframe.h>
21 #include <qlayout.h>
22 #include <qlineedit.h>
23 #include <qlabel.h>
24 #include <qpixmap.h>
25 #include <qgroupbox.h>
26
27 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& aHypType )
28 : myHypType( aHypType ),
29   myIsCreate( false )
30 {
31 }
32
33 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
34 {
35 }
36
37 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
38                                                 QWidget* parent)
39 {
40   MESSAGE( "Creation of hypothesis with initial params" );
41
42   if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
43     myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
44   create( false, parent );
45 }
46
47 void SMESHGUI_GenericHypothesisCreator::create( const bool isAlgo, QWidget* parent )
48 {
49   MESSAGE( "Creation of hypothesis" );
50
51   // Get default name for hypothesis/algorithm creation
52   HypothesisData* aHypData = SMESH::GetHypothesisData( hypType().latin1() );
53   QString aHypName = aHypData ? aHypData->Label : hypType();
54
55   myIsCreate = true;
56
57   // Create hypothesis/algorithm
58   if (isAlgo)
59     SMESH::CreateHypothesis( hypType(), aHypName, isAlgo );
60
61   else
62   {
63     SMESH::SMESH_Hypothesis_var newHypo = SMESH::SMESH_Hypothesis::_narrow
64       ( SMESH::CreateHypothesis( hypType(), aHypName, false ) );
65   
66     if( !editHypothesis( newHypo.in(), parent ) )
67     { //remove just created hypothesis
68       _PTR(SObject) SHyp = SMESH::FindSObject( newHypo.in() );
69       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
70       if( aStudy && !aStudy->GetProperties()->IsLocked() )
71       {
72         _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
73         aBuilder->RemoveObjectWithChildren( SHyp );
74       }
75     }
76   }
77   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
78 }
79
80 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
81 {
82   if( CORBA::is_nil( h ) )
83     return;
84
85   MESSAGE("Edition of hypothesis");
86
87   myIsCreate = false;
88
89   if( !editHypothesis( h, parent ) )
90     return;
91
92   SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( h );
93   if( listSOmesh.size() > 0 )
94     for( int i=0; i<listSOmesh.size(); i++ )
95     {
96       _PTR(SObject) submSO = listSOmesh[i];
97       SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( submSO );
98       SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( submSO );
99       if( !aSubMesh->_is_nil() )
100         aMesh = aSubMesh->GetFather();
101       _PTR(SObject) meshSO = SMESH::FindSObject( aMesh );
102       SMESH::ModifiedMesh( meshSO, false);
103     }
104   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
105 }
106
107 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
108 {
109   if( CORBA::is_nil( h ) )
110     return false;
111
112   bool res = true;
113   myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
114
115   QFrame* fr = buildFrame();
116   if( fr )
117   {
118     SMESHGUI_HypothesisDlg* dlg = 
119       new SMESHGUI_HypothesisDlg( const_cast<SMESHGUI_GenericHypothesisCreator*>( this ), parent );
120     dlg->setCustomFrame( fr );
121     dlg->setCaption( caption() );
122     dlg->setHIcon( icon() );
123     dlg->setType( type() );
124     retrieveParams();
125     res = dlg->exec()==QDialog::Accepted;
126     if( res ) {
127       QString paramValues = storeParams();
128       if ( !paramValues.isEmpty() ) {
129         if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
130           SMESH::SetValue( SHyp, paramValues );
131       }
132     }
133     delete dlg;
134   }
135   changeWidgets().clear();
136   myHypo = SMESH::SMESH_Hypothesis::_nil();
137   myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
138   return res;
139 }
140
141 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
142 {
143   if( CORBA::is_nil( hypothesis() ) )
144     return 0;
145
146   ListOfStdParams params;
147   if( !stdParams( params ) || params.isEmpty() )
148     return 0;
149
150   QFrame* fr = new QFrame( 0, "myframe" );
151   QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
152
153   QGroupBox* GroupC1 = new QGroupBox( fr, "GroupC1" );
154   lay->addWidget( GroupC1 );
155
156   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
157   GroupC1->setColumnLayout(0, Qt::Vertical );
158   GroupC1->layout()->setSpacing( 0 );
159   GroupC1->layout()->setMargin( 0 );
160   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
161   GroupC1Layout->setAlignment( Qt::AlignTop );
162   GroupC1Layout->setSpacing( 6 );
163   GroupC1Layout->setMargin( 11 );
164
165   ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
166   for( int i=0; anIt!=aLast; anIt++, i++ )
167   {
168     QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
169     GroupC1Layout->addWidget( lab, i, 0 );
170
171     QWidget* w = getCustomWidget( *anIt, GroupC1 );
172     if ( !w ) 
173       switch( (*anIt).myValue.type() )
174       {
175       case QVariant::Int:
176         {
177           QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1, (*anIt).myName.latin1() );
178           attuneStdWidget( sb, i );
179           sb->setValue( (*anIt).myValue.toInt() );
180           connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
181           w = sb;
182         }
183         break;
184       case QVariant::Double:
185         {
186           QtxDblSpinBox* sb = new SMESHGUI_SpinBox( GroupC1, (*anIt).myName.latin1() );
187           attuneStdWidget( sb, i );
188           sb->setValue( (*anIt).myValue.toDouble() );
189           connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
190           w = sb;
191         }
192         break;
193       case QVariant::String:
194         {
195           QLineEdit* le = new QLineEdit( GroupC1, (*anIt).myName.latin1() );
196           attuneStdWidget( le, i );
197           le->setText( (*anIt).myValue.toString() );
198           connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
199           w = le;
200         }
201         break;
202       }
203
204     if( w )
205     {
206       GroupC1Layout->addWidget( w, i, 1 );
207       changeWidgets().append( w );
208     }
209   }
210
211   return fr;
212 }
213
214 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
215 {
216 }
217
218 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
219 {
220   return false;
221 }
222
223 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
224 {
225   bool res = true;
226   StdParam item;
227   ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
228   for( ; anIt!=aLast; anIt++ )
229   {
230     item.myName = (*anIt)->name();
231     if( (*anIt)->inherits( "QtxIntSpinBox" ) )
232     {
233       QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
234       item.myValue = sb->value();
235       params.append( item );
236     }
237     
238     else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
239     {
240       QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
241       item.myValue = sb->value();
242       params.append( item );
243     }
244
245     else if( (*anIt)->inherits( "QLineEdit" ) )
246     {
247       QLineEdit* line = ( QLineEdit* )( *anIt );
248       item.myValue = line->text();
249       params.append( item );
250     }
251
252     else if ( getParamFromCustomWidget( item, *anIt ))
253     {
254       params.append( item );
255     }
256
257     else
258       res = false;
259   }
260   return res;
261 }
262
263 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
264 {
265   QString valueStr = "";
266   ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
267   for( int i=0; param!=aLast; param++, i++ )
268   {
269     if ( i > 0 )
270       valueStr += "; ";
271     switch( (*param).myValue.type() )
272     {
273     case QVariant::Int:
274       valueStr += valueStr.number( (*param).myValue.toInt() );
275       break;
276     case QVariant::Double:
277       valueStr += valueStr.number( (*param).myValue.toDouble() );
278       break;
279     case QVariant::String:
280       valueStr += (*param).myValue.toString();
281       break;
282     default:
283       QVariant valCopy = (*param).myValue;
284       valueStr += valCopy.asString();
285     }
286   }
287   return valueStr;
288 }
289
290 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
291 {
292   return myHypo;
293 }
294
295 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
296 {
297   if ( CORBA::is_nil( myInitParamsHypo ))
298     return myHypo;
299   return myInitParamsHypo;
300 }
301
302 QString SMESHGUI_GenericHypothesisCreator::hypType() const
303 {
304   return myHypType;
305 }
306
307 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
308 {
309   return myParamWidgets;
310 }
311
312 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
313 {
314   return myParamWidgets;
315 }
316
317 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
318 {
319   return myIsCreate;
320 }
321
322 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
323 {
324 }
325
326 QString SMESHGUI_GenericHypothesisCreator::caption() const
327 {
328   return QString();
329 }
330
331 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
332 {
333   return QPixmap();
334 }
335
336 QString SMESHGUI_GenericHypothesisCreator::type() const
337 {
338   return QString();
339 }
340 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
341                                                              QWidget*   /*parent*/) const
342 {
343   return 0;
344 }
345 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
346 {
347   return false;
348 }
349
350
351
352
353 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
354 : QtxDialog( parent, "", true, true ),
355   myCreator( creator )
356 {
357   setMinimumSize( 300, height() );
358 //  setFixedSize( 300, height() );
359   myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
360
361   QFrame* titFrame = new QFrame( mainFrame() );
362   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
363   
364   myIconLabel = new QLabel( titFrame );
365   myIconLabel->setScaledContents( false );
366   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
367   myTypeLabel = new QLabel( titFrame );
368   if( creator )
369     myTypeLabel->setText( creator->hypType() );
370
371   titLay->addWidget( myIconLabel, 0 );
372   titLay->addWidget( myTypeLabel, 0 );
373   titLay->addStretch( 1 );
374
375   myLayout->addWidget( titFrame, 0 );
376
377   QString aHypType = creator->hypType();
378   if ( aHypType == "LocalLength" )
379     myHelpFileName = "/files/arithmetic_1d.htm#Average_length";
380   else if ( aHypType == "Arithmetic1D")
381     myHelpFileName = "/files/arithmetic_1d.htm#arithmetic_1D";
382   else if ( aHypType == "MaxElementArea")
383     myHelpFileName = "/files/max._element_area_hypothesis.htm";
384   else if ( aHypType == "MaxElementVolume")
385     myHelpFileName = "/files/max._element_volume_hypothsis.htm";
386   else if ( aHypType == "StartEndLength")
387     myHelpFileName = "/files/arithmetic_1d.htm#start_and_end_length";
388   else if ( aHypType == "Deflection1D")
389     myHelpFileName = "/files/arithmetic_1d.htm#deflection_1D";
390   else if ( aHypType == "AutomaticLength")
391     myHelpFileName = "/files/arithmetic_1d.htm#automatic_length";
392   else if ( aHypType == "NumberOfSegments")
393     myHelpFileName = "/files/arithmetic_1d.htm#Number_of_elements";
394   else
395     myHelpFileName = "";
396
397   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
398
399 }
400
401 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
402 {
403 }
404
405 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
406 {
407   if( f )
408   {
409     f->reparent( mainFrame(), QPoint( 0, 0 ) );
410     myLayout->insertWidget( 1, f, 1 );
411   }
412 }
413
414 void SMESHGUI_HypothesisDlg::accept()
415 {
416   if( !myCreator || myCreator->checkParams() )
417     QtxDialog::accept();
418 }
419
420 void SMESHGUI_HypothesisDlg::onHelp()
421 {
422   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
423   if (app) {
424     SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
425     app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
426   }
427   else {
428     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
429                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
430                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
431                            QObject::tr("BUT_OK"));
432   }
433 }
434
435 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
436 {
437   myIconLabel->setPixmap( p );  
438 }
439
440 void SMESHGUI_HypothesisDlg::setType( const QString& t )
441 {
442   myTypeLabel->setText( t );
443 }