Salome HOME
IPAL21120 SIGSEGV on Meshing attached Compound with Automatic Hexadralization
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Hypotheses.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 #include "SMESHGUI_Hypotheses.h"
23 #include "SMESHGUI.h"
24 #include "SMESHGUI_HypothesesUtils.h"
25 #include "SMESHGUI_Utils.h"
26 #include "SMESHGUI_SpinBox.h"
27
28 #include <SALOMEDSClient_Study.hxx>
29 #include <utilities.h>
30
31 #include <SMESHGUI.h>
32
33 #include <QtxIntSpinBox.h>
34
35 #include <SUIT_Session.h>
36 #include <SUIT_MessageBox.h>
37
38 #include <LightApp_Application.h>
39
40 #include <qframe.h>
41 #include <qlayout.h>
42 #include <qlineedit.h>
43 #include <qlabel.h>
44 #include <qpixmap.h>
45 #include <qgroupbox.h>
46
47 #include <qapplication.h>
48
49 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& theHypType )
50   : myHypType( theHypType ), myIsCreate( false ), myDlg( 0 )
51 {
52 }
53
54 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
55 {
56 }
57
58 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
59                                                 const QString& theHypName,
60                                                 QWidget* parent)
61 {
62   MESSAGE( "Creation of hypothesis with initial params" );
63
64   if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
65     myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
66   create( false, theHypName, parent );
67 }
68
69 void SMESHGUI_GenericHypothesisCreator::create( bool isAlgo,
70                                                 const QString& theHypName,
71                                                 QWidget* theParent )
72 {
73   MESSAGE( "Creation of hypothesis" );
74
75   myIsCreate = true;
76
77   // Create hypothesis/algorithm
78   if (isAlgo)
79     SMESH::CreateHypothesis( hypType(), theHypName, isAlgo );
80   else
81   {
82     SMESH::SMESH_Hypothesis_var aHypothesis = 
83       SMESH::CreateHypothesis( hypType(), theHypName, false );
84     if( !editHypothesis( aHypothesis.in(), theHypName, theParent ) )
85     { //remove just created hypothesis
86       _PTR(SObject) aHypSObject = SMESH::FindSObject( aHypothesis.in() );
87       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
88       if( aStudy && !aStudy->GetProperties()->IsLocked() )
89       {
90         _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
91         aBuilder->RemoveObjectWithChildren( aHypSObject );
92       }
93     }
94   }
95   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
96 }
97
98 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr theHypothesis,
99                                               const QString& theHypName,
100                                               QWidget* theParent )
101 {
102   if( CORBA::is_nil( theHypothesis ) )
103     return;
104
105   MESSAGE("Edition of hypothesis");
106
107   myIsCreate = false;
108
109   if( !editHypothesis( theHypothesis, theHypName, theParent ) )
110     return;
111
112   SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( theHypothesis );
113   if( listSOmesh.size() > 0 )
114     for( int i = 0; i < listSOmesh.size(); i++ )
115     {
116       _PTR(SObject) submSO = listSOmesh[i];
117       SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( submSO );
118       SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( submSO );
119       if( !aSubMesh->_is_nil() )
120         aMesh = aSubMesh->GetFather();
121       _PTR(SObject) meshSO = SMESH::FindSObject( aMesh );
122       SMESH::ModifiedMesh( meshSO, false, aMesh->NbNodes()==0);
123     }
124   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
125 }
126
127 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, 
128                                                         const QString& theHypName,
129                                                         QWidget* theParent )
130 {
131   if( CORBA::is_nil( h ) )
132     return false;
133
134   bool res = true;
135   myHypName = theHypName;
136   myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
137
138   SMESHGUI_HypothesisDlg* Dlg = new SMESHGUI_HypothesisDlg( this, theParent );
139   myDlg = Dlg;
140   QFrame* fr = buildFrame();
141   if( fr )
142   {
143     Dlg->setCustomFrame( fr );
144     Dlg->setCaption( caption() );
145     Dlg->setName( theHypName );
146     Dlg->setHIcon( icon() );
147     Dlg->setType( type() );
148     retrieveParams();
149     Dlg->show();
150     qApp->enter_loop(); // make myDlg not modal
151     res = myDlg->result();
152     if( res ) {
153       QString paramValues = storeParams();
154       if ( !paramValues.isEmpty() ) {
155         if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
156           SMESH::SetValue( SHyp, paramValues );
157       }
158     }
159   }
160   delete Dlg; myDlg = 0;
161   changeWidgets().clear();
162   myHypo = SMESH::SMESH_Hypothesis::_nil();
163   myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
164   return res;
165 }
166   
167 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
168 {
169   if( CORBA::is_nil( hypothesis() ) )
170     return 0;
171
172   ListOfStdParams params;
173   if( !stdParams( params ) || params.isEmpty() )
174     return 0;
175
176   QFrame* fr = new QFrame( 0, "myframe" );
177   QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
178
179   QGroupBox* GroupC1 = new QGroupBox( fr, "GroupC1" );
180   lay->addWidget( GroupC1 );
181
182   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
183   GroupC1->setColumnLayout(0, Qt::Vertical );
184   GroupC1->layout()->setSpacing( 0 );
185   GroupC1->layout()->setMargin( 0 );
186   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
187   GroupC1Layout->setAlignment( Qt::AlignTop );
188   GroupC1Layout->setSpacing( 6 );
189   GroupC1Layout->setMargin( 11 );
190
191   ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
192   for( int i=0; anIt!=aLast; anIt++, i++ )
193   {
194     QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
195     GroupC1Layout->addWidget( lab, i, 0 );
196
197     QWidget* w = getCustomWidget( *anIt, GroupC1, i );
198     if ( !w ) 
199       switch( (*anIt).myValue.type() )
200       {
201       case QVariant::Int:
202         {
203           QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1, (*anIt).myName.latin1() );
204           attuneStdWidget( sb, i );
205           sb->setValue( (*anIt).myValue.toInt() );
206           connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
207           w = sb;
208         }
209         break;
210       case QVariant::Double:
211         {
212           QtxDblSpinBox* sb = new SMESHGUI_SpinBox( GroupC1, (*anIt).myName.latin1() );
213           attuneStdWidget( sb, i );
214           sb->setValue( (*anIt).myValue.toDouble() );
215           connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
216           w = sb;
217         }
218         break;
219       case QVariant::String:
220         {
221           QLineEdit* le = new QLineEdit( GroupC1, (*anIt).myName.latin1() );
222           attuneStdWidget( le, i );
223           le->setText( (*anIt).myValue.toString() );
224           connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
225           w = le;
226         }
227         break;
228       }
229
230     if( w )
231     {
232       GroupC1Layout->addWidget( w, i, 1 );
233       changeWidgets().append( w );
234     }
235   }
236
237   return fr;
238 }
239
240 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
241 {
242 }
243
244 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
245 {
246   return false;
247 }
248
249 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
250 {
251   bool res = true;
252   StdParam item;
253   ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
254   for( ; anIt!=aLast; anIt++ )
255   {
256     item.myName = (*anIt)->name();
257     if( (*anIt)->inherits( "QtxIntSpinBox" ) )
258     {
259       QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
260       item.myValue = sb->value();
261       params.append( item );
262     }
263     
264     else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
265     {
266       QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
267       item.myValue = sb->value();
268       params.append( item );
269     }
270
271     else if( (*anIt)->inherits( "QLineEdit" ) )
272     {
273       QLineEdit* line = ( QLineEdit* )( *anIt );
274       item.myValue = line->text();
275       params.append( item );
276     }
277
278     else if ( getParamFromCustomWidget( item, *anIt ))
279     {
280       params.append( item );
281     }
282
283     else
284       res = false;
285   }
286   return res;
287 }
288
289 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
290 {
291   QString valueStr = "";
292   ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
293   uint len0 = 0;
294   for( int i=0; param!=aLast; param++, i++ )
295   {
296     if ( valueStr.length() > len0 ) {
297       valueStr += "; ";
298       len0 = valueStr.length();
299     }
300     switch( (*param).myValue.type() )
301     {
302     case QVariant::Int:
303       valueStr += valueStr.number( (*param).myValue.toInt() );
304       break;
305     case QVariant::Double:
306       valueStr += valueStr.number( (*param).myValue.toDouble() );
307       break;
308     case QVariant::String:
309       valueStr += (*param).myValue.toString();
310       break;
311     default:
312       QVariant valCopy = (*param).myValue;
313       valueStr += valCopy.asString();
314     }
315   }
316   return valueStr;
317 }
318
319 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
320 {
321   return myHypo;
322 }
323
324 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
325 {
326   if ( CORBA::is_nil( myInitParamsHypo ))
327     return myHypo;
328   return myInitParamsHypo;
329 }
330
331 QString SMESHGUI_GenericHypothesisCreator::hypType() const
332 {
333   return myHypType;
334 }
335
336 QString SMESHGUI_GenericHypothesisCreator::hypName() const
337 {
338   return myHypName;
339 }
340
341 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
342 {
343   return myParamWidgets;
344 }
345
346 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
347 {
348   return myParamWidgets;
349 }
350
351 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
352 {
353   return myIsCreate;
354 }
355
356 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
357 {
358 }
359
360 QString SMESHGUI_GenericHypothesisCreator::caption() const
361 {
362   return QString();
363 }
364
365 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
366 {
367   return QPixmap();
368 }
369
370 QString SMESHGUI_GenericHypothesisCreator::type() const
371 {
372   return QString();
373 }
374 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
375                                                              QWidget*   /*parent*/,
376                                                              const int  /*index*/) const
377 {
378   return 0;
379 }
380 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
381 {
382   return false;
383 }
384
385 void SMESHGUI_GenericHypothesisCreator::onReject()
386 {
387 }
388
389 QString SMESHGUI_GenericHypothesisCreator::helpPage() const
390 {
391   QString aHypType = hypType();
392   QString aHelpFileName;
393   if ( aHypType == "LocalLength" )
394     aHelpFileName = "a1d_meshing_hypo_page.html#average_length_anchor";
395   else if ( aHypType == "Arithmetic1D")
396     aHelpFileName = "a1d_meshing_hypo_page.html#arithmetic_1d_anchor";
397   else if ( aHypType == "MaxElementArea")
398     aHelpFileName = "a2d_meshing_hypo_page.html#max_element_area_anchor";
399   else if ( aHypType == "MaxElementVolume")
400     aHelpFileName = "max_element_volume_hypo_page.html";
401   else if ( aHypType == "StartEndLength")
402     aHelpFileName = "a1d_meshing_hypo_page.html#start_and_end_length_anchor";
403   else if ( aHypType == "Deflection1D")
404     aHelpFileName = "a1d_meshing_hypo_page.html#deflection_1d_anchor";
405   else if ( aHypType == "AutomaticLength")
406     aHelpFileName = "a1d_meshing_hypo_page.html#automatic_length_anchor";
407   else if ( aHypType == "NumberOfSegments")
408     aHelpFileName = "a1d_meshing_hypo_page.html#number_of_segments_anchor";
409   else if ( aHypType == "ProjectionSource1D")
410     aHelpFileName = "projection_algos_page.html";
411   else if ( aHypType == "ProjectionSource2D")
412     aHelpFileName = "projection_algos_page.html";
413   else if ( aHypType == "ProjectionSource3D")
414     aHelpFileName = "projection_algos_page.html";
415   else if ( aHypType == "NumberOfLayers")
416     aHelpFileName = "radial_prism_algo_page.html";
417   else if ( aHypType == "LayerDistribution")
418     aHelpFileName = "radial_prism_algo_page.html";
419   else if ( aHypType == "SegmentLengthAroundVertex")
420     aHelpFileName = "segments_around_vertex_algo.html";
421   else
422     aHelpFileName = "";
423   return aHelpFileName;
424 }
425
426
427 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
428 : QtxDialog( parent, "", false, true ),
429   myCreator( creator )
430 {
431   setMinimumSize( 300, height() );
432 //  setFixedSize( 300, height() );
433   myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
434
435   QFrame* titFrame = new QFrame( mainFrame() );
436   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
437   
438   myIconLabel = new QLabel( titFrame );
439   myIconLabel->setScaledContents( false );
440   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
441   myTypeLabel = new QLabel( titFrame );
442   if( creator )
443     myTypeLabel->setText( creator->hypType() );
444
445   titLay->addWidget( myIconLabel, 0 );
446   titLay->addWidget( myTypeLabel, 0 );
447   titLay->addStretch( 1 );
448
449   myLayout->addWidget( titFrame, 0 );
450
451   myHelpFileName = creator->helpPage();
452
453   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
454 }
455
456 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
457 {
458 }
459
460 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
461 {
462   if( f )
463   {
464     f->reparent( mainFrame(), QPoint( 0, 0 ) );
465     myLayout->insertWidget( 1, f, 1 );
466   }
467 }
468
469 void SMESHGUI_HypothesisDlg::accept()
470 {
471   if ( myCreator && !myCreator->checkParams() )
472     return;
473   QtxDialog::accept();
474   qApp->exit_loop();
475 }
476
477 void SMESHGUI_HypothesisDlg::reject()
478 {
479   if ( myCreator ) myCreator->onReject();
480   QtxDialog::reject();
481   qApp->exit_loop();
482 }
483
484 void SMESHGUI_HypothesisDlg::onHelp()
485 {
486   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
487   if (app) {
488     SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
489     app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
490   }
491   else {
492                 QString platform;
493 #ifdef WIN32
494                 platform = "winapplication";
495 #else
496                 platform = "application";
497 #endif
498     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
499                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
500                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
501                            QObject::tr("BUT_OK"));
502   }
503 }
504
505 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
506 {
507   myIconLabel->setPixmap( p );  
508 }
509
510 void SMESHGUI_HypothesisDlg::setType( const QString& t )
511 {
512   myTypeLabel->setText( t );
513 }