Salome HOME
Movement of examples to CVS EXAMPLES SAMPLES_SRC.
[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
38 #include "SUIT_Application.h"
39 #include "SUIT_Desktop.h"
40 #include "SUIT_MessageBox.h"
41 #include "SUIT_OverrideCursor.h"
42 #include "utilities.h"
43
44 #include "SalomeApp_Tools.h"
45 #include "SalomeApp_Application.h"
46
47 #include "OB_Browser.h"
48
49 // QT Includes
50 #include <qgroupbox.h>
51 #include <qlabel.h>
52 #include <qlineedit.h>
53 #include <qpushbutton.h>
54 #include <qlayout.h>
55 #include <qpixmap.h>
56 #include <qspinbox.h>
57 #include <qtextedit.h>
58 #include <qcombobox.h>
59 #include <qcheckbox.h>
60
61 using namespace std;
62
63 //=================================================================================
64 // class    : StdMeshersGUI_CreateHypothesisDlg()
65 // purpose  : Constructs a StdMeshersGUI_CreateHypothesisDlg which is a child of 'parent', with the 
66 //            name 'name' and widget flags set to 'f'.
67 //            The dialog will by default be modeless, unless you set 'modal' to
68 //            TRUE to construct a modal dialog.
69 //=================================================================================
70 StdMeshersGUI_CreateHypothesisDlg::StdMeshersGUI_CreateHypothesisDlg (const QString& hypType,
71                                                                       QWidget* parent,
72                                                                       const char* name,
73                                                                       bool modal,
74                                                                       WFlags /*fl*/)
75      : QDialog (parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose), myHypType( hypType )
76 {
77 }
78
79 //=======================================================================
80 //function : CreateDlgLayout
81 //purpose  : 
82 //=======================================================================
83
84 void StdMeshersGUI_CreateHypothesisDlg::CreateDlgLayout(const QString & theCaption,
85                                                         const QPixmap & theHypIcon,
86                                                         const QString & theHypTypeName)
87 {
88   setCaption( theCaption );
89
90   setSizeGripEnabled( TRUE );
91   QVBoxLayout* StdMeshersGUI_CreateHypothesisDlgLayout = new QVBoxLayout( this ); 
92   StdMeshersGUI_CreateHypothesisDlgLayout->setSpacing( 6 );
93   StdMeshersGUI_CreateHypothesisDlgLayout->setMargin( 11 );
94
95   /***************************************************************/
96   QFrame* titFrame = new QFrame( this );
97   QHBoxLayout* titLay = new QHBoxLayout( titFrame, 0, 0 );
98   
99   iconLabel = new QLabel( titFrame );
100   iconLabel->setPixmap( theHypIcon );
101   iconLabel->setScaledContents( false );
102   iconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
103   typeLabel = new QLabel( titFrame );
104   typeLabel->setText( theHypTypeName );
105   titLay->addWidget( iconLabel, 0 );
106   titLay->addWidget( typeLabel, 0 );
107   titLay->addStretch( 1 );
108   
109   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( titFrame, 0);
110     
111   /***************************************************************/
112   GroupC1 = new QGroupBox( this, "GroupC1" );
113   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
114   GroupC1->setColumnLayout(0, Qt::Vertical );
115   GroupC1->layout()->setSpacing( 0 );
116   GroupC1->layout()->setMargin( 0 );
117   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
118   GroupC1Layout->setAlignment( Qt::AlignTop );
119   GroupC1Layout->setSpacing( 6 );
120   GroupC1Layout->setMargin( 11 );
121
122   TextLabel_NameHypothesis = new QLabel( GroupC1, "TextLabel_NameHypothesis" );
123   TextLabel_NameHypothesis->setText( tr( "SMESH_NAME"  ) );
124   GroupC1Layout->addWidget( TextLabel_NameHypothesis, 0, 0 );
125
126   LineEdit_NameHypothesis = new QLineEdit( GroupC1, "LineEdit_NameHypothesis" );
127   GroupC1Layout->addWidget( LineEdit_NameHypothesis, 0, 1 );
128
129   myParamMap.clear();
130   std::list<SMESHGUI_aParameterPtr> aParamList;
131   GetParameters( myHypType, aParamList );
132   ASSERT( !aParamList.empty() );
133
134   /* Spin boxes with labels */
135   list<SMESHGUI_aParameterPtr>::iterator paramIt = aParamList.begin();
136   for ( int row = 1; paramIt != aParamList.end(); paramIt++ , row++ )
137   {
138     SMESHGUI_aParameterPtr param = (*paramIt);
139     QLabel * label = new QLabel( GroupC1, "TextLabel" );
140     GroupC1Layout->addWidget( label, row, 0 );
141     label->setText( param->Label() );
142     QWidget* aWidget = param->CreateWidget( GroupC1 );
143
144     if ( aWidget ) {
145       GroupC1Layout->addWidget( aWidget, row, 1 );
146       aWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) );
147       aWidget->setMinimumSize( 150, 0 );
148
149       QString sig = param->sigValueChanged();
150       if( !sig.isEmpty() && param->GetType()!=SMESHGUI_aParameter::TABLE )
151         connect( aWidget, sig.latin1(), this, SLOT( onValueChanged() ) );
152          
153       param->InitializeWidget( aWidget );
154
155       ParamInfo info;
156       info.editor = aWidget;
157       info.label = label;
158       info.order = row-1;
159       
160       myParamMap.insert( param, info );
161     }
162   }
163   
164   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupC1, 1 );
165
166   /***************************************************************/
167   GroupButtons = new QGroupBox( this, "GroupButtons" );
168   GroupButtons->setColumnLayout(0, Qt::Vertical );
169   GroupButtons->layout()->setSpacing( 0 );
170   GroupButtons->layout()->setMargin( 0 );
171   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
172   GroupButtonsLayout->setAlignment( Qt::AlignTop );
173   GroupButtonsLayout->setSpacing( 6 );
174   GroupButtonsLayout->setMargin( 11 );
175   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
176   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
177   buttonOk->setAutoDefault( TRUE );
178   buttonOk->setDefault( TRUE );
179   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
180   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
181   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
182   buttonApply->setAutoDefault( TRUE );
183   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
184   QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
185   GroupButtonsLayout->addItem( spacer, 0, 2 );
186   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
187   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
188   buttonCancel->setAutoDefault( TRUE );
189   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
190   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( GroupButtons, 0 );
191
192   /***************************************************************/
193   Init() ;
194 }
195
196
197 //=================================================================================
198 // function : ~StdMeshersGUI_CreateHypothesisDlg()
199 // purpose  : Destroys the object and frees any allocated resources
200 //=================================================================================
201 StdMeshersGUI_CreateHypothesisDlg::~StdMeshersGUI_CreateHypothesisDlg()
202 {
203     // no need to delete child widgets, Qt does it all for us
204 }
205
206
207 //=================================================================================
208 // function : Init()
209 // purpose  :
210 //=================================================================================
211 void StdMeshersGUI_CreateHypothesisDlg::Init()
212 {
213   ParameterMap::const_iterator anIt = myParamMap.begin(),
214                                aLast = myParamMap.end();
215   for( ; anIt!=aLast; anIt++ )
216     UpdateShown( anIt.key() );
217    
218   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
219
220   char* sHypType = const_cast<char*>(myHypType.latin1());
221   HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
222   LineEdit_NameHypothesis->setText( aHypData ? aHypData->Label : QString("") );
223
224   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
225
226   /* signals and slots connections */
227   connect( buttonOk, SIGNAL(clicked()),     this, SLOT(ClickOnOk()) );
228   connect( buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()) ) ;
229   connect( buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()) );
230
231   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
232   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
233
234   /* Move widget on the botton right corner of main widget */
235   int x, y ;
236   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
237   this->move( x, y ) ;
238   this->show() ; /* displays Dialog */
239 }
240
241
242 //=================================================================================
243 // function : ClickOnOk()
244 // purpose  :
245 //=================================================================================
246 void StdMeshersGUI_CreateHypothesisDlg::ClickOnOk()
247 {
248   if ( ClickOnApply() )
249     ClickOnCancel() ;
250 }
251
252 //=================================================================================
253 // function : ClickOnApply()
254 // purpose  :
255 //=================================================================================
256 bool StdMeshersGUI_CreateHypothesisDlg::ClickOnApply()
257 {
258   if ( !mySMESHGUI || mySMESHGUI->isActiveStudyLocked() )
259     return false;
260
261   QString myHypName = LineEdit_NameHypothesis->text().stripWhiteSpace();
262   if ( myHypName.isEmpty() ) {
263     SUIT_MessageBox::warn1 (this, tr( "SMESH_WRN_WARNING" ),
264                            tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
265     return false;
266   }
267
268   SUIT_OverrideCursor wc;
269
270   SMESH::SMESH_Hypothesis_var Hyp = SMESH::SMESH_Hypothesis::_narrow
271       ( SMESH::CreateHypothesis( myHypType, myHypName, false ) ); // isAlgorithm
272   
273   try {
274
275     list<SMESHGUI_aParameterPtr> aParamList;
276     ParameterMap::const_iterator anIt = myParamMap.begin(),
277                                  aLast = myParamMap.end();
278     for( int i=0; i<myParamMap.count(); i++ )
279       for( anIt=myParamMap.begin(); anIt!=aLast; anIt++ )
280         if( (*anIt).order==i )
281         {
282           anIt.key()->TakeValue( anIt.data().editor );
283           aParamList.push_back( anIt.key() );
284           break;
285         }
286
287     SetParameters( Hyp, aParamList );
288
289     //set new Attribute Comment for hypothesis which parameters were set
290     QString aParams = "";
291     StdMeshersGUI_Parameters::GetParameters( Hyp.in(), aParamList, aParams );
292     _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
293     if (SHyp)
294       if (!aParams.isEmpty())
295       {
296         SMESH::SetValue(SHyp, aParams);
297         mySMESHGUI->getApp()->objectBrowser()->updateTree();
298       }
299   }
300   catch (const SALOME::SALOME_Exception& S_ex)
301   {
302     wc.suspend();
303
304     _PTR(SObject) SHyp = SMESH::FindSObject(Hyp.in());
305     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
306     if( aStudy && !aStudy->GetProperties()->IsLocked() )
307     {
308       _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
309       aBuilder->RemoveObjectWithChildren( SHyp );
310       mySMESHGUI->updateObjBrowser( true, 0 );
311     }
312     
313     SalomeApp_Tools::QtCatchCorbaException(S_ex);
314     return false;
315   }
316   return true;
317 }
318
319 //=================================================================================
320 // function : ClickOnCancel()
321 // purpose  :
322 //=================================================================================
323 void StdMeshersGUI_CreateHypothesisDlg::ClickOnCancel()
324 {
325   close();
326 }
327
328 //=================================================================================
329 // function : DeactivateActiveDialog()
330 // purpose  :
331 //=================================================================================
332 void StdMeshersGUI_CreateHypothesisDlg::DeactivateActiveDialog()
333 {
334 //   iconLabel->setEnabled(false) ;
335 //   typeLabel->setEnabled(false) ;
336 //   GroupC1->setEnabled(false) ;
337 //   GroupButtons->setEnabled(false) ;
338   setEnabled(false);
339 }
340
341
342 //=================================================================================
343 // function : ActivateThisDialog()
344 // purpose  :
345 //=================================================================================
346 void StdMeshersGUI_CreateHypothesisDlg::ActivateThisDialog()
347 {
348   if (! isEnabled() ) {
349     mySMESHGUI->EmitSignalDeactivateDialog() ;   
350 //     iconLabel->setEnabled(true) ;
351 //     typeLabel->setEnabled(true) ;
352 //     GroupC1->setEnabled(true) ;
353 //     GroupButtons->setEnabled(true) ;
354     setEnabled(true) ;
355   }
356 }
357
358
359 //=================================================================================
360 // function : enterEvent()
361 // purpose  :
362 //=================================================================================
363 void StdMeshersGUI_CreateHypothesisDlg::enterEvent(QEvent* e)
364 {
365   ActivateThisDialog() ;
366 }
367
368
369 //=================================================================================
370 // function : closeEvent()
371 // purpose  :
372 //=================================================================================
373 void StdMeshersGUI_CreateHypothesisDlg::closeEvent( QCloseEvent* e )
374 {
375   mySMESHGUI->ResetState();
376   QDialog::closeEvent( e );
377 }
378
379 //=================================================================================
380 // function : onValueChanged()
381 // purpose  :
382 //=================================================================================
383 void StdMeshersGUI_CreateHypothesisDlg::onValueChanged()
384 {
385   if( sender()->inherits( "QWidget" ) )
386   {
387     QWidget* w = ( QWidget* )sender();
388
389     SMESHGUI_aParameterPtr param;
390
391     ParameterMap::const_iterator anIt = myParamMap.begin(),
392                                  aLast = myParamMap.end();
393     for( ; anIt!=aLast; anIt++ )
394       if( anIt.data().editor == w )
395       {
396         param = anIt.key();
397         param->TakeValue( w );
398         UpdateShown( param );
399         break;
400       }
401   }
402 }
403
404 //=================================================================================
405 // function : UpdateShown()
406 // purpose  :
407 //=================================================================================
408 void StdMeshersGUI_CreateHypothesisDlg::UpdateShown( const SMESHGUI_aParameterPtr param )
409 {
410   SMESHGUI_dependParameter* depPar = dynamic_cast<SMESHGUI_enumParameter*>( param.get() );
411   if( !depPar )
412     depPar = dynamic_cast<SMESHGUI_boolParameter*>( param.get() );
413
414   if( !depPar )
415     return;
416
417   SMESHGUI_dependParameter::ShownMap& map = depPar->shownMap();
418   if( map.isEmpty() )
419     return;
420
421   int val;
422   depPar->TakeValue( myParamMap[ param ].editor );
423   depPar->GetNewInt( val );
424   
425   bool hasValue = map.contains( val );
426
427   ParameterMap::const_iterator anIt = myParamMap.begin(),
428                                 aLast = myParamMap.end();
429   for( ; anIt!=aLast; anIt++ )
430   {
431     bool shown = hasValue && map[ val ].contains( (*anIt).order );
432     (*anIt).editor->setShown( shown );
433     (*anIt).label->setShown( shown );
434   }
435 }