Salome HOME
PAL8238 - Hypothesis for non-regular 1D meshing
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_CreateHypothesisDlg.cxx
1 //  SMESH StdMeshersGUI : GUI for StdMeshers plugin
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : StdMeshersGUI_CreateHypothesisDlg.cxx
25 //           Moved here from SMESHGUI_CreateHypothesisDlg.cxx
26 //  Author : Nicolas REJNERI
27 //  Module : SMESH
28 //  $Header$
29
30 #include "StdMeshersGUI_CreateHypothesisDlg.h"
31 #include "StdMeshersGUI_Parameters.h"
32 #include "SMESHGUI.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"
38
39 #include "SUIT_Application.h"
40 #include "SUIT_Desktop.h"
41 #include "SUIT_MessageBox.h"
42 #include "SUIT_OverrideCursor.h"
43 #include "utilities.h"
44
45 #include "SalomeApp_Tools.h"
46 #include "SalomeApp_Application.h"
47
48 #include "OB_Browser.h"
49
50 // QT Includes
51 #include <qgroupbox.h>
52 #include <qlabel.h>
53 #include <qlineedit.h>
54 #include <qpushbutton.h>
55 #include <qlayout.h>
56 #include <qpixmap.h>
57 #include <qspinbox.h>
58 #include <qtextedit.h>
59 #include <qcombobox.h>
60 #include <qcheckbox.h>
61
62 using namespace std;
63
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,
72                                                                       QWidget* parent,
73                                                                       const char* name,
74                                                                       bool modal,
75                                                                       WFlags /*fl*/)
76      : QDialog (parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose), myHypType( hypType )
77 {
78 }
79
80 //=======================================================================
81 //function : CreateDlgLayout
82 //purpose  : 
83 //=======================================================================
84
85 void StdMeshersGUI_CreateHypothesisDlg::CreateDlgLayout(const QString & theCaption,
86                                                         const QPixmap & theHypIcon,
87                                                         const QString & theHypTypeName)
88 {
89   setCaption( theCaption );
90
91   setSizeGripEnabled( TRUE );
92   QVBoxLayout* StdMeshersGUI_CreateHypothesisDlgLayout = new QVBoxLayout( this ); 
93   StdMeshersGUI_CreateHypothesisDlgLayout->setSpacing( 6 );
94   StdMeshersGUI_CreateHypothesisDlgLayout->setMargin( 11 );
95
96   /***************************************************************/
97   QFrame* titFrame = new QFrame( this );
98   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 0 );
99   
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 );
109   
110   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( titFrame, 0);
111     
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 );
122
123   TextLabel_NameHypothesis = new QLabel( GroupC1, "TextLabel_NameHypothesis" );
124   TextLabel_NameHypothesis->setText( tr( "SMESH_NAME"  ) );
125   GroupC1Layout->addWidget( TextLabel_NameHypothesis, 0, 0 );
126
127   LineEdit_NameHypothesis = new QLineEdit( GroupC1, "LineEdit_NameHypothesis" );
128   GroupC1Layout->addWidget( LineEdit_NameHypothesis, 0, 1 );
129
130   myParamMap.clear();
131   std::list<SMESHGUI_aParameterPtr> aParamList;
132   GetParameters( myHypType, aParamList );
133   ASSERT( !aParamList.empty() );
134
135   /* Spin boxes with labels */
136   list<SMESHGUI_aParameterPtr>::iterator paramIt = aParamList.begin();
137   int row;
138   for ( row = 1; paramIt != aParamList.end(); paramIt++ , row++ )
139   {
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 );
145
146     if ( aWidget ) {
147       GroupC1Layout->addWidget( aWidget, row, 1 );
148       aWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) );
149       aWidget->setMinimumSize( 150, 0 );
150
151       QString sig = param->sigValueChanged();
152       if( !sig.isEmpty() /* && param->GetType()!=SMESHGUI_aParameter::TABLE*/ )
153         connect( aWidget, sig.latin1(), this, SLOT( onValueChanged() ) );
154          
155       param->InitializeWidget( aWidget );
156
157       ParamInfo info;
158       info.editor = aWidget;
159       info.label = label;
160       info.order = row-1;
161       
162       myParamMap.insert( param, info );
163     }
164   }
165   myPreview = new SMESHGUI_FunctionPreview( GroupC1 );
166   GroupC1Layout->addWidget( myPreview, row, 1 );
167   
168   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupC1, 1 );
169
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 );
195
196   /***************************************************************/
197   Init() ;
198 }
199
200
201 //=================================================================================
202 // function : ~StdMeshersGUI_CreateHypothesisDlg()
203 // purpose  : Destroys the object and frees any allocated resources
204 //=================================================================================
205 StdMeshersGUI_CreateHypothesisDlg::~StdMeshersGUI_CreateHypothesisDlg()
206 {
207     // no need to delete child widgets, Qt does it all for us
208 }
209
210
211 //=================================================================================
212 // function : Init()
213 // purpose  :
214 //=================================================================================
215 void StdMeshersGUI_CreateHypothesisDlg::Init()
216 {
217   ParameterMap::const_iterator anIt = myParamMap.begin(),
218                                aLast = myParamMap.end();
219   for( ; anIt!=aLast; anIt++ )
220     UpdateShown( anIt.key() );
221    
222   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
223
224   char* sHypType = const_cast<char*>(myHypType.latin1());
225   HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
226   LineEdit_NameHypothesis->setText( aHypData ? aHypData->Label : QString("") );
227
228   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
229
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()) );
234
235   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
236   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
237
238   /* Move widget on the botton right corner of main widget */
239   int x, y ;
240   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
241   this->move( x, y ) ;
242   this->show() ; /* displays Dialog */
243 }
244
245
246 //=================================================================================
247 // function : ClickOnOk()
248 // purpose  :
249 //=================================================================================
250 void StdMeshersGUI_CreateHypothesisDlg::ClickOnOk()
251 {
252   if ( ClickOnApply() )
253     ClickOnCancel() ;
254 }
255
256 //=================================================================================
257 // function : ClickOnApply()
258 // purpose  :
259 //=================================================================================
260 bool StdMeshersGUI_CreateHypothesisDlg::ClickOnApply()
261 {
262   if ( !mySMESHGUI || mySMESHGUI->isActiveStudyLocked() )
263     return false;
264
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" ) );
269     return false;
270   }
271
272   SUIT_OverrideCursor wc;
273
274   SMESH::SMESH_Hypothesis_var Hyp = SMESH::SMESH_Hypothesis::_narrow
275       ( SMESH::CreateHypothesis( myHypType, myHypName, false ) ); // isAlgorithm
276   
277   try {
278
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 )
285         {
286           anIt.key()->TakeValue( anIt.data().editor );
287           aParamList.push_back( anIt.key() );
288           break;
289         }
290
291     if( !SetParameters( Hyp, aParamList ) )
292       return false;
293
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());
298     if (SHyp)
299       if (!aParams.isEmpty())
300       {
301         SMESH::SetValue(SHyp, aParams);
302         mySMESHGUI->getApp()->objectBrowser()->updateTree();
303       }
304   }
305   catch (const SALOME::SALOME_Exception& S_ex)
306   {
307     wc.suspend();
308
309     _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
310     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
311     if( aStudy && !aStudy->GetProperties()->IsLocked() )
312     {
313       _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
314       aBuilder->RemoveObjectWithChildren( SHyp );
315       mySMESHGUI->updateObjBrowser( true, 0 );
316     }
317     
318     SalomeApp_Tools::QtCatchCorbaException(S_ex);
319     return false;
320   }
321   return true;
322 }
323
324 //=================================================================================
325 // function : ClickOnCancel()
326 // purpose  :
327 //=================================================================================
328 void StdMeshersGUI_CreateHypothesisDlg::ClickOnCancel()
329 {
330   close();
331 }
332
333 //=================================================================================
334 // function : DeactivateActiveDialog()
335 // purpose  :
336 //=================================================================================
337 void StdMeshersGUI_CreateHypothesisDlg::DeactivateActiveDialog()
338 {
339 //   iconLabel->setEnabled(false) ;
340 //   typeLabel->setEnabled(false) ;
341 //   GroupC1->setEnabled(false) ;
342 //   GroupButtons->setEnabled(false) ;
343   setEnabled(false);
344 }
345
346
347 //=================================================================================
348 // function : ActivateThisDialog()
349 // purpose  :
350 //=================================================================================
351 void StdMeshersGUI_CreateHypothesisDlg::ActivateThisDialog()
352 {
353   if (! isEnabled() ) {
354     mySMESHGUI->EmitSignalDeactivateDialog() ;   
355 //     iconLabel->setEnabled(true) ;
356 //     typeLabel->setEnabled(true) ;
357 //     GroupC1->setEnabled(true) ;
358 //     GroupButtons->setEnabled(true) ;
359     setEnabled(true) ;
360   }
361 }
362
363
364 //=================================================================================
365 // function : enterEvent()
366 // purpose  :
367 //=================================================================================
368 void StdMeshersGUI_CreateHypothesisDlg::enterEvent(QEvent* e)
369 {
370   ActivateThisDialog() ;
371 }
372
373
374 //=================================================================================
375 // function : closeEvent()
376 // purpose  :
377 //=================================================================================
378 void StdMeshersGUI_CreateHypothesisDlg::closeEvent( QCloseEvent* e )
379 {
380   mySMESHGUI->ResetState();
381   QDialog::closeEvent( e );
382 }
383
384 //=================================================================================
385 // function : onValueChanged()
386 // purpose  :
387 //=================================================================================
388 void StdMeshersGUI_CreateHypothesisDlg::onValueChanged()
389 {
390   if( sender()->inherits( "QWidget" ) )
391   {
392     QWidget* w = ( QWidget* )sender();
393
394     SMESHGUI_aParameterPtr param;
395
396     ParameterMap::const_iterator anIt = myParamMap.begin(),
397                                  aLast = myParamMap.end();
398     for( ; anIt!=aLast; anIt++ )
399       if( anIt.data().editor == w )
400       {
401         param = anIt.key();
402         param->TakeValue( w );
403
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->() );
407
408         if( str_param && str_param->needPreview() )
409         {
410           QString val; str_param->GetNewText( val );
411           myPreview->setParams( val );
412         }
413         else if( tab_param && tab_param->needPreview() )
414         {
415           SMESH::double_array d;
416           tab_param->data( d );
417           myPreview->setParams( d );
418         }
419         else if( bool_param && bool_param->needPreview() )
420         {
421           int exp=0;
422           bool_param->GetNewInt( exp );
423           myPreview->setIsExp( exp );
424         }
425         UpdateShown( param );
426         break;
427       }
428   }
429 }
430
431 //=================================================================================
432 // function : UpdateShown()
433 // purpose  :
434 //=================================================================================
435 void StdMeshersGUI_CreateHypothesisDlg::UpdateShown( const SMESHGUI_aParameterPtr param )
436 {
437   SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
438   if( !depPar )
439     depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
440
441   if( !depPar )
442     return;
443
444   SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
445   if( map.isEmpty() )
446     return;
447
448   int val;
449   depPar->TakeValue( myParamMap[ param ].editor );
450   depPar->GetNewInt( val );
451   
452   bool hasValue = map.contains( val );
453
454   ParameterMap::const_iterator anIt = myParamMap.begin(),
455                                 aLast = myParamMap.end();
456   bool preview = false;
457   for( ; anIt!=aLast; anIt++ )
458   {
459     bool shown = hasValue && map[ val ].contains( (*anIt).order );
460     (*anIt).editor->setShown( shown );
461     (*anIt).label->setShown( shown );
462     if( shown )
463     {
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() );
467     }
468   }
469   myPreview->setShown( preview );
470 }