1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "SMESHGUI_Hypotheses.h"
23 #include "SMESHGUI_HypothesesUtils.h"
24 #include "SMESHGUI_Utils.h"
25 #include "SMESHGUI_SpinBox.h"
27 #include <SALOMEDSClient_Study.hxx>
28 #include <utilities.h>
32 #include <QtxIntSpinBox.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_MessageBox.h>
37 #include <LightApp_Application.h>
41 #include <qlineedit.h>
44 #include <qgroupbox.h>
46 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& aHypType )
47 : myHypType( aHypType ),
52 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
56 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
59 MESSAGE( "Creation of hypothesis with initial params" );
61 if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
62 myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
63 create( false, parent );
66 void SMESHGUI_GenericHypothesisCreator::create( const bool isAlgo, QWidget* parent )
68 MESSAGE( "Creation of hypothesis" );
70 // Get default name for hypothesis/algorithm creation
71 HypothesisData* aHypData = SMESH::GetHypothesisData( hypType().latin1() );
72 QString aHypName = aHypData ? aHypData->Label : hypType();
76 // Create hypothesis/algorithm
78 SMESH::CreateHypothesis( hypType(), aHypName, isAlgo );
82 SMESH::SMESH_Hypothesis_var newHypo = SMESH::SMESH_Hypothesis::_narrow
83 ( SMESH::CreateHypothesis( hypType(), aHypName, false ) );
85 if( !editHypothesis( newHypo.in(), parent ) )
86 { //remove just created hypothesis
87 _PTR(SObject) SHyp = SMESH::FindSObject( newHypo.in() );
88 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
89 if( aStudy && !aStudy->GetProperties()->IsLocked() )
91 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
92 aBuilder->RemoveObjectWithChildren( SHyp );
96 SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
99 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
101 if( CORBA::is_nil( h ) )
104 MESSAGE("Edition of hypothesis");
108 if( !editHypothesis( h, parent ) )
111 SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( h );
112 if( listSOmesh.size() > 0 )
113 for( int i=0; i<listSOmesh.size(); i++ )
115 _PTR(SObject) submSO = listSOmesh[i];
116 SMESH::SMESH_Mesh_var aMesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( submSO );
117 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( submSO );
118 if( !aSubMesh->_is_nil() )
119 aMesh = aSubMesh->GetFather();
120 _PTR(SObject) meshSO = SMESH::FindSObject( aMesh );
121 SMESH::ModifiedMesh( meshSO, false);
123 SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
126 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h, QWidget* parent )
128 if( CORBA::is_nil( h ) )
132 myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
134 QFrame* fr = buildFrame();
137 SMESHGUI_HypothesisDlg* dlg =
138 new SMESHGUI_HypothesisDlg( const_cast<SMESHGUI_GenericHypothesisCreator*>( this ), parent );
139 dlg->setCustomFrame( fr );
140 dlg->setCaption( caption() );
141 dlg->setHIcon( icon() );
142 dlg->setType( type() );
144 res = dlg->exec()==QDialog::Accepted;
146 QString paramValues = storeParams();
147 if ( !paramValues.isEmpty() ) {
148 if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
149 SMESH::SetValue( SHyp, paramValues );
154 changeWidgets().clear();
155 myHypo = SMESH::SMESH_Hypothesis::_nil();
156 myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
160 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
162 if( CORBA::is_nil( hypothesis() ) )
165 ListOfStdParams params;
166 if( !stdParams( params ) || params.isEmpty() )
169 QFrame* fr = new QFrame( 0, "myframe" );
170 QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
172 QGroupBox* GroupC1 = new QGroupBox( fr, "GroupC1" );
173 lay->addWidget( GroupC1 );
175 GroupC1->setTitle( tr( "SMESH_ARGUMENTS" ) );
176 GroupC1->setColumnLayout(0, Qt::Vertical );
177 GroupC1->layout()->setSpacing( 0 );
178 GroupC1->layout()->setMargin( 0 );
179 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
180 GroupC1Layout->setAlignment( Qt::AlignTop );
181 GroupC1Layout->setSpacing( 6 );
182 GroupC1Layout->setMargin( 11 );
184 ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
185 for( int i=0; anIt!=aLast; anIt++, i++ )
187 QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
188 GroupC1Layout->addWidget( lab, i, 0 );
190 QWidget* w = getCustomWidget( *anIt, GroupC1 );
192 switch( (*anIt).myValue.type() )
196 QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1, (*anIt).myName.latin1() );
197 attuneStdWidget( sb, i );
198 sb->setValue( (*anIt).myValue.toInt() );
199 connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
203 case QVariant::Double:
205 QtxDblSpinBox* sb = new SMESHGUI_SpinBox( GroupC1, (*anIt).myName.latin1() );
206 attuneStdWidget( sb, i );
207 sb->setValue( (*anIt).myValue.toDouble() );
208 connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
212 case QVariant::String:
214 QLineEdit* le = new QLineEdit( GroupC1, (*anIt).myName.latin1() );
215 attuneStdWidget( le, i );
216 le->setText( (*anIt).myValue.toString() );
217 connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
225 GroupC1Layout->addWidget( w, i, 1 );
226 changeWidgets().append( w );
233 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
237 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
242 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
246 ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
247 for( ; anIt!=aLast; anIt++ )
249 item.myName = (*anIt)->name();
250 if( (*anIt)->inherits( "QtxIntSpinBox" ) )
252 QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
253 item.myValue = sb->value();
254 params.append( item );
257 else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
259 QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
260 item.myValue = sb->value();
261 params.append( item );
264 else if( (*anIt)->inherits( "QLineEdit" ) )
266 QLineEdit* line = ( QLineEdit* )( *anIt );
267 item.myValue = line->text();
268 params.append( item );
271 else if ( getParamFromCustomWidget( item, *anIt ))
273 params.append( item );
282 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
284 QString valueStr = "";
285 ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
286 for( int i=0; param!=aLast; param++, i++ )
290 switch( (*param).myValue.type() )
293 valueStr += valueStr.number( (*param).myValue.toInt() );
295 case QVariant::Double:
296 valueStr += valueStr.number( (*param).myValue.toDouble() );
298 case QVariant::String:
299 valueStr += (*param).myValue.toString();
302 QVariant valCopy = (*param).myValue;
303 valueStr += valCopy.asString();
309 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
314 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
316 if ( CORBA::is_nil( myInitParamsHypo ))
318 return myInitParamsHypo;
321 QString SMESHGUI_GenericHypothesisCreator::hypType() const
326 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
328 return myParamWidgets;
331 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
333 return myParamWidgets;
336 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
341 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
345 QString SMESHGUI_GenericHypothesisCreator::caption() const
350 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
355 QString SMESHGUI_GenericHypothesisCreator::type() const
359 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
360 QWidget* /*parent*/) const
364 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
372 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
373 : QtxDialog( parent, "", true, true ),
376 setMinimumSize( 300, height() );
377 // setFixedSize( 300, height() );
378 myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
380 QFrame* titFrame = new QFrame( mainFrame() );
381 QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
383 myIconLabel = new QLabel( titFrame );
384 myIconLabel->setScaledContents( false );
385 myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
386 myTypeLabel = new QLabel( titFrame );
388 myTypeLabel->setText( creator->hypType() );
390 titLay->addWidget( myIconLabel, 0 );
391 titLay->addWidget( myTypeLabel, 0 );
392 titLay->addStretch( 1 );
394 myLayout->addWidget( titFrame, 0 );
396 QString aHypType = creator->hypType();
397 if ( aHypType == "LocalLength" )
398 myHelpFileName = "/files/arithmetic_1d.htm#Average_length";
399 else if ( aHypType == "Arithmetic1D")
400 myHelpFileName = "/files/arithmetic_1d.htm#arithmetic_1D";
401 else if ( aHypType == "MaxElementArea")
402 myHelpFileName = "/files/max._element_area_hypothesis.htm";
403 else if ( aHypType == "MaxElementVolume")
404 myHelpFileName = "/files/max._element_volume_hypothsis.htm";
405 else if ( aHypType == "StartEndLength")
406 myHelpFileName = "/files/arithmetic_1d.htm#start_and_end_length";
407 else if ( aHypType == "Deflection1D")
408 myHelpFileName = "/files/arithmetic_1d.htm#deflection_1D";
409 else if ( aHypType == "AutomaticLength")
410 myHelpFileName = "/files/arithmetic_1d.htm#automatic_length";
411 else if ( aHypType == "NumberOfSegments")
412 myHelpFileName = "/files/arithmetic_1d.htm#Number_of_elements";
416 connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
420 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
424 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
428 f->reparent( mainFrame(), QPoint( 0, 0 ) );
429 myLayout->insertWidget( 1, f, 1 );
433 void SMESHGUI_HypothesisDlg::accept()
435 if( !myCreator || myCreator->checkParams() )
439 void SMESHGUI_HypothesisDlg::onHelp()
441 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
443 SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
444 app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
449 platform = "winapplication";
451 platform = "application";
453 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
454 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
455 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
456 QObject::tr("BUT_OK"));
460 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
462 myIconLabel->setPixmap( p );
465 void SMESHGUI_HypothesisDlg::setType( const QString& t )
467 myTypeLabel->setText( t );