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