Salome HOME
Merging with WPdev
[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::StdMeshersGUI_LayerDistributionParamWdg
52 ( SMESH::SMESH_Hypothesis_ptr hyp,
53   QDialog*                    dlg ): QHGroupBox(), myDlg( dlg )
54 {
55   init();
56   set( hyp );
57 //   if ( IsOk() )
58 //     onEdit();
59 }
60
61 //================================================================================
62 /*!
63  * \brief initialize fields with hypothesis
64   * \param hyp - hypothesis
65  */
66 //================================================================================
67
68 void StdMeshersGUI_LayerDistributionParamWdg::set(SMESH::SMESH_Hypothesis_ptr hyp)
69 {
70   myHyp = SMESH::SMESH_Hypothesis::_nil();
71   if ( !CORBA::is_nil( hyp )) {
72     myHyp = SMESH::SMESH_Hypothesis::_duplicate( hyp );
73     myEditButton->setEnabled( true );
74     myCreateButton->setText( tr("CHANGE_TYPE"));
75     myParamValue = hyp->GetName();
76   }
77   else {
78     myEditButton->setEnabled( false );
79     myCreateButton->setText( tr("CREATE"));
80     myParamValue = "";
81   }
82 }
83
84 //================================================================================
85 /*!
86  * \brief Destructor
87  */
88 //================================================================================
89
90 StdMeshersGUI_LayerDistributionParamWdg::~StdMeshersGUI_LayerDistributionParamWdg()
91 {
92   if ( myHypTypePopup )
93     delete myHypTypePopup;
94 }
95
96 //================================================================================
97 /*!
98  * \brief Create a leayout, initialize fields
99  */
100 //================================================================================
101
102 void StdMeshersGUI_LayerDistributionParamWdg::init()
103 {
104   setFrameStyle(QFrame::NoFrame);
105   setInsideMargin(0);
106
107   mySMESHGUI = SMESHGUI::GetSMESHGUI();
108
109   myCreateButton = new QPushButton( this, "createBut");
110   myEditButton   = new QPushButton( tr("EDIT"), this, "createBut");
111
112 //   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
113 //   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
114 //   mySelButton->setPixmap(iconSlct);
115 //   mySelButton->setToggleButton( true );
116
117   myHypTypePopup = new QPopupMenu();
118
119   // Add to pop-up hypotheses of "Regular_1D" algo
120   myHypTypePopup->clear();
121   HypothesisData* algoData = SMESH::GetHypothesisData( "Regular_1D" );
122   myHypTypes = SMESH::GetAvailableHypotheses( false, 0 );
123   QStringList::const_iterator anIter = myHypTypes.begin();
124   for ( ; anIter != myHypTypes.end(); ++anIter )
125   {
126     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
127     bool bidon;
128     if ( SMESH::IsAvailableHypothesis( algoData, hypData->TypeName, bidon ))
129       myHypTypePopup->insertItem( hypData->Label );
130   }
131
132   connect( myCreateButton, SIGNAL(clicked()), SLOT(onCreate()));
133   connect( myEditButton,   SIGNAL(clicked()), SLOT(onEdit()));
134   connect( myHypTypePopup, SIGNAL( activated( int ) ), SLOT( onHypTypePopup( int ) ) );
135 }
136
137 //================================================================================
138 /*!
139  * \brief Create a new hyp of selected type
140   * \param int - selected type index
141  */
142 //================================================================================
143
144 void StdMeshersGUI_LayerDistributionParamWdg::onHypTypePopup( int theIndex )
145 {
146   SMESH::SMESH_Gen_var gen = mySMESHGUI->GetSMESHGen();
147
148   // avoid publishing a new 1D hyp
149   gen->SetCurrentStudy( SALOMEDS::Study::_nil() );
150
151   // create a hyp
152   HypothesisData* aHypData = 0;
153   QStringList::const_iterator anIter = myHypTypes.begin();
154   for ( ; !aHypData && anIter != myHypTypes.end(); ++anIter )
155   {
156     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
157     if ( myHypTypePopup->text( theIndex ) == hypData->Label )
158       aHypData = hypData;
159   }
160   QString aServLib = aHypData->ServerLibName;
161   QString aHypType = aHypData->TypeName;
162   try {
163     set( gen->CreateHypothesis(aHypType, aServLib));
164   }
165   catch (const SALOME::SALOME_Exception & S_ex) {
166     SalomeApp_Tools::QtCatchCorbaException(S_ex);
167   }
168
169   // restore current study
170   mySMESHGUI->GetSMESHGen();
171
172   onEdit();
173 }
174
175 //================================================================================
176 /*!
177  * \brief Show popup with available types
178  */
179 //================================================================================
180
181 void StdMeshersGUI_LayerDistributionParamWdg::onCreate()
182 {
183   myHypTypePopup->exec( QCursor::pos() );
184 }
185
186 //================================================================================
187 /*!
188  * \brief Edit hypothesis
189  */
190 //================================================================================
191
192 void StdMeshersGUI_LayerDistributionParamWdg::onEdit()
193 {
194   if ( myHyp->_is_nil() )
195     return;
196
197   CORBA::String_var hypType = myHyp->GetName();
198   SMESHGUI_GenericHypothesisCreator*
199     editor = SMESH::GetHypothesisCreator( hypType.in() );
200   if ( !editor ) return;
201
202   if ( myDlg ) myDlg->hide();
203
204   try {
205     QWidget* parent = this;
206     if ( myDlg ) parent = myDlg->parentWidget();
207     editor->edit( myHyp, parent );
208   }
209   catch(...) {
210   }
211
212   if ( myDlg ) myDlg->show();
213 }