Salome HOME
0022523: [CEA 1096] Add a colomn "biquadratic" in "Mesh Information"
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_LayerDistributionParamWdg.cxx
1 // Copyright (C) 2007-2014  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(), myName(name), myDlg( dlg )
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
115   myEditButton   = new QPushButton( tr("EDIT"), this );
116   myEditButton->setObjectName( "editBut" );
117
118   myHypTypePopup = new QMenu( this );
119
120   // Add to pop-up hypotheses of "Regular_1D" algo
121   HypothesisData* algoData = SMESH::GetHypothesisData( "Regular_1D" );
122   myHypTypes = SMESH::GetAvailableHypotheses( false, 1 );
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->addAction( hypData->Label );
130   }
131
132   aHBox->addWidget( myCreateButton );
133   aHBox->addWidget( myEditButton );
134   aHBox->addStretch();
135
136   connect( myCreateButton, SIGNAL(clicked()), SLOT(onCreate()));
137   connect( myEditButton,   SIGNAL(clicked()), SLOT(onEdit()));
138   connect( myHypTypePopup, SIGNAL(triggered( QAction* ) ), SLOT( onHypTypePopup( QAction* ) ) );
139 }
140
141 //================================================================================
142 /*!
143  * \brief Create a new hyp of selected type
144   * \param int - selected type index
145  */
146 //================================================================================
147
148 void StdMeshersGUI_LayerDistributionParamWdg::onHypTypePopup( QAction* a )
149 {
150   SMESH::SMESH_Gen_var gen = mySMESHGUI->GetSMESHGen();
151
152   // avoid publishing a new 1D hyp
153   gen->SetCurrentStudy( SALOMEDS::Study::_nil() );
154
155   // create a hyp
156   HypothesisData* aHypData = 0;
157   QStringList::const_iterator anIter = myHypTypes.begin();
158
159   for ( ; !aHypData && anIter != myHypTypes.end(); ++anIter )
160   {
161     HypothesisData* hypData = SMESH::GetHypothesisData( *anIter );
162     if ( a->text() == hypData->Label )
163       aHypData = hypData;
164   }
165   QString aServLib = aHypData->ServerLibName;
166   QString aHypType = aHypData->TypeName;
167   try {
168     set( gen->CreateHypothesis(aHypType.toLatin1().data(), aServLib.toLatin1().data()));
169   }
170   catch (const SALOME::SALOME_Exception & S_ex) {
171     SalomeApp_Tools::QtCatchCorbaException(S_ex);
172   }
173
174   // restore current study
175   mySMESHGUI->GetSMESHGen();
176
177   onEdit();
178 }
179
180 //================================================================================
181 /*!
182  * \brief Show popup with available types
183  */
184 //================================================================================
185
186 void StdMeshersGUI_LayerDistributionParamWdg::onCreate()
187 {
188   myHypTypePopup->exec( QCursor::pos() );
189 }
190
191 //================================================================================
192 /*!
193  * \brief Edit hypothesis
194  */
195 //================================================================================
196
197 void StdMeshersGUI_LayerDistributionParamWdg::onEdit()
198 {
199   if ( myHyp->_is_nil() )
200     return;
201
202   CORBA::String_var hypType = myHyp->GetName();
203   // BUG 0020378
204   SMESHGUI_GenericHypothesisCreator* editor = SMESH::GetHypothesisCreator(hypType.in());
205   if ( !editor ) return;
206
207   if ( myDlg )
208     myDlg->hide();
209
210   try {
211     QWidget* parent = this;
212     if ( myDlg )
213       parent = myDlg->parentWidget();
214     editor->edit( myHyp, myName, parent, this, SLOT( onEdited( int ) ) );
215   }
216   catch(...)
217   {
218   }
219 }
220
221 void StdMeshersGUI_LayerDistributionParamWdg::onEdited( int result )
222 {
223   if ( myDlg )
224     myDlg->show();
225 }