Salome HOME
make same as in V5_1_main
[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 //  SMESH StdMeshersGUI
23 //  File   : StdMeshersGUI_LayerDistributionParamWdg.cxx
24 //  Module : SMESH
25 //
26 #include "StdMeshersGUI_LayerDistributionParamWdg.h"
27
28 #include <qpushbutton.h>
29 #include <qcursor.h>
30 #include <qpopupmenu.h>
31 #include <qdialog.h>
32
33 #include "SMESHGUI.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_HypothesesUtils.h"
36 #include "SMESHGUI_Hypotheses.h"
37 // #include "SUIT_ResourceMgr.h"
38 // #include "LightApp_SelectionMgr.h"
39 #include "SALOMEDSClient_SObject.hxx"
40 #include "SALOMEDS_Study.hxx"
41 #include "SalomeApp_Tools.h"
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   QHGroupBox(), 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   if ( myHypTypePopup )
94     delete myHypTypePopup;
95 }
96
97 //================================================================================
98 /*!
99  * \brief Create a leayout, initialize fields
100  */
101 //================================================================================
102
103 void StdMeshersGUI_LayerDistributionParamWdg::init()
104 {
105   setFrameStyle(QFrame::NoFrame);
106   setInsideMargin(0);
107
108   mySMESHGUI = SMESHGUI::GetSMESHGUI();
109
110   myCreateButton = new QPushButton( this, "createBut");
111   myEditButton   = new QPushButton( tr("EDIT"), this, "createBut");
112
113 //   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
114 //   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
115 //   mySelButton->setPixmap(iconSlct);
116 //   mySelButton->setToggleButton( true );
117
118   myHypTypePopup = new QPopupMenu();
119
120   // Add to pop-up hypotheses of "Regular_1D" algo
121   myHypTypePopup->clear();
122   HypothesisData* algoData = SMESH::GetHypothesisData( "Regular_1D" );
123   myHypTypes = SMESH::GetAvailableHypotheses( false, 1 );
124   QStringList::const_iterator anIter = myHypTypes.begin();
125   for ( ; anIter != myHypTypes.end(); ++anIter )
126   {
127     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
128     bool bidon;
129     if ( SMESH::IsAvailableHypothesis( algoData, hypData->TypeName, bidon ))
130       myHypTypePopup->insertItem( hypData->Label );
131   }
132
133   connect( myCreateButton, SIGNAL(clicked()), SLOT(onCreate()));
134   connect( myEditButton,   SIGNAL(clicked()), SLOT(onEdit()));
135   connect( myHypTypePopup, SIGNAL( activated( int ) ), SLOT( onHypTypePopup( int ) ) );
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( int theIndex )
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   for ( ; !aHypData && anIter != myHypTypes.end(); ++anIter )
156   {
157     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
158     if ( myHypTypePopup->text( theIndex ) == hypData->Label )
159       aHypData = hypData;
160   }
161   QString aServLib = aHypData->ServerLibName;
162   QString aHypType = aHypData->TypeName;
163   try {
164     set( gen->CreateHypothesis(aHypType, aServLib));
165   }
166   catch (const SALOME::SALOME_Exception & S_ex) {
167     SalomeApp_Tools::QtCatchCorbaException(S_ex);
168   }
169
170   // restore current study
171   mySMESHGUI->GetSMESHGen();
172
173   onEdit();
174 }
175
176 //================================================================================
177 /*!
178  * \brief Show popup with available types
179  */
180 //================================================================================
181
182 void StdMeshersGUI_LayerDistributionParamWdg::onCreate()
183 {
184   myHypTypePopup->exec( QCursor::pos() );
185 }
186
187 //================================================================================
188 /*!
189  * \brief Edit hypothesis
190  */
191 //================================================================================
192
193 void StdMeshersGUI_LayerDistributionParamWdg::onEdit()
194 {
195   if ( myHyp->_is_nil() )
196     return;
197
198   CORBA::String_var hypType = myHyp->GetName();
199   SMESHGUI_GenericHypothesisCreator*
200     editor = SMESH::GetHypothesisCreator( hypType.in() );
201   if ( !editor ) return;
202
203   if ( myDlg ) myDlg->hide();
204
205   try {
206     QWidget* parent = this;
207     if ( myDlg ) parent = myDlg->parentWidget();
208     editor->edit( myHyp, myName, parent );
209   }
210   catch(...) {
211   }
212
213   if ( myDlg ) myDlg->show();
214 }