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