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