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