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