Salome HOME
Merging with WPdev
[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& aHypType )
49   : myHypType( aHypType ), 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                                                 QWidget* parent)
59 {
60   MESSAGE( "Creation of hypothesis with initial params" );
61
62   if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
63     myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
64   create( false, parent );
65 }
66
67 void SMESHGUI_GenericHypothesisCreator::create( const bool isAlgo, QWidget* parent )
68 {
69   MESSAGE( "Creation of hypothesis" );
70
71   // Get default name for hypothesis/algorithm creation
72   HypothesisData* aHypData = SMESH::GetHypothesisData( hypType().latin1() );
73   QString aHypName = aHypData ? aHypData->Label : hypType();
74
75   myIsCreate = true;
76
77   // Create hypothesis/algorithm
78   if (isAlgo)
79     SMESH::CreateHypothesis( hypType(), aHypName, isAlgo );
80
81   else
82   {
83     SMESH::SMESH_Hypothesis_var newHypo = SMESH::SMESH_Hypothesis::_narrow
84       ( SMESH::CreateHypothesis( hypType(), aHypName, false ) );
85   
86     if( !editHypothesis( newHypo.in(), parent ) )
87     { //remove just created hypothesis
88       _PTR(SObject) SHyp = SMESH::FindSObject( newHypo.in() );
89       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
90       if( aStudy && !aStudy->GetProperties()->IsLocked() )
91       {
92         _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
93         aBuilder->RemoveObjectWithChildren( SHyp );
94       }
95     }
96   }
97   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
98 }
99
100 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
101 {
102   if( CORBA::is_nil( h ) )
103     return;
104
105   MESSAGE("Edition of hypothesis");
106
107   myIsCreate = false;
108
109   if( !editHypothesis( h, parent ) )
110     return;
111
112   SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( h );
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);
123     }
124   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
125 }
126
127 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
128 {
129   if( CORBA::is_nil( h ) )
130     return false;
131
132   bool res = true;
133   myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
134
135   SMESHGUI_HypothesisDlg* Dlg =
136     new SMESHGUI_HypothesisDlg( const_cast<SMESHGUI_GenericHypothesisCreator*>( this ), parent );
137   myDlg = Dlg;
138   QFrame* fr = buildFrame();
139   if( fr )
140   {
141     Dlg->setCustomFrame( fr );
142     Dlg->setCaption( caption() );
143     Dlg->setHIcon( icon() );
144     Dlg->setType( type() );
145     retrieveParams();
146     Dlg->show();
147     //connect(myDlg, SIGNAL( closed() ), this, SLOT( onDlgClosed() ));
148     qApp->enter_loop(); // make myDlg not modal
149 //     res = myDlg->exec()==QDialog::Accepted;
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 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
336 {
337   return myParamWidgets;
338 }
339
340 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
341 {
342   return myParamWidgets;
343 }
344
345 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
346 {
347   return myIsCreate;
348 }
349
350 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
351 {
352 }
353
354 QString SMESHGUI_GenericHypothesisCreator::caption() const
355 {
356   return QString();
357 }
358
359 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
360 {
361   return QPixmap();
362 }
363
364 QString SMESHGUI_GenericHypothesisCreator::type() const
365 {
366   return QString();
367 }
368 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
369                                                              QWidget*   /*parent*/,
370                                                              const int  /*index*/) const
371 {
372   return 0;
373 }
374 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
375 {
376   return false;
377 }
378
379 void SMESHGUI_GenericHypothesisCreator::onReject()
380 {
381 }
382
383
384
385
386 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
387 : QtxDialog( parent, "", false, true ),
388   myCreator( creator )
389 {
390   setMinimumSize( 300, height() );
391 //  setFixedSize( 300, height() );
392   myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
393
394   QFrame* titFrame = new QFrame( mainFrame() );
395   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
396   
397   myIconLabel = new QLabel( titFrame );
398   myIconLabel->setScaledContents( false );
399   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
400   myTypeLabel = new QLabel( titFrame );
401   if( creator )
402     myTypeLabel->setText( creator->hypType() );
403
404   titLay->addWidget( myIconLabel, 0 );
405   titLay->addWidget( myTypeLabel, 0 );
406   titLay->addStretch( 1 );
407
408   myLayout->addWidget( titFrame, 0 );
409
410   QString aHypType = creator->hypType();
411   if ( aHypType == "LocalLength" )
412     myHelpFileName = "/files/arithmetic_1d.htm#Average_length";
413   else if ( aHypType == "Arithmetic1D")
414     myHelpFileName = "/files/arithmetic_1d.htm#arithmetic_1D";
415   else if ( aHypType == "MaxElementArea")
416     myHelpFileName = "/files/max._element_area_hypothesis.htm";
417   else if ( aHypType == "MaxElementVolume")
418     myHelpFileName = "/files/max._element_volume_hypothsis.htm";
419   else if ( aHypType == "StartEndLength")
420     myHelpFileName = "/files/arithmetic_1d.htm#start_and_end_length";
421   else if ( aHypType == "Deflection1D")
422     myHelpFileName = "/files/arithmetic_1d.htm#deflection_1D";
423   else if ( aHypType == "AutomaticLength")
424     myHelpFileName = "/files/arithmetic_1d.htm#automatic_length";
425   else if ( aHypType == "NumberOfSegments")
426     myHelpFileName = "/files/arithmetic_1d.htm#Number_of_elements";
427   else
428     myHelpFileName = "";
429
430   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
431 }
432
433 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
434 {
435 }
436
437 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
438 {
439   if( f )
440   {
441     f->reparent( mainFrame(), QPoint( 0, 0 ) );
442     myLayout->insertWidget( 1, f, 1 );
443   }
444 }
445
446 void SMESHGUI_HypothesisDlg::accept()
447 {
448   if ( myCreator && !myCreator->checkParams() )
449     return;
450   QtxDialog::accept();
451   qApp->exit_loop();
452 }
453
454 void SMESHGUI_HypothesisDlg::reject()
455 {
456   if ( myCreator ) myCreator->onReject();
457   QtxDialog::reject();
458   qApp->exit_loop();
459 }
460
461 void SMESHGUI_HypothesisDlg::onHelp()
462 {
463   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
464   if (app) {
465     SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
466     app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
467   }
468   else {
469                 QString platform;
470 #ifdef WIN32
471                 platform = "winapplication";
472 #else
473                 platform = "application";
474 #endif
475     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
476                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
477                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
478                            QObject::tr("BUT_OK"));
479   }
480 }
481
482 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
483 {
484   myIconLabel->setPixmap( p );  
485 }
486
487 void SMESHGUI_HypothesisDlg::setType( const QString& t )
488 {
489   myTypeLabel->setText( t );
490 }