Salome HOME
'Extrusion Along Path' dialog box : rearrange widgets
[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 "QAD_Application.h"
39 #include "QAD_Desktop.h"
40 #include "utilities.h"
41
42 #include "SALOMEGUI_QtCatchCorbaException.hxx"
43 #include "QAD_MessageBox.h"
44 #include "QAD_WaitCursor.h"
45
46 // QT Includes
47 #include <qgroupbox.h>
48 #include <qlabel.h>
49 #include <qlineedit.h>
50 #include <qpushbutton.h>
51 #include <qlayout.h>
52 #include <qpixmap.h>
53 #include <qspinbox.h>
54
55 using namespace std;
56
57 //=================================================================================
58 // class    : StdMeshersGUI_CreateHypothesisDlg()
59 // purpose  : Constructs a StdMeshersGUI_CreateHypothesisDlg which is a child of 'parent', with the 
60 //            name 'name' and widget flags set to 'f'.
61 //            The dialog will by default be modeless, unless you set 'modal' to
62 //            TRUE to construct a modal dialog.
63 //=================================================================================
64 StdMeshersGUI_CreateHypothesisDlg::StdMeshersGUI_CreateHypothesisDlg (const QString& hypType,
65                                                                       QWidget* parent,
66                                                                       const char* name,
67                                                                       bool modal,
68                                                                       WFlags /*fl*/)
69      : QDialog (parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose), myHypType( hypType )
70 {
71 }
72
73 //=======================================================================
74 //function : CreateDlgLayout
75 //purpose  : 
76 //=======================================================================
77
78 void StdMeshersGUI_CreateHypothesisDlg::CreateDlgLayout(const QString & theCaption,
79                                                         const QPixmap & theHypIcon,
80                                                         const QString & theHypTypeName)
81 {
82   setCaption( theCaption );
83
84   setSizeGripEnabled( TRUE );
85   QGridLayout* StdMeshersGUI_CreateHypothesisDlgLayout = new QGridLayout( this ); 
86   StdMeshersGUI_CreateHypothesisDlgLayout->setSpacing( 6 );
87   StdMeshersGUI_CreateHypothesisDlgLayout->setMargin( 11 );
88
89   /***************************************************************/
90   iconLabel = new QLabel( this );
91   iconLabel->setPixmap( theHypIcon );
92   iconLabel->setScaledContents( false );
93   iconLabel->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
94   typeLabel = new QLabel( this );
95   typeLabel->setText( theHypTypeName );
96   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( iconLabel, 0, 0 );
97   StdMeshersGUI_CreateHypothesisDlgLayout->addWidget( typeLabel, 0, 1 );
98     
99   /***************************************************************/
100   GroupC1 = new QGroupBox( this, "GroupC1" );
101   GroupC1->setTitle( tr( "SMESH_ARGUMENTS"  ) );
102   GroupC1->setColumnLayout(0, Qt::Vertical );
103   GroupC1->layout()->setSpacing( 0 );
104   GroupC1->layout()->setMargin( 0 );
105   QGridLayout* GroupC1Layout = new QGridLayout( GroupC1->layout() );
106   GroupC1Layout->setAlignment( Qt::AlignTop );
107   GroupC1Layout->setSpacing( 6 );
108   GroupC1Layout->setMargin( 11 );
109
110   TextLabel_NameHypothesis = new QLabel( GroupC1, "TextLabel_NameHypothesis" );
111   TextLabel_NameHypothesis->setText( tr( "SMESH_NAME"  ) );
112   GroupC1Layout->addWidget( TextLabel_NameHypothesis, 0, 0 );
113
114   LineEdit_NameHypothesis = new QLineEdit( GroupC1, "LineEdit_NameHypothesis" );
115   GroupC1Layout->addWidget( LineEdit_NameHypothesis, 0, 1 );
116
117   myParamList.clear();
118   GetParameters( myHypType, myParamList );
119   ASSERT( !myParamList.empty() );
120
121   /* Spin boxes with labels */
122   list<SMESHGUI_aParameterPtr>::iterator paramIt = myParamList.begin();
123   for ( int row = 1; paramIt != myParamList.end(); paramIt++ , row++ )
124   {
125     SMESHGUI_aParameterPtr param = (*paramIt);
126     QLabel * label = new QLabel( GroupC1, "TextLabel" );
127     GroupC1Layout->addWidget( label, row, 0 );
128     label->setText( param->Label() );
129     QWidget* aSpinWidget;
130     switch ( param->GetType() ) {
131     case SMESHGUI_aParameter::DOUBLE: {
132       SMESHGUI_SpinBox* spin = new SMESHGUI_SpinBox( GroupC1 );
133       aSpinWidget = spin;
134       spin->setPrecision( 12 );
135       break;
136     }
137     case SMESHGUI_aParameter::INT: {
138       QSpinBox* spin = new QSpinBox( GroupC1 );
139       aSpinWidget = spin;
140       break;
141     }
142     default:;
143     }
144     GroupC1Layout->addWidget( aSpinWidget, row, 1 );
145     aSpinWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ) );
146     aSpinWidget->setMinimumSize( 150, 0 );
147     param->InitializeWidget( aSpinWidget );
148     mySpinList.push_back( aSpinWidget );
149   }
150   
151   StdMeshersGUI_CreateHypothesisDlgLayout->addMultiCellWidget(GroupC1 , 1, 1, 0, 1 );
152
153   /***************************************************************/
154   GroupButtons = new QGroupBox( this, "GroupButtons" );
155   GroupButtons->setColumnLayout(0, Qt::Vertical );
156   GroupButtons->layout()->setSpacing( 0 );
157   GroupButtons->layout()->setMargin( 0 );
158   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
159   GroupButtonsLayout->setAlignment( Qt::AlignTop );
160   GroupButtonsLayout->setSpacing( 6 );
161   GroupButtonsLayout->setMargin( 11 );
162   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
163   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
164   buttonOk->setAutoDefault( TRUE );
165   buttonOk->setDefault( TRUE );
166   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
167   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
168   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
169   buttonApply->setAutoDefault( TRUE );
170   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
171   QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
172   GroupButtonsLayout->addItem( spacer, 0, 2 );
173   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
174   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
175   buttonCancel->setAutoDefault( TRUE );
176   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
177   StdMeshersGUI_CreateHypothesisDlgLayout->addMultiCellWidget( GroupButtons, 2, 2, 0, 1 );
178
179   /***************************************************************/
180   Init() ;
181 }
182
183
184 //=================================================================================
185 // function : ~StdMeshersGUI_CreateHypothesisDlg()
186 // purpose  : Destroys the object and frees any allocated resources
187 //=================================================================================
188 StdMeshersGUI_CreateHypothesisDlg::~StdMeshersGUI_CreateHypothesisDlg()
189 {
190     // no need to delete child widgets, Qt does it all for us
191 }
192
193
194 //=================================================================================
195 // function : Init()
196 // purpose  :
197 //=================================================================================
198 void StdMeshersGUI_CreateHypothesisDlg::Init()
199 {
200   mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
201
202   char* sHypType = const_cast<char*>(myHypType.latin1());
203   HypothesisData* aHypData = SMESH::GetHypothesisData(sHypType);
204   LineEdit_NameHypothesis->setText( aHypData ? aHypData->Label : QString("") );
205
206   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
207
208   /* signals and slots connections */
209   connect( buttonOk, SIGNAL(clicked()),     this, SLOT(ClickOnOk()) );
210   connect( buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()) ) ;
211   connect( buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()) );
212
213   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;  
214   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
215
216   /* Move widget on the botton right corner of main widget */
217   int x, y ;
218   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
219   this->move( x, y ) ; 
220   this->show() ; /* displays Dialog */
221 }
222
223
224 //=================================================================================
225 // function : ClickOnOk()
226 // purpose  :
227 //=================================================================================
228 void StdMeshersGUI_CreateHypothesisDlg::ClickOnOk()
229 {
230   if ( ClickOnApply() )
231     ClickOnCancel() ;
232 }
233
234 //=================================================================================
235 // function : ClickOnApply()
236 // purpose  :
237 //=================================================================================
238 bool StdMeshersGUI_CreateHypothesisDlg::ClickOnApply()
239 {
240   if ( !mySMESHGUI || mySMESHGUI->ActiveStudyLocked() )
241     return false;
242
243   QString myHypName = LineEdit_NameHypothesis->text().stripWhiteSpace();
244   if ( myHypName.isEmpty() ) {
245     QAD_MessageBox::warn1 (this, tr( "SMESH_WRN_WARNING" ),
246                            tr( "SMESH_WRN_EMPTY_NAME" ), tr( "SMESH_BUT_OK" ) );
247     return false;
248   }
249
250   QAD_WaitCursor wc;
251
252   try {
253     SMESH::SMESH_Hypothesis_var Hyp = SMESH::SMESH_Hypothesis::_narrow
254       ( SMESH::CreateHypothesis (myHypType,
255                                  myHypName,
256                                  false )); // isAlgorithm
257     
258     list<SMESHGUI_aParameterPtr>::iterator paramIt = myParamList.begin();
259     list<QWidget*>::iterator              widgetIt = mySpinList.begin();
260     for ( ;
261          paramIt != myParamList.end() && widgetIt != mySpinList.end();
262          paramIt++ , widgetIt++ )
263       (*paramIt)->TakeValue( *widgetIt );
264
265     SetParameters( Hyp, myParamList );
266
267     //set new Attribute Comment for hypothesis which parameters were set
268     QString aParams = "";
269     StdMeshersGUI_Parameters::GetParameters( Hyp.in(), myParamList, aParams );
270     SALOMEDS::SObject_var SHyp = SMESH::FindSObject(Hyp.in());
271     if (!SHyp->_is_nil()) 
272       if (!aParams.isEmpty()) {
273         SMESH::SetValue(SHyp, aParams);
274         mySMESHGUI->GetActiveStudy()->updateObjBrowser(true);
275       }    
276   }
277   catch (const SALOME::SALOME_Exception& S_ex) {
278     wc.stop();
279     QtCatchCorbaException(S_ex);
280     return false;
281   }
282   return true;
283 }
284
285 //=================================================================================
286 // function : ClickOnCancel()
287 // purpose  :
288 //=================================================================================
289 void StdMeshersGUI_CreateHypothesisDlg::ClickOnCancel()
290 {
291   close();
292 }
293
294 //=================================================================================
295 // function : DeactivateActiveDialog()
296 // purpose  :
297 //=================================================================================
298 void StdMeshersGUI_CreateHypothesisDlg::DeactivateActiveDialog()
299 {
300 //   iconLabel->setEnabled(false) ;
301 //   typeLabel->setEnabled(false) ;
302 //   GroupC1->setEnabled(false) ;
303 //   GroupButtons->setEnabled(false) ;
304   setEnabled(false);
305 }
306
307
308 //=================================================================================
309 // function : ActivateThisDialog()
310 // purpose  :
311 //=================================================================================
312 void StdMeshersGUI_CreateHypothesisDlg::ActivateThisDialog()
313 {
314   if (! isEnabled() ) {
315     mySMESHGUI->EmitSignalDeactivateDialog() ;   
316 //     iconLabel->setEnabled(true) ;
317 //     typeLabel->setEnabled(true) ;
318 //     GroupC1->setEnabled(true) ;
319 //     GroupButtons->setEnabled(true) ;
320     setEnabled(true) ;
321   }
322 }
323
324
325 //=================================================================================
326 // function : enterEvent()
327 // purpose  :
328 //=================================================================================
329 void StdMeshersGUI_CreateHypothesisDlg::enterEvent(QEvent* e)
330 {
331   ActivateThisDialog() ;
332 }
333
334
335 //=================================================================================
336 // function : closeEvent()
337 // purpose  :
338 //=================================================================================
339 void StdMeshersGUI_CreateHypothesisDlg::closeEvent( QCloseEvent* e )
340 {
341   mySMESHGUI->ResetState();
342   QDialog::closeEvent( e );
343 }