1 // SMESH StdMeshersGUI : GUI for StdMeshers plugin
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : StdMeshersGUI_CreateHypothesisDlg.cxx
25 // Moved here from SMESHGUI_CreateHypothesisDlg.cxx
26 // Author : Nicolas REJNERI
30 #include "StdMeshersGUI_CreateHypothesisDlg.h"
31 #include "StdMeshersGUI_Parameters.h"
33 #include "SMESHGUI_SpinBox.h"
34 #include "SMESHGUI_Hypotheses.h"
35 #include "SMESHGUI_HypothesesUtils.h"
36 #include "SMESHGUI_Utils.h"
37 #include "SMESHGUI_FunctionPreview.h"
39 #include "SUIT_Application.h"
40 #include "SUIT_Desktop.h"
41 #include "SUIT_MessageBox.h"
42 #include "SUIT_OverrideCursor.h"
43 #include "utilities.h"
45 #include "SalomeApp_Tools.h"
46 #include "SalomeApp_Application.h"
48 #include "OB_Browser.h"
51 #include <qgroupbox.h>
53 #include <qlineedit.h>
54 #include <qpushbutton.h>
58 #include <qtextedit.h>
59 #include <qcombobox.h>
60 #include <qcheckbox.h>
64 //=================================================================================
65 // class : StdMeshersGUI_CreateHypothesisDlg()
66 // purpose : Constructs a StdMeshersGUI_CreateHypothesisDlg which is a child of 'parent', with the
67 // name 'name' and widget flags set to 'f'.
68 // The dialog will by default be modeless, unless you set 'modal' to
69 // TRUE to construct a modal dialog.
70 //=================================================================================
71 StdMeshersGUI_CreateHypothesisDlg::StdMeshersGUI_CreateHypothesisDlg (const QString& hypType,
76 : QDialog (parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose), myHypType( hypType )
80 //=======================================================================
81 //function : CreateDlgLayout
83 //=======================================================================
85 void StdMeshersGUI_CreateHypothesisDlg::CreateDlgLayout(const QString & theCaption,
86 const QPixmap & theHypIcon,
87 const QString & theHypTypeName)
89 setCaption( theCaption );
91 setSizeGripEnabled( TRUE );
92 QVBoxLayout* StdMeshersGUI_CreateHypothesisDlgLayout = new QVBoxLayout( this );
93 StdMeshersGUI_CreateHypothesisDlgLayout->setSpacing( 6 );
94 StdMeshersGUI_CreateHypothesisDlgLayout->setMargin( 11 );
96 /***************************************************************/
97 QFrame* titFrame = new QFrame( this );
98 QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 0 );
100 iconLabel = new QLabel( titFrame );
101 iconLabel->setPixmap( theHypIcon );
102 iconLabel->setScaledContents( false );
103 iconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
104 typeLabel = new QLabel( titFrame );
105 typeLabel->setText( theHypTypeName );
106 titLay->addWidget( iconLabel, 0 );
107 titLay->addWidget( typeLabel, 0 );
108 titLay->addStretch( 1 );
110 StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( titFrame, 0);
112 /***************************************************************/
113 GroupC1 = new QGroupBox( this, "GroupC1" );
114 GroupC1->setTitle( tr( "SMESH_ARGUMENTS" ) );
115 GroupC1->setColumnLayout(0, Qt::Vertical );
116 GroupC1->layout()->setSpacing( 0 );
117 GroupC1->layout()->setMargin( 0 );
118 QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
119 GroupC1Layout->setAlignment( Qt::AlignTop );
120 GroupC1Layout->setSpacing( 6 );
121 GroupC1Layout->setMargin( 11 );
123 TextLabel_NameHypothesis = new QLabel( GroupC1, "TextLabel_NameHypothesis" );
124 TextLabel_NameHypothesis->setText( tr( "SMESH_NAME" ) );
125 GroupC1Layout->addWidget( TextLabel_NameHypothesis, 0, 0 );
127 LineEdit_NameHypothesis = new QLineEdit( GroupC1, "LineEdit_NameHypothesis" );
128 GroupC1Layout->addWidget( LineEdit_NameHypothesis, 0, 1 );
131 std::list<SMESHGUI_aParameterPtr> aParamList;
132 GetParameters( myHypType, aParamList );
133 ASSERT( !aParamList.empty() );
135 /* Spin boxes with labels */
136 list<SMESHGUI_aParameterPtr>::iterator paramIt = aParamList.begin();
138 for ( row = 1; paramIt != aParamList.end(); paramIt++ , row++ )
140 SMESHGUI_aParameterPtr param = (*paramIt);
141 QLabel * label = new QLabel( GroupC1, "TextLabel" );
142 GroupC1Layout->addWidget( label, row, 0 );
143 label->setText( param->Label() );
144 QWidget* aWidget = param->CreateWidget( GroupC1 );
147 GroupC1Layout->addWidget( aWidget, row, 1 );
148 aWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) );
149 aWidget->setMinimumSize( 150, 0 );
151 QString sig = param->sigValueChanged();
152 if( !sig.isEmpty() /* && param->GetType()!=SMESHGUI_aParameter::TABLE*/ )
153 connect( aWidget, sig.latin1(), this, SLOT( onValueChanged() ) );
155 param->InitializeWidget( aWidget );
158 info.editor = aWidget;
162 myParamMap.insert( param, info );
165 myPreview = new SMESHGUI_FunctionPreview( GroupC1 );
167 GroupC1Layout->addWidget( myPreview, row, 1 );
169 StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupC1, 1 );
171 /***************************************************************/
172 GroupButtons = new QGroupBox( this, "GroupButtons" );
173 GroupButtons->setColumnLayout(0, Qt::Vertical );
174 GroupButtons->layout()->setSpacing( 0 );
175 GroupButtons->layout()->setMargin( 0 );
176 QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
177 GroupButtonsLayout->setAlignment( Qt::AlignTop );
178 GroupButtonsLayout->setSpacing( 6 );
179 GroupButtonsLayout->setMargin( 11 );
180 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
181 buttonOk->setText( tr( "SMESH_BUT_OK" ) );
182 buttonOk->setAutoDefault( TRUE );
183 buttonOk->setDefault( TRUE );
184 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
185 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
186 buttonApply->setText( tr( "SMESH_BUT_APPLY" ) );
187 buttonApply->setAutoDefault( TRUE );
188 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
189 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
190 GroupButtonsLayout->addItem( spacer, 0, 2 );
191 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
192 buttonCancel->setText( tr( "SMESH_BUT_CLOSE" ) );
193 buttonCancel->setAutoDefault( TRUE );
194 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
195 StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupButtons, 0 );
197 /***************************************************************/
202 //=================================================================================
203 // function : ~StdMeshersGUI_CreateHypothesisDlg()
204 // purpose : Destroys the object and frees any allocated resources
205 //=================================================================================
206 StdMeshersGUI_CreateHypothesisDlg::~StdMeshersGUI_CreateHypothesisDlg()
208 // no need to delete child widgets, Qt does it all for us
212 //=================================================================================
215 //=================================================================================
216 void StdMeshersGUI_CreateHypothesisDlg::Init()
218 ParameterMap::const_iterator anIt = myParamMap.begin(),
219 aLast = myParamMap.end();
220 for( ; anIt!=aLast; anIt++ )
221 UpdateShown( anIt.key() );
223 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
225 char* sHypType = const_cast<char*>(myHypType.latin1());
226 HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
227 LineEdit_NameHypothesis->setText( aHypData ? aHypData->Label : QString("") );
229 mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
231 /* signals and slots connections */
232 connect( buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()) );
233 connect( buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()) ) ;
234 connect( buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()) );
236 connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
237 connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
239 /* Move widget on the botton right corner of main widget */
241 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
243 this->show() ; /* displays Dialog */
247 //=================================================================================
248 // function : ClickOnOk()
250 //=================================================================================
251 void StdMeshersGUI_CreateHypothesisDlg::ClickOnOk()
253 if ( ClickOnApply() )
257 //=================================================================================
258 // function : ClickOnApply()
260 //=================================================================================
261 bool StdMeshersGUI_CreateHypothesisDlg::ClickOnApply()
263 if ( !mySMESHGUI || mySMESHGUI->isActiveStudyLocked() )
266 QString myHypName = LineEdit_NameHypothesis->text().stripWhiteSpace();
267 if ( myHypName.isEmpty() ) {
268 SUIT_MessageBox::warn1 (this, tr( "SMESH_WRN_WARNING" ),
269 tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
273 SUIT_OverrideCursor wc;
275 SMESH::SMESH_Hypothesis_var Hyp = SMESH::SMESH_Hypothesis::_narrow
276 ( SMESH::CreateHypothesis( myHypType, myHypName, false ) ); // isAlgorithm
280 list<SMESHGUI_aParameterPtr> aParamList;
281 ParameterMap::const_iterator anIt = myParamMap.begin(),
282 aLast = myParamMap.end();
283 for( int i=0; i<myParamMap.count(); i++ )
284 for( anIt=myParamMap.begin(); anIt!=aLast; anIt++ )
285 if( (*anIt).order==i )
287 anIt.key()->TakeValue( anIt.data().editor );
288 aParamList.push_back( anIt.key() );
292 if( !SetParameters( Hyp, aParamList ) )
295 //set new Attribute Comment for hypothesis which parameters were set
296 QString aParams = "";
297 StdMeshersGUI_Parameters::GetParameters( Hyp.in(), aParamList, aParams );
298 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
300 if (!aParams.isEmpty())
302 SMESH::SetValue(SHyp, aParams);
303 mySMESHGUI->getApp()->objectBrowser()->updateTree();
306 catch (const SALOME::SALOME_Exception& S_ex)
310 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
311 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
312 if( aStudy && !aStudy->GetProperties()->IsLocked() )
314 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
315 aBuilder->RemoveObjectWithChildren( SHyp );
316 mySMESHGUI->updateObjBrowser( true, 0 );
319 SalomeApp_Tools::QtCatchCorbaException(S_ex);
325 //=================================================================================
326 // function : ClickOnCancel()
328 //=================================================================================
329 void StdMeshersGUI_CreateHypothesisDlg::ClickOnCancel()
334 //=================================================================================
335 // function : DeactivateActiveDialog()
337 //=================================================================================
338 void StdMeshersGUI_CreateHypothesisDlg::DeactivateActiveDialog()
340 // iconLabel->setEnabled(false) ;
341 // typeLabel->setEnabled(false) ;
342 // GroupC1->setEnabled(false) ;
343 // GroupButtons->setEnabled(false) ;
348 //=================================================================================
349 // function : ActivateThisDialog()
351 //=================================================================================
352 void StdMeshersGUI_CreateHypothesisDlg::ActivateThisDialog()
354 if (! isEnabled() ) {
355 mySMESHGUI->EmitSignalDeactivateDialog() ;
356 // iconLabel->setEnabled(true) ;
357 // typeLabel->setEnabled(true) ;
358 // GroupC1->setEnabled(true) ;
359 // GroupButtons->setEnabled(true) ;
365 //=================================================================================
366 // function : enterEvent()
368 //=================================================================================
369 void StdMeshersGUI_CreateHypothesisDlg::enterEvent(QEvent* e)
371 ActivateThisDialog() ;
375 //=================================================================================
376 // function : closeEvent()
378 //=================================================================================
379 void StdMeshersGUI_CreateHypothesisDlg::closeEvent( QCloseEvent* e )
381 mySMESHGUI->ResetState();
382 QDialog::closeEvent( e );
385 //=================================================================================
386 // function : onValueChanged()
388 //=================================================================================
389 void StdMeshersGUI_CreateHypothesisDlg::onValueChanged()
391 if( sender()->inherits( "QWidget" ) )
393 QWidget* w = ( QWidget* )sender();
395 SMESHGUI_aParameterPtr param;
397 ParameterMap::const_iterator anIt = myParamMap.begin(),
398 aLast = myParamMap.end();
399 for( ; anIt!=aLast; anIt++ )
400 if( anIt.data().editor == w )
403 param->TakeValue( w );
405 SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( param.operator->() );
406 SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( param.operator->() );
407 SMESHGUI_boolParameter* bool_param = dynamic_cast<SMESHGUI_boolParameter*>( param.operator->() );
409 if( str_param && str_param->needPreview() )
411 QString val; str_param->GetNewText( val );
412 myPreview->setParams( val );
414 else if( tab_param && tab_param->needPreview() )
416 SMESH::double_array d;
417 tab_param->data( d );
418 myPreview->setParams( d );
420 else if( bool_param && bool_param->needPreview() )
423 bool_param->GetNewInt( exp );
424 myPreview->setIsExp( exp );
426 UpdateShown( param );
432 //=================================================================================
433 // function : UpdateShown()
435 //=================================================================================
436 void StdMeshersGUI_CreateHypothesisDlg::UpdateShown( const SMESHGUI_aParameterPtr param )
438 SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
440 depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
445 SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
450 depPar->TakeValue( myParamMap[ param ].editor );
451 depPar->GetNewInt( val );
453 bool hasValue = map.contains( val );
455 ParameterMap::const_iterator anIt = myParamMap.begin(),
456 aLast = myParamMap.end();
457 bool preview = false;
458 for( ; anIt!=aLast; anIt++ )
460 bool shown = hasValue && map[ val ].contains( (*anIt).order );
461 (*anIt).editor->setShown( shown );
462 (*anIt).label->setShown( shown );
465 SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( anIt.key().operator->() );
466 SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( anIt.key().operator->() );
467 preview = preview || ( str_param && str_param->needPreview() ) || ( tab_param && tab_param->needPreview() );
470 myPreview->setShown( preview );