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