]> SALOME platform Git repositories - plugins/blsurfplugin.git/blob - src/GUI/BLSURFPluginGUI_HypothesisCreator.cxx
Salome HOME
1f200cd0533292b7f1fce21598045e385166998f
[plugins/blsurfplugin.git] / src / GUI / BLSURFPluginGUI_HypothesisCreator.cxx
1         //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // ---
20 // File    : BLSURFPluginGUI_HypothesisCreator.cxx
21 // Authors : Francis KLOSS (OCC) & Patrick LAUG (INRIA) & Lioka RAZAFINDRAZAKA (CEA)
22 //           & Aurelien ALLEAUME (DISTENE)
23 //           Size maps developement: Nicolas GEIMER (OCC) & Gilles DAVID (EURIWARE)
24 // ---
25 //
26 #include "BLSURFPluginGUI_HypothesisCreator.h"
27
28 #include <SMESHGUI_Utils.h>
29 #include <SMESHGUI_HypothesesUtils.h>
30 #include <SMESHGUI_Dialog.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Tools.h>
36 #include <QtxDoubleSpinBox.h>
37
38 #include <QComboBox>
39 #include <QLabel>
40 #include <QGroupBox>
41 #include <QFrame>
42 #include <QVBoxLayout>
43 #include <QGridLayout>
44 #include <QLineEdit>
45 #include <QCheckBox>
46 #include <QTabWidget>
47 #include <QSpinBox>
48 #include <QPushButton>
49 #include <QMenu>
50 #include <QTableWidget>
51 #include <QHeaderView>
52 #include <QApplication>
53 #include <QRadioButton>
54 #include <QStandardItemModel>
55 #include <QStandardItem>
56
57 #include <LightApp_SelectionMgr.h>
58 #include <SalomeApp_Application.h> 
59 #include <SALOME_ListIO.hxx>
60 #include <SALOME_ListIteratorOfListIO.hxx>
61
62 #include <GEOM_Client.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <SMESH_Gen_i.hxx>
65 #include <boost/shared_ptr.hpp> 
66 #include <structmember.h>
67
68 // #include <GeomSelectionTools.h>
69 #define WITH_SIZE_BOUNDARIES
70
71 enum Topology {
72     FromCAD,
73     Process,
74     Process2
75   } ;
76
77 enum PhysicalMesh
78   {
79     DefaultSize = 0,
80     PhysicalUserDefined,
81     SizeMap
82   };
83
84 enum GeometricMesh
85   {
86     DefaultGeom = 0,
87     UserDefined
88   };
89
90 enum {
91   STD_TAB = 0,
92   ADV_TAB,
93   SMP_TAB,
94   OPTION_ID_COLUMN = 0,
95   OPTION_NAME_COLUMN,
96   OPTION_VALUE_COLUMN,
97   NB_COLUMNS,
98   SMP_ENTRY_COLUMN = 0,
99   SMP_NAME_COLUMN,
100   SMP_SIZEMAP_COLUMN,
101   SMP_NB_COLUMNS
102 };
103
104
105
106 typedef struct {
107   PyObject_HEAD
108   int softspace;
109   std::string *out;
110   } PyStdOut;
111
112 static void
113 PyStdOut_dealloc(PyStdOut *self)
114 {
115   PyObject_Del(self);
116 }
117
118 static PyObject *
119 PyStdOut_write(PyStdOut *self, PyObject *args)
120 {
121   char *c;
122   int l;
123   if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
124     return NULL;
125
126   //std::cerr << c ;
127   *(self->out)=*(self->out)+c;
128
129   Py_INCREF(Py_None);
130   return Py_None;
131 }
132
133 static PyMethodDef PyStdOut_methods[] = {
134   {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
135     PyDoc_STR("write(string) -> None")},
136   {NULL,    NULL}   /* sentinel */
137 };
138
139 static PyMemberDef PyStdOut_memberlist[] = {
140   {"softspace", T_INT,  offsetof(PyStdOut, softspace), 0,
141    "flag indicating that a space needs to be printed; used by print"},
142   {NULL} /* Sentinel */
143 };
144
145 static PyTypeObject PyStdOut_Type = {
146   /* The ob_type field must be initialized in the module init function
147    * to be portable to Windows without using C++. */
148   PyObject_HEAD_INIT(NULL)
149   0,                            /*ob_size*/
150   "PyOut",                      /*tp_name*/
151   sizeof(PyStdOut),             /*tp_basicsize*/
152   0,                            /*tp_itemsize*/
153   /* methods */
154   (destructor)PyStdOut_dealloc, /*tp_dealloc*/
155   0,                            /*tp_print*/
156   0,                            /*tp_getattr*/
157   0,                            /*tp_setattr*/
158   0,                            /*tp_compare*/
159   0,                            /*tp_repr*/
160   0,                            /*tp_as_number*/
161   0,                            /*tp_as_sequence*/
162   0,                            /*tp_as_mapping*/
163   0,                            /*tp_hash*/
164   0,                            /*tp_call*/
165   0,                            /*tp_str*/
166   PyObject_GenericGetAttr,      /*tp_getattro*/
167   /* softspace is writable:  we must supply tp_setattro */
168   PyObject_GenericSetAttr,      /* tp_setattro */
169   0,                            /*tp_as_buffer*/
170   Py_TPFLAGS_DEFAULT,           /*tp_flags*/
171   0,                            /*tp_doc*/
172   0,                            /*tp_traverse*/
173   0,                            /*tp_clear*/
174   0,                            /*tp_richcompare*/
175   0,                            /*tp_weaklistoffset*/
176   0,                            /*tp_iter*/
177   0,                            /*tp_iternext*/
178   PyStdOut_methods,             /*tp_methods*/
179   PyStdOut_memberlist,          /*tp_members*/
180   0,                            /*tp_getset*/
181   0,                            /*tp_base*/
182   0,                            /*tp_dict*/
183   0,                            /*tp_descr_get*/
184   0,                            /*tp_descr_set*/
185   0,                            /*tp_dictoffset*/
186   0,                            /*tp_init*/
187   0,                            /*tp_alloc*/
188   0,                            /*tp_new*/
189   0,                            /*tp_free*/
190   0,                            /*tp_is_gc*/
191 };
192
193 PyObject * newPyStdOut( std::string& out )
194 {
195   PyStdOut *self;
196   self = PyObject_New(PyStdOut, &PyStdOut_Type);
197   if (self == NULL)
198     return NULL;
199   self->softspace = 0;
200   self->out=&out;
201   return (PyObject*)self;
202 }
203
204    
205 BLSURFPluginGUI_HypothesisCreator::BLSURFPluginGUI_HypothesisCreator( const QString& theHypType )
206   : SMESHGUI_GenericHypothesisCreator( theHypType )
207 {
208   cout << "BLSURFPluginGUI_HypothesisCreator::BLSURFPluginGUI_HypothesisCreator" << endl; 
209   this->mySMPMap.clear();
210
211   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
212   GeomToolSelected = new GeomSelectionTools(aStudy);
213
214   aSel = GeomToolSelected->selectionMgr();
215
216   /* Initialize the Python interpreter */
217   //assert(Py_IsInitialized());
218   if (not Py_IsInitialized())
219     throw ("Erreur: Python interpreter is not initialized");
220   PyGILState_STATE gstate;
221   gstate = PyGILState_Ensure();
222   
223   main_mod = NULL;
224   main_mod = PyImport_AddModule("__main__");
225   
226   main_dict = NULL;
227   main_dict = PyModule_GetDict(main_mod);
228   
229   PyRun_SimpleString("from math import *");
230   PyGILState_Release(gstate);
231
232 }
233
234 BLSURFPluginGUI_HypothesisCreator::~BLSURFPluginGUI_HypothesisCreator()
235 {
236 }
237
238 GeomSelectionTools* BLSURFPluginGUI_HypothesisCreator::getGeomSelectionTool()
239 {
240   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
241   if (that->GeomToolSelected == NULL) {
242     cout << "GeomToolSelected is created" << endl;
243     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
244     that->GeomToolSelected = new GeomSelectionTools(aStudy);
245   }
246   return that->GeomToolSelected;
247 }
248
249 namespace {
250   inline bool isDouble( const QString& theText, const bool emptyOK=false ) {
251     QString str = theText.trimmed();
252     bool isOk = true;
253     if ( !str.isEmpty() )
254       str.toDouble(&isOk);
255     else
256       isOk = emptyOK;
257     return isOk;
258   }
259 }
260
261 bool BLSURFPluginGUI_HypothesisCreator::checkParams() const
262 {
263   cout << "BLSURFPluginGUI_HypothesisCreator::checkParams" << endl; 
264   bool ok = true;
265   if ( !isDouble( myPhySize->text(), false )) {
266     if ( myPhySize->text().isEmpty() )
267       myPhySize->setText(tr("OBLIGATORY_VALUE"));
268     myPhySize->selectAll();
269     ok = false;
270   }
271   if ( !isDouble( myPhyMin->text(), true )) {
272     myPhyMin->selectAll();
273     ok = false;
274   }
275   if ( !isDouble( myPhyMax->text(), true )) {
276     myPhyMax->selectAll();
277     ok = false;
278   }
279   if ( !isDouble( myGeoMin->text(), true )) {
280     myGeoMin->selectAll();
281     ok = false;
282   }
283   if ( !isDouble( myGeoMin->text(), true )) {
284     myGeoMin->selectAll();
285     ok = false;
286   }
287   if ( ok )
288   {
289     myOptionTable->setFocus();
290     QApplication::instance()->processEvents();
291
292     BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
293       BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
294
295     int row = 0, nbRows = myOptionTable->rowCount();
296     for ( ; row < nbRows; ++row )
297     {
298       QString name  = myOptionTable->item( row, OPTION_NAME_COLUMN )->text();
299       QString value = myOptionTable->item( row, OPTION_VALUE_COLUMN )->text().trimmed();
300       if ( !value.isEmpty() ) {
301         try {
302           h->SetOptionValue( name.toLatin1().constData(), value.toLatin1().constData() );
303         }
304         catch ( const SALOME::SALOME_Exception& ex )
305         {
306           SUIT_MessageBox::critical( dlg(),
307                                      tr("SMESH_ERROR"),
308                                      ex.details.text.in() );
309           ok = false;
310         }
311       }
312     }
313     h->SetOptionValues( myOptions ); // restore values
314   }
315
316   // SizeMap
317   if ( ok )
318   {
319     mySizeMapTable->setFocus();
320     QApplication::instance()->processEvents();
321
322     BLSURFPlugin::BLSURFPlugin_Hypothesis_var h = BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
323     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
324
325     int row = 0, nbRows = mySizeMapTable->rowCount();
326     for ( ; row < nbRows; ++row )
327     {
328       QString entry   = mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->text();
329       QString sizeMap = mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->text().trimmed();
330       if ( !sizeMap.isEmpty() ) {
331         if (that->sizeMapValidationFromRow(row))
332         {
333           try {
334             const char* e = entry.toLatin1().constData();
335             const char* s = that->mySMPMap[entry].toLatin1().constData();
336             h->SetSizeMapEntry( e, s );
337           }
338           catch ( const SALOME::SALOME_Exception& ex )
339           {
340             SUIT_MessageBox::critical( dlg(),
341                                        tr("SMESH_ERROR"),
342                                        ex.details.text.in() );
343             ok = false;
344           }
345         }
346         else {
347           ok = false;
348         }
349       }
350     }
351   }
352
353   return ok;
354 }
355
356 QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
357 {
358   cout << "BLSURFPluginGUI_HypothesisCreator::buildFrame" << endl; 
359   QFrame* fr = new QFrame( 0 );
360   QVBoxLayout* lay = new QVBoxLayout( fr );
361   lay->setMargin( 5 );
362   lay->setSpacing( 0 );
363
364   // tab
365   QTabWidget* tab = new QTabWidget( fr );
366   tab->setTabShape( QTabWidget::Rounded );
367   tab->setTabPosition( QTabWidget::North );
368   lay->addWidget( tab );
369
370   // basic parameters
371   myStdGroup = new QWidget();
372   QGridLayout* aStdLayout = new QGridLayout( myStdGroup );
373   aStdLayout->setSpacing( 6 );
374   aStdLayout->setMargin( 11 );
375
376   int row = 0;
377   myName = 0;
378   if( isCreation() ) {
379     aStdLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), myStdGroup ), row, 0, 1, 1 );
380     myName = new QLineEdit( myStdGroup );
381     aStdLayout->addWidget( myName, row++, 1, 1, 1 );
382   }
383
384   aStdLayout->addWidget( new QLabel( tr( "BLSURF_PHY_MESH" ), myStdGroup ), row, 0, 1, 1 );
385   myPhysicalMesh = new QComboBox( myStdGroup );
386   aStdLayout->addWidget( myPhysicalMesh, row++, 1, 1, 1 );
387   QStringList physicalTypes;
388   physicalTypes << tr( "BLSURF_DEFAULT_USER" ) << tr( "BLSURF_CUSTOM_USER" ) << tr( "BLSURF_SIZE_MAP");
389   myPhysicalMesh->addItems( physicalTypes );
390
391   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HPHYDEF" ), myStdGroup), row, 0, 1, 1 );
392   myPhySize = new QLineEdit( myStdGroup );
393   aStdLayout->addWidget( myPhySize, row++, 1, 1, 1 );
394
395 #ifdef WITH_SIZE_BOUNDARIES
396   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HPHYMIN" ), myStdGroup ), row, 0, 1, 1 );
397   myPhyMin = new QLineEdit( myStdGroup );
398   aStdLayout->addWidget( myPhyMin, row++, 1, 1, 1 );
399
400   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HPHYMAX" ), myStdGroup ), row, 0, 1, 1 );
401   myPhyMax = new QLineEdit( myStdGroup );
402   aStdLayout->addWidget( myPhyMax, row++, 1, 1, 1 );
403 #endif
404
405   aStdLayout->addWidget( new QLabel( tr( "BLSURF_GEOM_MESH" ), myStdGroup ), row, 0, 1, 1 );
406   myGeometricMesh = new QComboBox( myStdGroup );
407   aStdLayout->addWidget( myGeometricMesh, row++, 1, 1, 1 );
408   QStringList types;
409   types << tr( "BLSURF_DEFAULT_GEOM" ) << tr( "BLSURF_CUSTOM_GEOM" );
410   myGeometricMesh->addItems( types );
411
412   aStdLayout->addWidget( new QLabel( tr( "BLSURF_ANGLE_MESH_S" ), myStdGroup ), row, 0, 1, 1 );
413   myAngleMeshS = new QtxDoubleSpinBox( myStdGroup );
414   aStdLayout->addWidget( myAngleMeshS, row++, 1, 1, 1 );
415   myAngleMeshS->setMinimum( 0 );
416   myAngleMeshS->setMaximum( 16 );
417   myAngleMeshS->setSingleStep( 0.5 );
418   
419   aStdLayout->addWidget( new QLabel( tr( "BLSURF_ANGLE_MESH_C" ), myStdGroup ), row, 0, 1, 1 );
420   myAngleMeshC = new QtxDoubleSpinBox( myStdGroup );
421   aStdLayout->addWidget( myAngleMeshC, row++, 1, 1, 1 );
422   myAngleMeshC->setMinimum( 0 );
423   myAngleMeshC->setMaximum( 16 );
424   myAngleMeshC->setSingleStep( 0.5 );
425   
426   aStdLayout->addWidget( new QLabel( tr( "BLSURF_GRADATION" ), myStdGroup ), row, 0, 1, 1 );
427   myGradation = new QtxDoubleSpinBox( myStdGroup );
428   aStdLayout->addWidget( myGradation, row++, 1, 1, 1 );
429   myGradation->setMinimum( 1.1 );
430   myGradation->setMaximum( 2.5 );
431   myGradation->setSingleStep( 0.1 );
432
433 #ifdef WITH_SIZE_BOUNDARIES
434   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HGEOMIN" ), myStdGroup ), row, 0, 1, 1 );
435   myGeoMin = new QLineEdit( myStdGroup );
436   aStdLayout->addWidget( myGeoMin, row++, 1, 1, 1 );
437
438   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HGEOMAX" ), myStdGroup ), row, 0, 1, 1 );
439   myGeoMax = new QLineEdit( myStdGroup );
440   aStdLayout->addWidget( myGeoMax, row++, 1, 1, 1 );
441 #endif
442
443   myAllowQuadrangles = new QCheckBox( tr( "BLSURF_ALLOW_QUADRANGLES" ), myStdGroup );
444   aStdLayout->addWidget( myAllowQuadrangles, row++, 0, 1, 2 );
445
446   myDecimesh = new QCheckBox( tr( "BLSURF_DECIMESH" ), myStdGroup );
447   aStdLayout->addWidget( myDecimesh, row++, 0, 1, 2 );
448   
449   // advanced parameters
450   myAdvGroup = new QWidget();
451   QGridLayout* anAdvLayout = new QGridLayout( myAdvGroup );
452   anAdvLayout->setSpacing( 6 );
453   anAdvLayout->setMargin( 11 );
454
455   anAdvLayout->addWidget( new QLabel( tr( "BLSURF_TOPOLOGY" ), myAdvGroup ), 0, 0, 1, 1 );
456   myTopology = new QComboBox( myAdvGroup );
457   anAdvLayout->addWidget( myTopology, 0, 1, 1, 1 );
458   QStringList topologyTypes;
459   topologyTypes << tr( "BLSURF_TOPOLOGY_CAD" ) << tr( "BLSURF_TOPOLOGY_PROCESS" ) << tr( "BLSURF_TOPOLOGY_PROCESS2" );
460   myTopology->addItems( topologyTypes );
461
462   anAdvLayout->addWidget( new QLabel( tr( "BLSURF_VERBOSITY" ), myAdvGroup ), 1, 0, 1, 1 );
463   myVerbosity = new QSpinBox( myAdvGroup );
464   anAdvLayout->addWidget( myVerbosity, 1, 1, 1, 1 );
465   myVerbosity->setMinimum( 0 );
466   myVerbosity->setMaximum( 100 );
467   myVerbosity->setSingleStep( 5 );
468
469   myOptionTable = new QTableWidget( 0, NB_COLUMNS, myAdvGroup );
470   anAdvLayout->addWidget( myOptionTable, 2, 0, 3, 2 );
471   QStringList headers;
472   headers << tr( "OPTION_ID_COLUMN" ) << tr( "OPTION_NAME_COLUMN" ) << tr( "OPTION_VALUE_COLUMN" );
473   myOptionTable->setHorizontalHeaderLabels( headers );
474   myOptionTable->horizontalHeader()->hideSection( OPTION_ID_COLUMN );
475   //myOptionTable->setColumnReadOnly( OPTION_NAME_COLUMN, TRUE );//////
476   //myOptionTable->setColumnReadOnly( OPTION_VALUE_COLUMN, FALSE );/////
477   myOptionTable->verticalHeader()->hide();
478   //myOptionTable->setSelectionBehavior( QAbstractItemView::SelectRows );
479
480   QPushButton* addBtn = new QPushButton( tr( "ADD_OPTION"),  myAdvGroup );
481   anAdvLayout->addWidget( addBtn, 2, 2, 1, 1 );
482   addBtn->setMenu( new QMenu() );
483
484   QPushButton* rmBtn = new QPushButton( tr( "REMOVE_OPTION"), myAdvGroup );
485   anAdvLayout->addWidget( rmBtn, 3, 2, 1, 1 );
486
487   anAdvLayout->setRowStretch( 4, 5 );
488   anAdvLayout->setColumnStretch( 1, 5 );
489
490   // Size Maps parameters
491   
492   mySmpGroup = new QWidget();
493   QGridLayout* anSmpLayout = new QGridLayout(mySmpGroup);
494
495   mySizeMapTable = new QTableWidget( 0, SMP_NB_COLUMNS, mySmpGroup );
496   anSmpLayout->addWidget(mySizeMapTable, 1, 0, 8, 1);
497   QStringList sizeMapHeaders;
498   sizeMapHeaders << tr( "SMP_ENTRY_COLUMN" )<< tr( "SMP_NAME_COLUMN" ) << tr( "SMP_SIZEMAP_COLUMN" ); 
499   mySizeMapTable->setHorizontalHeaderLabels(sizeMapHeaders);
500   mySizeMapTable->horizontalHeader()->hideSection( SMP_ENTRY_COLUMN );
501   mySizeMapTable->resizeColumnToContents(SMP_NAME_COLUMN);
502   mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
503   mySizeMapTable->setAlternatingRowColors(true);
504   mySizeMapTable->verticalHeader()->hide();
505
506   addAttractorButton = new QPushButton(tr("BLSURF_SM_ATTRACTOR"),mySmpGroup);
507   anSmpLayout->addWidget(addAttractorButton, 1, 1, 1, 1);
508
509   QFrame *line = new QFrame(mySmpGroup);
510   line->setFrameShape(QFrame::HLine);
511   line->setFrameShadow(QFrame::Sunken);
512   anSmpLayout->addWidget(line, 2, 1, 1, 1);
513
514   addSurfaceButton = new QPushButton(tr("BLSURF_SM_SURFACE"),mySmpGroup);
515   anSmpLayout->addWidget(addSurfaceButton, 3, 1, 1, 1);
516
517   addEdgeButton = new QPushButton(tr("BLSURF_SM_EDGE"),mySmpGroup);
518   anSmpLayout->addWidget(addEdgeButton, 4, 1, 1, 1);
519
520   addPointButton = new QPushButton(tr("BLSURF_SM_POINT"),mySmpGroup);
521   anSmpLayout->addWidget(addPointButton, 5, 1, 1, 1);
522
523   QFrame *line2 = new QFrame(mySmpGroup);
524   line2->setFrameShape(QFrame::HLine);
525   line2->setFrameShadow(QFrame::Sunken);
526   anSmpLayout->addWidget(line2, 6, 1, 1, 1);
527
528   removeButton = new QPushButton(tr("BLSURF_SM_REMOVE"),mySmpGroup);
529   anSmpLayout->addWidget(removeButton, 7, 1, 1, 1);
530   
531
532   // ---
533   tab->insertTab( STD_TAB, myStdGroup, tr( "SMESH_ARGUMENTS" ) );
534   tab->insertTab( ADV_TAB, myAdvGroup, tr( "GHS3D_ADV_ARGS" ) );
535   tab->insertTab( SMP_TAB, mySmpGroup, tr( "BLSURF_SIZE_MAP" ) );
536
537   tab->setCurrentIndex( STD_TAB );
538
539   // ---
540   connect( myGeometricMesh, SIGNAL( activated( int ) ), this, SLOT( onGeometricMeshChanged() ) );
541   connect( myPhysicalMesh,  SIGNAL( activated( int ) ), this, SLOT( onPhysicalMeshChanged() ) );
542   connect( addBtn->menu(),  SIGNAL( aboutToShow() ),    this, SLOT( onAddOption() ) );
543   connect( addBtn->menu(),  SIGNAL( triggered( QAction* ) ), this, SLOT( onOptionChosenInPopup( QAction* ) ) );
544   connect( rmBtn,           SIGNAL( clicked()),         this, SLOT( onDeleteOption() ) );
545   
546   connect(addSurfaceButton, SIGNAL(clicked()), this, SLOT(onAddMapOnSurface()));
547   connect(addEdgeButton, SIGNAL(clicked()), this, SLOT(onAddMapOnEdge()));
548   connect(addPointButton, SIGNAL(clicked()), this, SLOT(onAddMapOnPoint()));
549   connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemoveMap()));
550   connect(mySizeMapTable, SIGNAL(cellChanged ( int, int  )),this,SLOT (onSetSizeMap(int,int )));
551
552 //  connect(tab, SIGNAL(currentChanged ( int  )),this,SLOT (onSetSelectionFilter(int)));
553
554   return fr;
555 }
556
557
558 void BLSURFPluginGUI_HypothesisCreator::onSetSelectionFilter(int page) 
559 {
560   cout << "BLSURFPluginGUI_HypothesisCreator::onSetSelectionFilter(" << page << ")" << endl;
561   aSel->clearFilters();
562   if (page == SMP_TAB) {
563     cout << "Page is SIZEMAP" << endl;
564     aSel = GeomToolSelected->selectionMgr();
565   }
566 }
567
568 void BLSURFPluginGUI_HypothesisCreator::retrieveParams() const
569 {
570   cout << "BLSURFPluginGUI_HypothesisCreator::retrieveParams" << endl; 
571   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
572
573   BlsurfHypothesisData data;
574   that->readParamsFromHypo( data );
575
576   if ( myName ) {
577     myName->setText( data.myName );
578     QFontMetrics metrics( myName->font() );
579     myName->setMinimumWidth( metrics.width( data.myName )+5 );
580   }
581   myTopology->setCurrentIndex( data.myTopology );
582   myPhysicalMesh->setCurrentIndex( data.myPhysicalMesh );
583   myPhySize->setText( data.myPhySize );
584 #ifdef WITH_SIZE_BOUNDARIES
585   myPhyMin->setText( data.myPhyMin );
586   myPhyMax->setText( data.myPhyMax );
587   myGeoMin->setText( data.myGeoMin );
588   myGeoMax->setText( data.myGeoMax );
589 #endif
590   myGeometricMesh->setCurrentIndex( data.myGeometricMesh );
591   myAngleMeshS->setValue( data.myAngleMeshS );
592   myAngleMeshC->setValue( data.myAngleMeshC );
593   myGradation->setValue( data.myGradation );
594   myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
595   myDecimesh->setChecked( data.myDecimesh );
596   myVerbosity->setValue( data.myVerbosity );
597   
598   if ( myOptions.operator->() ) {
599     printf("retrieveParams():myOptions->length()=%d\n",myOptions->length());
600     for ( int i = 0, nb = myOptions->length(); i < nb; ++i ) {
601       QString option = that->myOptions[i].in();
602       QStringList name_value = option.split( ":", QString::KeepEmptyParts );
603       if ( name_value.count() > 1 ) {
604         QString idStr = QString("%1").arg( i );
605         int row = myOptionTable->rowCount();
606         myOptionTable->setRowCount( row+1 );
607         myOptionTable->setItem( row, OPTION_ID_COLUMN, new QTableWidgetItem( idStr ) );
608         myOptionTable->item( row, OPTION_ID_COLUMN )->setFlags( 0 );
609         myOptionTable->setItem( row, OPTION_NAME_COLUMN, new QTableWidgetItem( name_value[0] ) );
610         myOptionTable->item( row, OPTION_NAME_COLUMN )->setFlags( 0 );
611         myOptionTable->setItem( row, OPTION_VALUE_COLUMN, new QTableWidgetItem( name_value[1] ) );
612         myOptionTable->item( row, OPTION_VALUE_COLUMN )->setFlags( Qt::ItemIsSelectable | 
613                                                                    Qt::ItemIsEditable   | 
614                                                                    Qt::ItemIsEnabled );
615       }
616     } 
617   }
618   myOptionTable->resizeColumnToContents( OPTION_NAME_COLUMN );
619
620   printf("retrieveParams():that->mySMPMap.size()=%d\n",that->mySMPMap.size());
621   QMapIterator<QString, QString> i(that->mySMPMap);
622   while (i.hasNext()) {
623     i.next();
624     const QString entry = i.key();
625     string shapeName = GeomToolSelected->getNameFromEntry(entry.toStdString());
626     const QString sizeMap = i.value();
627     int row = mySizeMapTable->rowCount();
628     mySizeMapTable->setRowCount( row+1 );
629     mySizeMapTable->setItem( row, SMP_ENTRY_COLUMN, new QTableWidgetItem( entry ) );
630     mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->setFlags( 0 );
631     mySizeMapTable->setItem( row, SMP_NAME_COLUMN, new QTableWidgetItem( QString::fromStdString(shapeName) ) );
632     mySizeMapTable->item( row, SMP_NAME_COLUMN )->setFlags( 0 );
633     mySizeMapTable->setItem( row, SMP_SIZEMAP_COLUMN, new QTableWidgetItem( sizeMap ) );
634     mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->setFlags( Qt::ItemIsSelectable |
635                                                                Qt::ItemIsEditable   |
636                                                                Qt::ItemIsEnabled );
637     }
638   
639   mySizeMapTable->resizeColumnToContents( SMP_NAME_COLUMN );
640   mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
641
642   // update widgets
643   that->onPhysicalMeshChanged();
644   that->onGeometricMeshChanged();
645 }
646
647 QString BLSURFPluginGUI_HypothesisCreator::storeParams() const
648 {
649   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
650
651   BlsurfHypothesisData data;
652   QString guiHyp = that->readParamsFromWidgets( data );
653   that->storeParamsToHypo( data );
654
655   //cout<<"BLSURFPluginGUI_HypothesisCreator::storeParams(), guiHyp: "<<endl<<guiHyp.toStdString()<<endl;
656   return guiHyp;
657 }
658
659 bool BLSURFPluginGUI_HypothesisCreator::readParamsFromHypo( BlsurfHypothesisData& h_data ) const
660 {
661   cout << "BLSURFPluginGUI_HypothesisCreator::readParamsFromHypo" << endl; 
662   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
663     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
664
665   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
666   h_data.myName = isCreation() && data ? hypName() : "";
667
668   h_data.myTopology         = (int) h->GetTopology();
669   h_data.myPhysicalMesh     = (int) h->GetPhysicalMesh();
670   h_data.myPhySize          = QString::number( h->GetPhySize() );
671   h_data.myGeometricMesh    = (int) h->GetGeometricMesh();
672   h_data.myAngleMeshS       = h->GetAngleMeshS();
673   h_data.myAngleMeshC       = h->GetAngleMeshC();
674   h_data.myGradation        = h->GetGradation();
675   h_data.myAllowQuadrangles = h->GetQuadAllowed();
676   h_data.myDecimesh         = h->GetDecimesh();
677   h_data.myVerbosity        = h->GetVerbosity();
678
679 #ifdef WITH_SIZE_BOUNDARIES
680   double PhyMin = h->GetPhyMin();
681   double PhyMax = h->GetPhyMax();
682   double GeoMin = h->GetGeoMin();
683   double GeoMax = h->GetGeoMax();
684   if ( PhyMin > 0 )
685   h_data.myPhyMin = PhyMin > 0 ? QString::number( h->GetPhyMin() ) : QString("");
686   h_data.myPhyMax = PhyMax > 0 ? QString::number( h->GetPhyMax() ) : QString("");
687   h_data.myGeoMin = GeoMin > 0 ? QString::number( h->GetGeoMin() ) : QString("");
688   h_data.myGeoMax = GeoMax > 0 ? QString::number( h->GetGeoMax() ) : QString("");
689 #endif
690
691   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
692   that->myOptions = h->GetOptionValues();
693
694   that->mySMPMap.clear();
695
696   // classic size maps
697   BLSURFPlugin::string_array_var mySizeMaps = h->GetSizeMapEntries();
698   cout << "mySizeMaps->length() = " << mySizeMaps->length() << endl;
699
700   for ( int i = 0;i<mySizeMaps->length(); ++i ) {
701     QString fullSizeMaps =  mySizeMaps[i].in();
702     QStringList fullSizeMapList = fullSizeMaps.split( "|", QString::KeepEmptyParts );
703     if ( fullSizeMapList.count() > 1 ) {
704       string fullSizeMap = fullSizeMapList[1].toStdString();
705       int pos = fullSizeMap.find("return")+7;
706       QString sizeMap = QString::fromStdString(fullSizeMap.substr(pos, fullSizeMap.size()-pos));
707       that->mySMPMap[fullSizeMapList[0]] = sizeMap;
708       that->mySMPShapeTypeMap[fullSizeMapList[0]] = GeomToolSelected->entryToShape(fullSizeMapList[0].toStdString()).ShapeType();
709       MESSAGE("mySMPMap[" << fullSizeMapList[0].toStdString() << "] = " << sizeMap.toStdString());
710       MESSAGE("mySMPShapeTypeMap[" << fullSizeMapList[0].toStdString() << "] = " << that->mySMPShapeTypeMap[fullSizeMapList[0]]);
711     }
712   }
713
714   // custom size maps
715 /*
716   BLSURFPlugin::string_array_var myCustomSizeMaps = h->GetCustomSizeMapEntries();
717   cout << "myCustomSizeMaps->length() = " << myCustomSizeMaps->length() << endl;
718
719   for ( int i = 0;i<myCustomSizeMaps->length(); ++i ) {
720     QString fullCustomSizeMaps =  myCustomSizeMaps[i].in();
721     QStringList fullCustomSizeMapList = fullCustomSizeMaps.split( "|", QString::KeepEmptyParts );
722     if ( fullCustomSizeMapList.count() > 1 ) {
723       that->mySMPMap[fullCustomSizeMapList[0]] = fullCustomSizeMapList[1];
724       that->mySMPShapeTypeMap[fullCustomSizeMapList[0]] = GeomToolSelected->entryToShape(fullCustomSizeMapList[0].toStdString()).ShapeType();
725       MESSAGE("mySMPMap[" << fullCustomSizeMapList[0].toStdString() << "] = " << fullCustomSizeMapList[1].toStdString());
726       MESSAGE("mySMPShapeTypeMap[" << fullCustomSizeMapList[0].toStdString() << "] = " << that->mySMPShapeTypeMap[fullCustomSizeMapList[0]]);
727     }
728   }
729 */
730   // attractor
731   BLSURFPlugin::string_array_var allMyAttractors = h->GetAttractorEntries();
732   cout << "myAttractors->length() = " << allMyAttractors->length() << endl;
733
734   for ( int i = 0;i<allMyAttractors->length(); ++i ) {
735     QString myAttractors =  allMyAttractors[i].in();
736     QStringList myAttractorList = myAttractors.split( "|", QString::KeepEmptyParts );
737     if ( myAttractorList.count() > 1 ) {
738       that->mySMPMap[myAttractorList[0]] = myAttractorList[1];
739       that->mySMPShapeTypeMap[myAttractorList[0]] = GeomToolSelected->entryToShape(myAttractorList[0].toStdString()).ShapeType();
740       MESSAGE("mySMPMap[" << myAttractorList[0].toStdString() << "] = " << myAttractorList[1].toStdString());
741       MESSAGE("mySMPShapeTypeMap[" << myAttractorList[0].toStdString() << "] = " << that->mySMPShapeTypeMap[myAttractorList[0]]);
742     }
743   }
744
745   return true;
746 }
747
748 bool BLSURFPluginGUI_HypothesisCreator::storeParamsToHypo( const BlsurfHypothesisData& h_data ) const
749 {
750   cout << "BLSURFPluginGUI_HypothesisCreator::storeParamsToHypo" << endl; 
751   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
752     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( hypothesis() );
753
754   bool ok = true;
755   try
756   {
757     if( isCreation() )
758       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().constData() );
759
760     if ( h->GetTopology() != h_data.myTopology ) // avoid duplication of DumpPython commands
761       h->SetTopology( (int) h_data.myTopology );
762     if ( h->GetPhysicalMesh() != h_data.myPhysicalMesh )
763       h->SetPhysicalMesh( (int) h_data.myPhysicalMesh );
764     if ( h->GetGeometricMesh() != (int) h_data.myGeometricMesh )
765       h->SetGeometricMesh( (int) h_data.myGeometricMesh );
766     if ( h->GetGradation() !=  h_data.myGradation )
767       h->SetGradation( h_data.myGradation );
768     if ( h->GetQuadAllowed() != h_data.myAllowQuadrangles )
769       h->SetQuadAllowed( h_data.myAllowQuadrangles );
770     if ( h->GetDecimesh() != h_data.myDecimesh )
771       h->SetDecimesh( h_data.myDecimesh );
772     if ( h->GetVerbosity() != h_data.myVerbosity )
773       h->SetVerbosity( h_data.myVerbosity );
774
775     if( ((int) h_data.myPhysicalMesh == PhysicalUserDefined)||((int) h_data.myPhysicalMesh == SizeMap) ) {
776       if ( h->GetPhySize() != h_data.myPhySize.toDouble() )
777         h->SetPhySize( h_data.myPhySize.toDouble() );
778     }
779     if( (int) h_data.myGeometricMesh == UserDefined ) {
780       if ( h->GetAngleMeshS() != h_data.myAngleMeshS )
781         h->SetAngleMeshS( h_data.myAngleMeshS );
782       if ( h->GetAngleMeshC() != h_data.myAngleMeshC )
783         h->SetAngleMeshC( h_data.myAngleMeshC );
784     }
785 #ifdef WITH_SIZE_BOUNDARIES
786     if ( !isDouble( h_data.myPhyMin ))
787       h->SetPhyMin( -1 );
788     else if ( h->GetPhyMin() != h_data.myPhyMin.toDouble() )
789       h->SetPhyMin( h_data.myPhyMin.toDouble() );
790     if ( !isDouble( h_data.myPhyMax ))
791       h->SetPhyMax( -1 );
792     else if ( h->GetPhyMax() != h_data.myPhyMax.toDouble() )
793       h->SetPhyMax( h_data.myPhyMax.toDouble() );
794     if ( !isDouble( h_data.myGeoMin ))
795       h->SetGeoMin( -1 );
796     else if ( h->GetGeoMin() != h_data.myGeoMin.toDouble() )
797       h->SetGeoMin( h_data.myGeoMin.toDouble() );
798     if ( !isDouble( h_data.myGeoMax ))
799       h->SetGeoMax( -1 );
800     else if ( h->GetGeoMax() != h_data.myGeoMax.toDouble() )
801       h->SetGeoMax( h_data.myGeoMax.toDouble() );
802 #endif
803
804     //printf("storeParamsToHypo():myOptions->length()=%d\n",myOptions->length());
805     h->SetOptionValues( myOptions ); // is set in checkParams()
806
807     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
808     QMapIterator<QString,QString> i(that->mySMPMap);
809     // Iterate over each size map
810     while (i.hasNext()) {
811       i.next();
812       const QString entry = i.key();
813       const QString sizeMap = i.value();
814
815 /*
816       if (not that->sizeMapValidationFromEntry(entry,false)) {
817         cout << "Size map for entry " << entry.toStdString() << " is not valid" << endl;
818         ok = false;
819         continue;
820       }
821 */
822       if (sizeMap == "__TO_DELETE__") {
823         cout << "Delete entry " << entry.toStdString() << " from engine" << endl;
824         h->UnsetEntry(entry.toLatin1().constData());
825       }
826       else if (sizeMap.startsWith("ATTRACTOR")) {
827         cout << "SetAttractorEntry(" << entry.toStdString() << ")= " << sizeMap.toStdString()  << endl;
828         h->SetAttractorEntry( entry.toLatin1().constData(), sizeMap.toLatin1().constData() );
829       }
830       else if (sizeMap.startsWith("def")) {
831         cout << "SetCustomSizeMapEntry(" << entry.toStdString() << ")= " << sizeMap.toStdString()  << endl;
832 //        h->SetCustomSizeMapEntry( entry.toLatin1().constData(), sizeMap.toLatin1().constData() );
833       }
834       else {
835         QString fullSizeMap;
836         fullSizeMap = QString("");
837         if (that->mySMPShapeTypeMap[entry]  == TopAbs_FACE)
838           fullSizeMap = QString("def f(u,v): return ") + sizeMap;
839         else if (that->mySMPShapeTypeMap[entry]  == TopAbs_EDGE)
840           fullSizeMap = QString("def f(t): return ") + sizeMap;
841         else if (that->mySMPShapeTypeMap[entry] == TopAbs_VERTEX)
842           fullSizeMap = QString("def f(): return ") + sizeMap;
843
844         cout << "SetSizeMapEntry("<<entry.toStdString()<<") = " <<fullSizeMap.toStdString() << endl;
845         h->SetSizeMapEntry( entry.toLatin1().constData(), fullSizeMap.toLatin1().constData() );
846       }
847     }
848   }
849   catch(const SALOME::SALOME_Exception& ex)
850   {
851     SalomeApp_Tools::QtCatchCorbaException(ex);
852     ok = false;
853   }
854   return ok;
855 }
856
857 QString BLSURFPluginGUI_HypothesisCreator::readParamsFromWidgets( BlsurfHypothesisData& h_data ) const
858 {
859   cout << "BLSURFPluginGUI_HypothesisCreator::readParamsFromWidgets" << endl; 
860   h_data.myName             = myName ? myName->text() : "";
861   h_data.myTopology         = myTopology->currentIndex();
862   h_data.myPhysicalMesh     = myPhysicalMesh->currentIndex();
863   h_data.myPhySize          = myPhySize->text();
864 #ifdef WITH_SIZE_BOUNDARIES
865   h_data.myPhyMin           = myPhyMin->text();
866   h_data.myPhyMax           = myPhyMax->text();
867   h_data.myGeoMin           = myGeoMin->text();
868   h_data.myGeoMax           = myGeoMax->text();
869 #endif
870   h_data.myGeometricMesh    = myGeometricMesh->currentIndex();
871   h_data.myAngleMeshS       = myAngleMeshS->value();
872   h_data.myAngleMeshC       = myAngleMeshC->value();
873   h_data.myGradation        = myGradation->value();
874   h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
875   h_data.myDecimesh         = myDecimesh->isChecked();
876   h_data.myVerbosity        = myVerbosity->value();
877
878   QString guiHyp;
879   guiHyp += tr("BLSURF_TOPOLOGY") + " = " + QString::number( h_data.myTopology ) + "; ";
880   guiHyp += tr("BLSURF_PHY_MESH") + " = " + QString::number( h_data.myPhysicalMesh ) + "; ";
881   guiHyp += tr("BLSURF_HPHYDEF") + " = " + h_data.myPhySize + "; ";
882   guiHyp += tr("BLSURF_GEOM_MESH") + " = " + QString::number( h_data.myGeometricMesh ) + "; ";
883   guiHyp += tr("BLSURF_ANGLE_MESH_S") + " = " + QString::number( h_data.myAngleMeshS ) + "; ";
884   guiHyp += tr("BLSURF_GRADATION") + " = " + QString::number( h_data.myGradation ) + "; ";
885   guiHyp += tr("BLSURF_ALLOW_QUADRANGLES") + " = " + QString(h_data.myAllowQuadrangles ? "yes" : "no") + "; ";
886   guiHyp += tr("BLSURF_DECIMESH") + " = " + QString(h_data.myDecimesh ? "yes" : "no") + "; ";
887 #ifdef WITH_SIZE_BOUNDARIES
888   if ( isDouble( h_data.myPhyMin )) guiHyp += "hphymin = " + h_data.myPhyMin + "; ";
889   if ( isDouble( h_data.myPhyMax )) guiHyp += "hphymax = " + h_data.myPhyMax + "; ";
890   if ( isDouble( h_data.myGeoMin )) guiHyp += "hgeomin = " + h_data.myGeoMin + "; ";
891   if ( isDouble( h_data.myGeoMax )) guiHyp += "hgeomax = " + h_data.myGeoMax + "; ";
892 #endif
893
894   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
895   int row = 0, nbRows = myOptionTable->rowCount();
896   for ( ; row < nbRows; ++row )
897   {
898     int id = myOptionTable->item( row, OPTION_ID_COLUMN )->text().toInt();
899     if ( id >= 0 && id < myOptions->length() )
900     {
901       QString name  = myOptionTable->item( row, OPTION_NAME_COLUMN )->text();
902       QString value = myOptionTable->item( row, OPTION_VALUE_COLUMN )->text().trimmed();
903       if ( value.isNull() )
904         value = "";
905       that->myOptions[ id ] = ( name + ":" + value).toLatin1().constData();
906       if ( value != "" )
907         guiHyp += name + " = " + value + "; ";
908     }
909   }
910   
911   // SizeMap
912   row = 0, nbRows = mySizeMapTable->rowCount();
913   for ( ; row < nbRows; ++row )
914   {
915       QString entry   = mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->text();
916       if ( that->mySMPMap.contains(entry) )
917         guiHyp += entry + " = " + that->mySMPMap[entry] + "; ";
918   }
919   
920   cout << "guiHyp : " << guiHyp.toLatin1().data() << endl;
921
922   return guiHyp;
923 }
924
925 void BLSURFPluginGUI_HypothesisCreator::onPhysicalMeshChanged() {
926   cout << "BLSURFPluginGUI_HypothesisCreator::onPhysicalMeshChanged" << endl; 
927   bool isCustom = ((myPhysicalMesh->currentIndex() == PhysicalUserDefined) || (myPhysicalMesh->currentIndex() == SizeMap)) ;
928   myPhySize->setEnabled(isCustom);
929   myPhyMax->setEnabled(isCustom);
930   myPhyMin->setEnabled(isCustom);
931
932   if ( !isCustom ) {
933     QString aPhySize = "";
934     switch( myPhysicalMesh->currentIndex() ) {
935       case DefaultSize:
936       default:
937         aPhySize = "10";
938         break;
939       }
940     myPhySize->setText( aPhySize );
941     if ( !isDouble( myPhyMin->text(), true ))
942       myPhyMin->setText("");
943     if ( !isDouble( myPhyMax->text(), true ))
944       myPhyMax->setText("");
945     if ( myGeometricMesh->currentIndex() == DefaultGeom ) {
946       myGeometricMesh->setCurrentIndex( UserDefined );
947       onGeometricMeshChanged();
948     }
949   }
950 }
951
952 void BLSURFPluginGUI_HypothesisCreator::onGeometricMeshChanged() {
953   cout << "BLSURFPluginGUI_HypothesisCreator::onGeometricMeshChanged" << endl; 
954   bool isCustom = (myGeometricMesh->currentIndex() == UserDefined);
955   myAngleMeshS->setEnabled(isCustom);
956   myAngleMeshC->setEnabled(isCustom);
957   myGradation->setEnabled(isCustom);
958   myGeoMax->setEnabled(isCustom);
959   myGeoMin->setEnabled(isCustom);
960
961   if ( ! isCustom ) {
962     double aAngleMeshS, aGradation;
963     switch( myGeometricMesh->currentIndex() ) {
964       case DefaultGeom:
965       default:
966         aAngleMeshS = 8;
967         aGradation = 1.1;
968         break;
969       }
970     myAngleMeshS->setValue( aAngleMeshS );
971     myAngleMeshC->setValue( aAngleMeshS );
972     myGradation->setValue( aGradation );
973     if ( !isDouble( myGeoMin->text(), true ))
974       myGeoMin->setText("");
975     if ( !isDouble( myGeoMax->text(), true ))
976       myGeoMax->setText("");
977     //  hphy_flag = 0 and hgeo_flag = 0 is not allowed (spec)
978     if ( myPhysicalMesh->currentIndex() == DefaultSize ) {
979       myPhysicalMesh->setCurrentIndex( PhysicalUserDefined );
980       onPhysicalMeshChanged();
981     }
982   }
983 }
984
985 void BLSURFPluginGUI_HypothesisCreator::onAddOption()
986 {
987   QMenu* menu = (QMenu*)sender();
988   // fill popup with option names
989   menu->clear();
990   if ( myOptions.operator->() ) {
991     for ( int i = 0, nb = myOptions->length(); i < nb; ++i ) {
992       QString name_value = myOptions[i].in();
993       QString name = name_value.split( ":", QString::KeepEmptyParts )[0];
994       menu->addAction( name );
995     }
996   }
997 }
998
999 void BLSURFPluginGUI_HypothesisCreator::onOptionChosenInPopup( QAction* a )
1000 {
1001   myOptionTable->setFocus();
1002   QMenu* menu = (QMenu*)( a->parent() );
1003   
1004   int idx = menu->actions().indexOf( a );
1005   QString idStr = QString("%1").arg( idx );
1006   QString option = myOptions[idx].in();
1007   QString optionName = option.split( ":", QString::KeepEmptyParts )[0];
1008
1009   // look for a row with optionName
1010   int row = 0, nbRows = myOptionTable->rowCount();
1011   for ( ; row < nbRows; ++row )
1012     if ( myOptionTable->item( row, OPTION_ID_COLUMN )->text() == idStr )
1013       break;
1014   // add a row if not found
1015   if ( row == nbRows ) {
1016     myOptionTable->setRowCount( row+1 );
1017     myOptionTable->setItem( row, OPTION_ID_COLUMN, new QTableWidgetItem( idStr ) );
1018     myOptionTable->item( row, OPTION_ID_COLUMN )->setFlags( 0 );
1019     myOptionTable->setItem( row, OPTION_NAME_COLUMN, new QTableWidgetItem( optionName ) );
1020     myOptionTable->item( row, OPTION_NAME_COLUMN )->setFlags( 0 );
1021     myOptionTable->setItem( row, OPTION_VALUE_COLUMN, new QTableWidgetItem( "" ) );
1022     myOptionTable->item( row, OPTION_VALUE_COLUMN )->setFlags( Qt::ItemIsSelectable | 
1023                                                                Qt::ItemIsEditable   | 
1024                                                                Qt::ItemIsEnabled );
1025     myOptionTable->resizeColumnToContents( OPTION_NAME_COLUMN );
1026   }
1027   myOptionTable->clearSelection();
1028   myOptionTable->scrollToItem( myOptionTable->item( row, OPTION_VALUE_COLUMN ) );
1029   //myOptionTable->item( row, OPTION_VALUE_COLUMN )->setSelected( true );
1030   myOptionTable->setCurrentCell( row, OPTION_VALUE_COLUMN );
1031   //myOptionTable->openPersistentEditor( myOptionTable->item( row, OPTION_VALUE_COLUMN ) );
1032 }
1033     
1034 void BLSURFPluginGUI_HypothesisCreator::onDeleteOption()
1035 {
1036   // clear option values and remember selected row
1037   QList<int> selectedRows;
1038   QList<QTableWidgetItem*> selected = myOptionTable->selectedItems(); 
1039   QTableWidgetItem* item;
1040   foreach( item, selected ) {
1041     int row = item->row();
1042     if ( !selectedRows.contains( row ) ) {
1043       selectedRows.append( row );
1044       int id = myOptionTable->item( row, OPTION_ID_COLUMN )->text().toInt();
1045       if ( id >= 0 && id < myOptions->length() )
1046         myOptions[ id ] = myOptionTable->item( row, OPTION_NAME_COLUMN )->text().toLatin1().constData();
1047     }
1048   }
1049   qSort( selectedRows );
1050   QListIterator<int> it( selectedRows );
1051   it.toBack();
1052   while ( it.hasPrevious() )
1053     myOptionTable->removeRow( it.previous() );
1054 }
1055
1056 // **********************
1057 // *** BEGIN SIZE MAP ***
1058 // **********************
1059
1060
1061 void BLSURFPluginGUI_HypothesisCreator::onRemoveMap()
1062 {
1063   cout<<"BLSURFPluginGUI_HypothesisCreator::onRemoveMap()"<<endl;
1064   QList<int> selectedRows;
1065   QList<QTableWidgetItem*> selected = mySizeMapTable->selectedItems();
1066   QTableWidgetItem* item;
1067   int row;
1068   foreach( item, selected ) {
1069     row = item->row();
1070     if ( !selectedRows.contains( row ) ) 
1071       selectedRows.append( row );
1072   }
1073
1074   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1075
1076   qSort( selectedRows );
1077   QListIterator<int> it( selectedRows );
1078   it.toBack();
1079   while ( it.hasPrevious() ) {
1080       row = it.previous();
1081       cout << "delete row #"<< row <<endl;
1082       QString entry = mySizeMapTable->item(row,SMP_ENTRY_COLUMN)->text();
1083       if (that->mySMPMap.contains(entry))
1084         that->mySMPMap[entry] = "__TO_DELETE__";
1085       if (that->mySMPShapeTypeMap.contains(entry))
1086         that->mySMPShapeTypeMap.remove(entry);
1087       mySizeMapTable->removeRow(row );
1088   }
1089   mySizeMapTable->resizeColumnToContents(SMP_NAME_COLUMN);
1090   mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
1091 }
1092
1093 void BLSURFPluginGUI_HypothesisCreator::onSetSizeMap(int row,int col)
1094 {
1095   cout<<"BLSURFPluginGUI_HypothesisCreator::onSetSizeMap("<< row << "," << col << ")"<<endl;
1096   if (col == SMP_SIZEMAP_COLUMN) {
1097     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1098     QString entry   = that->mySizeMapTable->item(row, SMP_ENTRY_COLUMN)->text();
1099     QString sizeMap = that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->text().trimmed();
1100     cout << "entry: " << entry.toStdString() << ", sizeMap: " << sizeMap.toStdString() << endl;
1101     if (not that->mySMPShapeTypeMap.contains(entry))
1102       return;
1103     if (that->mySMPMap.contains(entry))
1104       if (that->mySMPMap[entry] == sizeMap)
1105         return;
1106     QColor* bgColor = new QColor("white");
1107     QColor* fgColor = new QColor("black");
1108     if (not sizeMap.isEmpty()) {
1109       that->mySMPMap[entry] = sizeMap;
1110       if (not sizeMapValidationFromRow(row)) {
1111         bgColor->setRgb(255,0,0);
1112         fgColor->setRgb(255,255,255);
1113       }
1114     }
1115     else {
1116       cout << "Size map empty: reverse to precedent value" << endl;
1117       that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setText(that->mySMPMap[entry]);
1118     }
1119     that->mySizeMapTable->item(row, SMP_NAME_COLUMN)->setBackground(QBrush(*bgColor));
1120     that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setBackground(QBrush(*bgColor));
1121     that->mySizeMapTable->item(row, SMP_NAME_COLUMN)->setForeground(QBrush(*fgColor));
1122     that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setForeground(QBrush(*fgColor));
1123     mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
1124   }
1125 }
1126
1127 void BLSURFPluginGUI_HypothesisCreator::onAddMapOnSurface()
1128 {
1129  insertElementType(TopAbs_FACE);
1130 }
1131
1132 void BLSURFPluginGUI_HypothesisCreator::onAddMapOnEdge()
1133 {
1134  insertElementType(TopAbs_EDGE);
1135 }
1136
1137 void BLSURFPluginGUI_HypothesisCreator::onAddMapOnPoint()
1138 {
1139  insertElementType(TopAbs_VERTEX);
1140 }
1141
1142 void BLSURFPluginGUI_HypothesisCreator::insertElementType(TopAbs_ShapeEnum typeShapeAsked)
1143 {
1144   cout<<"BLSURFPluginGUI_HypothesisCreator::insertElementType()"<<endl;
1145
1146   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
1147     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis());
1148
1149   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1150
1151   TopoDS_Shape S;
1152   string entry, shapeName;
1153 //  LightApp_SelectionMgr* aSel = GeomToolSelected->selectionMgr();
1154
1155   SALOME_ListIO ListSelectedObjects;
1156   aSel->selectedObjects(ListSelectedObjects, NULL, false );
1157   if (!ListSelectedObjects.IsEmpty())
1158   {
1159     SALOME_ListIteratorOfListIO Object_It(ListSelectedObjects);
1160     for (; Object_It.More(); Object_It.Next()) 
1161     {
1162       Handle(SALOME_InteractiveObject) anObject = Object_It.Value();
1163       entry     = GeomToolSelected->getEntryOfObject(anObject);
1164       shapeName = anObject->getName();
1165       S         = GeomToolSelected->entryToShape(entry);
1166       if ((! S.IsNull()) && (S.ShapeType() == typeShapeAsked)) 
1167       { 
1168         mySizeMapTable->setFocus();
1169         QString shapeEntry;
1170         shapeEntry = QString::fromStdString(entry);
1171         double phySize = h->GetPhySize();
1172         std::ostringstream oss;
1173         oss << phySize;
1174         QString sizeMap;
1175         sizeMap  = QString::fromStdString(oss.str());
1176         if (that->mySMPMap.contains(shapeEntry)) {
1177           if (that->mySMPMap[shapeEntry] != "__TO_DELETE__") {
1178             MESSAGE("Size map for shape with name(entry): "<< shapeName << "(" << entry << ")");
1179             break;
1180           }
1181         }
1182         that->mySMPMap[shapeEntry] = sizeMap;
1183         that->mySMPShapeTypeMap[shapeEntry] = typeShapeAsked; 
1184         int row = mySizeMapTable->rowCount() ;
1185         mySizeMapTable->setRowCount( row+1 );
1186         mySizeMapTable->setItem( row, SMP_ENTRY_COLUMN, new QTableWidgetItem( shapeEntry ) );
1187         mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->setFlags( 0 );
1188         mySizeMapTable->setItem( row, SMP_NAME_COLUMN, new QTableWidgetItem( QString::fromStdString(shapeName) ) );
1189         mySizeMapTable->item( row, SMP_NAME_COLUMN )->setFlags( 0 );
1190         mySizeMapTable->setItem( row, SMP_SIZEMAP_COLUMN, new QTableWidgetItem( sizeMap ) );
1191         mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->setFlags( Qt::ItemIsSelectable |Qt::ItemIsEditable   |Qt::ItemIsEnabled );
1192         mySizeMapTable->resizeColumnToContents( SMP_NAME_COLUMN );
1193         mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
1194         mySizeMapTable->clearSelection();
1195         mySizeMapTable->scrollToItem( mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN ) );
1196
1197         if ( myPhysicalMesh->currentIndex() != SizeMap ) {
1198           myPhysicalMesh->setCurrentIndex( SizeMap );
1199           onPhysicalMeshChanged();
1200         }
1201       }
1202     }
1203   }
1204 }
1205
1206 bool BLSURFPluginGUI_HypothesisCreator::sizeMapsValidation()
1207 {
1208   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapsValidation()"<<endl;
1209   int row = 0, nbRows = mySizeMapTable->rowCount();
1210   for ( ; row < nbRows; ++row )
1211     if (not sizeMapValidationFromRow(row))
1212       return false;
1213   return true;
1214 }
1215
1216 bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow(int myRow, bool displayError)
1217 {
1218   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow()"<<endl;
1219   QString myEntry   = mySizeMapTable->item( myRow, SMP_ENTRY_COLUMN )->text();
1220   bool res = sizeMapValidationFromEntry(myEntry,displayError);
1221   mySizeMapTable->setFocus();
1222   return res; 
1223 }
1224
1225 bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEntry, bool displayError)
1226 {
1227   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry()"<<endl;
1228
1229   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1230      
1231   if (not that->mySMPMap.contains(myEntry)) {
1232     cout<<"Geometry with entry "<<myEntry.toStdString()<<" was not found."<<endl;
1233     return false;
1234   }
1235   if (not that->mySMPShapeTypeMap.contains(myEntry)) {
1236     cout<<"Shape type with entry "<<myEntry.toStdString()<<" was not found."<<endl;
1237     return false;
1238   }
1239
1240   string expr;
1241
1242   if (that->mySMPMap[myEntry].startsWith("def")) {
1243     cout << "custom function" << endl;
1244     expr = that->mySMPMap[myEntry].toStdString();
1245   }
1246   else if (that->mySMPMap[myEntry].startsWith("ATTRACTOR")) {
1247     cout << "Attractor" << endl;
1248 //    if ((that->mySMPMap[myEntry].count(QRegExp("ATTRACTOR([0-9])")) != 1))
1249     if ((that->mySMPMap[myEntry].count('(') != 1) or 
1250         (that->mySMPMap[myEntry].count(')') != 1) or
1251         (that->mySMPMap[myEntry].count(';') != 4) or
1252         (that->mySMPMap[myEntry].size() == 15)){
1253       if (displayError)
1254         SUIT_MessageBox::warning( dlg(),"Definition of attractor : Error" ,"An attractor is defined with the following pattern: ATTRACTOR(xa;ya;za;a;b)" );
1255       return false;
1256     }
1257     return true;
1258   }
1259   else {
1260     // case size map is empty
1261     if (that->mySMPMap[myEntry].isEmpty()) {
1262       if (displayError)
1263         SUIT_MessageBox::warning( dlg(),"Definition of size map : Error" , "Size map can't be empty");
1264       return false;
1265     }
1266
1267     if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_FACE)
1268       expr = "def f(u,v) : return " + that->mySMPMap[myEntry].toStdString();
1269     else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_EDGE)
1270       expr = "def f(t) : return " + that->mySMPMap[myEntry].toStdString();
1271     else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_VERTEX)
1272       expr = "def f() : return " + that->mySMPMap[myEntry].toStdString();
1273   }
1274   //assert(Py_IsInitialized());
1275   if (not Py_IsInitialized())
1276     throw ("Erreur: Python interpreter is not initialized");
1277   PyGILState_STATE gstate;
1278   gstate = PyGILState_Ensure();
1279  
1280   PyObject * obj = NULL;   
1281   PyObject* new_stderr = NULL;   
1282   string  err_description="";
1283   obj= PyRun_String(expr.c_str(), Py_file_input, main_dict, NULL);
1284   if (obj == NULL){
1285     fflush(stderr);
1286     err_description="";
1287     new_stderr=newPyStdOut(err_description);
1288     PySys_SetObject("stderr", new_stderr);
1289     PyErr_Print();
1290     PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
1291     Py_DECREF(new_stderr);
1292     if (displayError)
1293       SUIT_MessageBox::warning( dlg(),"Definition of Python Function : Error" ,err_description.c_str() );
1294     PyGILState_Release(gstate);
1295     return false;
1296   }
1297   Py_DECREF(obj);
1298    
1299   PyObject * func = NULL;
1300   func = PyObject_GetAttrString(main_mod, "f");
1301   if ( func == NULL){
1302     fflush(stderr);                                                                            
1303     err_description="";                                                                        
1304     new_stderr=newPyStdOut(err_description);                                         
1305     PySys_SetObject("stderr", new_stderr);                                                     
1306     PyErr_Print();                                                                             
1307     PySys_SetObject("stderr", PySys_GetObject("__stderr__"));                                  
1308     Py_DECREF(new_stderr);
1309     if (displayError)
1310       SUIT_MessageBox::warning( dlg(),"Python Error" ,err_description.c_str() );
1311     PyGILState_Release(gstate);
1312     return false;
1313   }
1314
1315   PyGILState_Release(gstate);
1316
1317   cout<<"SizeMap expression "<<expr<<" is valid"<<endl;
1318
1319   return true;
1320 }
1321
1322 /*
1323 void BLSURFPluginGUI_HypothesisCreator::OnEditMapFunction(QModelIndex* index) {
1324   int myRow = index->row();
1325   int myColumn = index->column();
1326   
1327   if (myColumn == 2){
1328      if (!myEditor) {
1329          myEditor = new BLSURFPluginGUI_MapFunctionEditor(sizeMapModel->item(myRow,0)->text());
1330          connect(myEditor, SIGNAL(FunctionEntered(QString)), this, SLOT(FunctionLightValidation(QString)));
1331      }
1332      myEditor->exec();
1333 //      myEditor->show();
1334 //      myEditor->raise();
1335 //      myEditor->activateWindow();
1336      
1337
1338 //     BLSURFPluginGUI_MapFunctionEditor* myEditor = new BLSURFPluginGUI_MapFunctionEditor(sizeMapModel->item(myRow,0)->text());
1339 //     myEditor->exec();
1340      QString myFunction = myEditor->GetFunctionText();
1341      // FIN RECUPERATION FONCTION
1342      
1343      if (! myFunction.isEmpty()) {
1344      
1345      // MAJ DE LA MAP
1346      
1347      BLSURFPlugin::BLSURFPlugin_Hypothesis_var h = 
1348        BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis());
1349
1350 //     h->SetSizeMapEntry(sizeMapModel->item(myRow,1)->text().toLatin1().constData(),
1351 //                        item->text().toLatin1().constData());
1352      h->SetSizeMapEntry(sizeMapModel->item(myRow,1)->text().toLatin1().constData(),
1353                         myFunction.toLatin1().constData());
1354      // FIN MAJ DE LA MAP
1355      }
1356   }
1357 }*/
1358  
1359 QString BLSURFPluginGUI_HypothesisCreator::caption() const
1360 {
1361   return tr( "BLSURF_TITLE" );
1362 }
1363
1364 QPixmap BLSURFPluginGUI_HypothesisCreator::icon() const
1365 {
1366   return SUIT_Session::session()->resourceMgr()->loadPixmap( "BLSURFPlugin", tr( "ICON_DLG_BLSURF_PARAMETERS") );
1367 }
1368
1369 QString BLSURFPluginGUI_HypothesisCreator::type() const
1370 {
1371   return tr( "BLSURF_HYPOTHESIS" );
1372 }
1373
1374 QString BLSURFPluginGUI_HypothesisCreator::helpPage() const
1375 {
1376   return "blsurf_hypo_page.html";
1377 }
1378
1379 LightApp_SelectionMgr* BLSURFPluginGUI_HypothesisCreator::selectionMgr()
1380 {
1381
1382   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1383   if( anApp )
1384     return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
1385   else
1386     return 0;
1387 }