Salome HOME
adadbfb76cbf39b359f5578ead48150ace5323d5
[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::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
65                                                 const QString& theHypName,
66                                                 QWidget* parent)
67 {
68   MESSAGE( "Creation of hypothesis with initial params" );
69
70   if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
71     myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
72   create( false, theHypName, parent );
73 }
74
75 void SMESHGUI_GenericHypothesisCreator::create( bool isAlgo,
76                                                 const QString& theHypName,
77                                                 QWidget* theParent )
78 {
79   MESSAGE( "Creation of hypothesis" );
80
81   myIsCreate = true;
82
83   // Create hypothesis/algorithm
84   if (isAlgo)
85     SMESH::CreateHypothesis( hypType(), theHypName, isAlgo );
86   else
87   {
88     SMESH::SMESH_Hypothesis_var aHypothesis = 
89       SMESH::CreateHypothesis( hypType(), theHypName, false );
90     if( !editHypothesis( aHypothesis.in(), theHypName, theParent ) )
91     { //remove just created hypothesis
92       _PTR(SObject) aHypSObject = SMESH::FindSObject( aHypothesis.in() );
93       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
94       if( aStudy && !aStudy->GetProperties()->IsLocked() )
95       {
96         _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
97         aBuilder->RemoveObjectWithChildren( aHypSObject );
98       }
99     }
100   }
101   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
102 }
103
104 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr theHypothesis,
105                                               const QString& theHypName,
106                                               QWidget* theParent )
107 {
108   if( CORBA::is_nil( theHypothesis ) )
109     return;
110
111   MESSAGE("Edition of hypothesis");
112
113   myIsCreate = false;
114
115   if( !editHypothesis( theHypothesis, theHypName, theParent ) )
116     return;
117
118   SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( theHypothesis );
119   if( listSOmesh.size() > 0 )
120     for( int i = 0; i < listSOmesh.size(); i++ )
121     {
122       _PTR(SObject) submSO = listSOmesh[i];
123       SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( submSO );
124       SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( submSO );
125       if( !aSubMesh->_is_nil() )
126         aMesh = aSubMesh->GetFather();
127       _PTR(SObject) meshSO = SMESH::FindSObject( aMesh );
128       SMESH::ModifiedMesh( meshSO, false, aMesh->NbNodes()==0);
129     }
130   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
131 }
132
133 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, 
134                                                         const QString& theHypName,
135                                                         QWidget* theParent )
136 {
137   if( CORBA::is_nil( h ) )
138     return false;
139
140   bool res = true;
141   myHypName = theHypName;
142   myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
143
144   SMESHGUI_HypothesisDlg* Dlg = new SMESHGUI_HypothesisDlg( this, theParent );
145   connect( Dlg, SIGNAL( finished( int ) ), this, SLOT( onDialogFinished( int ) ) );
146   myDlg = Dlg;
147   QFrame* fr = buildFrame();
148   if( fr )
149   {
150     Dlg->setCustomFrame( fr );
151     Dlg->setWindowTitle( caption() );
152     Dlg->setObjectName( theHypName );
153     Dlg->setHIcon( icon() );
154     Dlg->setType( type() );
155     Dlg->setMinimumSize( Dlg->minimumSizeHint().width(), Dlg->minimumSizeHint().height() );
156     retrieveParams();
157     Dlg->show();
158     if ( !myEventLoop )
159       myEventLoop = new QEventLoop( this );
160     myEventLoop->exec(); // make myDlg not modal
161     res = myDlg->result();
162     if( res ) {
163       QString paramValues = storeParams();
164       if ( !paramValues.isEmpty() ) {
165         if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
166           SMESH::SetValue( SHyp, paramValues );
167       }
168     }
169   }
170   delete Dlg; myDlg = 0;
171   changeWidgets().clear();
172   myHypo = SMESH::SMESH_Hypothesis::_nil();
173   myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
174   return res;
175 }
176   
177 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
178 {
179   if( CORBA::is_nil( hypothesis() ) )
180     return 0;
181
182   ListOfStdParams params;
183   if( !stdParams( params ) || params.isEmpty() )
184     return 0;
185
186   QFrame* fr = new QFrame( 0 );
187   QVBoxLayout* lay = new QVBoxLayout( fr );
188   lay->setMargin( 5 );
189   lay->setSpacing( 0 );
190
191   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
192   lay->addWidget( GroupC1 );
193
194   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1 );
195   GroupC1Layout->setSpacing( SPACING );
196   GroupC1Layout->setMargin( MARGIN );
197
198   ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
199   for( int i=0; anIt!=aLast; anIt++, i++ )
200   {
201     QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
202     GroupC1Layout->addWidget( lab, i, 0 );
203
204     QWidget* w = getCustomWidget( *anIt, GroupC1, i );
205     if ( !w ) 
206       switch( (*anIt).myValue.type() )
207       {
208       case QVariant::Int:
209         {
210           QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1 );
211           sb->setObjectName( (*anIt).myName );
212           attuneStdWidget( sb, i );
213           sb->setValue( (*anIt).myValue.toInt() );
214           connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
215           w = sb;
216         }
217         break;
218       case QVariant::Double:
219         {
220           QtxDoubleSpinBox* sb = new SMESHGUI_SpinBox( GroupC1 );
221           sb->setObjectName( (*anIt).myName );
222           attuneStdWidget( sb, i );
223           sb->setValue( (*anIt).myValue.toDouble() );
224           connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
225           w = sb;
226         }
227         break;
228       case QVariant::String:
229         {
230           QLineEdit* le = new QLineEdit( GroupC1 );
231           le->setObjectName( (*anIt).myName );
232           attuneStdWidget( le, i );
233           le->setText( (*anIt).myValue.toString() );
234           connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
235           w = le;
236         }
237         break;
238       }
239
240     if( w )
241     {
242       GroupC1Layout->addWidget( w, i, 1 );
243       changeWidgets().append( w );
244     }
245   }
246
247   return fr;
248 }
249
250 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
251 {
252 }
253
254 void SMESHGUI_GenericHypothesisCreator::onDialogFinished( int /*result*/ )
255 {
256   if ( myEventLoop )
257     myEventLoop->exit();
258 }
259
260 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
261 {
262   return false;
263 }
264
265 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
266 {
267   bool res = true;
268   StdParam item;
269   ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
270   for( ; anIt!=aLast; anIt++ )
271   {
272     item.myName = (*anIt)->objectName();
273     if( (*anIt)->inherits( "QtxIntSpinBox" ) )
274     {
275       QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
276       item.myValue = sb->value();
277       params.append( item );
278     }
279     
280     else if( (*anIt)->inherits( "QtxDoubleSpinBox" ) )
281     {
282       QtxDoubleSpinBox* sb = ( QtxDoubleSpinBox* )( *anIt );
283       item.myValue = sb->value();
284       params.append( item );
285     }
286
287     else if( (*anIt)->inherits( "QLineEdit" ) )
288     {
289       QLineEdit* line = ( QLineEdit* )( *anIt );
290       item.myValue = line->text();
291       params.append( item );
292     }
293
294     else if ( getParamFromCustomWidget( item, *anIt ))
295     {
296       params.append( item );
297     }
298
299     else
300       res = false;
301   }
302   return res;
303 }
304
305 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
306 {
307   QString valueStr = "";
308   ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
309   uint len0 = 0;
310   for( int i=0; param!=aLast; param++, i++ )
311   {
312     if ( valueStr.length() > len0 ) {
313       valueStr += "; ";
314       len0 = valueStr.length();
315     }
316     switch( (*param).myValue.type() )
317     {
318     case QVariant::Int:
319       valueStr += valueStr.number( (*param).myValue.toInt() );
320       break;
321     case QVariant::Double:
322       valueStr += valueStr.number( (*param).myValue.toDouble() );
323       break;
324     case QVariant::String:
325       valueStr += (*param).myValue.toString();
326       break;
327     default:
328       QVariant valCopy = (*param).myValue;
329       valueStr += valCopy.toString();
330     }
331   }
332   return valueStr;
333 }
334
335 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
336 {
337   return myHypo;
338 }
339
340 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
341 {
342   if ( CORBA::is_nil( myInitParamsHypo ))
343     return myHypo;
344   return myInitParamsHypo;
345 }
346
347 QString SMESHGUI_GenericHypothesisCreator::hypType() const
348 {
349   return myHypType;
350 }
351
352 QString SMESHGUI_GenericHypothesisCreator::hypName() const
353 {
354   return myHypName;
355 }
356
357 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
358 {
359   return myParamWidgets;
360 }
361
362 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
363 {
364   return myParamWidgets;
365 }
366
367 QtxDialog* SMESHGUI_GenericHypothesisCreator:: dlg() const
368
369   return myDlg;
370 }
371
372 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
373 {
374   return myIsCreate;
375 }
376
377 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
378 {
379 }
380
381 QString SMESHGUI_GenericHypothesisCreator::caption() const
382 {
383   return QString();
384 }
385
386 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
387 {
388   return QPixmap();
389 }
390
391 QString SMESHGUI_GenericHypothesisCreator::type() const
392 {
393   return QString();
394 }
395 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
396                                                              QWidget*   /*parent*/,
397                                                              const int  /*index*/) const
398 {
399   return 0;
400 }
401 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam&, QWidget* ) const
402 {
403   return false;
404 }
405
406 void SMESHGUI_GenericHypothesisCreator::onReject()
407 {
408 }
409
410
411
412
413 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
414 : QtxDialog( parent, false, true ),
415   myCreator( creator )
416 {
417   setMinimumSize( 300, height() );
418 //  setFixedSize( 300, height() );
419   QVBoxLayout* topLayout = new QVBoxLayout( mainFrame() );
420   topLayout->setMargin( 0 );
421   topLayout->setSpacing( 0 );
422
423   QFrame* titFrame = new QFrame( mainFrame() );
424   QHBoxLayout* titLay = new QHBoxLayout( titFrame );
425   titLay->setMargin( 0 );
426   titLay->setSpacing( SPACING );
427   
428   myIconLabel = new QLabel( titFrame );
429   myIconLabel->setScaledContents( false );
430   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
431   myTypeLabel = new QLabel( titFrame );
432   if( creator )
433     myTypeLabel->setText( creator->hypType() );
434
435   titLay->addWidget( myIconLabel, 0 );
436   titLay->addWidget( myTypeLabel, 0 );
437   titLay->addStretch( 1 );
438
439   topLayout->addWidget( titFrame, 0 );
440
441   QString aHypType = creator->hypType();
442   if ( aHypType == "LocalLength" )
443     myHelpFileName = "a1d_meshing_hypo_page.html#average_length_anchor";
444   else if ( aHypType == "Arithmetic1D")
445     myHelpFileName = "a1d_meshing_hypo_page.html#arithmetic_1d_anchor";
446   else if ( aHypType == "MaxElementArea")
447     myHelpFileName = "a2d_meshing_hypo_page.html#max_element_area_anchor";
448   else if ( aHypType == "MaxElementVolume")
449     myHelpFileName = "max_element_volume_hypo_page.html";
450   else if ( aHypType == "StartEndLength")
451     myHelpFileName = "a1d_meshing_hypo_page.html#start_and_end_length_anchor";
452   else if ( aHypType == "Deflection1D")
453     myHelpFileName = "a1d_meshing_hypo_page.html#deflection_1d_anchor";
454   else if ( aHypType == "AutomaticLength")
455     myHelpFileName = "a1d_meshing_hypo_page.html#automatic_length_anchor";
456   else if ( aHypType == "NumberOfSegments")
457     myHelpFileName = "a1d_meshing_hypo_page.html#number_of_segments_anchor";
458   else
459     myHelpFileName = "";
460
461   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
462 }
463
464 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
465 {
466 }
467
468 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
469 {
470   if( f )
471   {
472     f->setParent( mainFrame() );
473     qobject_cast<QVBoxLayout*>( mainFrame()->layout() )->insertWidget( 1, f, 1 );
474   }
475 }
476
477 void SMESHGUI_HypothesisDlg::accept()
478 {
479   if ( myCreator && !myCreator->checkParams() )
480     return;
481   QtxDialog::accept();
482 }
483
484 void SMESHGUI_HypothesisDlg::reject()
485 {
486   if ( myCreator ) myCreator->onReject();
487   QtxDialog::reject();
488 }
489
490 void SMESHGUI_HypothesisDlg::onHelp()
491 {
492   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
493   if (app) {
494     SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
495     app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
496   }
497   else {
498     QString platform;
499 #ifdef WIN32
500     platform = "winapplication";
501 #else
502     platform = "application";
503 #endif
504     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
505                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
506                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
507                                                                  platform)).
508                              arg(myHelpFileName));
509   }
510 }
511
512 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
513 {
514   myIconLabel->setPixmap( p );  
515 }
516
517 void SMESHGUI_HypothesisDlg::setType( const QString& t )
518 {
519   myTypeLabel->setText( t );
520 }
521
522 HypothesisData::HypothesisData( const QString& theTypeName,
523                                 const QString& thePluginName,
524                                 const QString& theServerLibName,
525                                 const QString& theClientLibName,
526                                 const QString& theLabel,
527                                 const QString& theIconId,
528                                 const QList<int>& theDim,
529                                 const bool theIsAux,
530                                 const QStringList& theNeededHypos,
531                                 const QStringList& theOptionalHypos,
532                                 const QStringList& theInputTypes,
533                                 const QStringList& theOutputTypes,
534                                 const bool theIsNeedGeometry )
535   : TypeName( theTypeName ),
536     PluginName( thePluginName ),
537     ServerLibName( theServerLibName ),
538     ClientLibName( theClientLibName ),
539     Label( theLabel ),
540     IconId( theIconId ),
541     Dim( theDim ),
542     IsAux( theIsAux ),
543     NeededHypos( theNeededHypos ), 
544     OptionalHypos( theOptionalHypos ),
545     InputTypes( theInputTypes ),
546     OutputTypes( theOutputTypes ),
547     IsNeedGeometry( theIsNeedGeometry )
548 {
549 }
550
551 HypothesesSet::HypothesesSet( const QString& theSetName ) 
552   : HypoSetName( theSetName )
553 {
554 }
555
556 HypothesesSet::HypothesesSet( const QString&     theSetName,
557                               const QStringList& theHypoList,
558                               const QStringList& theAlgoList )
559   : HypoSetName( theSetName ), 
560     HypoList( theHypoList ), 
561     AlgoList( theAlgoList )
562 {
563 }