Salome HOME
Join modifications from branch BR_3_1_0deb
[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 = 0;
148     switch( (*anIt).myValue.type() )
149     {
150     case QVariant::Int:
151       {
152         QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1, (*anIt).myName.latin1() );
153         attuneStdWidget( sb, i );
154         sb->setValue( (*anIt).myValue.toInt() );
155         connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
156         w = sb;
157       }
158       break;
159     case QVariant::Double:
160       {
161         QtxDblSpinBox* sb = new SMESHGUI_SpinBox( GroupC1, (*anIt).myName.latin1() );
162         attuneStdWidget( sb, i );
163         sb->setValue( (*anIt).myValue.toDouble() );
164         connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
165         w = sb;
166       }
167       break;
168     case QVariant::String:
169       {
170         QLineEdit* le = new QLineEdit( GroupC1, (*anIt).myName.latin1() );
171         attuneStdWidget( le, i );
172         le->setText( (*anIt).myValue.toString() );
173         connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
174         w = le;
175       }
176       break;
177     }
178
179     if( w )
180     {
181       GroupC1Layout->addWidget( w, i, 1 );
182       changeWidgets().append( w );
183     }
184   }
185
186   return fr;
187 }
188
189 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
190 {
191 }
192
193 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
194 {
195   return false;
196 }
197
198 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
199 {
200   bool res = true;
201   StdParam item;
202   ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
203   for( ; anIt!=aLast; anIt++ )
204   {
205     item.myName = (*anIt)->name();
206     if( (*anIt)->inherits( "QtxIntSpinBox" ) )
207     {
208       QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
209       item.myValue = sb->value();
210       params.append( item );
211     }
212     
213     else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
214     {
215       QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
216       item.myValue = sb->value();
217       params.append( item );
218     }
219
220     else if( (*anIt)->inherits( "QLineEdit" ) )
221     {
222       QLineEdit* line = ( QLineEdit* )( *anIt );
223       item.myValue = line->text();
224       params.append( item );
225     }
226
227     else 
228       res = false;
229   }
230   return res;
231 }
232
233 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
234 {
235   return myHypo;
236 }
237
238 QString SMESHGUI_GenericHypothesisCreator::hypType() const
239 {
240   return myHypType;
241 }
242
243 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
244 {
245   return myParamWidgets;
246 }
247
248 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
249 {
250   return myParamWidgets;
251 }
252
253 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
254 {
255   return myIsCreate;
256 }
257
258 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
259 {
260 }
261
262 QString SMESHGUI_GenericHypothesisCreator::caption() const
263 {
264   return QString();
265 }
266
267 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
268 {
269   return QPixmap();
270 }
271
272 QString SMESHGUI_GenericHypothesisCreator::type() const
273 {
274   return QString();
275 }
276
277
278
279
280
281
282
283
284 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
285 : QtxDialog( parent, "", true, true, QtxDialog::OKCancel ),
286   myCreator( creator )
287 {
288   setMinimumSize( 300, height() );
289 //  setFixedSize( 300, height() );
290   myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
291
292   QFrame* titFrame = new QFrame( mainFrame() );
293   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
294   
295   myIconLabel = new QLabel( titFrame );
296   myIconLabel->setScaledContents( false );
297   myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
298   myTypeLabel = new QLabel( titFrame );
299   if( creator )
300     myTypeLabel->setText( creator->hypType() );
301
302   titLay->addWidget( myIconLabel, 0 );
303   titLay->addWidget( myTypeLabel, 0 );
304   titLay->addStretch( 1 );
305
306   myLayout->addWidget( titFrame, 0 );
307 }
308
309 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
310 {
311 }
312
313 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
314 {
315   if( f )
316   {
317     f->reparent( mainFrame(), QPoint( 0, 0 ) );
318     myLayout->insertWidget( 1, f, 1 );
319   }
320 }
321
322 void SMESHGUI_HypothesisDlg::accept()
323 {
324   if( !myCreator || myCreator->checkParams() )
325     QtxDialog::accept();
326 }
327
328 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
329 {
330   myIconLabel->setPixmap( p );  
331 }
332
333 void SMESHGUI_HypothesisDlg::setType( const QString& t )
334 {
335   myTypeLabel->setText( t );
336 }