Salome HOME
Merge branch 'V7_dev'
[plugins/netgenplugin.git] / src / GUI / NETGENPluginGUI_HypothesisCreator.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  NETGENPlugin GUI: GUI for plugged-in mesher NETGENPlugin
24 //  File   : NETGENPluginGUI_HypothesisCreator.cxx
25 //  Author : Michael Zorin
26 //  Module : NETGENPlugin
27 //
28 #include "NETGENPluginGUI_HypothesisCreator.h"
29
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32 #include <SMESHGUI_SpinBox.h>
33 #include <GeomSelectionTools.h>
34
35 #include CORBA_SERVER_HEADER(NETGENPlugin_Algorithm)
36
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <SalomeApp_Tools.h>
41 #include <LightApp_SelectionMgr.h>
42 #include <SALOME_ListIO.hxx>
43
44 #include <QComboBox>
45 #include <QLabel>
46 #include <QGroupBox>
47 #include <QFrame>
48 #include <QLayout>
49 #include <QLineEdit>
50 #include <QCheckBox>
51 #include <QPixmap>
52 #include <QTableWidget>
53 #include <QHeaderView>
54 #include <QPushButton>
55
56 enum Fineness
57   {
58     VeryCoarse,
59     Coarse,
60     Moderate,
61     Fine,
62     VeryFine,
63     UserDefined
64   };
65
66 enum {
67   STD_TAB = 0,
68   LSZ_TAB
69 };
70
71 enum {
72   LSZ_ENTRY_COLUMN = 0,
73   LSZ_NAME_COLUMN,
74   LSZ_LOCALSIZE_COLUMN,
75   LSZ_NB_COLUMNS
76 };
77
78 enum {
79   LSZ_BTNS = 0,
80   LSZ_VERTEX_BTN,
81   LSZ_EDGE_BTN,
82   LSZ_FACE_BTN,
83   LSZ_SEPARATOR2,
84   LSZ_REMOVE_BTN
85 };
86
87 NETGENPluginGUI_HypothesisCreator::NETGENPluginGUI_HypothesisCreator( const QString& theHypType )
88   : SMESHGUI_GenericHypothesisCreator( theHypType )
89 {
90   myGeomSelectionTools = NULL;
91   myLocalSizeMap.clear();
92   myIs2D   = ( theHypType.startsWith("NETGEN_Parameters_2D"));
93   myIsONLY = ( theHypType == "NETGEN_Parameters_2D_ONLY" ||
94                theHypType == "NETGEN_Parameters_3D");
95 }
96
97 NETGENPluginGUI_HypothesisCreator::~NETGENPluginGUI_HypothesisCreator()
98 {
99 }
100
101 bool NETGENPluginGUI_HypothesisCreator::checkParams(QString& msg) const
102 {
103   NetgenHypothesisData data_old, data_new;
104   readParamsFromHypo( data_old );
105   readParamsFromWidgets( data_new );
106   bool res = storeParamsToHypo( data_new );
107   //storeParamsToHypo( data_old ); -- issue 0021364: Dump of netgen parameters has duplicate lines
108   
109   res = myMaxSize->isValid(msg,true) && res;
110   res = myMinSize->isValid(msg,true) && res;
111   res = myGrowthRate->isValid(msg,true) && res; ;
112   if ( myNbSegPerEdge )
113     res = myNbSegPerEdge->isValid(msg,true) && res;
114   if ( myNbSegPerRadius )
115     res = myNbSegPerRadius->isValid(msg,true) && res;
116
117   if ( !res ) //  -- issue 0021364: Dump of netgen parameters has duplicate lines
118     storeParamsToHypo( data_old );
119
120   return res;
121 }
122
123 QFrame* NETGENPluginGUI_HypothesisCreator::buildFrame()
124 {
125   QFrame* fr = new QFrame( 0 );
126   fr->setObjectName( "myframe" );
127   QVBoxLayout* lay = new QVBoxLayout( fr );
128   lay->setMargin( 5 );
129   lay->setSpacing( 0 );
130
131   QTabWidget* tab = new QTabWidget( fr );
132   tab->setTabShape( QTabWidget::Rounded );
133   tab->setTabPosition( QTabWidget::North );
134   lay->addWidget( tab );
135   QWidget* GroupC1 = new QWidget();
136   tab->insertTab( STD_TAB, GroupC1, tr( "SMESH_ARGUMENTS" ) );
137   
138   QGridLayout* aGroupLayout = new QGridLayout( GroupC1 );
139   aGroupLayout->setSpacing( 6 );
140   aGroupLayout->setMargin( 11 );
141   
142   int row = 0;
143   myName = 0;
144   if( isCreation() )
145   {
146     aGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
147     myName = new QLineEdit( GroupC1 );
148     myName->setMinimumWidth(160);
149     aGroupLayout->addWidget( myName, row, 1 );
150     row++;
151   }
152
153   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_MAX_SIZE" ), GroupC1 ), row, 0 );
154   myMaxSize = new SMESHGUI_SpinBox( GroupC1 );
155   myMaxSize->RangeStepAndValidator( 1e-07, 1e+06, 10., "length_precision" );
156   aGroupLayout->addWidget( myMaxSize, row, 1 );
157   row++;
158
159   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_MIN_SIZE" ), GroupC1 ), row, 0 );
160   myMinSize = new SMESHGUI_SpinBox( GroupC1 );
161   myMinSize->RangeStepAndValidator( 0.0, 1e+06, 10., "length_precision" );
162   aGroupLayout->addWidget( myMinSize, row, 1 );
163   row++;
164
165   mySecondOrder = 0;
166   if ( !myIsONLY )
167   {
168     mySecondOrder = new QCheckBox( tr( "NETGEN_SECOND_ORDER" ), GroupC1 );
169     aGroupLayout->addWidget( mySecondOrder, row, 0, 1, 2 );
170     row++;
171   }
172
173   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_FINENESS" ), GroupC1 ), row, 0 );
174   myFineness = new QComboBox( GroupC1 );
175   QStringList types;
176   types << tr( "NETGEN_VERYCOARSE" ) << tr( "NETGEN_COARSE" )   << tr( "NETGEN_MODERATE" ) <<
177            tr( "NETGEN_FINE" )       << tr( "NETGEN_VERYFINE" ) << tr( "NETGEN_CUSTOM" );
178   myFineness->addItems( types );
179   aGroupLayout->addWidget( myFineness, row, 1 );
180   connect( myFineness, SIGNAL( activated( int ) ), this, SLOT( onFinenessChanged() ) );
181   row++;
182
183   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_GROWTH_RATE" ), GroupC1 ), row, 0 );
184   myGrowthRate = new SMESHGUI_SpinBox( GroupC1 );
185   myGrowthRate->RangeStepAndValidator( .0001, 10., .1, "parametric_precision" );
186   aGroupLayout->addWidget( myGrowthRate, row, 1 );
187   row++;
188
189   myNbSegPerEdge = 0;
190   myNbSegPerRadius = 0;
191   if ( !myIsONLY )
192   {
193     const double VALUE_MAX = 1.0e+6;
194
195     aGroupLayout->addWidget( new QLabel( tr( "NETGEN_SEG_PER_EDGE" ), GroupC1 ), row, 0 );
196     myNbSegPerEdge = new SMESHGUI_SpinBox( GroupC1 );
197     myNbSegPerEdge->RangeStepAndValidator( .2, VALUE_MAX, .1, "parametric_precision" );
198     aGroupLayout->addWidget( myNbSegPerEdge, row, 1 );
199     row++;
200
201     aGroupLayout->addWidget( new QLabel( tr( "NETGEN_SEG_PER_RADIUS" ), GroupC1 ), row, 0 );
202     myNbSegPerRadius = new SMESHGUI_SpinBox( GroupC1 );
203     myNbSegPerRadius->RangeStepAndValidator( .2, VALUE_MAX, .1, "parametric_precision" );
204     aGroupLayout->addWidget( myNbSegPerRadius, row, 1 );
205     row++;
206   }
207
208   mySurfaceCurvature = 0;
209   if ( myIs2D || !myIsONLY )
210   {
211     mySurfaceCurvature = new QCheckBox( tr( "NETGEN_SURFACE_CURVATURE" ), GroupC1 );
212     aGroupLayout->addWidget( mySurfaceCurvature, row, 0, 1, 2 );
213     connect( mySurfaceCurvature, SIGNAL( stateChanged( int ) ), this, SLOT( onSurfaceCurvatureChanged() ) );
214     row++;
215   }
216
217   myAllowQuadrangles = 0;
218   if ( myIs2D || !myIsONLY ) // disable only for NETGEN 3D
219   {
220     myAllowQuadrangles = new QCheckBox( tr( "NETGEN_ALLOW_QUADRANGLES" ), GroupC1 );
221     aGroupLayout->addWidget( myAllowQuadrangles, row, 0, 1, 2 );
222     row++;
223   }
224
225   myOptimize = new QCheckBox( tr( "NETGEN_OPTIMIZE" ), GroupC1 );
226   aGroupLayout->addWidget( myOptimize, row, 0, 1, 2 );
227   row++;
228
229   myFuseEdges = 0;
230   if (!myIsONLY)
231   {
232     myFuseEdges = new QCheckBox( tr( "NETGEN_FUSE_EDGES" ), GroupC1 );
233     aGroupLayout->addWidget( myFuseEdges, row, 0, 1, 2 );
234     row++;
235   }
236
237   myLocalSizeTable = 0;
238   if ( !myIsONLY )
239   {
240     QWidget* localSizeGroup = new QWidget();
241     QGridLayout* localSizeLayout = new QGridLayout(localSizeGroup);
242
243     myLocalSizeTable = new QTableWidget(0, LSZ_NB_COLUMNS, localSizeGroup);
244     localSizeLayout->addWidget(myLocalSizeTable, 1, 0, 8, 1);
245     QStringList localSizeHeaders;
246     localSizeHeaders << tr( "LSZ_ENTRY_COLUMN" )<< tr( "LSZ_NAME_COLUMN" ) << tr( "LSZ_LOCALSIZE_COLUMN" );
247     myLocalSizeTable->setHorizontalHeaderLabels(localSizeHeaders);
248     myLocalSizeTable->horizontalHeader()->hideSection(LSZ_ENTRY_COLUMN);
249 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
250     myLocalSizeTable->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
251 #else
252     myLocalSizeTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
253 #endif
254     myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
255     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
256     myLocalSizeTable->setAlternatingRowColors(true);
257     myLocalSizeTable->verticalHeader()->hide();
258
259     QPushButton* addVertexButton = new QPushButton(tr("NETGEN_LSZ_VERTEX"), localSizeGroup);
260     localSizeLayout->addWidget(addVertexButton, LSZ_VERTEX_BTN, 1, 1, 1);
261     QPushButton* addEdgeButton = new QPushButton(tr("NETGEN_LSZ_EDGE"), localSizeGroup);
262     localSizeLayout->addWidget(addEdgeButton, LSZ_EDGE_BTN, 1, 1, 1);
263     QPushButton* addFaceButton = new QPushButton(tr("NETGEN_LSZ_FACE"), localSizeGroup);
264     localSizeLayout->addWidget(addFaceButton, LSZ_FACE_BTN, 1, 1, 1);
265
266     QFrame *line2 = new QFrame(localSizeGroup);
267     line2->setFrameShape(QFrame::HLine);
268     line2->setFrameShadow(QFrame::Sunken);
269     localSizeLayout->addWidget(line2, LSZ_SEPARATOR2, 1, 1, 1);
270
271     QPushButton* removeButton = new QPushButton(tr("NETGEN_LSZ_REMOVE"), localSizeGroup);
272     localSizeLayout->addWidget(removeButton, LSZ_REMOVE_BTN, 1, 1, 1);
273
274     connect( addVertexButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnVertex()));
275     connect( addEdgeButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnEdge()));
276     connect( addFaceButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnFace()));
277     connect( removeButton, SIGNAL(clicked()), this, SLOT(onRemoveLocalSizeOnShape()));
278     connect( myLocalSizeTable, SIGNAL(cellChanged(int, int)), this, SLOT(onSetLocalSize(int, int)));
279
280     tab->insertTab(LSZ_TAB, localSizeGroup, tr("NETGEN_LOCAL_SIZE"));
281   }
282   return fr;
283 }
284
285 void NETGENPluginGUI_HypothesisCreator::retrieveParams() const
286 {
287   NetgenHypothesisData data;
288   readParamsFromHypo( data );
289
290   if( myName )
291     myName->setText( data.myName );
292   if(data.myMaxSizeVar.isEmpty())
293     myMaxSize->setValue( data.myMaxSize );
294   else
295     myMaxSize->setText( data.myMaxSizeVar );
296
297   if(data.myMinSizeVar.isEmpty())
298     myMinSize->setValue( data.myMinSize );
299   else
300     myMinSize->setText( data.myMinSizeVar );
301
302   if ( mySecondOrder )
303     mySecondOrder->setChecked( data.mySecondOrder );
304   if ( myOptimize )
305     myOptimize->setChecked( data.myOptimize );
306   myFineness->setCurrentIndex( data.myFineness );
307
308   if(data.myGrowthRateVar.isEmpty())
309     myGrowthRate->setValue( data.myGrowthRate );
310   else
311     myGrowthRate->setText( data.myGrowthRateVar );
312
313   if ( myNbSegPerEdge )
314   {
315     if(data.myNbSegPerEdgeVar.isEmpty())
316       myNbSegPerEdge->setValue( data.myNbSegPerEdge );
317     else
318       myNbSegPerEdge->setText( data.myNbSegPerEdgeVar );
319   }
320   if ( myNbSegPerRadius )
321   {
322     if(data.myNbSegPerRadiusVar.isEmpty())
323       myNbSegPerRadius->setValue( data.myNbSegPerRadius );
324     else
325       myNbSegPerRadius->setText( data.myNbSegPerRadiusVar );
326   }  
327   if (myAllowQuadrangles)
328     myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
329
330   if (mySurfaceCurvature)
331     mySurfaceCurvature->setChecked( data.mySurfaceCurvature );
332
333   if (myFuseEdges)
334     myFuseEdges->setChecked( data.myFuseEdges );
335
336   // update widgets
337   ((NETGENPluginGUI_HypothesisCreator*) this )-> onSurfaceCurvatureChanged();
338
339   if ( myLocalSizeTable )
340   {
341     NETGENPluginGUI_HypothesisCreator* that = (NETGENPluginGUI_HypothesisCreator*)this;
342     QMapIterator<QString, QString> i(myLocalSizeMap);
343     GeomSelectionTools* geomSelectionTools = that->getGeomSelectionTools();
344     while (i.hasNext()) {
345       i.next();
346       const QString entry = i.key();
347       std::string shapeName = geomSelectionTools->getNameFromEntry(entry.toStdString());
348       const QString localSize = i.value();
349       int row = myLocalSizeTable->rowCount();
350       myLocalSizeTable->setRowCount(row+1);
351       myLocalSizeTable->setItem(row, LSZ_ENTRY_COLUMN, new QTableWidgetItem(entry));
352       myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN)->setFlags(0);
353       myLocalSizeTable->setItem(row, LSZ_NAME_COLUMN, new QTableWidgetItem(QString::fromStdString(shapeName)));
354       myLocalSizeTable->item(row, LSZ_NAME_COLUMN)->setFlags(0);
355       myLocalSizeTable->setItem(row, LSZ_LOCALSIZE_COLUMN, new QTableWidgetItem(localSize));
356       myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
357     }
358     myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
359     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
360   }
361 }
362
363 QString NETGENPluginGUI_HypothesisCreator::storeParams() const
364 {
365   NetgenHypothesisData data;
366   readParamsFromWidgets( data );
367   storeParamsToHypo( data );
368   
369   QString valStr = tr("NETGEN_MAX_SIZE") + " = " + QString::number( data.myMaxSize ) + "; ";
370   valStr += tr("NETGEN_MIN_SIZE") + " = " + QString::number( data.myMinSize ) + "; ";
371   if ( data.mySecondOrder )
372     valStr +=  tr("NETGEN_SECOND_ORDER") + "; ";
373   if ( data.myOptimize )
374     valStr +=  tr("NETGEN_OPTIMIZE") + "; ";
375   valStr += myFineness->currentText() + "(" +  QString::number( data.myGrowthRate )     + ", " +
376                                                QString::number( data.myNbSegPerEdge )   + ", " +
377                                                QString::number( data.myNbSegPerRadius ) + ")";
378
379   if ( myIs2D && data.myAllowQuadrangles )
380     valStr += "; " + tr("NETGEN_ALLOW_QUADRANGLES");
381   
382   if ( data.mySurfaceCurvature )
383     valStr += "; " + tr("NETGEN_SURFACE_CURVATURE");
384
385   if ( data.myFuseEdges )
386     valStr += "; " + tr("NETGEN_FUSE_EDGES");
387
388   return valStr;
389 }
390
391 bool NETGENPluginGUI_HypothesisCreator::readParamsFromHypo( NetgenHypothesisData& h_data ) const
392 {
393   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
394     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
395
396   //HypothesisData* data = SMESH::GetHypothesisData( hypType() );
397   h_data.myName = isCreation() ? hypName() : "";
398
399   h_data.myMaxSize = h->GetMaxSize();
400   h_data.myMaxSizeVar = getVariableName("SetMaxSize");
401   h_data.mySecondOrder = h->GetSecondOrder();
402   h_data.myOptimize = h->GetOptimize();
403
404   h_data.myFineness = (int) h->GetFineness();
405   h_data.myGrowthRate = h->GetGrowthRate();
406   h_data.myGrowthRateVar = getVariableName("SetGrowthRate");
407   h_data.myNbSegPerEdge = h->GetNbSegPerEdge();
408   h_data.myNbSegPerEdgeVar  = getVariableName("SetNbSegPerEdge");
409   h_data.myNbSegPerRadius = h->GetNbSegPerRadius();
410   h_data.myNbSegPerRadiusVar = getVariableName("SetNbSegPerRadius");
411   h_data.myMinSize = h->GetMinSize();
412   h_data.myMinSizeVar = getVariableName("SetMinSize");
413   h_data.mySurfaceCurvature = h->GetUseSurfaceCurvature();
414   h_data.myFuseEdges = h->GetFuseEdges();
415
416   //if ( myIs2D )
417     {
418       NETGENPlugin::NETGENPlugin_Hypothesis_var h =
419         NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
420
421       if ( !h->_is_nil() )
422         h_data.myAllowQuadrangles = h->GetQuadAllowed();
423     }
424   
425   NETGENPluginGUI_HypothesisCreator*  that = (NETGENPluginGUI_HypothesisCreator*)this;
426   NETGENPlugin::string_array_var myEntries = h->GetLocalSizeEntries();
427   for ( size_t i = 0; i < myEntries->length(); i++ )
428     {
429       QString entry = myEntries[i].in();
430       double val = h->GetLocalSizeOnEntry(entry.toStdString().c_str());
431       std::ostringstream tmp;
432       tmp << val;
433       QString valstring = QString::fromStdString(tmp.str());
434       if (myLocalSizeMap.contains(entry))
435         {
436           if (myLocalSizeMap[entry] == "__TO_DELETE__")
437             {
438               continue;
439             }
440         }
441       that->myLocalSizeMap[entry] = valstring;
442     }
443
444   return true;
445 }
446
447 bool NETGENPluginGUI_HypothesisCreator::storeParamsToHypo( const NetgenHypothesisData& h_data ) const
448 {
449   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
450     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( hypothesis() );
451
452   bool ok = true;
453   try
454   {
455     if( isCreation() )
456       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
457     h->SetVarParameter( h_data.myMaxSizeVar.toLatin1().constData(), "SetMaxSize");
458     h->SetMaxSize     ( h_data.myMaxSize );
459     h->SetSecondOrder ( h_data.mySecondOrder );
460     h->SetOptimize    ( h_data.myOptimize );
461     int fineness = h_data.myFineness;
462     h->SetFineness    ( fineness );
463
464     if( fineness==UserDefined )
465       {
466         h->SetVarParameter  ( h_data.myGrowthRateVar.toLatin1().constData(), "SetGrowthRate");
467         h->SetGrowthRate    ( h_data.myGrowthRate );
468         h->SetVarParameter  ( h_data.myNbSegPerEdgeVar.toLatin1().constData(), "SetNbSegPerEdge");
469         h->SetNbSegPerEdge  ( h_data.myNbSegPerEdge );
470         h->SetVarParameter  ( h_data.myNbSegPerRadiusVar.toLatin1().constData(), "SetNbSegPerRadius");
471         h->SetNbSegPerRadius( h_data.myNbSegPerRadius );
472       }
473     h->SetVarParameter       ( h_data.myMinSizeVar.toLatin1().constData(), "SetMinSize");
474     h->SetMinSize            ( h_data.myMinSize );
475     h->SetUseSurfaceCurvature( h_data.mySurfaceCurvature );
476     h->SetFuseEdges          ( h_data.myFuseEdges );
477     
478     //if ( myIs2D )
479       {
480         // NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
481         //   NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( h );
482         
483         // if ( !h_2d->_is_nil() )
484         //   h_2d->SetQuadAllowed( h_data.myAllowQuadrangles );
485         h->SetQuadAllowed( h_data.myAllowQuadrangles );
486       }
487
488     QMapIterator<QString,QString> i(myLocalSizeMap);
489     while (i.hasNext()) {
490       i.next();
491       const QString entry = i.key();
492       const QString localSize = i.value();
493       if (localSize == "__TO_DELETE__")
494         {
495           h->UnsetLocalSizeOnEntry(entry.toLatin1().constData());
496         }
497       else
498         {
499           std::istringstream tmp(localSize.toLatin1().constData());
500           double val;
501           tmp >> val;
502           h->SetLocalSizeOnEntry(entry.toLatin1().constData(), val);
503         }
504     }
505   }
506   catch(const SALOME::SALOME_Exception& ex)
507   {
508     SalomeApp_Tools::QtCatchCorbaException(ex);
509     ok = false;
510   }
511   return ok;
512 }
513
514 bool NETGENPluginGUI_HypothesisCreator::readParamsFromWidgets( NetgenHypothesisData& h_data ) const
515 {
516   h_data.myName           = myName ? myName->text() : "";
517   h_data.myMaxSize        = myMaxSize->value();
518   h_data.myMaxSizeVar     = myMaxSize->text();
519   h_data.myMinSize        = myMinSize->value();
520   h_data.myMinSizeVar     = myMinSize->text();
521   if ( mySecondOrder )
522     h_data.mySecondOrder  = mySecondOrder->isChecked();
523   if ( myOptimize )
524     h_data.myOptimize     = myOptimize->isChecked();
525   h_data.myFineness       = myFineness->currentIndex();
526   h_data.myGrowthRate     = myGrowthRate->value();
527   if ( myNbSegPerEdge )
528     h_data.myNbSegPerEdge = myNbSegPerEdge->value();
529   if ( myNbSegPerRadius )
530     h_data.myNbSegPerRadius = myNbSegPerRadius->value();
531
532   h_data.myGrowthRateVar  = myGrowthRate->text();
533   if ( myNbSegPerEdge )
534     h_data.myNbSegPerEdgeVar = myNbSegPerEdge->text();
535   if ( myNbSegPerRadius )
536     h_data.myNbSegPerRadiusVar = myNbSegPerRadius->text();
537
538   
539   if ( myAllowQuadrangles )
540     h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
541
542   if ( mySurfaceCurvature )
543     h_data.mySurfaceCurvature = mySurfaceCurvature->isChecked();
544
545   if ( myFuseEdges )
546     h_data.myFuseEdges = myFuseEdges->isChecked();
547
548   if ( myLocalSizeTable )
549   {
550     NETGENPluginGUI_HypothesisCreator* that = (NETGENPluginGUI_HypothesisCreator*)this;
551     int nbRows = myLocalSizeTable->rowCount();
552     for(int row=0 ; row < nbRows ; row++)
553     {
554       QString entry = myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN)->text();
555       QString localSize = myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN)->text().trimmed();
556       that->myLocalSizeMap[entry] = localSize;
557     }
558   }
559   return true;
560 }
561
562 void NETGENPluginGUI_HypothesisCreator::onSurfaceCurvatureChanged()
563 {
564   bool isSurfaceCurvature = (mySurfaceCurvature ? mySurfaceCurvature->isChecked() : true);
565   bool isCustom           = (myFineness->currentIndex() == UserDefined);
566   //myFineness->setEnabled(isSurfaceCurvature);
567   myGrowthRate->setEnabled(isCustom);
568   if ( myNbSegPerEdge )
569     myNbSegPerEdge->setEnabled(isCustom && isSurfaceCurvature);
570   if ( myNbSegPerRadius )
571     myNbSegPerRadius->setEnabled(isCustom && isSurfaceCurvature);
572 }
573
574 void NETGENPluginGUI_HypothesisCreator::onFinenessChanged()
575 {
576   bool isCustom = (myFineness->currentIndex() == UserDefined);
577   
578   myGrowthRate->setEnabled(isCustom);
579   if ( myNbSegPerEdge )
580     myNbSegPerEdge->setEnabled(isCustom);
581   if ( myNbSegPerRadius )
582     myNbSegPerRadius->setEnabled(isCustom);
583
584   if (!isCustom)
585     {
586       double aGrowthRate, aNbSegPerEdge, aNbSegPerRadius;
587       
588       switch ( myFineness->currentIndex() )
589         {
590         case VeryCoarse:
591           aGrowthRate = 0.7;
592           aNbSegPerEdge = 0.3;
593           aNbSegPerRadius = 1;
594           break;
595         case Coarse:
596           aGrowthRate = 0.5;
597           aNbSegPerEdge = 0.5;
598           aNbSegPerRadius = 1.5;
599           break;
600         case Fine:
601           aGrowthRate = 0.2;
602           aNbSegPerEdge = 2;
603           aNbSegPerRadius = 3;
604           break;
605         case VeryFine:
606           aGrowthRate = 0.1;
607           aNbSegPerEdge = 3;
608           aNbSegPerRadius = 5;
609           break;
610         case Moderate:
611         default:
612           aGrowthRate = 0.3;
613           aNbSegPerEdge = 1;
614           aNbSegPerRadius = 2;
615           break;
616         }
617       
618       myGrowthRate->setValue( aGrowthRate );
619       if ( myNbSegPerEdge )
620         myNbSegPerEdge->setValue( aNbSegPerEdge );
621       if ( myNbSegPerRadius )
622         myNbSegPerRadius->setValue( aNbSegPerRadius );
623     }
624 }
625
626 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnVertex()
627 {
628   addLocalSizeOnShape(TopAbs_VERTEX);
629 }
630
631 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnEdge()
632 {
633   addLocalSizeOnShape(TopAbs_EDGE);
634 }
635
636 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnFace()
637 {
638   addLocalSizeOnShape(TopAbs_FACE);
639 }
640
641 void NETGENPluginGUI_HypothesisCreator::addLocalSizeOnShape(TopAbs_ShapeEnum typeShapeAsked)
642 {
643   NETGENPlugin::NETGENPlugin_Hypothesis_var h = NETGENPlugin::NETGENPlugin_Hypothesis::_narrow(initParamsHypothesis());
644   GeomSelectionTools* geomSelectionTools = getGeomSelectionTools();
645   LightApp_SelectionMgr* mySel = geomSelectionTools->selectionMgr();
646   SALOME_ListIO ListSelectedObjects;
647   mySel->selectedObjects(ListSelectedObjects, NULL, false );
648   SALOME_ListIteratorOfListIO Object_It(ListSelectedObjects);
649   for ( ; Object_It.More() ; Object_It.Next())
650     {
651       Handle(SALOME_InteractiveObject) anObject = Object_It.Value();
652       std::string entry, shapeName;
653       entry = geomSelectionTools->getEntryOfObject(anObject);
654       shapeName = anObject->getName();
655       TopAbs_ShapeEnum shapeType;
656       shapeType = geomSelectionTools->entryToShapeType(entry);
657       if (shapeType == TopAbs_SHAPE)
658         {
659           // E.A. if shapeType == TopAbs_SHAPE, it is NOT a TopoDS_Shape !!!
660           continue;
661         }
662       // --
663       if(shapeType != typeShapeAsked)
664         {
665           continue;
666         }
667       // --
668       myLocalSizeTable->setFocus();
669       QString shapeEntry;
670       shapeEntry = QString::fromStdString(entry);
671       if (myLocalSizeMap.contains(shapeEntry))
672         {
673           if (myLocalSizeMap[shapeEntry] != "__TO_DELETE__")
674             {
675               continue;
676             }
677         }
678       double phySize = h->GetMaxSize();
679       std::ostringstream oss;
680       oss << phySize;
681       QString localSize;
682       localSize  = QString::fromStdString(oss.str());
683       // --
684       int row = myLocalSizeTable->rowCount() ;
685       myLocalSizeTable->setRowCount(row+1);
686       myLocalSizeTable->setItem(row, LSZ_ENTRY_COLUMN, new QTableWidgetItem(shapeEntry));
687       myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN )->setFlags(0);
688       myLocalSizeTable->setItem(row, LSZ_NAME_COLUMN, new QTableWidgetItem(QString::fromStdString(shapeName)));
689       myLocalSizeTable->item(row, LSZ_NAME_COLUMN )->setFlags(0);
690       myLocalSizeTable->setItem(row, LSZ_LOCALSIZE_COLUMN, new QTableWidgetItem(localSize));
691       myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN )->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
692       myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
693       myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
694       myLocalSizeTable->clearSelection();
695       myLocalSizeTable->scrollToItem( myLocalSizeTable->item( row, LSZ_LOCALSIZE_COLUMN ) );
696       // --
697     }
698 }
699
700 void NETGENPluginGUI_HypothesisCreator::onRemoveLocalSizeOnShape()
701 {
702   QList<int> selectedRows;
703   QList<QTableWidgetItem*> selected = myLocalSizeTable->selectedItems();
704   QTableWidgetItem* item;
705   int row;
706   foreach(item, selected) {
707     row = item->row();
708     if (!selectedRows.contains(row))
709       selectedRows.append( row );
710   }
711   qSort( selectedRows );
712   QListIterator<int> it( selectedRows );
713   it.toBack();
714   while (it.hasPrevious())
715     {
716       row = it.previous();
717       QString entry = myLocalSizeTable->item(row,LSZ_ENTRY_COLUMN)->text();
718       if (myLocalSizeMap.contains(entry))
719         {
720           myLocalSizeMap[entry] = "__TO_DELETE__";
721         }
722       myLocalSizeTable->removeRow(row );
723     }
724   myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
725   myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
726 }
727
728 void NETGENPluginGUI_HypothesisCreator::onSetLocalSize(int row,int col)
729 {
730   if (col == LSZ_LOCALSIZE_COLUMN) {
731     QString entry = myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN)->text();
732     QString localSize = myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN)->text().trimmed();
733     myLocalSizeMap[entry] = localSize;
734     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
735   }
736 }
737
738 GeomSelectionTools* NETGENPluginGUI_HypothesisCreator::getGeomSelectionTools()
739 {
740   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
741   if (myGeomSelectionTools == NULL || myGeomSelectionTools->getMyStudy() != aStudy) {
742     delete myGeomSelectionTools;
743     myGeomSelectionTools = new GeomSelectionTools(aStudy);
744   }
745   return myGeomSelectionTools;
746 }
747
748 QString NETGENPluginGUI_HypothesisCreator::caption() const
749 {
750   return tr( QString( "NETGEN_%1_TITLE" ).arg(myIs2D?QString("2D"):QString("3D")).toLatin1().data() );
751 }
752
753 QPixmap NETGENPluginGUI_HypothesisCreator::icon() const
754 {
755   QString hypIconName = tr( QString("ICON_DLG_NETGEN_PARAMETERS%1").arg(myIs2D?QString("_2D"):QString("")).toLatin1().data() );
756   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
757 }
758
759 QString NETGENPluginGUI_HypothesisCreator::type() const
760 {
761   return tr( QString( "NETGEN_%1_HYPOTHESIS" ).arg(myIs2D?QString("2D"):QString("3D")).toLatin1().data() );
762 }
763
764 QString NETGENPluginGUI_HypothesisCreator::helpPage() const
765 {
766   return "netgen_2d_3d_hypo_page.html";
767 }