2 #include "SMESHGUI_Hypotheses.h"
4 #include "SMESHGUI_HypothesesUtils.h"
5 #include "SMESHGUI_Utils.h"
6 #include "SMESHGUI_SpinBox.h"
8 #include <SALOMEDSClient_Study.hxx>
11 #include <QtxIntSpinBox.h>
15 #include <qlineedit.h>
18 #include <qgroupbox.h>
20 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& aHypType )
21 : myHypType( aHypType ),
26 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
30 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
33 MESSAGE( "Creation of hypothesis with initial params" );
35 if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
36 myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
37 create( false, parent );
40 void SMESHGUI_GenericHypothesisCreator::create( const bool isAlgo, QWidget* parent )
42 MESSAGE( "Creation of hypothesis" );
44 // Get default name for hypothesis/algorithm creation
45 HypothesisData* aHypData = SMESH::GetHypothesisData( hypType().latin1() );
46 QString aHypName = aHypData ? aHypData->Label : hypType();
50 // Create hypothesis/algorithm
52 SMESH::CreateHypothesis( hypType(), aHypName, isAlgo );
56 SMESH::SMESH_Hypothesis_var newHypo = SMESH::SMESH_Hypothesis::_narrow
57 ( SMESH::CreateHypothesis( hypType(), aHypName, false ) );
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() )
65 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
66 aBuilder->RemoveObjectWithChildren( SHyp );
70 SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
73 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
75 if( CORBA::is_nil( h ) )
78 MESSAGE("Edition of hypothesis");
82 if( !editHypothesis( h, parent ) )
85 SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( h );
86 if( listSOmesh.size() > 0 )
87 for( int i=0; i<listSOmesh.size(); i++ )
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);
97 SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
100 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
102 if( CORBA::is_nil( h ) )
106 myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
108 QFrame* fr = buildFrame();
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() );
118 res = dlg->exec()==QDialog::Accepted;
120 QString paramValues = storeParams();
121 if ( !paramValues.isEmpty() ) {
122 if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
123 SMESH::SetValue( SHyp, paramValues );
128 changeWidgets().clear();
129 myHypo = SMESH::SMESH_Hypothesis::_nil();
130 myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
134 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
136 if( CORBA::is_nil( hypothesis() ) )
139 ListOfStdParams params;
140 if( !stdParams( params ) || params.isEmpty() )
143 QFrame* fr = new QFrame( 0, "myframe" );
144 QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
146 QGroupBox* GroupC1 = new QGroupBox( fr, "GroupC1" );
147 lay->addWidget( GroupC1 );
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 );
158 ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
159 for( int i=0; anIt!=aLast; anIt++, i++ )
161 QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
162 GroupC1Layout->addWidget( lab, i, 0 );
164 QWidget* w = getCustomWidget( *anIt, GroupC1 );
166 switch( (*anIt).myValue.type() )
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() ) );
177 case QVariant::Double:
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() ) );
186 case QVariant::String:
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() ) );
199 GroupC1Layout->addWidget( w, i, 1 );
200 changeWidgets().append( w );
207 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
211 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
216 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
220 ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
221 for( ; anIt!=aLast; anIt++ )
223 item.myName = (*anIt)->name();
224 if( (*anIt)->inherits( "QtxIntSpinBox" ) )
226 QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
227 item.myValue = sb->value();
228 params.append( item );
231 else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
233 QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
234 item.myValue = sb->value();
235 params.append( item );
238 else if( (*anIt)->inherits( "QLineEdit" ) )
240 QLineEdit* line = ( QLineEdit* )( *anIt );
241 item.myValue = line->text();
242 params.append( item );
245 else if ( getParamFromCustomWidget( item, *anIt ))
247 params.append( item );
256 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
258 QString valueStr = "";
259 ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
260 for( int i=0; param!=aLast; param++, i++ )
264 switch( (*param).myValue.type() )
267 valueStr += valueStr.number( (*param).myValue.toInt() );
269 case QVariant::Double:
270 valueStr += valueStr.number( (*param).myValue.toDouble() );
272 case QVariant::String:
273 valueStr += (*param).myValue.toString();
276 QVariant valCopy = (*param).myValue;
277 valueStr += valCopy.asString();
283 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
288 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
290 if ( CORBA::is_nil( myInitParamsHypo ))
292 return myInitParamsHypo;
295 QString SMESHGUI_GenericHypothesisCreator::hypType() const
300 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
302 return myParamWidgets;
305 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
307 return myParamWidgets;
310 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
315 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
319 QString SMESHGUI_GenericHypothesisCreator::caption() const
324 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
329 QString SMESHGUI_GenericHypothesisCreator::type() const
333 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
334 QWidget* /*parent*/) const
338 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
346 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
347 : QtxDialog( parent, "", true, true, QtxDialog::OKCancel ),
350 setMinimumSize( 300, height() );
351 // setFixedSize( 300, height() );
352 myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
354 QFrame* titFrame = new QFrame( mainFrame() );
355 QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
357 myIconLabel = new QLabel( titFrame );
358 myIconLabel->setScaledContents( false );
359 myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
360 myTypeLabel = new QLabel( titFrame );
362 myTypeLabel->setText( creator->hypType() );
364 titLay->addWidget( myIconLabel, 0 );
365 titLay->addWidget( myTypeLabel, 0 );
366 titLay->addStretch( 1 );
368 myLayout->addWidget( titFrame, 0 );
371 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
375 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
379 f->reparent( mainFrame(), QPoint( 0, 0 ) );
380 myLayout->insertWidget( 1, f, 1 );
384 void SMESHGUI_HypothesisDlg::accept()
386 if( !myCreator || myCreator->checkParams() )
390 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
392 myIconLabel->setPixmap( p );
395 void SMESHGUI_HypothesisDlg::setType( const QString& t )
397 myTypeLabel->setText( t );