Salome HOME
PAL10665 - unnecessary Plot2d in CreateHypothesis dialog
[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   myPreview->hide();
167   GroupC1Layout->addWidget( myPreview, row, 1 );
168   
169   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupC1, 1 );
170
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 );
196
197   /***************************************************************/
198   Init() ;
199 }
200
201
202 //=================================================================================
203 // function : ~StdMeshersGUI_CreateHypothesisDlg()
204 // purpose  : Destroys the object and frees any allocated resources
205 //=================================================================================
206 StdMeshersGUI_CreateHypothesisDlg::~StdMeshersGUI_CreateHypothesisDlg()
207 {
208     // no need to delete child widgets, Qt does it all for us
209 }
210
211
212 //=================================================================================
213 // function : Init()
214 // purpose  :
215 //=================================================================================
216 void StdMeshersGUI_CreateHypothesisDlg::Init()
217 {
218   ParameterMap::const_iterator anIt = myParamMap.begin(),
219                                aLast = myParamMap.end();
220   for( ; anIt!=aLast; anIt++ )
221     UpdateShown( anIt.key() );
222    
223   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
224
225   char* sHypType = const_cast<char*>(myHypType.latin1());
226   HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
227   LineEdit_NameHypothesis->setText( aHypData ? aHypData->Label : QString("") );
228
229   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
230
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()) );
235
236   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
237   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
238
239   /* Move widget on the botton right corner of main widget */
240   int x, y ;
241   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
242   this->move( x, y ) ;
243   this->show() ; /* displays Dialog */
244 }
245
246
247 //=================================================================================
248 // function : ClickOnOk()
249 // purpose  :
250 //=================================================================================
251 void StdMeshersGUI_CreateHypothesisDlg::ClickOnOk()
252 {
253   if ( ClickOnApply() )
254     ClickOnCancel() ;
255 }
256
257 //=================================================================================
258 // function : ClickOnApply()
259 // purpose  :
260 //=================================================================================
261 bool StdMeshersGUI_CreateHypothesisDlg::ClickOnApply()
262 {
263   if ( !mySMESHGUI || mySMESHGUI->isActiveStudyLocked() )
264     return false;
265
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" ) );
270     return false;
271   }
272
273   SUIT_OverrideCursor wc;
274
275   SMESH::SMESH_Hypothesis_var Hyp = SMESH::SMESH_Hypothesis::_narrow
276       ( SMESH::CreateHypothesis( myHypType, myHypName, false ) ); // isAlgorithm
277   
278   try {
279
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 )
286         {
287           anIt.key()->TakeValue( anIt.data().editor );
288           aParamList.push_back( anIt.key() );
289           break;
290         }
291
292     if( !SetParameters( Hyp, aParamList ) )
293       return false;
294
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());
299     if (SHyp)
300       if (!aParams.isEmpty())
301       {
302         SMESH::SetValue(SHyp, aParams);
303         mySMESHGUI->getApp()->objectBrowser()->updateTree();
304       }
305   }
306   catch (const SALOME::SALOME_Exception& S_ex)
307   {
308     wc.suspend();
309
310     _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
311     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
312     if( aStudy && !aStudy->GetProperties()->IsLocked() )
313     {
314       _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
315       aBuilder->RemoveObjectWithChildren( SHyp );
316       mySMESHGUI->updateObjBrowser( true, 0 );
317     }
318     
319     SalomeApp_Tools::QtCatchCorbaException(S_ex);
320     return false;
321   }
322   return true;
323 }
324
325 //=================================================================================
326 // function : ClickOnCancel()
327 // purpose  :
328 //=================================================================================
329 void StdMeshersGUI_CreateHypothesisDlg::ClickOnCancel()
330 {
331   close();
332 }
333
334 //=================================================================================
335 // function : DeactivateActiveDialog()
336 // purpose  :
337 //=================================================================================
338 void StdMeshersGUI_CreateHypothesisDlg::DeactivateActiveDialog()
339 {
340 //   iconLabel->setEnabled(false) ;
341 //   typeLabel->setEnabled(false) ;
342 //   GroupC1->setEnabled(false) ;
343 //   GroupButtons->setEnabled(false) ;
344   setEnabled(false);
345 }
346
347
348 //=================================================================================
349 // function : ActivateThisDialog()
350 // purpose  :
351 //=================================================================================
352 void StdMeshersGUI_CreateHypothesisDlg::ActivateThisDialog()
353 {
354   if (! isEnabled() ) {
355     mySMESHGUI->EmitSignalDeactivateDialog() ;   
356 //     iconLabel->setEnabled(true) ;
357 //     typeLabel->setEnabled(true) ;
358 //     GroupC1->setEnabled(true) ;
359 //     GroupButtons->setEnabled(true) ;
360     setEnabled(true) ;
361   }
362 }
363
364
365 //=================================================================================
366 // function : enterEvent()
367 // purpose  :
368 //=================================================================================
369 void StdMeshersGUI_CreateHypothesisDlg::enterEvent(QEvent* e)
370 {
371   ActivateThisDialog() ;
372 }
373
374
375 //=================================================================================
376 // function : closeEvent()
377 // purpose  :
378 //=================================================================================
379 void StdMeshersGUI_CreateHypothesisDlg::closeEvent( QCloseEvent* e )
380 {
381   mySMESHGUI->ResetState();
382   QDialog::closeEvent( e );
383 }
384
385 //=================================================================================
386 // function : onValueChanged()
387 // purpose  :
388 //=================================================================================
389 void StdMeshersGUI_CreateHypothesisDlg::onValueChanged()
390 {
391   if( sender()->inherits( "QWidget" ) )
392   {
393     QWidget* w = ( QWidget* )sender();
394
395     SMESHGUI_aParameterPtr param;
396
397     ParameterMap::const_iterator anIt = myParamMap.begin(),
398                                  aLast = myParamMap.end();
399     for( ; anIt!=aLast; anIt++ )
400       if( anIt.data().editor == w )
401       {
402         param = anIt.key();
403         param->TakeValue( w );
404
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->() );
408
409         if( str_param && str_param->needPreview() )
410         {
411           QString val; str_param->GetNewText( val );
412           myPreview->setParams( val );
413         }
414         else if( tab_param && tab_param->needPreview() )
415         {
416           SMESH::double_array d;
417           tab_param->data( d );
418           myPreview->setParams( d );
419         }
420         else if( bool_param && bool_param->needPreview() )
421         {
422           int exp=0;
423           bool_param->GetNewInt( exp );
424           myPreview->setIsExp( exp );
425         }
426         UpdateShown( param );
427         break;
428       }
429   }
430 }
431
432 //=================================================================================
433 // function : UpdateShown()
434 // purpose  :
435 //=================================================================================
436 void StdMeshersGUI_CreateHypothesisDlg::UpdateShown( const SMESHGUI_aParameterPtr param )
437 {
438   SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
439   if( !depPar )
440     depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
441
442   if( !depPar )
443     return;
444
445   SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
446   if( map.isEmpty() )
447     return;
448
449   int val;
450   depPar->TakeValue( myParamMap[ param ].editor );
451   depPar->GetNewInt( val );
452   
453   bool hasValue = map.contains( val );
454
455   ParameterMap::const_iterator anIt = myParamMap.begin(),
456                                 aLast = myParamMap.end();
457   bool preview = false;
458   for( ; anIt!=aLast; anIt++ )
459   {
460     bool shown = hasValue && map[ val ].contains( (*anIt).order );
461     (*anIt).editor->setShown( shown );
462     (*anIt).label->setShown( shown );
463     if( shown )
464     {
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() );
468     }
469   }
470   myPreview->setShown( preview );
471 }