Salome HOME
Merge from V6_main 13/12/2012
[plugins/hexoticplugin.git] / src / GUI / HexoticPluginGUI_HypothesisCreator.cxx
1 // Copyright (C) 2007-2012  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 // ---
21 // File   : HexoticPluginGUI_HypothesisCreator.cxx
22 // Author : Lioka RAZAFINDRAZAKA (CEA)
23 // ---
24 //
25 #include "HexoticPluginGUI_HypothesisCreator.h"
26 #include "HexoticPluginGUI_Dlg.h"
27
28 #include <SMESHGUI_Utils.h>
29 #include <SMESHGUI_HypothesesUtils.h>
30
31 #include CORBA_SERVER_HEADER(HexoticPlugin_Algorithm)
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_FileDlg.h>
36 #include <SalomeApp_Tools.h>
37 #include <QtxIntSpinBox.h>
38
39 #include <QFrame>
40 #include <QGroupBox>
41 #include <QVBoxLayout>
42 #include <QGridLayout>
43 #include <QLineEdit>
44 #include <QLabel>
45 #include <QCheckBox>
46 #include <QPushButton>
47
48
49 HexoticPluginGUI_HypothesisCreator::HexoticPluginGUI_HypothesisCreator( const QString& theHypType )
50 : SMESHGUI_GenericHypothesisCreator( theHypType ),
51   myIs3D( true )
52 {
53 }
54
55 HexoticPluginGUI_HypothesisCreator::~HexoticPluginGUI_HypothesisCreator()
56 {
57 }
58
59 bool HexoticPluginGUI_HypothesisCreator::checkParams(QString& msg) const
60 {
61   msg.clear();
62   HexoticHypothesisData data_old, data_new;
63   readParamsFromHypo( data_old );
64   readParamsFromWidgets( data_new );
65
66   bool res = storeParamsToHypo( data_new );
67   if ( !res ) {
68     storeParamsToHypo( data_old );
69     return res;
70   }
71
72   res = data_new.myMinSize <= data_new.myMaxSize;
73   if ( !res ) {
74     msg = tr(QString("Min size (%1) is higher than max size (%2)").arg(data_new.myMinSize).arg(data_new.myMaxSize).toStdString().c_str());
75     return res;
76   }
77
78   res = data_new.myHexesMinLevel == 0  || \
79       ( data_new.myHexesMinLevel != 0  && (data_new.myHexesMinLevel < data_new.myHexesMaxLevel) );
80   if ( !res ) {
81     msg = tr(QString("Min hexes level (%1) is higher than max hexes level (%2)").arg(data_new.myHexesMinLevel).arg(data_new.myHexesMaxLevel).toStdString().c_str());
82     return res;
83   }
84
85   return true;
86 }
87
88 QFrame* HexoticPluginGUI_HypothesisCreator::buildFrame()
89 {
90   QFrame* fr = new QFrame( 0 );
91   QVBoxLayout* lay = new QVBoxLayout( fr );
92   lay->setMargin( 0 );
93   lay->setSpacing( 6 );
94
95   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
96   lay->addWidget( GroupC1 );
97   
98   QGridLayout* l = new QGridLayout( GroupC1 );
99   l->setSpacing( 6 );
100   l->setMargin( 11 );
101   
102   int row = 0;
103   myName = 0;
104   if( isCreation() ) {
105     l->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0, 1, 1 );
106     myName = new QLineEdit( GroupC1 );
107     l->addWidget( myName, row++, 1, 1, 2 );
108     myName->setMinimumWidth( 150 );
109   }
110
111   HexoticPlugin::HexoticPlugin_Hypothesis_var h =
112   HexoticPlugin::HexoticPlugin_Hypothesis::_narrow( initParamsHypothesis() );
113   
114   myStdWidget = new HexoticPluginGUI_StdWidget(GroupC1);
115   l->addWidget( myStdWidget, row++, 0, 1, 3 );
116   myStdWidget->onSdModeSelected(SD_MODE_4);
117
118   myIs3D = true;
119 //  resizeEvent();
120   return fr;
121 }
122
123 //=================================================================================
124 // function : resizeEvent [REDEFINED]
125 // purpose  :
126 //=================================================================================
127 void HexoticPluginGUI_HypothesisCreator::resizeEvent(QResizeEvent */*event*/) {
128     QSize scaledSize = myStdWidget->imageSdMode.size();
129     scaledSize.scale(myStdWidget->sdModeLabel->size(), Qt::KeepAspectRatioByExpanding);
130     if (!myStdWidget->sdModeLabel->pixmap() || scaledSize != myStdWidget->sdModeLabel->pixmap()->size())
131         myStdWidget->sdModeLabel->setPixmap(myStdWidget->imageSdMode.scaled(myStdWidget->sdModeLabel->size(),
132                         Qt::KeepAspectRatio,
133                         Qt::SmoothTransformation));
134 }
135
136 void HexoticPluginGUI_HypothesisCreator::retrieveParams() const
137 {
138   HexoticHypothesisData data;
139   readParamsFromHypo( data );
140   printData(data);
141
142   if( myName )
143     myName->setText( data.myName );
144
145   myStdWidget->myMinSize->setCleared(data.myMinSize == 0);
146   if (data.myMinSize == 0)
147     myStdWidget->myMinSize->setText("");
148   else
149     myStdWidget->myMinSize->setValue( data.myMinSize );
150
151   myStdWidget->myMaxSize->setCleared(data.myMaxSize == 0);
152   if (data.myMaxSize == 0)
153     myStdWidget->myMaxSize->setText("");
154   else
155     myStdWidget->myMaxSize->setValue( data.myMaxSize );
156
157   myStdWidget->myHexesMinLevel->setCleared(data.myHexesMinLevel == 0);
158   if (data.myHexesMinLevel == 0)
159     myStdWidget->myHexesMinLevel->setText("");
160   else
161     myStdWidget->myHexesMinLevel->setValue( data.myHexesMinLevel );
162
163   myStdWidget->myHexesMaxLevel->setCleared(data.myHexesMaxLevel == 0);
164   if (data.myHexesMaxLevel == 0)
165     myStdWidget->myHexesMaxLevel->setText("");
166   else
167     myStdWidget->myHexesMaxLevel->setValue( data.myHexesMaxLevel );
168
169   myStdWidget->myHexoticIgnoreRidges->setChecked( data.myHexoticIgnoreRidges );
170   myStdWidget->myHexoticInvalidElements->setChecked( data.myHexoticInvalidElements );
171   
172   myStdWidget->myHexoticSharpAngleThreshold->setCleared(data.myHexoticSharpAngleThreshold == 0);
173   if (data.myHexoticSharpAngleThreshold == 0)
174     myStdWidget->myHexoticSharpAngleThreshold->setText("");
175   else
176     myStdWidget->myHexoticSharpAngleThreshold->setValue( data.myHexoticSharpAngleThreshold );
177
178   myStdWidget->myHexoticNbProc->setValue( data.myHexoticNbProc );
179   myStdWidget->myHexoticWorkingDir->setText( data.myHexoticWorkingDir );
180
181   myStdWidget->myHexoticVerbosity->setValue( data.myHexoticVerbosity );
182
183   myStdWidget->myHexoticMaxMemory->setValue( data.myHexoticMaxMemory );
184
185   myStdWidget->myHexoticSdMode->setCurrentIndex(data.myHexoticSdMode);
186
187   std::cout << "myStdWidget->myMinSize->value(): " << myStdWidget->myMinSize->value() << std::endl;
188   std::cout << "myStdWidget->myMaxSize->value(): " << myStdWidget->myMaxSize->value() << std::endl;
189   std::cout << "myStdWidget->myHexesMinLevel->value(): " << myStdWidget->myHexesMinLevel->value() << std::endl;
190   std::cout << "myStdWidget->myHexesMaxLevel->value(): " << myStdWidget->myHexesMaxLevel->value() << std::endl;
191   std::cout << "myStdWidget->myHexoticSharpAngleThreshold->value(): " << myStdWidget->myHexoticSharpAngleThreshold->value() << std::endl;
192
193 }
194
195 void HexoticPluginGUI_HypothesisCreator::printData( HexoticHypothesisData& data) const
196 {
197   QString valStr;
198   valStr += tr("Hexotic_MIN_SIZE") + " = " + QString::number( data.myMinSize )   + "; ";
199   valStr += tr("Hexotic_MAX_SIZE") + " = " + QString::number( data.myMaxSize ) + "; ";
200   valStr += tr("Hexotic_HEXES_MIN_LEVEL") + " = " + QString::number( data.myHexesMinLevel )   + "; ";
201   valStr += tr("Hexotic_HEXES_MAX_LEVEL") + " = " + QString::number( data.myHexesMaxLevel ) + "; ";
202   valStr += tr("Hexotic_IGNORE_RIDGES")  + " = " + QString::number( data.myHexoticIgnoreRidges ) + "; ";
203   valStr += tr("Hexotic_INVALID_ELEMENTS")  + " = " + QString::number( data.myHexoticInvalidElements ) + "; ";
204   valStr += tr("Hexotic_SHARP_ANGLE_THRESHOLD") + " = " + QString::number( data.myHexoticSharpAngleThreshold ) + "; ";
205   valStr += tr("Hexotic_NB_PROC") + " = " + QString::number( data.myHexoticNbProc ) + "; ";
206   valStr += tr("Hexotic_WORKING_DIR") + " = " + data.myHexoticWorkingDir + "; ";
207   valStr += tr("Hexotic_VERBOSITY") + " = " + QString::number( data.myHexoticVerbosity ) + "; ";
208   valStr += tr("Hexotic_MAX_MEMORY") + " = " + QString::number( data.myHexoticMaxMemory ) + "; ";
209   valStr += tr("Hexotic_SD_MODE") + " = " + QString::number( data.myHexoticSdMode ) + "; ";
210
211   std::cout << "Data: " << valStr.toStdString() << std::endl;
212 }
213
214 QString HexoticPluginGUI_HypothesisCreator::storeParams() const
215 {
216   HexoticHypothesisData data;
217   readParamsFromWidgets( data );
218   storeParamsToHypo( data );
219
220   QString valStr;
221   valStr += tr("Hexotic_MIN_SIZE") + " = " + QString::number( data.myMinSize )   + "; ";
222   valStr += tr("Hexotic_MAX_SIZE") + " = " + QString::number( data.myMaxSize ) + "; ";
223   valStr += tr("Hexotic_HEXES_MIN_LEVEL") + " = " + QString::number( data.myHexesMinLevel )   + "; ";
224   valStr += tr("Hexotic_HEXES_MAX_LEVEL") + " = " + QString::number( data.myHexesMaxLevel ) + "; ";
225   valStr += tr("Hexotic_IGNORE_RIDGES")  + " = " + QString::number( data.myHexoticIgnoreRidges ) + "; ";
226   valStr += tr("Hexotic_INVALID_ELEMENTS")  + " = " + QString::number( data.myHexoticInvalidElements ) + "; ";
227   valStr += tr("Hexotic_SHARP_ANGLE_THRESHOLD") + " = " + QString::number( data.myHexoticSharpAngleThreshold ) + "; ";
228   valStr += tr("Hexotic_NB_PROC") + " = " + QString::number( data.myHexoticNbProc ) + "; ";
229   valStr += tr("Hexotic_WORKING_DIR") + " = " + data.myHexoticWorkingDir + "; ";
230   valStr += tr("Hexotic_VERBOSITY") + " = " + QString::number( data.myHexoticVerbosity) + "; ";
231   valStr += tr("Hexotic_MAX_MEMORY") + " = " + QString::number( data.myHexoticMaxMemory ) + "; ";
232   valStr += tr("Hexotic_SD_MODE") + " = " + QString::number( data.myHexoticSdMode) + "; ";
233
234 //  std::cout << "Data: " << valStr.toStdString() << std::endl;
235
236   return valStr;
237 }
238
239 bool HexoticPluginGUI_HypothesisCreator::readParamsFromHypo( HexoticHypothesisData& h_data ) const
240 {
241   HexoticPlugin::HexoticPlugin_Hypothesis_var h =
242     HexoticPlugin::HexoticPlugin_Hypothesis::_narrow( initParamsHypothesis() );
243
244   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
245   h_data.myName = isCreation() && data ? data->Label : "";
246   h_data.myMinSize = h->GetMinSize();
247   h_data.myMaxSize = h->GetMaxSize();
248   h_data.myHexesMinLevel = h->GetHexesMinLevel();
249   h_data.myHexesMaxLevel = h->GetHexesMaxLevel();
250   h_data.myHexoticIgnoreRidges = h->GetHexoticIgnoreRidges();
251   h_data.myHexoticInvalidElements = h->GetHexoticInvalidElements();
252   h_data.myHexoticSharpAngleThreshold = h->GetHexoticSharpAngleThreshold();
253   h_data.myHexoticNbProc = h->GetHexoticNbProc();
254   h_data.myHexoticWorkingDir = h->GetHexoticWorkingDirectory();
255   h_data.myHexoticVerbosity = h->GetHexoticVerbosity();
256   h_data.myHexoticMaxMemory = h->GetHexoticMaxMemory();
257   h_data.myHexoticSdMode = h->GetHexoticSdMode()-1;
258
259   return true;
260 }
261
262 bool HexoticPluginGUI_HypothesisCreator::storeParamsToHypo( const HexoticHypothesisData& h_data ) const
263 {
264   HexoticPlugin::HexoticPlugin_Hypothesis_var h =
265     HexoticPlugin::HexoticPlugin_Hypothesis::_narrow( hypothesis() );
266
267   bool ok = true;
268
269   try
270   {
271     if( isCreation() )
272       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().constData() );
273
274     h->SetMinSize( h_data.myMinSize );
275     h->SetMaxSize( h_data.myMaxSize );
276     h->SetHexesMinLevel( h_data.myHexesMinLevel );
277     h->SetHexesMaxLevel( h_data.myHexesMaxLevel );
278     h->SetHexoticIgnoreRidges( h_data.myHexoticIgnoreRidges );
279     h->SetHexoticInvalidElements( h_data.myHexoticInvalidElements );
280     h->SetHexoticSharpAngleThreshold( h_data.myHexoticSharpAngleThreshold );
281     h->SetHexoticNbProc( h_data.myHexoticNbProc );
282     h->SetHexoticWorkingDirectory( h_data.myHexoticWorkingDir.toLatin1().constData() );
283     h->SetHexoticVerbosity( h_data.myHexoticVerbosity );
284     h->SetHexoticMaxMemory( h_data.myHexoticMaxMemory );
285     h->SetHexoticSdMode( h_data.myHexoticSdMode+1 );
286   }
287   catch(const SALOME::SALOME_Exception& ex)
288   {
289     SalomeApp_Tools::QtCatchCorbaException(ex);
290     ok = false;
291   }
292   return ok;
293 }
294
295 bool HexoticPluginGUI_HypothesisCreator::readParamsFromWidgets( HexoticHypothesisData& h_data ) const
296 {
297   h_data.myName    = myName ? myName->text() : "";
298
299   h_data.myHexoticIgnoreRidges = myStdWidget->myHexoticIgnoreRidges->isChecked();
300   h_data.myHexoticInvalidElements = myStdWidget->myHexoticInvalidElements->isChecked();
301
302   h_data.myHexoticNbProc = myStdWidget->myHexoticNbProc->value();
303   h_data.myHexoticWorkingDir = myStdWidget->myHexoticWorkingDir->text();
304   h_data.myHexoticVerbosity = myStdWidget->myHexoticVerbosity->value();
305   h_data.myHexoticMaxMemory = myStdWidget->myHexoticMaxMemory->value();
306   h_data.myHexoticSdMode = myStdWidget->myHexoticSdMode->currentIndex();
307
308   h_data.myMinSize = myStdWidget->myMinSize->text().isEmpty() ? 0.0 : myStdWidget->myMinSize->value();
309   h_data.myMaxSize = myStdWidget->myMaxSize->text().isEmpty() ? 0.0 : myStdWidget->myMaxSize->value();
310   h_data.myHexesMinLevel = myStdWidget->myHexesMinLevel->text().isEmpty() ? 0 : myStdWidget->myHexesMinLevel->value();
311   h_data.myHexesMaxLevel = myStdWidget->myHexesMaxLevel->text().isEmpty() ? 0 : myStdWidget->myHexesMaxLevel->value();
312   h_data.myHexoticSharpAngleThreshold = myStdWidget->myHexoticSharpAngleThreshold->text().isEmpty() ? 0 : myStdWidget->myHexoticSharpAngleThreshold->value();
313
314   printData(h_data);
315
316   return true;
317 }
318
319 QString HexoticPluginGUI_HypothesisCreator::caption() const
320 {
321   return myIs3D ? tr( "Hexotic_3D_TITLE" ) : tr( "Hexotic_3D_TITLE" ); // ??? 3D/2D ???
322 }
323
324 QPixmap HexoticPluginGUI_HypothesisCreator::icon() const
325 {
326   QString hypIconName = myIs3D ? tr( "ICON_DLG_Hexotic_PARAMETERS" ) : tr( "ICON_DLG_Hexotic_PARAMETERS" );
327   return SUIT_Session::session()->resourceMgr()->loadPixmap( "HexoticPLUGIN", hypIconName );
328 }
329
330 QString HexoticPluginGUI_HypothesisCreator::type() const
331 {
332   return myIs3D ? tr( "Hexotic_3D_HYPOTHESIS" ) : tr( "Hexotic_3D_HYPOTHESIS" ); // ??? 3D/2D ???
333 }
334
335 QString HexoticPluginGUI_HypothesisCreator::helpPage() const
336 {
337   return "hexotic_hypo_page.html";
338 }