Salome HOME
Porting SMESH module to Qt 4
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_LayerDistributionParamWdg.cxx
1 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 //
4 // This library is free software; you can redistribute it and/or 
5 // modify it under the terms of the GNU Lesser General Public 
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License. 
8 //
9 // This library is distributed in the hope that it will be useful, 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details. 
13 //
14 // You should have received a copy of the GNU Lesser General Public 
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : StdMeshersGUI_LayerDistributionParamWdg.cxx
21 // Author : Open CASCADE S.A.S.
22 //
23
24 // SMESH includes
25 #include "StdMeshersGUI_LayerDistributionParamWdg.h"
26
27 #include <SMESHGUI.h>
28 #include <SMESHGUI_HypothesesUtils.h>
29 #include <SMESHGUI_Hypotheses.h>
30
31 // SALOME GUI includes
32 #include <SalomeApp_Tools.h>
33
34 // Qt includes
35 #include <QPushButton>
36 #include <QCursor>
37 #include <QMenu>
38 #include <QDialog>
39 #include <QHBoxLayout>
40
41 #define SPACING 6
42
43 //================================================================================
44 /*!
45  * \brief Constructor initialized by filter
46   * \param f - object filter
47  */
48 //================================================================================
49
50 StdMeshersGUI_LayerDistributionParamWdg
51 ::StdMeshersGUI_LayerDistributionParamWdg(SMESH::SMESH_Hypothesis_ptr hyp,
52                                           const QString& theName,
53                                           QDialog* dlg): 
54   QWidget(), myName(theName), myDlg( dlg )
55 {
56   init();
57   set( hyp );
58 //   if ( IsOk() )
59 //     onEdit();
60 }
61
62 //================================================================================
63 /*!
64  * \brief initialize fields with hypothesis
65   * \param hyp - hypothesis
66  */
67 //================================================================================
68
69 void StdMeshersGUI_LayerDistributionParamWdg::set(SMESH::SMESH_Hypothesis_ptr hyp)
70 {
71   myHyp = SMESH::SMESH_Hypothesis::_nil();
72   if ( !CORBA::is_nil( hyp )) {
73     myHyp = SMESH::SMESH_Hypothesis::_duplicate( hyp );
74     myEditButton->setEnabled( true );
75     myCreateButton->setText( tr("CHANGE_TYPE"));
76     myParamValue = hyp->GetName();
77   }
78   else {
79     myEditButton->setEnabled( false );
80     myCreateButton->setText( tr("CREATE"));
81     myParamValue = "";
82   }
83 }
84
85 //================================================================================
86 /*!
87  * \brief Destructor
88  */
89 //================================================================================
90
91 StdMeshersGUI_LayerDistributionParamWdg::~StdMeshersGUI_LayerDistributionParamWdg()
92 {
93 }
94
95 //================================================================================
96 /*!
97  * \brief Create a leayout, initialize fields
98  */
99 //================================================================================
100
101 void StdMeshersGUI_LayerDistributionParamWdg::init()
102 {
103   QHBoxLayout* aHBox = new QHBoxLayout( this );
104   aHBox->setMargin( 0 );
105   aHBox->setSpacing( SPACING );
106
107   mySMESHGUI = SMESHGUI::GetSMESHGUI();
108
109   myCreateButton = new QPushButton( this );
110   myCreateButton->setObjectName( "createBut" );
111
112   myEditButton   = new QPushButton( tr("EDIT"), this );
113   myEditButton->setObjectName( "editBut" );
114
115   myHypTypePopup = new QMenu( this );
116
117   // Add to pop-up hypotheses of "Regular_1D" algo
118   HypothesisData* algoData = SMESH::GetHypothesisData( "Regular_1D" );
119   myHypTypes = SMESH::GetAvailableHypotheses( false, 1 );
120   QStringList::const_iterator anIter = myHypTypes.begin();
121   for ( ; anIter != myHypTypes.end(); ++anIter )
122   {
123     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
124     bool bidon;
125     if ( SMESH::IsAvailableHypothesis( algoData, hypData->TypeName, bidon ))
126       myHypTypePopup->addAction( hypData->Label );
127   }
128
129   aHBox->addWidget( myCreateButton );
130   aHBox->addWidget( myEditButton );
131   aHBox->addStretch();
132
133   connect( myCreateButton, SIGNAL(clicked()), SLOT(onCreate()));
134   connect( myEditButton,   SIGNAL(clicked()), SLOT(onEdit()));
135   connect( myHypTypePopup, SIGNAL(triggered( QAction* ) ), SLOT( onHypTypePopup( QAction* ) ) );
136 }
137
138 //================================================================================
139 /*!
140  * \brief Create a new hyp of selected type
141   * \param int - selected type index
142  */
143 //================================================================================
144
145 void StdMeshersGUI_LayerDistributionParamWdg::onHypTypePopup( QAction* a )
146 {
147   SMESH::SMESH_Gen_var gen = mySMESHGUI->GetSMESHGen();
148
149   // avoid publishing a new 1D hyp
150   gen->SetCurrentStudy( SALOMEDS::Study::_nil() );
151
152   // create a hyp
153   HypothesisData* aHypData = 0;
154   QStringList::const_iterator anIter = myHypTypes.begin();
155
156   for ( ; !aHypData && anIter != myHypTypes.end(); ++anIter )
157   {
158     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
159     if ( a->text() == hypData->Label )
160       aHypData = hypData;
161   }
162   QString aServLib = aHypData->ServerLibName;
163   QString aHypType = aHypData->TypeName;
164   try {
165     set( gen->CreateHypothesis(aHypType.toLatin1().data(), aServLib.toLatin1().data()));
166   }
167   catch (const SALOME::SALOME_Exception & S_ex) {
168     SalomeApp_Tools::QtCatchCorbaException(S_ex);
169   }
170
171   // restore current study
172   mySMESHGUI->GetSMESHGen();
173
174   onEdit();
175 }
176
177 //================================================================================
178 /*!
179  * \brief Show popup with available types
180  */
181 //================================================================================
182
183 void StdMeshersGUI_LayerDistributionParamWdg::onCreate()
184 {
185   myHypTypePopup->exec( QCursor::pos() );
186 }
187
188 //================================================================================
189 /*!
190  * \brief Edit hypothesis
191  */
192 //================================================================================
193
194 void StdMeshersGUI_LayerDistributionParamWdg::onEdit()
195 {
196   if ( myHyp->_is_nil() )
197     return;
198
199   CORBA::String_var hypType = myHyp->GetName();
200   SMESHGUI_GenericHypothesisCreator*
201     editor = SMESH::GetHypothesisCreator( hypType.in() );
202   if ( !editor ) return;
203
204   if ( myDlg ) myDlg->hide();
205
206   try {
207     QWidget* parent = this;
208     if ( myDlg ) parent = myDlg->parentWidget();
209     editor->edit( myHyp, myName, parent );
210   }
211   catch(...) {
212   }
213
214   if ( myDlg ) myDlg->show();
215 }