Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_LayerDistributionParamWdg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : StdMeshersGUI_LayerDistributionParamWdg.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "StdMeshersGUI_LayerDistributionParamWdg.h"
28
29 #include <SMESHGUI.h>
30 #include <SMESHGUI_HypothesesUtils.h>
31 #include <SMESHGUI_Hypotheses.h>
32
33 // SALOME GUI includes
34 #include <SalomeApp_Tools.h>
35
36 // Qt includes
37 #include <QPushButton>
38 #include <QCursor>
39 #include <QMenu>
40 #include <QDialog>
41 #include <QHBoxLayout>
42
43 #define SPACING 6
44
45 //================================================================================
46 /*!
47  * \brief Constructor initialized by filter
48   * \param f - object filter
49  */
50 //================================================================================
51
52 StdMeshersGUI_LayerDistributionParamWdg
53 ::StdMeshersGUI_LayerDistributionParamWdg(SMESH::SMESH_Hypothesis_ptr hyp,
54                                           const QString& theName,
55                                           QDialog* dlg): 
56   QWidget(), myName(theName), myDlg( dlg )
57 {
58   init();
59   set( hyp );
60 //   if ( IsOk() )
61 //     onEdit();
62 }
63
64 //================================================================================
65 /*!
66  * \brief initialize fields with hypothesis
67   * \param hyp - hypothesis
68  */
69 //================================================================================
70
71 void StdMeshersGUI_LayerDistributionParamWdg::set(SMESH::SMESH_Hypothesis_ptr hyp)
72 {
73   myHyp = SMESH::SMESH_Hypothesis::_nil();
74   if ( !CORBA::is_nil( hyp )) {
75     myHyp = SMESH::SMESH_Hypothesis::_duplicate( hyp );
76     myEditButton->setEnabled( true );
77     myCreateButton->setText( tr("CHANGE_TYPE"));
78     myParamValue = hyp->GetName();
79   }
80   else {
81     myEditButton->setEnabled( false );
82     myCreateButton->setText( tr("CREATE"));
83     myParamValue = "";
84   }
85 }
86
87 //================================================================================
88 /*!
89  * \brief Destructor
90  */
91 //================================================================================
92
93 StdMeshersGUI_LayerDistributionParamWdg::~StdMeshersGUI_LayerDistributionParamWdg()
94 {
95 }
96
97 //================================================================================
98 /*!
99  * \brief Create a leayout, initialize fields
100  */
101 //================================================================================
102
103 void StdMeshersGUI_LayerDistributionParamWdg::init()
104 {
105   QHBoxLayout* aHBox = new QHBoxLayout( this );
106   aHBox->setMargin( 0 );
107   aHBox->setSpacing( SPACING );
108
109   mySMESHGUI = SMESHGUI::GetSMESHGUI();
110
111   myCreateButton = new QPushButton( this );
112   myCreateButton->setObjectName( "createBut" );
113
114   myEditButton   = new QPushButton( tr("EDIT"), this );
115   myEditButton->setObjectName( "editBut" );
116
117   myHypTypePopup = new QMenu( this );
118
119   // Add to pop-up hypotheses of "Regular_1D" algo
120   HypothesisData* algoData = SMESH::GetHypothesisData( "Regular_1D" );
121   myHypTypes = SMESH::GetAvailableHypotheses( false, 1 );
122   QStringList::const_iterator anIter = myHypTypes.begin();
123   for ( ; anIter != myHypTypes.end(); ++anIter )
124   {
125     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
126     bool bidon;
127     if ( SMESH::IsAvailableHypothesis( algoData, hypData->TypeName, bidon ))
128       myHypTypePopup->addAction( hypData->Label );
129   }
130
131   aHBox->addWidget( myCreateButton );
132   aHBox->addWidget( myEditButton );
133   aHBox->addStretch();
134
135   connect( myCreateButton, SIGNAL(clicked()), SLOT(onCreate()));
136   connect( myEditButton,   SIGNAL(clicked()), SLOT(onEdit()));
137   connect( myHypTypePopup, SIGNAL(triggered( QAction* ) ), SLOT( onHypTypePopup( QAction* ) ) );
138 }
139
140 //================================================================================
141 /*!
142  * \brief Create a new hyp of selected type
143   * \param int - selected type index
144  */
145 //================================================================================
146
147 void StdMeshersGUI_LayerDistributionParamWdg::onHypTypePopup( QAction* a )
148 {
149   SMESH::SMESH_Gen_var gen = mySMESHGUI->GetSMESHGen();
150
151   // avoid publishing a new 1D hyp
152   gen->SetCurrentStudy( SALOMEDS::Study::_nil() );
153
154   // create a hyp
155   HypothesisData* aHypData = 0;
156   QStringList::const_iterator anIter = myHypTypes.begin();
157
158   for ( ; !aHypData && anIter != myHypTypes.end(); ++anIter )
159   {
160     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
161     if ( a->text() == hypData->Label )
162       aHypData = hypData;
163   }
164   QString aServLib = aHypData->ServerLibName;
165   QString aHypType = aHypData->TypeName;
166   try {
167     set( gen->CreateHypothesis(aHypType.toLatin1().data(), aServLib.toLatin1().data()));
168   }
169   catch (const SALOME::SALOME_Exception & S_ex) {
170     SalomeApp_Tools::QtCatchCorbaException(S_ex);
171   }
172
173   // restore current study
174   mySMESHGUI->GetSMESHGen();
175
176   onEdit();
177 }
178
179 //================================================================================
180 /*!
181  * \brief Show popup with available types
182  */
183 //================================================================================
184
185 void StdMeshersGUI_LayerDistributionParamWdg::onCreate()
186 {
187   myHypTypePopup->exec( QCursor::pos() );
188 }
189
190 //================================================================================
191 /*!
192  * \brief Edit hypothesis
193  */
194 //================================================================================
195
196 void StdMeshersGUI_LayerDistributionParamWdg::onEdit()
197 {
198   if ( myHyp->_is_nil() )
199     return;
200
201   CORBA::String_var hypType = myHyp->GetName();
202   // BUG 0020378
203   //SMESHGUI_GenericHypothesisCreator* editor = SMESH::GetHypothesisCreator(hypType.in());
204   SMESHGUI_GenericHypothesisCreator* editor = SMESH::GetHypothesisCreator(hypType.in());
205   if ( !editor ) return;
206
207   if ( myDlg )
208     myDlg->hide();
209
210   try {
211     QWidget* parent = this;
212     if ( myDlg )
213       parent = myDlg->parentWidget();
214     editor->edit( myHyp, myName, parent, this, SLOT( onEdited( int ) ) );
215   }
216   catch(...)
217   {
218   }
219 }
220
221 void StdMeshersGUI_LayerDistributionParamWdg::onEdited( int result )
222 {
223   if ( myDlg )
224     myDlg->show();
225 }