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