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