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 );
166 GroupC1Layout->addWidget( myPreview, row, 1 );
168 StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupC1, 1 );
170 /***************************************************************/
171 GroupButtons = new QGroupBox( this, "GroupButtons" );
172 GroupButtons->setColumnLayout(0, Qt::Vertical );
173 GroupButtons->layout()->setSpacing( 0 );
174 GroupButtons->layout()->setMargin( 0 );
175 QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
176 GroupButtonsLayout->setAlignment( Qt::AlignTop );
177 GroupButtonsLayout->setSpacing( 6 );
178 GroupButtonsLayout->setMargin( 11 );
179 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
180 buttonOk->setText( tr( "SMESH_BUT_OK" ) );
181 buttonOk->setAutoDefault( TRUE );
182 buttonOk->setDefault( TRUE );
183 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
184 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
185 buttonApply->setText( tr( "SMESH_BUT_APPLY" ) );
186 buttonApply->setAutoDefault( TRUE );
187 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
188 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
189 GroupButtonsLayout->addItem( spacer, 0, 2 );
190 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
191 buttonCancel->setText( tr( "SMESH_BUT_CLOSE" ) );
192 buttonCancel->setAutoDefault( TRUE );
193 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
194 StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupButtons, 0 );
196 /***************************************************************/
201 //=================================================================================
202 // function : ~StdMeshersGUI_CreateHypothesisDlg()
203 // purpose : Destroys the object and frees any allocated resources
204 //=================================================================================
205 StdMeshersGUI_CreateHypothesisDlg::~StdMeshersGUI_CreateHypothesisDlg()
207 // no need to delete child widgets, Qt does it all for us
211 //=================================================================================
214 //=================================================================================
215 void StdMeshersGUI_CreateHypothesisDlg::Init()
217 ParameterMap::const_iterator anIt = myParamMap.begin(),
218 aLast = myParamMap.end();
219 for( ; anIt!=aLast; anIt++ )
220 UpdateShown( anIt.key() );
222 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
224 char* sHypType = const_cast<char*>(myHypType.latin1());
225 HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
226 LineEdit_NameHypothesis->setText( aHypData ? aHypData->Label : QString("") );
228 mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
230 /* signals and slots connections */
231 connect( buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()) );
232 connect( buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()) ) ;
233 connect( buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()) );
235 connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
236 connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
238 /* Move widget on the botton right corner of main widget */
240 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
242 this->show() ; /* displays Dialog */
246 //=================================================================================
247 // function : ClickOnOk()
249 //=================================================================================
250 void StdMeshersGUI_CreateHypothesisDlg::ClickOnOk()
252 if ( ClickOnApply() )
256 //=================================================================================
257 // function : ClickOnApply()
259 //=================================================================================
260 bool StdMeshersGUI_CreateHypothesisDlg::ClickOnApply()
262 if ( !mySMESHGUI || mySMESHGUI->isActiveStudyLocked() )
265 QString myHypName = LineEdit_NameHypothesis->text().stripWhiteSpace();
266 if ( myHypName.isEmpty() ) {
267 SUIT_MessageBox::warn1 (this, tr( "SMESH_WRN_WARNING" ),
268 tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
272 SUIT_OverrideCursor wc;
274 SMESH::SMESH_Hypothesis_var Hyp = SMESH::SMESH_Hypothesis::_narrow
275 ( SMESH::CreateHypothesis( myHypType, myHypName, false ) ); // isAlgorithm
279 list<SMESHGUI_aParameterPtr> aParamList;
280 ParameterMap::const_iterator anIt = myParamMap.begin(),
281 aLast = myParamMap.end();
282 for( int i=0; i<myParamMap.count(); i++ )
283 for( anIt=myParamMap.begin(); anIt!=aLast; anIt++ )
284 if( (*anIt).order==i )
286 anIt.key()->TakeValue( anIt.data().editor );
287 aParamList.push_back( anIt.key() );
291 if( !SetParameters( Hyp, aParamList ) )
294 //set new Attribute Comment for hypothesis which parameters were set
295 QString aParams = "";
296 StdMeshersGUI_Parameters::GetParameters( Hyp.in(), aParamList, aParams );
297 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
299 if (!aParams.isEmpty())
301 SMESH::SetValue(SHyp, aParams);
302 mySMESHGUI->getApp()->objectBrowser()->updateTree();
305 catch (const SALOME::SALOME_Exception& S_ex)
309 _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
310 _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
311 if( aStudy && !aStudy->GetProperties()->IsLocked() )
313 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
314 aBuilder->RemoveObjectWithChildren( SHyp );
315 mySMESHGUI->updateObjBrowser( true, 0 );
318 SalomeApp_Tools::QtCatchCorbaException(S_ex);
324 //=================================================================================
325 // function : ClickOnCancel()
327 //=================================================================================
328 void StdMeshersGUI_CreateHypothesisDlg::ClickOnCancel()
333 //=================================================================================
334 // function : DeactivateActiveDialog()
336 //=================================================================================
337 void StdMeshersGUI_CreateHypothesisDlg::DeactivateActiveDialog()
339 // iconLabel->setEnabled(false) ;
340 // typeLabel->setEnabled(false) ;
341 // GroupC1->setEnabled(false) ;
342 // GroupButtons->setEnabled(false) ;
347 //=================================================================================
348 // function : ActivateThisDialog()
350 //=================================================================================
351 void StdMeshersGUI_CreateHypothesisDlg::ActivateThisDialog()
353 if (! isEnabled() ) {
354 mySMESHGUI->EmitSignalDeactivateDialog() ;
355 // iconLabel->setEnabled(true) ;
356 // typeLabel->setEnabled(true) ;
357 // GroupC1->setEnabled(true) ;
358 // GroupButtons->setEnabled(true) ;
364 //=================================================================================
365 // function : enterEvent()
367 //=================================================================================
368 void StdMeshersGUI_CreateHypothesisDlg::enterEvent(QEvent* e)
370 ActivateThisDialog() ;
374 //=================================================================================
375 // function : closeEvent()
377 //=================================================================================
378 void StdMeshersGUI_CreateHypothesisDlg::closeEvent( QCloseEvent* e )
380 mySMESHGUI->ResetState();
381 QDialog::closeEvent( e );
384 //=================================================================================
385 // function : onValueChanged()
387 //=================================================================================
388 void StdMeshersGUI_CreateHypothesisDlg::onValueChanged()
390 if( sender()->inherits( "QWidget" ) )
392 QWidget* w = ( QWidget* )sender();
394 SMESHGUI_aParameterPtr param;
396 ParameterMap::const_iterator anIt = myParamMap.begin(),
397 aLast = myParamMap.end();
398 for( ; anIt!=aLast; anIt++ )
399 if( anIt.data().editor == w )
402 param->TakeValue( w );
404 SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( param.operator->() );
405 SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( param.operator->() );
406 SMESHGUI_boolParameter* bool_param = dynamic_cast<SMESHGUI_boolParameter*>( param.operator->() );
408 if( str_param && str_param->needPreview() )
410 QString val; str_param->GetNewText( val );
411 myPreview->setParams( val );
413 else if( tab_param && tab_param->needPreview() )
415 SMESH::double_array d;
416 tab_param->data( d );
417 myPreview->setParams( d );
419 else if( bool_param && bool_param->needPreview() )
422 bool_param->GetNewInt( exp );
423 myPreview->setIsExp( exp );
425 UpdateShown( param );
431 //=================================================================================
432 // function : UpdateShown()
434 //=================================================================================
435 void StdMeshersGUI_CreateHypothesisDlg::UpdateShown( const SMESHGUI_aParameterPtr param )
437 SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
439 depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
444 SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
449 depPar->TakeValue( myParamMap[ param ].editor );
450 depPar->GetNewInt( val );
452 bool hasValue = map.contains( val );
454 ParameterMap::const_iterator anIt = myParamMap.begin(),
455 aLast = myParamMap.end();
456 bool preview = false;
457 for( ; anIt!=aLast; anIt++ )
459 bool shown = hasValue && map[ val ].contains( (*anIt).order );
460 (*anIt).editor->setShown( shown );
461 (*anIt).label->setShown( shown );
464 SMESHGUI_strParameter* str_param = dynamic_cast<SMESHGUI_strParameter*>( anIt.key().operator->() );
465 SMESHGUI_tableParameter* tab_param = dynamic_cast<SMESHGUI_tableParameter*>( anIt.key().operator->() );
466 preview = preview || ( str_param && str_param->needPreview() ) || ( tab_param && tab_param->needPreview() );
469 myPreview->setShown( preview );