Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_LayerDistributionParamWdg.cxx
1 //  SMESH StdMeshersGUI
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : StdMeshersGUI_LayerDistributionParamWdg.cxx
25 //  Module : SMESH
26
27 #include "StdMeshersGUI_LayerDistributionParamWdg.h"
28
29 #include <qpushbutton.h>
30 #include <qcursor.h>
31 #include <qpopupmenu.h>
32 #include <qdialog.h>
33
34 #include "SMESHGUI.h"
35 #include "SMESHGUI_Utils.h"
36 #include "SMESHGUI_HypothesesUtils.h"
37 #include "SMESHGUI_Hypotheses.h"
38 // #include "SUIT_ResourceMgr.h"
39 // #include "LightApp_SelectionMgr.h"
40 #include "SALOMEDSClient_SObject.hxx"
41 #include "SALOMEDS_Study.hxx"
42 #include "SalomeApp_Tools.h"
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   QHGroupBox(), 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   if ( myHypTypePopup )
95     delete myHypTypePopup;
96 }
97
98 //================================================================================
99 /*!
100  * \brief Create a leayout, initialize fields
101  */
102 //================================================================================
103
104 void StdMeshersGUI_LayerDistributionParamWdg::init()
105 {
106   setFrameStyle(QFrame::NoFrame);
107   setInsideMargin(0);
108
109   mySMESHGUI = SMESHGUI::GetSMESHGUI();
110
111   myCreateButton = new QPushButton( this, "createBut");
112   myEditButton   = new QPushButton( tr("EDIT"), this, "createBut");
113
114 //   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
115 //   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
116 //   mySelButton->setPixmap(iconSlct);
117 //   mySelButton->setToggleButton( true );
118
119   myHypTypePopup = new QPopupMenu();
120
121   // Add to pop-up hypotheses of "Regular_1D" algo
122   myHypTypePopup->clear();
123   HypothesisData* algoData = SMESH::GetHypothesisData( "Regular_1D" );
124   myHypTypes = SMESH::GetAvailableHypotheses( false, 1 );
125   QStringList::const_iterator anIter = myHypTypes.begin();
126   for ( ; anIter != myHypTypes.end(); ++anIter )
127   {
128     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
129     bool bidon;
130     if ( SMESH::IsAvailableHypothesis( algoData, hypData->TypeName, bidon ))
131       myHypTypePopup->insertItem( hypData->Label );
132   }
133
134   connect( myCreateButton, SIGNAL(clicked()), SLOT(onCreate()));
135   connect( myEditButton,   SIGNAL(clicked()), SLOT(onEdit()));
136   connect( myHypTypePopup, SIGNAL( activated( int ) ), SLOT( onHypTypePopup( int ) ) );
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( int theIndex )
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   for ( ; !aHypData && anIter != myHypTypes.end(); ++anIter )
157   {
158     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
159     if ( myHypTypePopup->text( theIndex ) == hypData->Label )
160       aHypData = hypData;
161   }
162   QString aServLib = aHypData->ServerLibName;
163   QString aHypType = aHypData->TypeName;
164   try {
165     set( gen->CreateHypothesis(aHypType, aServLib));
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 }