]> SALOME platform Git repositories - plugins/blsurfplugin.git/blob - src/GUI/BLSURFPluginGUI_HypothesisCreator.cxx
Salome HOME
First version of SizeMap for BLSurf :
[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   /* Initialize the Python interpreter */
215   assert(Py_IsInitialized());
216   PyGILState_STATE gstate;
217   gstate = PyGILState_Ensure();
218   
219   main_mod = NULL;
220   main_mod = PyImport_AddModule("__main__");
221   
222   main_dict = NULL;
223   main_dict = PyModule_GetDict(main_mod);
224   
225   PyRun_SimpleString("from math import *");
226   PyGILState_Release(gstate);
227 }
228
229 BLSURFPluginGUI_HypothesisCreator::~BLSURFPluginGUI_HypothesisCreator()
230 {
231 }
232
233 namespace {
234   inline bool isDouble( const QString& theText, const bool emptyOK=false ) {
235     QString str = theText.trimmed();
236     bool isOk = true;
237     if ( !str.isEmpty() )
238       str.toDouble(&isOk);
239     else
240       isOk = emptyOK;
241     return isOk;
242   }
243 }
244
245 bool BLSURFPluginGUI_HypothesisCreator::checkParams() const
246 {
247   cout << "BLSURFPluginGUI_HypothesisCreator::checkParams" << endl; 
248   bool ok = true;
249   if ( !isDouble( myPhySize->text(), false )) {
250     if ( myPhySize->text().isEmpty() )
251       myPhySize->setText(tr("OBLIGATORY_VALUE"));
252     myPhySize->selectAll();
253     ok = false;
254   }
255   if ( !isDouble( myPhyMin->text(), true )) {
256     myPhyMin->selectAll();
257     ok = false;
258   }
259   if ( !isDouble( myPhyMax->text(), true )) {
260     myPhyMax->selectAll();
261     ok = false;
262   }
263   if ( !isDouble( myGeoMin->text(), true )) {
264     myGeoMin->selectAll();
265     ok = false;
266   }
267   if ( !isDouble( myGeoMin->text(), true )) {
268     myGeoMin->selectAll();
269     ok = false;
270   }
271   if ( ok )
272   {
273     myOptionTable->setFocus();
274     QApplication::instance()->processEvents();
275
276     BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
277       BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
278
279     int row = 0, nbRows = myOptionTable->rowCount();
280     for ( ; row < nbRows; ++row )
281     {
282       QString name  = myOptionTable->item( row, OPTION_NAME_COLUMN )->text();
283       QString value = myOptionTable->item( row, OPTION_VALUE_COLUMN )->text().trimmed();
284       if ( !value.isEmpty() ) {
285         try {
286           h->SetOptionValue( name.toLatin1().constData(), value.toLatin1().constData() );
287         }
288         catch ( const SALOME::SALOME_Exception& ex )
289         {
290           SUIT_MessageBox::critical( dlg(),
291                                      tr("SMESH_ERROR"),
292                                      ex.details.text.in() );
293           ok = false;
294         }
295       }
296     }
297     h->SetOptionValues( myOptions ); // restore values
298   }
299
300   // SizeMap
301   if ( ok )
302   {
303     mySizeMapTable->setFocus();
304     QApplication::instance()->processEvents();
305
306     BLSURFPlugin::BLSURFPlugin_Hypothesis_var h = BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
307     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
308
309     int row = 0, nbRows = mySizeMapTable->rowCount();
310     for ( ; row < nbRows; ++row )
311     {
312       QString entry   = mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->text();
313       QString sizeMap = mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->text().trimmed();
314       if ( !sizeMap.isEmpty() ) {
315         if (that->sizeMapValidationFromRow(row))
316         {
317           try {
318             const char* e = entry.toLatin1().constData();
319             const char* s = that->mySMPMap[entry].toLatin1().constData();
320             h->SetSizeMapEntry( e, s );
321           }
322           catch ( const SALOME::SALOME_Exception& ex )
323           {
324             SUIT_MessageBox::critical( dlg(),
325                                        tr("SMESH_ERROR"),
326                                        ex.details.text.in() );
327             ok = false;
328           }
329         }
330         else {
331           ok = false;
332         }
333       }
334     }
335   }
336
337   return ok;
338 }
339
340 QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
341 {
342   cout << "BLSURFPluginGUI_HypothesisCreator::buildFrame" << endl; 
343   QFrame* fr = new QFrame( 0 );
344   QVBoxLayout* lay = new QVBoxLayout( fr );
345   lay->setMargin( 5 );
346   lay->setSpacing( 0 );
347
348   // tab
349   QTabWidget* tab = new QTabWidget( fr );
350   tab->setTabShape( QTabWidget::Rounded );
351   tab->setTabPosition( QTabWidget::North );
352   lay->addWidget( tab );
353
354   // basic parameters
355   myStdGroup = new QWidget();
356   QGridLayout* aStdLayout = new QGridLayout( myStdGroup );
357   aStdLayout->setSpacing( 6 );
358   aStdLayout->setMargin( 11 );
359
360   int row = 0;
361   myName = 0;
362   if( isCreation() ) {
363     aStdLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), myStdGroup ), row, 0, 1, 1 );
364     myName = new QLineEdit( myStdGroup );
365     aStdLayout->addWidget( myName, row++, 1, 1, 1 );
366   }
367
368   aStdLayout->addWidget( new QLabel( tr( "BLSURF_PHY_MESH" ), myStdGroup ), row, 0, 1, 1 );
369   myPhysicalMesh = new QComboBox( myStdGroup );
370   aStdLayout->addWidget( myPhysicalMesh, row++, 1, 1, 1 );
371   QStringList physicalTypes;
372   physicalTypes << tr( "BLSURF_DEFAULT_USER" ) << tr( "BLSURF_CUSTOM_USER" ) << tr( "BLSURF_SIZE_MAP");
373   myPhysicalMesh->addItems( physicalTypes );
374
375   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HPHYDEF" ), myStdGroup), row, 0, 1, 1 );
376   myPhySize = new QLineEdit( myStdGroup );
377   aStdLayout->addWidget( myPhySize, row++, 1, 1, 1 );
378
379 #ifdef WITH_SIZE_BOUNDARIES
380   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HPHYMIN" ), myStdGroup ), row, 0, 1, 1 );
381   myPhyMin = new QLineEdit( myStdGroup );
382   aStdLayout->addWidget( myPhyMin, row++, 1, 1, 1 );
383
384   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HPHYMAX" ), myStdGroup ), row, 0, 1, 1 );
385   myPhyMax = new QLineEdit( myStdGroup );
386   aStdLayout->addWidget( myPhyMax, row++, 1, 1, 1 );
387 #endif
388
389   aStdLayout->addWidget( new QLabel( tr( "BLSURF_GEOM_MESH" ), myStdGroup ), row, 0, 1, 1 );
390   myGeometricMesh = new QComboBox( myStdGroup );
391   aStdLayout->addWidget( myGeometricMesh, row++, 1, 1, 1 );
392   QStringList types;
393   types << tr( "BLSURF_DEFAULT_GEOM" ) << tr( "BLSURF_CUSTOM_GEOM" );
394   myGeometricMesh->addItems( types );
395
396   aStdLayout->addWidget( new QLabel( tr( "BLSURF_ANGLE_MESH_S" ), myStdGroup ), row, 0, 1, 1 );
397   myAngleMeshS = new QtxDoubleSpinBox( myStdGroup );
398   aStdLayout->addWidget( myAngleMeshS, row++, 1, 1, 1 );
399   myAngleMeshS->setMinimum( 0 );
400   myAngleMeshS->setMaximum( 16 );
401   myAngleMeshS->setSingleStep( 0.5 );
402   
403   aStdLayout->addWidget( new QLabel( tr( "BLSURF_ANGLE_MESH_C" ), myStdGroup ), row, 0, 1, 1 );
404   myAngleMeshC = new QtxDoubleSpinBox( myStdGroup );
405   aStdLayout->addWidget( myAngleMeshC, row++, 1, 1, 1 );
406   myAngleMeshC->setMinimum( 0 );
407   myAngleMeshC->setMaximum( 16 );
408   myAngleMeshC->setSingleStep( 0.5 );
409   
410   aStdLayout->addWidget( new QLabel( tr( "BLSURF_GRADATION" ), myStdGroup ), row, 0, 1, 1 );
411   myGradation = new QtxDoubleSpinBox( myStdGroup );
412   aStdLayout->addWidget( myGradation, row++, 1, 1, 1 );
413   myGradation->setMinimum( 1.1 );
414   myGradation->setMaximum( 2.5 );
415   myGradation->setSingleStep( 0.1 );
416
417 #ifdef WITH_SIZE_BOUNDARIES
418   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HGEOMIN" ), myStdGroup ), row, 0, 1, 1 );
419   myGeoMin = new QLineEdit( myStdGroup );
420   aStdLayout->addWidget( myGeoMin, row++, 1, 1, 1 );
421
422   aStdLayout->addWidget( new QLabel( tr( "BLSURF_HGEOMAX" ), myStdGroup ), row, 0, 1, 1 );
423   myGeoMax = new QLineEdit( myStdGroup );
424   aStdLayout->addWidget( myGeoMax, row++, 1, 1, 1 );
425 #endif
426
427   myAllowQuadrangles = new QCheckBox( tr( "BLSURF_ALLOW_QUADRANGLES" ), myStdGroup );
428   aStdLayout->addWidget( myAllowQuadrangles, row++, 0, 1, 2 );
429
430   myDecimesh = new QCheckBox( tr( "BLSURF_DECIMESH" ), myStdGroup );
431   aStdLayout->addWidget( myDecimesh, row++, 0, 1, 2 );
432   
433   // advanced parameters
434   myAdvGroup = new QWidget();
435   QGridLayout* anAdvLayout = new QGridLayout( myAdvGroup );
436   anAdvLayout->setSpacing( 6 );
437   anAdvLayout->setMargin( 11 );
438
439   anAdvLayout->addWidget( new QLabel( tr( "BLSURF_TOPOLOGY" ), myAdvGroup ), 0, 0, 1, 1 );
440   myTopology = new QComboBox( myAdvGroup );
441   anAdvLayout->addWidget( myTopology, 0, 1, 1, 1 );
442   QStringList topologyTypes;
443   topologyTypes << tr( "BLSURF_TOPOLOGY_CAD" ) << tr( "BLSURF_TOPOLOGY_PROCESS" ) << tr( "BLSURF_TOPOLOGY_PROCESS2" );
444   myTopology->addItems( topologyTypes );
445
446   anAdvLayout->addWidget( new QLabel( tr( "BLSURF_VERBOSITY" ), myAdvGroup ), 1, 0, 1, 1 );
447   myVerbosity = new QSpinBox( myAdvGroup );
448   anAdvLayout->addWidget( myVerbosity, 1, 1, 1, 1 );
449   myVerbosity->setMinimum( 0 );
450   myVerbosity->setMaximum( 100 );
451   myVerbosity->setSingleStep( 5 );
452
453   myOptionTable = new QTableWidget( 0, NB_COLUMNS, myAdvGroup );
454   anAdvLayout->addWidget( myOptionTable, 2, 0, 3, 2 );
455   QStringList headers;
456   headers << tr( "OPTION_ID_COLUMN" ) << tr( "OPTION_NAME_COLUMN" ) << tr( "OPTION_VALUE_COLUMN" );
457   myOptionTable->setHorizontalHeaderLabels( headers );
458   myOptionTable->horizontalHeader()->hideSection( OPTION_ID_COLUMN );
459   //myOptionTable->setColumnReadOnly( OPTION_NAME_COLUMN, TRUE );//////
460   //myOptionTable->setColumnReadOnly( OPTION_VALUE_COLUMN, FALSE );/////
461   myOptionTable->verticalHeader()->hide();
462   //myOptionTable->setSelectionBehavior( QAbstractItemView::SelectRows );
463
464   QPushButton* addBtn = new QPushButton( tr( "ADD_OPTION"),  myAdvGroup );
465   anAdvLayout->addWidget( addBtn, 2, 2, 1, 1 );
466   addBtn->setMenu( new QMenu() );
467
468   QPushButton* rmBtn = new QPushButton( tr( "REMOVE_OPTION"), myAdvGroup );
469   anAdvLayout->addWidget( rmBtn, 3, 2, 1, 1 );
470
471   anAdvLayout->setRowStretch( 4, 5 );
472   anAdvLayout->setColumnStretch( 1, 5 );
473
474   // Size Maps parameters
475   
476   mySmpGroup = new QWidget();
477   QGridLayout* anSmpLayout = new QGridLayout(mySmpGroup);
478
479   mySizeMapTable = new QTableWidget( 0, SMP_NB_COLUMNS, mySmpGroup );
480   anSmpLayout->addWidget(mySizeMapTable, 1, 0, 6, 1);
481   QStringList sizeMapHeaders;
482   sizeMapHeaders << tr( "SMP_ENTRY_COLUMN" )<< tr( "SMP_NAME_COLUMN" ) << tr( "SMP_SIZEMAP_COLUMN" ); 
483   mySizeMapTable->setHorizontalHeaderLabels(sizeMapHeaders);
484   mySizeMapTable->horizontalHeader()->hideSection( SMP_ENTRY_COLUMN );
485   mySizeMapTable->resizeColumnToContents(SMP_NAME_COLUMN);
486   mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
487   mySizeMapTable->setAlternatingRowColors(true);
488   mySizeMapTable->verticalHeader()->hide();
489
490   addSurfaceButton = new QPushButton(tr("BLSURF_SM_SURFACE"),mySmpGroup);
491   anSmpLayout->addWidget(addSurfaceButton, 1, 1, 1, 1);
492
493   addEdgeButton = new QPushButton(tr("BLSURF_SM_EDGE"),mySmpGroup);
494   anSmpLayout->addWidget(addEdgeButton, 2, 1, 1, 1);
495
496   addPointButton = new QPushButton(tr("BLSURF_SM_POINT"),mySmpGroup);
497   anSmpLayout->addWidget(addPointButton, 3, 1, 1, 1);
498
499   QFrame *line = new QFrame(mySmpGroup);
500   line->setFrameShape(QFrame::HLine);
501   line->setFrameShadow(QFrame::Sunken);
502   anSmpLayout->addWidget(line, 4, 1, 1, 1);
503
504   removeButton = new QPushButton(tr("BLSURF_SM_REMOVE"),mySmpGroup);
505   anSmpLayout->addWidget(removeButton, 5, 1, 1, 1);
506   
507
508   // ---
509   tab->insertTab( STD_TAB, myStdGroup, tr( "SMESH_ARGUMENTS" ) );
510   tab->insertTab( ADV_TAB, myAdvGroup, tr( "GHS3D_ADV_ARGS" ) );
511   tab->insertTab( SMP_TAB, mySmpGroup, tr( "BLSURF_SIZE_MAP" ) );
512
513   tab->setCurrentIndex( STD_TAB );
514
515   // ---
516   connect( myGeometricMesh, SIGNAL( activated( int ) ), this, SLOT( onGeometricMeshChanged() ) );
517   connect( myPhysicalMesh,  SIGNAL( activated( int ) ), this, SLOT( onPhysicalMeshChanged() ) );
518   connect( addBtn->menu(),  SIGNAL( aboutToShow() ),    this, SLOT( onAddOption() ) );
519   connect( addBtn->menu(),  SIGNAL( triggered( QAction* ) ), this, SLOT( onOptionChosenInPopup( QAction* ) ) );
520   connect( rmBtn,           SIGNAL( clicked()),         this, SLOT( onDeleteOption() ) );
521   
522   connect(addSurfaceButton, SIGNAL(clicked()), this, SLOT(onAddMapOnSurface()));
523   connect(addEdgeButton, SIGNAL(clicked()), this, SLOT(onAddMapOnEdge()));
524   connect(addPointButton, SIGNAL(clicked()), this, SLOT(onAddMapOnPoint()));
525   connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemoveMap()));
526   connect(mySizeMapTable, SIGNAL(cellChanged ( int, int  )),this,SLOT (onSetSizeMap(int,int )));
527
528   return fr;
529 }
530
531
532 void BLSURFPluginGUI_HypothesisCreator::retrieveParams() const
533 {
534   cout << "BLSURFPluginGUI_HypothesisCreator::retrieveParams" << endl; 
535   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
536
537   BlsurfHypothesisData data;
538   that->readParamsFromHypo( data );
539
540   if ( myName ) {
541     myName->setText( data.myName );
542     QFontMetrics metrics( myName->font() );
543     myName->setMinimumWidth( metrics.width( data.myName )+5 );
544   }
545   myTopology->setCurrentIndex( data.myTopology );
546   myPhysicalMesh->setCurrentIndex( data.myPhysicalMesh );
547   myPhySize->setText( data.myPhySize );
548 #ifdef WITH_SIZE_BOUNDARIES
549   myPhyMin->setText( data.myPhyMin );
550   myPhyMax->setText( data.myPhyMax );
551   myGeoMin->setText( data.myGeoMin );
552   myGeoMax->setText( data.myGeoMax );
553 #endif
554   myGeometricMesh->setCurrentIndex( data.myGeometricMesh );
555   myAngleMeshS->setValue( data.myAngleMeshS );
556   myAngleMeshC->setValue( data.myAngleMeshC );
557   myGradation->setValue( data.myGradation );
558   myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
559   myDecimesh->setChecked( data.myDecimesh );
560   myVerbosity->setValue( data.myVerbosity );
561   
562   if ( myOptions.operator->() ) {
563     printf("retrieveParams():myOptions->length()=%d\n",myOptions->length());
564     for ( int i = 0, nb = myOptions->length(); i < nb; ++i ) {
565       QString option = that->myOptions[i].in();
566       QStringList name_value = option.split( ":", QString::KeepEmptyParts );
567       if ( name_value.count() > 1 ) {
568         QString idStr = QString("%1").arg( i );
569         int row = myOptionTable->rowCount();
570         myOptionTable->setRowCount( row+1 );
571         myOptionTable->setItem( row, OPTION_ID_COLUMN, new QTableWidgetItem( idStr ) );
572         myOptionTable->item( row, OPTION_ID_COLUMN )->setFlags( 0 );
573         myOptionTable->setItem( row, OPTION_NAME_COLUMN, new QTableWidgetItem( name_value[0] ) );
574         myOptionTable->item( row, OPTION_NAME_COLUMN )->setFlags( 0 );
575         myOptionTable->setItem( row, OPTION_VALUE_COLUMN, new QTableWidgetItem( name_value[1] ) );
576         myOptionTable->item( row, OPTION_VALUE_COLUMN )->setFlags( Qt::ItemIsSelectable | 
577                                                                    Qt::ItemIsEditable   | 
578                                                                    Qt::ItemIsEnabled );
579       }
580     } 
581   }
582   myOptionTable->resizeColumnToContents( OPTION_NAME_COLUMN );
583
584   printf("retrieveParams():that->mySMPMap.size()=%d\n",that->mySMPMap.size());
585   QMapIterator<QString, QString> i(that->mySMPMap);
586   while (i.hasNext()) {
587     i.next();
588     const QString entry = i.key();
589     string shapeName = GeomToolSelected->getNameFromEntry(entry.toStdString());
590     const QString sizeMap = i.value();
591     int row = mySizeMapTable->rowCount();
592     mySizeMapTable->setRowCount( row+1 );
593     mySizeMapTable->setItem( row, SMP_ENTRY_COLUMN, new QTableWidgetItem( entry ) );
594     mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->setFlags( 0 );
595     mySizeMapTable->setItem( row, SMP_NAME_COLUMN, new QTableWidgetItem( QString::fromStdString(shapeName) ) );
596     mySizeMapTable->item( row, SMP_NAME_COLUMN )->setFlags( 0 );
597     mySizeMapTable->setItem( row, SMP_SIZEMAP_COLUMN, new QTableWidgetItem( sizeMap ) );
598     mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->setFlags( Qt::ItemIsSelectable |
599                                                                Qt::ItemIsEditable   |
600                                                                Qt::ItemIsEnabled );
601     }
602   
603   mySizeMapTable->resizeColumnToContents( SMP_NAME_COLUMN );
604   mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
605
606   // update widgets
607   that->onPhysicalMeshChanged();
608   that->onGeometricMeshChanged();
609 }
610
611 QString BLSURFPluginGUI_HypothesisCreator::storeParams() const
612 {
613   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
614
615   BlsurfHypothesisData data;
616   QString guiHyp = that->readParamsFromWidgets( data );
617   that->storeParamsToHypo( data );
618
619   //cout<<"BLSURFPluginGUI_HypothesisCreator::storeParams(), guiHyp: "<<endl<<guiHyp.toStdString()<<endl;
620   return guiHyp;
621 }
622
623 bool BLSURFPluginGUI_HypothesisCreator::readParamsFromHypo( BlsurfHypothesisData& h_data ) const
624 {
625   cout << "BLSURFPluginGUI_HypothesisCreator::readParamsFromHypo" << endl; 
626   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
627     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis() );
628
629   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
630   h_data.myName = isCreation() && data ? hypName() : "";
631
632   h_data.myTopology         = (int) h->GetTopology();
633   h_data.myPhysicalMesh     = (int) h->GetPhysicalMesh();
634   h_data.myPhySize          = QString::number( h->GetPhySize() );
635   h_data.myGeometricMesh    = (int) h->GetGeometricMesh();
636   h_data.myAngleMeshS       = h->GetAngleMeshS();
637   h_data.myAngleMeshC       = h->GetAngleMeshC();
638   h_data.myGradation        = h->GetGradation();
639   h_data.myAllowQuadrangles = h->GetQuadAllowed();
640   h_data.myDecimesh         = h->GetDecimesh();
641   h_data.myVerbosity        = h->GetVerbosity();
642
643 #ifdef WITH_SIZE_BOUNDARIES
644   double PhyMin = h->GetPhyMin();
645   double PhyMax = h->GetPhyMax();
646   double GeoMin = h->GetGeoMin();
647   double GeoMax = h->GetGeoMax();
648   if ( PhyMin > 0 )
649   h_data.myPhyMin = PhyMin > 0 ? QString::number( h->GetPhyMin() ) : QString("");
650   h_data.myPhyMax = PhyMax > 0 ? QString::number( h->GetPhyMax() ) : QString("");
651   h_data.myGeoMin = GeoMin > 0 ? QString::number( h->GetGeoMin() ) : QString("");
652   h_data.myGeoMax = GeoMax > 0 ? QString::number( h->GetGeoMax() ) : QString("");
653 #endif
654
655   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
656   that->myOptions = h->GetOptionValues();
657
658
659   BLSURFPlugin::string_array_var mySMPString = h->GetSizeMapEntries();
660   
661   cout << "mySMPString->length() = " << mySMPString->length() << endl;
662   that->mySMPMap.clear();       
663
664   TopoDS_Shape S;
665
666   for ( int i = 0;i<mySMPString->length(); ++i ) {
667      QString sizemaps_line =  mySMPString[i].in();
668      QStringList entry_sm = sizemaps_line.split( "|", QString::KeepEmptyParts );
669      if ( entry_sm.count() > 1 ) {
670        string fullSizeMap = entry_sm[1].toStdString();
671        int pos = fullSizeMap.find("return")+7;
672        QString sizeMap = QString::fromStdString(fullSizeMap.substr(pos, fullSizeMap.size()-pos));
673        that->mySMPMap[entry_sm[0]] = sizeMap;
674        that->mySMPShapeTypeMap[entry_sm[0]] = GeomToolSelected->entryToShape(entry_sm[0].toStdString()).ShapeType();
675        MESSAGE("mySMPMap[" << entry_sm[0].toStdString() << "] = " << sizeMap.toStdString());
676        MESSAGE("mySMPShapeTypeMap[" << entry_sm[0].toStdString() << "] = " << that->mySMPShapeTypeMap[entry_sm[0]]);
677       }
678     }
679
680   return true;
681 }
682
683 bool BLSURFPluginGUI_HypothesisCreator::storeParamsToHypo( const BlsurfHypothesisData& h_data ) const
684 {
685   cout << "BLSURFPluginGUI_HypothesisCreator::storeParamsToHypo" << endl; 
686   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
687     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( hypothesis() );
688
689   bool ok = true;
690   try
691   {
692     if( isCreation() )
693       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().constData() );
694
695     if ( h->GetTopology() != h_data.myTopology ) // avoid duplication of DumpPython commands
696       h->SetTopology( (int) h_data.myTopology );
697     if ( h->GetPhysicalMesh() != h_data.myPhysicalMesh )
698       h->SetPhysicalMesh( (int) h_data.myPhysicalMesh );
699     if ( h->GetGeometricMesh() != (int) h_data.myGeometricMesh )
700       h->SetGeometricMesh( (int) h_data.myGeometricMesh );
701     if ( h->GetGradation() !=  h_data.myGradation )
702       h->SetGradation( h_data.myGradation );
703     if ( h->GetQuadAllowed() != h_data.myAllowQuadrangles )
704       h->SetQuadAllowed( h_data.myAllowQuadrangles );
705     if ( h->GetDecimesh() != h_data.myDecimesh )
706       h->SetDecimesh( h_data.myDecimesh );
707     if ( h->GetVerbosity() != h_data.myVerbosity )
708       h->SetVerbosity( h_data.myVerbosity );
709
710     if( ((int) h_data.myPhysicalMesh == PhysicalUserDefined)||((int) h_data.myPhysicalMesh == SizeMap) ) {
711       if ( h->GetPhySize() != h_data.myPhySize.toDouble() )
712         h->SetPhySize( h_data.myPhySize.toDouble() );
713     }
714     if( (int) h_data.myGeometricMesh == UserDefined ) {
715       if ( h->GetAngleMeshS() != h_data.myAngleMeshS )
716         h->SetAngleMeshS( h_data.myAngleMeshS );
717       if ( h->GetAngleMeshC() != h_data.myAngleMeshC )
718         h->SetAngleMeshC( h_data.myAngleMeshC );
719     }
720 #ifdef WITH_SIZE_BOUNDARIES
721     if ( !isDouble( h_data.myPhyMin ))
722       h->SetPhyMin( -1 );
723     else if ( h->GetPhyMin() != h_data.myPhyMin.toDouble() )
724       h->SetPhyMin( h_data.myPhyMin.toDouble() );
725     if ( !isDouble( h_data.myPhyMax ))
726       h->SetPhyMax( -1 );
727     else if ( h->GetPhyMax() != h_data.myPhyMax.toDouble() )
728       h->SetPhyMax( h_data.myPhyMax.toDouble() );
729     if ( !isDouble( h_data.myGeoMin ))
730       h->SetGeoMin( -1 );
731     else if ( h->GetGeoMin() != h_data.myGeoMin.toDouble() )
732       h->SetGeoMin( h_data.myGeoMin.toDouble() );
733     if ( !isDouble( h_data.myGeoMax ))
734       h->SetGeoMax( -1 );
735     else if ( h->GetGeoMax() != h_data.myGeoMax.toDouble() )
736       h->SetGeoMax( h_data.myGeoMax.toDouble() );
737 #endif
738
739     //printf("storeParamsToHypo():myOptions->length()=%d\n",myOptions->length());
740     h->SetOptionValues( myOptions ); // is set in checkParams()
741
742     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
743     //printf("storeParamsToHypo():that->mySMPMap.size()=%d\n",that->mySMPMap.size());
744     QMapIterator<QString,QString> i(that->mySMPMap);
745     // Reset the size maps in engine
746     // h->ClearSizeMaps();
747     // Iterate over each size map
748     while (i.hasNext()) {
749       i.next();
750       const QString entry = i.key();
751       const QString sizeMap = i.value();
752
753       if (sizeMap == "__TO_DELETE__") {
754         cout << "Delete entry " << entry.toStdString() << " from engine" << endl;
755         h->UnsetEntry(entry.toLatin1().constData());
756         ok = false;
757         break;
758       }
759
760       QString fullSizeMap;
761       fullSizeMap = QString("");
762       if (that->mySMPShapeTypeMap[entry]  == TopAbs_FACE)
763         fullSizeMap = QString("def f(u,v): return ") + sizeMap;
764       else if (that->mySMPShapeTypeMap[entry]  == TopAbs_EDGE)
765         fullSizeMap = QString("def f(t): return ") + sizeMap;
766       else if (that->mySMPShapeTypeMap[entry] == TopAbs_VERTEX)
767         fullSizeMap = QString("def f(): return ") + sizeMap;
768
769       cout << "SetSizeMapEntry("<<entry.toStdString()<<") = " <<fullSizeMap.toStdString() << endl;
770       h->SetSizeMapEntry( entry.toLatin1().constData(), fullSizeMap.toLatin1().constData() );
771     }
772   }
773   catch(const SALOME::SALOME_Exception& ex)
774   {
775     SalomeApp_Tools::QtCatchCorbaException(ex);
776     ok = false;
777   }
778   return ok;
779 }
780
781 QString BLSURFPluginGUI_HypothesisCreator::readParamsFromWidgets( BlsurfHypothesisData& h_data ) const
782 {
783   cout << "BLSURFPluginGUI_HypothesisCreator::readParamsFromWidgets" << endl; 
784   h_data.myName             = myName ? myName->text() : "";
785   h_data.myTopology         = myTopology->currentIndex();
786   h_data.myPhysicalMesh     = myPhysicalMesh->currentIndex();
787   h_data.myPhySize          = myPhySize->text();
788 #ifdef WITH_SIZE_BOUNDARIES
789   h_data.myPhyMin           = myPhyMin->text();
790   h_data.myPhyMax           = myPhyMax->text();
791   h_data.myGeoMin           = myGeoMin->text();
792   h_data.myGeoMax           = myGeoMax->text();
793 #endif
794   h_data.myGeometricMesh    = myGeometricMesh->currentIndex();
795   h_data.myAngleMeshS       = myAngleMeshS->value();
796   h_data.myAngleMeshC       = myAngleMeshC->value();
797   h_data.myGradation        = myGradation->value();
798   h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
799   h_data.myDecimesh         = myDecimesh->isChecked();
800   h_data.myVerbosity        = myVerbosity->value();
801
802   QString guiHyp;
803   guiHyp += tr("BLSURF_TOPOLOGY") + " = " + QString::number( h_data.myTopology ) + "; ";
804   guiHyp += tr("BLSURF_PHY_MESH") + " = " + QString::number( h_data.myPhysicalMesh ) + "; ";
805   guiHyp += tr("BLSURF_HPHYDEF") + " = " + h_data.myPhySize + "; ";
806   guiHyp += tr("BLSURF_GEOM_MESH") + " = " + QString::number( h_data.myGeometricMesh ) + "; ";
807   guiHyp += tr("BLSURF_ANGLE_MESH_S") + " = " + QString::number( h_data.myAngleMeshS ) + "; ";
808   guiHyp += tr("BLSURF_GRADATION") + " = " + QString::number( h_data.myGradation ) + "; ";
809   guiHyp += tr("BLSURF_ALLOW_QUADRANGLES") + " = " + QString(h_data.myAllowQuadrangles ? "yes" : "no") + "; ";
810   guiHyp += tr("BLSURF_DECIMESH") + " = " + QString(h_data.myDecimesh ? "yes" : "no") + "; ";
811 #ifdef WITH_SIZE_BOUNDARIES
812   if ( isDouble( h_data.myPhyMin )) guiHyp += "hphymin = " + h_data.myPhyMin + "; ";
813   if ( isDouble( h_data.myPhyMax )) guiHyp += "hphymax = " + h_data.myPhyMax + "; ";
814   if ( isDouble( h_data.myGeoMin )) guiHyp += "hgeomin = " + h_data.myGeoMin + "; ";
815   if ( isDouble( h_data.myGeoMax )) guiHyp += "hgeomax = " + h_data.myGeoMax + "; ";
816 #endif
817
818   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
819   int row = 0, nbRows = myOptionTable->rowCount();
820   for ( ; row < nbRows; ++row )
821   {
822     int id = myOptionTable->item( row, OPTION_ID_COLUMN )->text().toInt();
823     if ( id >= 0 && id < myOptions->length() )
824     {
825       QString name  = myOptionTable->item( row, OPTION_NAME_COLUMN )->text();
826       QString value = myOptionTable->item( row, OPTION_VALUE_COLUMN )->text().trimmed();
827       if ( value.isNull() )
828         value = "";
829       that->myOptions[ id ] = ( name + ":" + value).toLatin1().constData();
830       if ( value != "" )
831         guiHyp += name + " = " + value + "; ";
832     }
833   }
834   
835   // SizeMap
836   row = 0, nbRows = mySizeMapTable->rowCount();
837   for ( ; row < nbRows; ++row )
838   {
839       QString entry   = mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->text();
840       if ( that->mySMPMap.contains(entry) )
841         guiHyp += entry + " = " + that->mySMPMap[entry] + "; ";
842   }
843   
844   cout << "guiHyp : " << guiHyp.toLatin1().data() << endl;
845
846   return guiHyp;
847 }
848
849 void BLSURFPluginGUI_HypothesisCreator::onPhysicalMeshChanged() {
850   cout << "BLSURFPluginGUI_HypothesisCreator::onPhysicalMeshChanged" << endl; 
851   bool isCustom = ((myPhysicalMesh->currentIndex() == PhysicalUserDefined) || (myPhysicalMesh->currentIndex() == SizeMap)) ;
852   myPhySize->setEnabled(isCustom);
853   myPhyMax->setEnabled(isCustom);
854   myPhyMin->setEnabled(isCustom);
855
856   if ( !isCustom ) {
857     QString aPhySize = "";
858     switch( myPhysicalMesh->currentIndex() ) {
859       case DefaultSize:
860       default:
861         aPhySize = "10";
862         break;
863       }
864     myPhySize->setText( aPhySize );
865     if ( !isDouble( myPhyMin->text(), true ))
866       myPhyMin->setText("");
867     if ( !isDouble( myPhyMax->text(), true ))
868       myPhyMax->setText("");
869     if ( myGeometricMesh->currentIndex() == DefaultGeom ) {
870       myGeometricMesh->setCurrentIndex( UserDefined );
871       onGeometricMeshChanged();
872     }
873   }
874 }
875
876 void BLSURFPluginGUI_HypothesisCreator::onGeometricMeshChanged() {
877   cout << "BLSURFPluginGUI_HypothesisCreator::onGeometricMeshChanged" << endl; 
878   bool isCustom = (myGeometricMesh->currentIndex() == UserDefined);
879   myAngleMeshS->setEnabled(isCustom);
880   myAngleMeshC->setEnabled(isCustom);
881   myGradation->setEnabled(isCustom);
882   myGeoMax->setEnabled(isCustom);
883   myGeoMin->setEnabled(isCustom);
884
885   if ( ! isCustom ) {
886     double aAngleMeshS, aGradation;
887     switch( myGeometricMesh->currentIndex() ) {
888       case DefaultGeom:
889       default:
890         aAngleMeshS = 8;
891         aGradation = 1.1;
892         break;
893       }
894     myAngleMeshS->setValue( aAngleMeshS );
895     myAngleMeshC->setValue( aAngleMeshS );
896     myGradation->setValue( aGradation );
897     if ( !isDouble( myGeoMin->text(), true ))
898       myGeoMin->setText("");
899     if ( !isDouble( myGeoMax->text(), true ))
900       myGeoMax->setText("");
901     //  hphy_flag = 0 and hgeo_flag = 0 is not allowed (spec)
902     if ( myPhysicalMesh->currentIndex() == DefaultSize ) {
903       myPhysicalMesh->setCurrentIndex( PhysicalUserDefined );
904       onPhysicalMeshChanged();
905     }
906   }
907 }
908
909 void BLSURFPluginGUI_HypothesisCreator::onAddOption()
910 {
911   QMenu* menu = (QMenu*)sender();
912   // fill popup with option names
913   menu->clear();
914   if ( myOptions.operator->() ) {
915     for ( int i = 0, nb = myOptions->length(); i < nb; ++i ) {
916       QString name_value = myOptions[i].in();
917       QString name = name_value.split( ":", QString::KeepEmptyParts )[0];
918       menu->addAction( name );
919     }
920   }
921 }
922
923 void BLSURFPluginGUI_HypothesisCreator::onOptionChosenInPopup( QAction* a )
924 {
925   myOptionTable->setFocus();
926   QMenu* menu = (QMenu*)( a->parent() );
927   
928   int idx = menu->actions().indexOf( a );
929   QString idStr = QString("%1").arg( idx );
930   QString option = myOptions[idx].in();
931   QString optionName = option.split( ":", QString::KeepEmptyParts )[0];
932
933   // look for a row with optionName
934   int row = 0, nbRows = myOptionTable->rowCount();
935   for ( ; row < nbRows; ++row )
936     if ( myOptionTable->item( row, OPTION_ID_COLUMN )->text() == idStr )
937       break;
938   // add a row if not found
939   if ( row == nbRows ) {
940     myOptionTable->setRowCount( row+1 );
941     myOptionTable->setItem( row, OPTION_ID_COLUMN, new QTableWidgetItem( idStr ) );
942     myOptionTable->item( row, OPTION_ID_COLUMN )->setFlags( 0 );
943     myOptionTable->setItem( row, OPTION_NAME_COLUMN, new QTableWidgetItem( optionName ) );
944     myOptionTable->item( row, OPTION_NAME_COLUMN )->setFlags( 0 );
945     myOptionTable->setItem( row, OPTION_VALUE_COLUMN, new QTableWidgetItem( "" ) );
946     myOptionTable->item( row, OPTION_VALUE_COLUMN )->setFlags( Qt::ItemIsSelectable | 
947                                                                Qt::ItemIsEditable   | 
948                                                                Qt::ItemIsEnabled );
949     myOptionTable->resizeColumnToContents( OPTION_NAME_COLUMN );
950   }
951   myOptionTable->clearSelection();
952   myOptionTable->scrollToItem( myOptionTable->item( row, OPTION_VALUE_COLUMN ) );
953   //myOptionTable->item( row, OPTION_VALUE_COLUMN )->setSelected( true );
954   myOptionTable->setCurrentCell( row, OPTION_VALUE_COLUMN );
955   //myOptionTable->openPersistentEditor( myOptionTable->item( row, OPTION_VALUE_COLUMN ) );
956 }
957     
958 void BLSURFPluginGUI_HypothesisCreator::onDeleteOption()
959 {
960   // clear option values and remember selected row
961   QList<int> selectedRows;
962   QList<QTableWidgetItem*> selected = myOptionTable->selectedItems(); 
963   QTableWidgetItem* item;
964   foreach( item, selected ) {
965     int row = item->row();
966     if ( !selectedRows.contains( row ) ) {
967       selectedRows.append( row );
968       int id = myOptionTable->item( row, OPTION_ID_COLUMN )->text().toInt();
969       if ( id >= 0 && id < myOptions->length() )
970         myOptions[ id ] = myOptionTable->item( row, OPTION_NAME_COLUMN )->text().toLatin1().constData();
971     }
972   }
973   qSort( selectedRows );
974   QListIterator<int> it( selectedRows );
975   it.toBack();
976   while ( it.hasPrevious() )
977     myOptionTable->removeRow( it.previous() );
978 }
979
980 // **********************
981 // *** BEGIN SIZE MAP ***
982 // **********************
983
984
985 void BLSURFPluginGUI_HypothesisCreator::onRemoveMap()
986 {
987   cout<<"BLSURFPluginGUI_HypothesisCreator::onRemoveMap()"<<endl;
988   QList<int> selectedRows;
989   QList<QTableWidgetItem*> selected = mySizeMapTable->selectedItems();
990   QTableWidgetItem* item;
991   int row;
992   foreach( item, selected ) {
993     row = item->row();
994     if ( !selectedRows.contains( row ) ) 
995       selectedRows.append( row );
996   }
997
998   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
999
1000   qSort( selectedRows );
1001   QListIterator<int> it( selectedRows );
1002   it.toBack();
1003   while ( it.hasPrevious() ) {
1004       row = it.previous();
1005       cout << "delete row #"<< row <<endl;
1006       QString entry = mySizeMapTable->item(row,SMP_ENTRY_COLUMN)->text();
1007       if (that->mySMPMap.contains(entry))
1008         that->mySMPMap[entry] = "__TO_DELETE__";
1009       if (that->mySMPShapeTypeMap.contains(entry))
1010         that->mySMPShapeTypeMap.remove(entry);
1011       mySizeMapTable->removeRow(row );
1012   }
1013   mySizeMapTable->resizeColumnToContents(SMP_NAME_COLUMN);
1014   mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
1015 }
1016
1017 void BLSURFPluginGUI_HypothesisCreator::onSetSizeMap(int row,int col)
1018 {
1019   cout<<"BLSURFPluginGUI_HypothesisCreator::onSetSizeMap()"<<endl;
1020   if (col == SMP_SIZEMAP_COLUMN) {
1021     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1022     QString entry   = that->mySizeMapTable->item(row, SMP_ENTRY_COLUMN)->text();
1023     QString sizeMap = that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->text().trimmed();
1024     if (not that->mySMPShapeTypeMap.contains(entry))
1025       return;
1026     QString prevSizeMap = that->mySMPMap[entry];
1027     that->mySMPMap[entry] = sizeMap;
1028     if (not sizeMapValidationFromEntry(entry)) {
1029       that->mySMPMap[entry] = prevSizeMap;
1030       that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setText(prevSizeMap);
1031     }
1032   }
1033 }
1034
1035 void BLSURFPluginGUI_HypothesisCreator::onAddMapOnSurface()
1036 {
1037  insertElementType(TopAbs_FACE);
1038 }
1039
1040 void BLSURFPluginGUI_HypothesisCreator::onAddMapOnEdge()
1041 {
1042  insertElementType(TopAbs_EDGE);
1043 }
1044
1045 void BLSURFPluginGUI_HypothesisCreator::onAddMapOnPoint()
1046 {
1047  insertElementType(TopAbs_VERTEX);
1048 }
1049
1050 void BLSURFPluginGUI_HypothesisCreator::insertElementType(TopAbs_ShapeEnum typeShapeAsked)
1051 {
1052   cout<<"BLSURFPluginGUI_HypothesisCreator::insertElementType()"<<endl;
1053
1054   BLSURFPlugin::BLSURFPlugin_Hypothesis_var h =
1055     BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis());
1056
1057   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1058
1059   TopoDS_Shape S;
1060   string entry, shapeName;
1061   LightApp_SelectionMgr* aSel = GeomToolSelected->selectionMgr();
1062   SALOME_ListIO ListSelectedObjects;
1063   aSel->selectedObjects(ListSelectedObjects, NULL, false );
1064   if (!ListSelectedObjects.IsEmpty())
1065   {
1066     SALOME_ListIteratorOfListIO Object_It(ListSelectedObjects);
1067     for (; Object_It.More(); Object_It.Next()) 
1068     {
1069       Handle(SALOME_InteractiveObject) anObject = Object_It.Value();
1070       entry     = GeomToolSelected->getEntryOfObject(anObject);
1071       shapeName = anObject->getName();
1072       S         = GeomToolSelected->entryToShape(entry);
1073       if ((! S.IsNull()) && (S.ShapeType() == typeShapeAsked)) 
1074       { 
1075         mySizeMapTable->setFocus();
1076         QString shapeEntry;
1077         shapeEntry = QString::fromStdString(entry);
1078         double phySize = h->GetPhySize();
1079         std::ostringstream oss;
1080         oss << phySize;
1081         QString sizeMap;
1082         sizeMap  = QString::fromStdString(oss.str());
1083         if (that->mySMPMap.contains(shapeEntry)) {
1084           if (that->mySMPMap[shapeEntry] != "__TO_DELETE__") {
1085             MESSAGE("Size map for shape with name(entry): "<< shapeName << "(" << entry << ")");
1086             break;
1087           }
1088         }
1089         that->mySMPMap[shapeEntry] = sizeMap;
1090         that->mySMPShapeTypeMap[shapeEntry] = typeShapeAsked; 
1091         int row = mySizeMapTable->rowCount() ;
1092         mySizeMapTable->setRowCount( row+1 );
1093         mySizeMapTable->setItem( row, SMP_ENTRY_COLUMN, new QTableWidgetItem( shapeEntry ) );
1094         mySizeMapTable->item( row, SMP_ENTRY_COLUMN )->setFlags( 0 );
1095         mySizeMapTable->setItem( row, SMP_NAME_COLUMN, new QTableWidgetItem( QString::fromStdString(shapeName) ) );
1096         mySizeMapTable->item( row, SMP_NAME_COLUMN )->setFlags( 0 );
1097         mySizeMapTable->setItem( row, SMP_SIZEMAP_COLUMN, new QTableWidgetItem( sizeMap ) );
1098         mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->setFlags( Qt::ItemIsSelectable |Qt::ItemIsEditable   |Qt::ItemIsEnabled );
1099         mySizeMapTable->resizeColumnToContents( SMP_NAME_COLUMN );
1100         mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
1101         mySizeMapTable->clearSelection();
1102         mySizeMapTable->scrollToItem( mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN ) );
1103
1104         if ( myPhysicalMesh->currentIndex() != SizeMap ) {
1105           myPhysicalMesh->setCurrentIndex( SizeMap );
1106           onPhysicalMeshChanged();
1107         }
1108       }
1109     }
1110   }
1111 }
1112
1113 bool BLSURFPluginGUI_HypothesisCreator::sizeMapsValidation()
1114 {
1115   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapsValidation()"<<endl;
1116   int row = 0, nbRows = mySizeMapTable->rowCount();
1117   for ( ; row < nbRows; ++row )
1118     if (not sizeMapValidationFromRow(row))
1119       return false;
1120   return true;
1121 }
1122
1123 bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow(int myRow)
1124 {
1125   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow()"<<endl;
1126   QString myEntry   = mySizeMapTable->item( myRow, SMP_ENTRY_COLUMN )->text();
1127   return sizeMapValidationFromEntry(myEntry); 
1128 }
1129
1130 bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEntry)
1131 {
1132   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry()"<<endl;
1133
1134   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
1135      
1136   if (not that->mySMPMap.contains(myEntry)) {
1137     cout<<"Geometry with entry "<<myEntry.toStdString()<<" was not found."<<endl;
1138     return false;
1139   }
1140   if (not that->mySMPShapeTypeMap.contains(myEntry)) {
1141     cout<<"Shape type with entry "<<myEntry.toStdString()<<" was not found."<<endl;
1142     return false;
1143   }
1144
1145   string expr;
1146
1147   if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_FACE)
1148     expr = "def f(u,v) : return " + that->mySMPMap[myEntry].toStdString();
1149   else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_EDGE)
1150     expr = "def f(t) : return " + that->mySMPMap[myEntry].toStdString();
1151   else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_VERTEX)
1152     expr = "def f() : return " + that->mySMPMap[myEntry].toStdString();
1153
1154   assert(Py_IsInitialized());
1155   PyGILState_STATE gstate;
1156   gstate = PyGILState_Ensure();
1157  
1158   PyObject * obj = NULL;   
1159   PyObject* new_stderr = NULL;   
1160   string  err_description="";
1161   obj= PyRun_String(expr.c_str(), Py_file_input, main_dict, NULL);
1162   if (obj == NULL){
1163     fflush(stderr);
1164     err_description="";
1165     new_stderr=newPyStdOut(err_description);
1166     PySys_SetObject("stderr", new_stderr);
1167     PyErr_Print();
1168     PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
1169     Py_DECREF(new_stderr);
1170     SUIT_MessageBox::warning( dlg(),"Definition of Python Function : Error" ,err_description.c_str() );
1171     PyGILState_Release(gstate);
1172     return false;
1173   }
1174   Py_DECREF(obj);
1175    
1176   PyObject * func = NULL;
1177   func = PyObject_GetAttrString(main_mod, "f");
1178   if ( func == NULL){
1179     fflush(stderr);                                                                            
1180     err_description="";                                                                        
1181     new_stderr=newPyStdOut(err_description);                                         
1182     PySys_SetObject("stderr", new_stderr);                                                     
1183     PyErr_Print();                                                                             
1184     PySys_SetObject("stderr", PySys_GetObject("__stderr__"));                                  
1185     Py_DECREF(new_stderr);
1186     SUIT_MessageBox::warning( dlg(),"Python Error" ,err_description.c_str() );
1187     PyGILState_Release(gstate);
1188     return false;
1189   }
1190
1191   PyGILState_Release(gstate);
1192
1193   cout<<"SizeMap expression "<<expr<<" is valid"<<endl;
1194
1195   return true;
1196 }
1197
1198 /*
1199 void BLSURFPluginGUI_HypothesisCreator::OnEditMapFunction(QModelIndex* index) {
1200   int myRow = index->row();
1201   int myColumn = index->column();
1202   
1203   if (myColumn == 2){
1204      if (!myEditor) {
1205          myEditor = new BLSURFPluginGUI_MapFunctionEditor(sizeMapModel->item(myRow,0)->text());
1206          connect(myEditor, SIGNAL(FunctionEntered(QString)), this, SLOT(FunctionLightValidation(QString)));
1207      }
1208      myEditor->exec();
1209 //      myEditor->show();
1210 //      myEditor->raise();
1211 //      myEditor->activateWindow();
1212      
1213
1214 //     BLSURFPluginGUI_MapFunctionEditor* myEditor = new BLSURFPluginGUI_MapFunctionEditor(sizeMapModel->item(myRow,0)->text());
1215 //     myEditor->exec();
1216      QString myFunction = myEditor->GetFunctionText();
1217      // FIN RECUPERATION FONCTION
1218      
1219      if (! myFunction.isEmpty()) {
1220      
1221      // MAJ DE LA MAP
1222      
1223      BLSURFPlugin::BLSURFPlugin_Hypothesis_var h = 
1224        BLSURFPlugin::BLSURFPlugin_Hypothesis::_narrow( initParamsHypothesis());
1225
1226 //     h->SetSizeMapEntry(sizeMapModel->item(myRow,1)->text().toLatin1().constData(),
1227 //                        item->text().toLatin1().constData());
1228      h->SetSizeMapEntry(sizeMapModel->item(myRow,1)->text().toLatin1().constData(),
1229                         myFunction.toLatin1().constData());
1230      // FIN MAJ DE LA MAP
1231      }
1232   }
1233 }*/
1234  
1235 QString BLSURFPluginGUI_HypothesisCreator::caption() const
1236 {
1237   return tr( "BLSURF_TITLE" );
1238 }
1239
1240 QPixmap BLSURFPluginGUI_HypothesisCreator::icon() const
1241 {
1242   return SUIT_Session::session()->resourceMgr()->loadPixmap( "BLSURFPlugin", tr( "ICON_DLG_BLSURF_PARAMETERS") );
1243 }
1244
1245 QString BLSURFPluginGUI_HypothesisCreator::type() const
1246 {
1247   return tr( "BLSURF_HYPOTHESIS" );
1248 }
1249
1250 QString BLSURFPluginGUI_HypothesisCreator::helpPage() const
1251 {
1252   return "blsurf_hypo_page.html";
1253 }
1254
1255 LightApp_SelectionMgr* BLSURFPluginGUI_HypothesisCreator::selectionMgr()
1256 {
1257
1258   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1259   if( anApp )
1260     return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
1261   else
1262     return 0;
1263 }