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 #include <qapplication.h>
48 SMESHGUI_GenericHypothesisCreator::SMESHGUI_GenericHypothesisCreator( const QString& theHypType )
49 : myHypType( theHypType ), myIsCreate( false ), myDlg( 0 )
53 SMESHGUI_GenericHypothesisCreator::~SMESHGUI_GenericHypothesisCreator()
57 void SMESHGUI_GenericHypothesisCreator::create( SMESH::SMESH_Hypothesis_ptr initParamsHyp,
58 const QString& theHypName,
61 MESSAGE( "Creation of hypothesis with initial params" );
63 if ( !CORBA::is_nil( initParamsHyp ) && hypType() == initParamsHyp->GetName() )
64 myInitParamsHypo = SMESH::SMESH_Hypothesis::_duplicate( initParamsHyp );
65 create( false, theHypName, parent );
68 void SMESHGUI_GenericHypothesisCreator::create( bool isAlgo,
69 const QString& theHypName,
72 MESSAGE( "Creation of hypothesis" );
76 // Create hypothesis/algorithm
78 SMESH::CreateHypothesis( hypType(), theHypName, isAlgo );
81 SMESH::SMESH_Hypothesis_var aHypothesis =
82 SMESH::CreateHypothesis( hypType(), theHypName, false );
83 if( !editHypothesis( aHypothesis.in(), theHypName, theParent ) )
84 { //remove just created hypothesis
85 _PTR(SObject) aHypSObject = SMESH::FindSObject( aHypothesis.in() );
86 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
87 if( aStudy && !aStudy->GetProperties()->IsLocked() )
89 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
90 aBuilder->RemoveObjectWithChildren( aHypSObject );
94 SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
97 void SMESHGUI_GenericHypothesisCreator::edit( SMESH::SMESH_Hypothesis_ptr theHypothesis,
98 const QString& theHypName,
101 if( CORBA::is_nil( theHypothesis ) )
104 MESSAGE("Edition of hypothesis");
108 if( !editHypothesis( theHypothesis, theHypName, theParent ) )
111 SMESH::SObjectList listSOmesh = SMESH::GetMeshesUsingAlgoOrHypothesis( theHypothesis );
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, aMesh->NbNodes()==0);
123 SMESHGUI::GetSMESHGUI()->updateObjBrowser( true, 0 );
126 bool SMESHGUI_GenericHypothesisCreator::editHypothesis( SMESH::SMESH_Hypothesis_ptr h,
127 const QString& theHypName,
130 if( CORBA::is_nil( h ) )
134 myHypName = theHypName;
135 myHypo = SMESH::SMESH_Hypothesis::_duplicate( h );
137 SMESHGUI_HypothesisDlg* Dlg = new SMESHGUI_HypothesisDlg( this, theParent );
139 QFrame* fr = buildFrame();
142 Dlg->setCustomFrame( fr );
143 Dlg->setCaption( caption() );
144 Dlg->setName( theHypName );
145 Dlg->setHIcon( icon() );
146 Dlg->setType( type() );
149 qApp->enter_loop(); // make myDlg not modal
150 res = myDlg->result();
152 QString paramValues = storeParams();
153 if ( !paramValues.isEmpty() ) {
154 if ( _PTR(SObject) SHyp = SMESH::FindSObject( myHypo ))
155 SMESH::SetValue( SHyp, paramValues );
159 delete Dlg; myDlg = 0;
160 changeWidgets().clear();
161 myHypo = SMESH::SMESH_Hypothesis::_nil();
162 myInitParamsHypo = SMESH::SMESH_Hypothesis::_nil();
166 QFrame* SMESHGUI_GenericHypothesisCreator::buildStdFrame()
168 if( CORBA::is_nil( hypothesis() ) )
171 ListOfStdParams params;
172 if( !stdParams( params ) || params.isEmpty() )
175 QFrame* fr = new QFrame( 0, "myframe" );
176 QVBoxLayout* lay = new QVBoxLayout( fr, 5, 0 );
178 QGroupBox* GroupC1 = new QGroupBox( fr, "GroupC1" );
179 lay->addWidget( GroupC1 );
181 GroupC1->setTitle( tr( "SMESH_ARGUMENTS" ) );
182 GroupC1->setColumnLayout(0, Qt::Vertical );
183 GroupC1->layout()->setSpacing( 0 );
184 GroupC1->layout()->setMargin( 0 );
185 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
186 GroupC1Layout->setAlignment( Qt::AlignTop );
187 GroupC1Layout->setSpacing( 6 );
188 GroupC1Layout->setMargin( 11 );
190 ListOfStdParams::const_iterator anIt = params.begin(), aLast = params.end();
191 for( int i=0; anIt!=aLast; anIt++, i++ )
193 QLabel* lab = new QLabel( (*anIt).myName, GroupC1 );
194 GroupC1Layout->addWidget( lab, i, 0 );
196 QWidget* w = getCustomWidget( *anIt, GroupC1, i );
198 switch( (*anIt).myValue.type() )
202 QtxIntSpinBox* sb = new QtxIntSpinBox( GroupC1, (*anIt).myName.latin1() );
203 attuneStdWidget( sb, i );
204 sb->setValue( (*anIt).myValue.toInt() );
205 connect( sb, SIGNAL( valueChanged( int ) ), this, SLOT( onValueChanged() ) );
209 case QVariant::Double:
211 QtxDblSpinBox* sb = new SMESHGUI_SpinBox( GroupC1, (*anIt).myName.latin1() );
212 attuneStdWidget( sb, i );
213 sb->setValue( (*anIt).myValue.toDouble() );
214 connect( sb, SIGNAL( valueChanged( double ) ), this, SLOT( onValueChanged() ) );
218 case QVariant::String:
220 QLineEdit* le = new QLineEdit( GroupC1, (*anIt).myName.latin1() );
221 attuneStdWidget( le, i );
222 le->setText( (*anIt).myValue.toString() );
223 connect( le, SIGNAL( textChanged( const QString& ) ), this, SLOT( onValueChanged() ) );
231 GroupC1Layout->addWidget( w, i, 1 );
232 changeWidgets().append( w );
239 void SMESHGUI_GenericHypothesisCreator::onValueChanged()
243 bool SMESHGUI_GenericHypothesisCreator::stdParams( ListOfStdParams& ) const
248 bool SMESHGUI_GenericHypothesisCreator::getStdParamFromDlg( ListOfStdParams& params ) const
252 ListOfWidgets::const_iterator anIt = widgets().begin(), aLast = widgets().end();
253 for( ; anIt!=aLast; anIt++ )
255 item.myName = (*anIt)->name();
256 if( (*anIt)->inherits( "QtxIntSpinBox" ) )
258 QtxIntSpinBox* sb = ( QtxIntSpinBox* )( *anIt );
259 item.myValue = sb->value();
260 params.append( item );
263 else if( (*anIt)->inherits( "QtxDblSpinBox" ) )
265 QtxDblSpinBox* sb = ( QtxDblSpinBox* )( *anIt );
266 item.myValue = sb->value();
267 params.append( item );
270 else if( (*anIt)->inherits( "QLineEdit" ) )
272 QLineEdit* line = ( QLineEdit* )( *anIt );
273 item.myValue = line->text();
274 params.append( item );
277 else if ( getParamFromCustomWidget( item, *anIt ))
279 params.append( item );
288 QString SMESHGUI_GenericHypothesisCreator::stdParamValues( const ListOfStdParams& params)
290 QString valueStr = "";
291 ListOfStdParams::const_iterator param = params.begin(), aLast = params.end();
293 for( int i=0; param!=aLast; param++, i++ )
295 if ( valueStr.length() > len0 ) {
297 len0 = valueStr.length();
299 switch( (*param).myValue.type() )
302 valueStr += valueStr.number( (*param).myValue.toInt() );
304 case QVariant::Double:
305 valueStr += valueStr.number( (*param).myValue.toDouble() );
307 case QVariant::String:
308 valueStr += (*param).myValue.toString();
311 QVariant valCopy = (*param).myValue;
312 valueStr += valCopy.asString();
318 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::hypothesis() const
323 SMESH::SMESH_Hypothesis_var SMESHGUI_GenericHypothesisCreator::initParamsHypothesis() const
325 if ( CORBA::is_nil( myInitParamsHypo ))
327 return myInitParamsHypo;
330 QString SMESHGUI_GenericHypothesisCreator::hypType() const
335 QString SMESHGUI_GenericHypothesisCreator::hypName() const
340 const SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::widgets() const
342 return myParamWidgets;
345 SMESHGUI_GenericHypothesisCreator::ListOfWidgets& SMESHGUI_GenericHypothesisCreator::changeWidgets()
347 return myParamWidgets;
350 bool SMESHGUI_GenericHypothesisCreator::isCreation() const
355 void SMESHGUI_GenericHypothesisCreator::attuneStdWidget( QWidget*, const int ) const
359 QString SMESHGUI_GenericHypothesisCreator::caption() const
364 QPixmap SMESHGUI_GenericHypothesisCreator::icon() const
369 QString SMESHGUI_GenericHypothesisCreator::type() const
373 QWidget* SMESHGUI_GenericHypothesisCreator::getCustomWidget( const StdParam & /*param*/,
375 const int /*index*/) const
379 bool SMESHGUI_GenericHypothesisCreator::getParamFromCustomWidget( StdParam& , QWidget* ) const
384 void SMESHGUI_GenericHypothesisCreator::onReject()
391 SMESHGUI_HypothesisDlg::SMESHGUI_HypothesisDlg( SMESHGUI_GenericHypothesisCreator* creator, QWidget* parent )
392 : QtxDialog( parent, "", false, true ),
395 setMinimumSize( 300, height() );
396 // setFixedSize( 300, height() );
397 myLayout = new QVBoxLayout( mainFrame(), 0, 0 );
399 QFrame* titFrame = new QFrame( mainFrame() );
400 QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 5 );
402 myIconLabel = new QLabel( titFrame );
403 myIconLabel->setScaledContents( false );
404 myIconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
405 myTypeLabel = new QLabel( titFrame );
407 myTypeLabel->setText( creator->hypType() );
409 titLay->addWidget( myIconLabel, 0 );
410 titLay->addWidget( myTypeLabel, 0 );
411 titLay->addStretch( 1 );
413 myLayout->addWidget( titFrame, 0 );
415 QString aHypType = creator->hypType();
416 if ( aHypType == "LocalLength" )
417 myHelpFileName = "a1d_meshing_hypo_page.html#average_length_anchor";
418 else if ( aHypType == "Arithmetic1D")
419 myHelpFileName = "a1d_meshing_hypo_page.html#arithmetic_1d_anchor";
420 else if ( aHypType == "MaxElementArea")
421 myHelpFileName = "a2d_meshing_hypo_page.html#max_element_area_anchor";
422 else if ( aHypType == "MaxElementVolume")
423 myHelpFileName = "max_element_volume_hypo_page.html";
424 else if ( aHypType == "StartEndLength")
425 myHelpFileName = "a1d_meshing_hypo_page.html#start_and_end_length_anchor";
426 else if ( aHypType == "Deflection1D")
427 myHelpFileName = "a1d_meshing_hypo_page.html#deflection_1d_anchor";
428 else if ( aHypType == "AutomaticLength")
429 myHelpFileName = "a1d_meshing_hypo_page.html#automatic_length_anchor";
430 else if ( aHypType == "NumberOfSegments")
431 myHelpFileName = "a1d_meshing_hypo_page.html#number_of_segments_anchor";
435 connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
438 SMESHGUI_HypothesisDlg::~SMESHGUI_HypothesisDlg()
442 void SMESHGUI_HypothesisDlg::setCustomFrame( QFrame* f )
446 f->reparent( mainFrame(), QPoint( 0, 0 ) );
447 myLayout->insertWidget( 1, f, 1 );
451 void SMESHGUI_HypothesisDlg::accept()
453 if ( myCreator && !myCreator->checkParams() )
459 void SMESHGUI_HypothesisDlg::reject()
461 if ( myCreator ) myCreator->onReject();
466 void SMESHGUI_HypothesisDlg::onHelp()
468 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
470 SMESHGUI* aSMESHGUI = dynamic_cast<SMESHGUI*>( app->activeModule() );
471 app->onHelpContextModule(aSMESHGUI ? app->moduleName(aSMESHGUI->moduleName()) : QString(""), myHelpFileName);
476 platform = "winapplication";
478 platform = "application";
480 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
481 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
482 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
483 QObject::tr("BUT_OK"));
487 void SMESHGUI_HypothesisDlg::setHIcon( const QPixmap& p )
489 myIconLabel->setPixmap( p );
492 void SMESHGUI_HypothesisDlg::setType( const QString& t )
494 myTypeLabel->setText( t );