Salome HOME
69f1c3cc5c295fd02d3ebb3a81f6ab01ec88f146
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Hypotheses.cxx
1 // SMESH SMESHGUI : GUI for SMESH component
2 //
3 // Copyright (C) 2003  CEA
4 //
5 // This library is free software; you can redistribute it and/or 
6 // modify it under the terms of the GNU Lesser General Public 
7 // License as published by the Free Software Foundation; either 
8 // version 2.1 of the License. 
9 //
10 // This library is distributed in the hope that it will be useful, 
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 // Lesser General Public License for more details. 
14 //
15 // You should have received a copy of the GNU Lesser General Public 
16 // License along with this library; if not, write to the Free Software 
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 //
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : SMESHGUI_Hypotheses.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
23 //
24
25 // SMESH includes
26 #include "SMESHGUI_Hypotheses.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_HypothesesUtils.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_SpinBox.h"
32
33 // SALOME KERNEL includes
34 #include <SALOMEDSClient_Study.hxx>
35 #include <utilities.h>
36
37 // SALOME GUI includes
38 #include <QtxIntSpinBox.h>
39 #include <SUIT_Session.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <LightApp_Application.h>
43
44 // Qt includes
45 #include <QFrame>
46 #include <QLineEdit>
47 #include <QLabel>
48 #include <QGroupBox>
49 #include <QVBoxLayout>
50 #include <QEventLoop>
51
52 #define SPACING 6
53 #define MARGIN  11
54
55 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& theHypType )
56   : myHypType( theHypType ), myIsCreate( false ), myDlg( 0 ), myEventLoop( 0 )
57 {
58 }
59
60 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
61 {
62 }
63
64 void SMESHGUI_GenericHypothesisCreator::setInitParamsHypothesis(SMESH::SMESH_Hypothesis_ptr hyp)
65 {
66   if ( !CORBA::is_nil( hyp ) && hypType() == hyp->GetName() )
67     myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( hyp );
68 }
69
70 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
71                                                 const QString& theHypName,
72                                                 QWidget* parent)
73 {
74   MESSAGE( "Creation of hypothesis with initial params" );
75   setInitParamsHypothesis( initParamsHyp );
76   create( false, theHypName, parent );
77 }
78
79 void SMESHGUI_GenericHypothesisCreator::create( bool isAlgo,
80                                                 const QString& theHypName,
81                                                 QWidget* theParent )
82 {
83   MESSAGE( "Creation of hypothesis" );
84
85   myIsCreate = true;
86
87   // Create hypothesis/algorithm
88   if (isAlgo)
89     SMESH::CreateHypothesis( hypType(), theHypName, isAlgo );
90   else
91   {
92     SMESH::SMESH_Hypothesis_var aHypothesis = 
93       SMESH::CreateHypothesis( hypType(), theHypName, false );
94     if( !editHypothesis( aHypothesis.in(), theHypName, theParent ) )
95     { //remove just created hypothesis
96       _PTR(SObject) aHypSObject = SMESH::FindSObject( aHypothesis.in() );
97       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
98       if( aStudy && !aStudy->GetProperties()->IsLocked() )
99       {
100         _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
101         aBuilder->RemoveObjectWithChildren( aHypSObject );
102       }
103     }
104   }
105   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
106 }
107
108 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr theHypothesis,
109                                               const QString& theHypName,
110                                               QWidget* theParent )
111 {
112   if( CORBA::is_nil( theHypothesis ) )
113     return;
114
115   MESSAGE("Edition of hypothesis");
116
117   myIsCreate = false;
118
119   if( !editHypothesis( theHypothesis, theHypName, theParent ) )
120     return;
121
122   SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( theHypothesis );
123   if( listSOmesh.size() > 0 )
124     for( int i = 0; i < listSOmesh.size(); i++ )
125     {
126       _PTR(SObject) submSO = listSOmesh[i];
127       SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( submSO );
128       SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( submSO );
129       if( !aSubMesh->_is_nil() )
130         aMesh = aSubMesh->GetFather();
131       _PTR(SObject) meshSO = SMESH::FindSObject( aMesh );
132       SMESH::ModifiedMesh( meshSO, false, aMesh->NbNodes()==0);
133     }
134   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
135 }
136
137 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, 
138                                                         const QString& theHypName,
139                                                         QWidget* theParent )
140 {
141   if( CORBA::is_nil( h ) )
142     return false;
143
144   bool res = true;
145   myHypName = theHypName;
146   myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
147
148   SMESHGUI_HypothesisDlg* Dlg = new SMESHGUI_HypothesisDlg( this, theParent );
149   connect( Dlg, SIGNAL( finished( int ) ), this, SLOT( onDialogFinished( int ) ) );
150   myDlg = Dlg;
151   QFrame* fr = buildFrame();
152   if( fr )
153   {
154     Dlg->setCustomFrame( fr );
155     Dlg->setWindowTitle( caption() );
156     Dlg->setObjectName( theHypName );
157     Dlg->setHIcon( icon() );
158     Dlg->setType( type() );
159     retrieveParams();
160     Dlg->show();
161     if ( !myEventLoop )
162       myEventLoop = new QEventLoop( this );
163     myEventLoop->exec(); // make myDlg not modal
164     res = myDlg->result();
165     if( res ) {
166       QString paramValues = storeParams();
167       if ( !paramValues.isEmpty() ) {
168         if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
169           SMESH::SetValue( SHyp, paramValues );
170       }
171     }
172   }
173   delete Dlg; myDlg = 0;
174   changeWidgets().clear();
175   myHypo = SMESH::SMESH_Hypothesis::_nil();
176   myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
177   return res;
178 }
179   
180 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
181 {
182   if( CORBA::is_nil( hypothesis() ) )
183     return 0;
184
185   ListOfStdParams params;
186   if( !stdParams( params ) || params.isEmpty() )
187     return 0;
188
189   QFrame* fr = new QFrame( 0 );
190   QVBoxLayout* lay = new QVBoxLayout( fr );
191   lay->setMargin( 5 );
192   lay->setSpacing( 0 );
193
194   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
195   lay->addWidget( GroupC1 );
196
197   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1 );
198   GroupC1Layout->setSpacing( SPACING );
199   GroupC1Layout->setMargin( MARGIN );
200
201   ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
202   for( int i=0; anIt!=aLast; anIt++, i++ )
203   {
204     QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
205     GroupC1Layout->addWidget( lab, i, 0 );
206
207     QWidget* w = getCustomWidget( *anIt, GroupC1, i );
208     if ( !w ) 
209       switch( (*anIt).myValue.type() )
210       {
211       case QVariant::Int:
212         {
213           QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1 );
214           sb->setObjectName( (*anIt).myName );
215           attuneStdWidget( sb, i );
216           sb->setValue( (*anIt).myValue.toInt() );
217           connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
218           w = sb;
219         }
220         break;
221       case QVariant::Double:
222         {
223           QtxDoubleSpinBox* sb = new SMESHGUI_SpinBox( GroupC1 );
224           sb->setObjectName( (*anIt).myName );
225           attuneStdWidget( sb, i );
226           sb->setValue( (*anIt).myValue.toDouble() );
227           connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
228           w = sb;
229         }
230         break;
231       case QVariant::String:
232         {
233           QLineEdit* le = new QLineEdit( GroupC1 );
234           le->setObjectName( (*anIt).myName );
235           attuneStdWidget( le, i );
236           le->setText( (*anIt).myValue.toString() );
237           connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
238           w = le;
239         }
240         break;
241       }
242
243     if( w )
244     {
245       GroupC1Layout->addWidget( w, i, 1 );
246       changeWidgets().append( w );
247     }
248   }
249
250   return fr;
251 }
252
253 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
254 {
255   valueChanged( (QWidget*) sender() );
256 }
257
258 void SMESHGUI_GenericHypothesisCreator::valueChanged( QWidget* )
259 {
260 }
261
262 void SMESHGUI_GenericHypothesisCreator::onDialogFinished( int /*result*/ )
263 {
264   if ( myEventLoop )
265     myEventLoop->exit();
266 }
267
268 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
269 {
270   return false;
271 }
272
273 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
274 {
275   bool res = true;
276   StdParam item;
277   ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
278   for( ; anIt!=aLast; anIt++ )
279   {
280     item.myName = (*anIt)->objectName();
281     if( (*anIt)->inherits( "QtxIntSpinBox" ) )
282     {
283       QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
284       item.myValue = sb->value();
285       params.append( item );
286     }
287     
288     else if( (*anIt)->inherits( "QtxDoubleSpinBox" ) )
289     {
290       QtxDoubleSpinBox* sb = ( QtxDoubleSpinBox* )( *anIt );
291       item.myValue = sb->value();
292       params.append( item );
293     }
294
295     else if( (*anIt)->inherits( "QLineEdit" ) )
296     {
297       QLineEdit* line = ( QLineEdit* )( *anIt );
298       item.myValue = line->text();
299       params.append( item );
300     }
301
302     else if ( getParamFromCustomWidget( item, *anIt ))
303     {
304       params.append( item );
305     }
306
307     else
308       res = false;
309   }
310   return res;
311 }
312
313 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
314 {
315   QString valueStr = "";
316   ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
317   uint len0 = 0;
318   for( int i=0; param!=aLast; param++, i++ )
319   {
320     if ( valueStr.length() > len0 ) {
321       valueStr += "; ";
322       len0 = valueStr.length();
323     }
324     switch( (*param).myValue.type() )
325     {
326     case QVariant::Int:
327       valueStr += valueStr.number( (*param).myValue.toInt() );
328       break;
329     case QVariant::Double:
330       valueStr += valueStr.number( (*param).myValue.toDouble() );
331       break;
332     case QVariant::String:
333       valueStr += (*param).myValue.toString();
334       break;
335     default:
336       QVariant valCopy = (*param).myValue;
337       valueStr += valCopy.toString();
338     }
339   }
340   return valueStr;
341 }
342
343 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
344 {
345   return myHypo;
346 }
347
348 //================================================================================
349 /*!
350  * \brief Return hypothesis containing initial parameters
351  *  \param strictly - if true, always return myInitParamsHypo,
352  *                    else, return myInitParamsHypo only in creation mode and if it
353  *                    is non-nil
354  */
355 //================================================================================
356
357 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis(const bool strictly) const
358 {
359   if ( strictly )
360     return myInitParamsHypo;
361   if ( !isCreation() || CORBA::is_nil( myInitParamsHypo ))
362     return myHypo;
363   return myInitParamsHypo;
364 }
365
366 bool SMESHGUI_GenericHypothesisCreator::hasInitParamsHypothesis() const
367 {
368   return !CORBA::is_nil( myInitParamsHypo );
369 }
370
371 QString SMESHGUI_GenericHypothesisCreator::hypType() const
372 {
373   return myHypType;
374 }
375
376 QString SMESHGUI_GenericHypothesisCreator::hypName() const
377 {
378   return myHypName;
379 }
380
381 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
382 {
383   return myParamWidgets;
384 }
385
386 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
387 {
388   return myParamWidgets;
389 }
390
391 QtxDialog* SMESHGUI_GenericHypothesisCreator:: dlg() const
392
393   return myDlg;
394 }
395
396 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
397 {
398   return myIsCreate;
399 }
400
401 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
402 {
403 }
404
405 QString SMESHGUI_GenericHypothesisCreator::caption() const
406 {
407   return QString();
408 }
409
410 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
411 {
412   return QPixmap();
413 }
414
415 QString SMESHGUI_GenericHypothesisCreator::type() const
416 {
417   return QString();
418 }
419 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
420                                                              QWidget*   /*parent*/,
421                                                              const int  /*index*/) const
422 {
423   return 0;
424 }
425 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam&, QWidget* ) const
426 {
427   return false;
428 }
429
430 void SMESHGUI_GenericHypothesisCreator::onReject()
431 {
432 }
433
434 QString SMESHGUI_GenericHypothesisCreator::helpPage() const
435 {
436   QString aHypType = hypType();
437   QString aHelpFileName;
438   if ( aHypType == "LocalLength" )
439     aHelpFileName = "a1d_meshing_hypo_page.html#average_length_anchor";
440   else if ( aHypType == "Arithmetic1D")
441     aHelpFileName = "a1d_meshing_hypo_page.html#arithmetic_1d_anchor";
442   else if ( aHypType == "MaxElementArea")
443     aHelpFileName = "a2d_meshing_hypo_page.html#max_element_area_anchor";
444   else if ( aHypType == "MaxElementVolume")
445     aHelpFileName = "max_element_volume_hypo_page.html";
446   else if ( aHypType == "StartEndLength")
447     aHelpFileName = "a1d_meshing_hypo_page.html#start_and_end_length_anchor";
448   else if ( aHypType == "Deflection1D")
449     aHelpFileName = "a1d_meshing_hypo_page.html#deflection_1d_anchor";
450   else if ( aHypType == "AutomaticLength")
451     aHelpFileName = "a1d_meshing_hypo_page.html#automatic_length_anchor";
452   else if ( aHypType == "NumberOfSegments")
453     aHelpFileName = "a1d_meshing_hypo_page.html#number_of_segments_anchor";
454   else
455     aHelpFileName = "";
456   return aHelpFileName;
457 }
458
459
460
461
462 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
463 : QtxDialog( parent, false, true ),
464   myCreator( creator )
465 {
466   setMinimumSize( 300, height() );
467 //  setFixedSize( 300, height() );
468   QVBoxLayout* topLayout = new QVBoxLayout( mainFrame() );
469   topLayout->setMargin( 0 );
470   topLayout->setSpacing( 0 );
471
472   QFrame* titFrame = new QFrame( mainFrame() );
473   QHBoxLayout* titLay = new QHBoxLayout( titFrame );
474   titLay->setMargin( 0 );
475   titLay->setSpacing( SPACING );
476   
477   myIconLabel = new QLabel( titFrame );
478   myIconLabel->setScaledContents( false );
479   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
480   myTypeLabel = new QLabel( titFrame );
481   if( creator )
482     myTypeLabel->setText( creator->hypType() );
483
484   titLay->addWidget( myIconLabel, 0 );
485   titLay->addWidget( myTypeLabel, 0 );
486   titLay->addStretch( 1 );
487
488   topLayout->addWidget( titFrame, 0 );
489
490   myHelpFileName = creator->helpPage();
491
492   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
493 }
494
495 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
496 {
497 }
498
499 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
500 {
501   if( f )
502   {
503     f->setParent( mainFrame() );
504     qobject_cast<QVBoxLayout*>( mainFrame()->layout() )->insertWidget( 1, f, 1 );
505   }
506 }
507
508 void SMESHGUI_HypothesisDlg::accept()
509 {
510   if ( myCreator && !myCreator->checkParams() )
511     return;
512   QtxDialog::accept();
513 }
514
515 void SMESHGUI_HypothesisDlg::reject()
516 {
517   if ( myCreator ) myCreator->onReject();
518   QtxDialog::reject();
519 }
520
521 void SMESHGUI_HypothesisDlg::onHelp()
522 {
523   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
524   if (app) {
525     SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
526     app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
527   }
528   else {
529     QString platform;
530 #ifdef WIN32
531     platform = "winapplication";
532 #else
533     platform = "application";
534 #endif
535     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
536                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
537                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
538                                                                  platform)).
539                              arg(myHelpFileName));
540   }
541 }
542
543 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
544 {
545   myIconLabel->setPixmap( p );  
546 }
547
548 void SMESHGUI_HypothesisDlg::setType( const QString& t )
549 {
550   myTypeLabel->setText( t );
551 }
552
553 HypothesisData::HypothesisData( const QString& theTypeName,
554                                 const QString& thePluginName,
555                                 const QString& theServerLibName,
556                                 const QString& theClientLibName,
557                                 const QString& theLabel,
558                                 const QString& theIconId,
559                                 const QList<int>& theDim,
560                                 const bool theIsAux,
561                                 const QStringList& theNeededHypos,
562                                 const QStringList& theOptionalHypos,
563                                 const QStringList& theInputTypes,
564                                 const QStringList& theOutputTypes,
565                                 const bool theIsNeedGeometry,
566                                 const bool supportSub)
567   : TypeName( theTypeName ),
568     PluginName( thePluginName ),
569     ServerLibName( theServerLibName ),
570     ClientLibName( theClientLibName ),
571     Label( theLabel ),
572     IconId( theIconId ),
573     Dim( theDim ),
574     IsAux( theIsAux ),
575     NeededHypos( theNeededHypos ), 
576     OptionalHypos( theOptionalHypos ),
577     InputTypes( theInputTypes ),
578     OutputTypes( theOutputTypes ),
579     IsNeedGeometry( theIsNeedGeometry ),
580     IsSupportSubmeshes( supportSub )
581 {
582 }
583
584 HypothesesSet::HypothesesSet( const QString& theSetName ) 
585   : HypoSetName( theSetName )
586 {
587 }
588
589 HypothesesSet::HypothesesSet( const QString&     theSetName,
590                               const QStringList& theHypoList,
591                               const QStringList& theAlgoList )
592   : HypoSetName( theSetName ), 
593     HypoList( theHypoList ), 
594     AlgoList( theAlgoList )
595 {
596 }