Salome HOME
Merge from OCC_development_generic_2006
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Hypotheses.cxx
1
2 #include "SMESHGUI_Hypotheses.h"
3 #include "SMESHGUI.h"
4 #include "SMESHGUI_HypothesesUtils.h"
5 #include "SMESHGUI_Utils.h"
6 #include "SMESHGUI_SpinBox.h"
7
8 #include <SALOMEDSClient_Study.hxx>
9 #include <utilities.h>
10
11 #include <QtxIntSpinBox.h>
12
13 #include <qframe.h>
14 #include <qlayout.h>
15 #include <qlineedit.h>
16 #include <qlabel.h>
17 #include <qpixmap.h>
18 #include <qgroupbox.h>
19
20 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& aHypType )
21 : myHypType( aHypType ),
22   myIsCreate( false )
23 {
24 }
25
26 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
27 {
28 }
29
30 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
31                                                 QWidget* parent)
32 {
33   MESSAGE( "Creation of hypothesis with initial params" );
34
35   if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
36     myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
37   create( false, parent );
38 }
39
40 void SMESHGUI_GenericHypothesisCreator::create( const bool isAlgo, QWidget* parent )
41 {
42   MESSAGE( "Creation of hypothesis" );
43
44   // Get default name for hypothesis/algorithm creation
45   HypothesisData* aHypData = SMESH::GetHypothesisData( hypType().latin1() );
46   QString aHypName = aHypData ? aHypData->Label : hypType();
47
48   myIsCreate = true;
49
50   // Create hypothesis/algorithm
51   if (isAlgo)
52     SMESH::CreateHypothesis( hypType(), aHypName, isAlgo );
53
54   else
55   {
56     SMESH::SMESH_Hypothesis_var newHypo = SMESH::SMESH_Hypothesis::_narrow
57       ( SMESH::CreateHypothesis( hypType(), aHypName, false ) );
58   
59     if( !editHypothesis( newHypo.in(), parent ) )
60     { //remove just created hypothesis
61       _PTR(SObject) SHyp = SMESH::FindSObject( newHypo.in() );
62       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
63       if( aStudy && !aStudy->GetProperties()->IsLocked() )
64       {
65         _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
66         aBuilder->RemoveObjectWithChildren( SHyp );
67       }
68     }
69   }
70   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
71 }
72
73 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
74 {
75   if( CORBA::is_nil( h ) )
76     return;
77
78   MESSAGE("Edition of hypothesis");
79
80   myIsCreate = false;
81
82   if( !editHypothesis( h, parent ) )
83     return;
84
85   SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( h );
86   if( listSOmesh.size() > 0 )
87     for( int i=0; i<listSOmesh.size(); i++ )
88     {
89       _PTR(SObject) submSO = listSOmesh[i];
90       SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( submSO );
91       SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( submSO );
92       if( !aSubMesh->_is_nil() )
93         aMesh = aSubMesh->GetFather();
94       _PTR(SObject) meshSO = SMESH::FindSObject( aMesh );
95       SMESH::ModifiedMesh( meshSO, false);
96     }
97   SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
98 }
99
100 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
101 {
102   if( CORBA::is_nil( h ) )
103     return false;
104
105   bool res = true;
106   myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
107
108   QFrame* fr = buildFrame();
109   if( fr )
110   {
111     SMESHGUI_HypothesisDlg* dlg = 
112       new SMESHGUI_HypothesisDlg( const_cast<SMESHGUI_GenericHypothesisCreator*>( this ), parent );
113     dlg->setCustomFrame( fr );
114     dlg->setCaption( caption() );
115     dlg->setHIcon( icon() );
116     dlg->setType( type() );
117     retrieveParams();
118     res = dlg->exec()==QDialog::Accepted;
119     if( res ) {
120       QString paramValues = storeParams();
121       if ( !paramValues.isEmpty() ) {
122         if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
123           SMESH::SetValue( SHyp, paramValues );
124       }
125     }
126     delete dlg;
127   }
128   changeWidgets().clear();
129   myHypo = SMESH::SMESH_Hypothesis::_nil();
130   myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
131   return res;
132 }
133
134 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
135 {
136   if( CORBA::is_nil( hypothesis() ) )
137     return 0;
138
139   ListOfStdParams params;
140   if( !stdParams( params ) || params.isEmpty() )
141     return 0;
142
143   QFrame* fr = new QFrame( 0, "myframe" );
144   QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
145
146   QGroupBox* GroupC1 = new QGroupBox( fr, "GroupC1" );
147   lay->addWidget( GroupC1 );
148
149   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
150   GroupC1->setColumnLayout(0, Qt::Vertical );
151   GroupC1->layout()->setSpacing( 0 );
152   GroupC1->layout()->setMargin( 0 );
153   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
154   GroupC1Layout->setAlignment( Qt::AlignTop );
155   GroupC1Layout->setSpacing( 6 );
156   GroupC1Layout->setMargin( 11 );
157
158   ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
159   for( int i=0; anIt!=aLast; anIt++, i++ )
160   {
161     QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
162     GroupC1Layout->addWidget( lab, i, 0 );
163
164     QWidget* w = getCustomWidget( *anIt, GroupC1 );
165     if ( !w ) 
166       switch( (*anIt).myValue.type() )
167       {
168       case QVariant::Int:
169         {
170           QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1, (*anIt).myName.latin1() );
171           attuneStdWidget( sb, i );
172           sb->setValue( (*anIt).myValue.toInt() );
173           connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
174           w = sb;
175         }
176         break;
177       case QVariant::Double:
178         {
179           QtxDblSpinBox* sb = new SMESHGUI_SpinBox( GroupC1, (*anIt).myName.latin1() );
180           attuneStdWidget( sb, i );
181           sb->setValue( (*anIt).myValue.toDouble() );
182           connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
183           w = sb;
184         }
185         break;
186       case QVariant::String:
187         {
188           QLineEdit* le = new QLineEdit( GroupC1, (*anIt).myName.latin1() );
189           attuneStdWidget( le, i );
190           le->setText( (*anIt).myValue.toString() );
191           connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
192           w = le;
193         }
194         break;
195       }
196
197     if( w )
198     {
199       GroupC1Layout->addWidget( w, i, 1 );
200       changeWidgets().append( w );
201     }
202   }
203
204   return fr;
205 }
206
207 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
208 {
209 }
210
211 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
212 {
213   return false;
214 }
215
216 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
217 {
218   bool res = true;
219   StdParam item;
220   ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
221   for( ; anIt!=aLast; anIt++ )
222   {
223     item.myName = (*anIt)->name();
224     if( (*anIt)->inherits( "QtxIntSpinBox" ) )
225     {
226       QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
227       item.myValue = sb->value();
228       params.append( item );
229     }
230     
231     else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
232     {
233       QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
234       item.myValue = sb->value();
235       params.append( item );
236     }
237
238     else if( (*anIt)->inherits( "QLineEdit" ) )
239     {
240       QLineEdit* line = ( QLineEdit* )( *anIt );
241       item.myValue = line->text();
242       params.append( item );
243     }
244
245     else if ( getParamFromCustomWidget( item, *anIt ))
246     {
247       params.append( item );
248     }
249
250     else
251       res = false;
252   }
253   return res;
254 }
255
256 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
257 {
258   QString valueStr = "";
259   ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
260   for( int i=0; param!=aLast; param++, i++ )
261   {
262     if ( i > 0 )
263       valueStr += "; ";
264     switch( (*param).myValue.type() )
265     {
266     case QVariant::Int:
267       valueStr += valueStr.number( (*param).myValue.toInt() );
268       break;
269     case QVariant::Double:
270       valueStr += valueStr.number( (*param).myValue.toDouble() );
271       break;
272     case QVariant::String:
273       valueStr += (*param).myValue.toString();
274       break;
275     default:
276       QVariant valCopy = (*param).myValue;
277       valueStr += valCopy.asString();
278     }
279   }
280   return valueStr;
281 }
282
283 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
284 {
285   return myHypo;
286 }
287
288 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
289 {
290   if ( CORBA::is_nil( myInitParamsHypo ))
291     return myHypo;
292   return myInitParamsHypo;
293 }
294
295 QString SMESHGUI_GenericHypothesisCreator::hypType() const
296 {
297   return myHypType;
298 }
299
300 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
301 {
302   return myParamWidgets;
303 }
304
305 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
306 {
307   return myParamWidgets;
308 }
309
310 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
311 {
312   return myIsCreate;
313 }
314
315 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
316 {
317 }
318
319 QString SMESHGUI_GenericHypothesisCreator::caption() const
320 {
321   return QString();
322 }
323
324 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
325 {
326   return QPixmap();
327 }
328
329 QString SMESHGUI_GenericHypothesisCreator::type() const
330 {
331   return QString();
332 }
333 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
334                                                              QWidget*   /*parent*/) const
335 {
336   return 0;
337 }
338 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
339 {
340   return false;
341 }
342
343
344
345
346 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
347 : QtxDialog( parent, "", true, true, QtxDialog::OKCancel ),
348   myCreator( creator )
349 {
350   setMinimumSize( 300, height() );
351 //  setFixedSize( 300, height() );
352   myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
353
354   QFrame* titFrame = new QFrame( mainFrame() );
355   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
356   
357   myIconLabel = new QLabel( titFrame );
358   myIconLabel->setScaledContents( false );
359   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
360   myTypeLabel = new QLabel( titFrame );
361   if( creator )
362     myTypeLabel->setText( creator->hypType() );
363
364   titLay->addWidget( myIconLabel, 0 );
365   titLay->addWidget( myTypeLabel, 0 );
366   titLay->addStretch( 1 );
367
368   myLayout->addWidget( titFrame, 0 );
369 }
370
371 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
372 {
373 }
374
375 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
376 {
377   if( f )
378   {
379     f->reparent( mainFrame(), QPoint( 0, 0 ) );
380     myLayout->insertWidget( 1, f, 1 );
381   }
382 }
383
384 void SMESHGUI_HypothesisDlg::accept()
385 {
386   if( !myCreator || myCreator->checkParams() )
387     QtxDialog::accept();
388 }
389
390 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
391 {
392   myIconLabel->setPixmap( p );  
393 }
394
395 void SMESHGUI_HypothesisDlg::setType( const QString& t )
396 {
397   myTypeLabel->setText( t );
398 }