Salome HOME
new version of select parameters IHM
[modules/filter.git] / src / FILTERGUI / SelectParams.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <stdlib.h>
22 #include <math.h>
23 #include "SelectParams.h"
24 #include "utilities.h"
25
26 #include <SUIT_FileDlg.h>
27
28 #include "Utils_SALOME_Exception.hxx"
29 #include <SalomeApp_Tools.h>
30
31 #include <qlabel.h>
32 #include <qgroupbox.h>
33 #include <qframe.h>
34 #include <qlayout.h>
35 #include <qlineedit.h>
36 #include <qbuttongroup.h>
37 #include <qradiobutton.h>
38 #include <qpushbutton.h>
39 #include <qfiledialog.h>
40
41 SelectParams::SelectParams(SelectField *sel)
42   : _size(1024),
43   _myExpr( 0 ),
44   _myFunc( 0 ),
45   _myFieldB( 0 ),
46   _myLinear( 0 ),
47   _myLog( 0 ),
48   _myHisto( 0 ),
49   _myNbThresh( 0 ),
50   _myCutNeg( 0 ),
51   _myOneThresh( 0 ),
52   _myTwoThresh( 0 ),
53   _myArea( 0 ),
54   _myVThresh( 0 ),
55   _myOutFile( 0 ),
56   _myOFB( 0 ),
57   _myProc( 0 ),
58   _myFScale( 0 ),
59   _myGroupLayout( 0 ),
60   _myGroupLayout2( 0 ),
61   _myPlot( 0 ),
62   _GroupC1( 0 ),
63   _GroupC2( 0 ),
64   _lay( 0 ),
65   _fr( 0 )
66 {
67   _x = new double[_size];
68   _y = new double[_size];
69   if(sel){
70     _inputFile = sel->getFile();
71     _inputMesh = sel->getMesh();
72     _inputField = sel->getField();
73     _inputTS = sel->getTimeStep();
74     if( _inputMesh.isNull() || _inputField.isNull() ){
75       MESSAGE("Select an input Field in MED file before filtering!!");
76       throw SALOME_Exception("Salome Exception");
77     }
78   }
79   else{
80     MESSAGE("Select an input Field in MED file before filtering!!");
81     throw SALOME_Exception("Salome Exception");
82   }
83
84 }
85
86 SelectParams::~SelectParams()
87 {
88   cout << "destructor called" << endl;
89 //   delete _x;
90 //   delete _y;
91 //   delete _myExpr;
92 //   delete _myFunc;
93 //   delete _myFieldB;
94 //   delete _myLinear;
95 //   delete _myHisto;
96 //   delete _myNbThresh;
97 //   delete _myCutNeg;
98 //   delete _myOneThresh;
99 //   delete _myTwoThresh;
100 //   delete _myArea;
101 //   delete _myVThresh;
102 //   delete _myOutFile;
103 //   delete _myOFB;
104 //   delete _myProc;
105 //   delete _myFScale;
106 //   delete _myGroupLayout;
107 //   delete _myGroupLayout2;
108 //   delete _myPlot;
109 //   delete _GroupC1;
110 //   delete _GroupC2;
111 //   delete _lay;
112 //   delete _fr;
113 }
114
115 QFrame* SelectParams::buildFrame()
116 {
117   QFrame* _fr = new QFrame( 0, "myframe" );
118   QGridLayout* _lay = new QGridLayout( _fr, 1, 2 );
119
120   QGroupBox* _GroupC1 = new QGroupBox( _fr, "GroupC1" );
121   _lay->addWidget( _GroupC1,0,0 );
122
123   _GroupC1->setTitle( tr( "FILTER_PARAMS"  ) );
124   _GroupC1->setColumnLayout(0, Qt::Vertical );
125   _GroupC1->layout()->setSpacing( 0 );
126   _GroupC1->layout()->setMargin( 0 );
127   _myGroupLayout = new QGridLayout( _GroupC1->layout() );
128   _myGroupLayout->setAlignment( Qt::AlignTop );
129   _myGroupLayout->setSpacing( 6 );
130   _myGroupLayout->setMargin( 11 );
131   _myGroupLayout->setColStretch( 0, 0 );
132   _myGroupLayout->setColStretch( 1, 1 );
133
134   int row = 0;
135
136   QString qs1(tr("FILTER_INPUT_FILE"));
137   qs1.append(basename(_inputFile));
138   _myGroupLayout->addWidget( new QLabel( qs1, _GroupC1 ), row, 0 );
139   row++;
140
141   QString qs2(tr("FILTER_INPUT_MESH"));
142   qs2.append(_inputMesh);
143   _myGroupLayout->addWidget( new QLabel( qs2, _GroupC1 ), row, 0 );
144   row++;
145
146   QString qs3(tr("FILTER_INPUT_FIELD"));
147   qs3.append(_inputField);
148   _myGroupLayout->addWidget( new QLabel( qs3, _GroupC1 ), row, 0 );
149   row++;
150
151   QString qs4(tr("FILTER_INPUT_TS"));
152   char strTS[128];
153   sprintf(strTS,"%d\0",_inputTS);
154   qs4.append(strTS);
155   _myGroupLayout->addWidget( new QLabel( qs4, _GroupC1 ), row, 0 );
156   row++;
157
158   // 0)  field function to calculate histogram (radiogroup)
159   _myFunc = new QButtonGroup( tr("FILTER_SELECT_FUNC"), _GroupC1 );
160   _myFunc->setExclusive( true );
161   _myFunc->setColumnLayout( 0, Qt::Horizontal );
162
163   _myFieldB = new QRadioButton( tr("FILTER_FIELD"), _myFunc );
164   _myFieldB->setChecked(true);
165
166   QGridLayout* convLay = new QGridLayout( _myFunc->layout() );
167   convLay->addWidget( _myFieldB, 0, 0 );
168   convLay->addWidget( _myCutNeg = new QRadioButton( tr("FILTER_GRADIENT"), _myFunc ), 0, 1 );
169   _myGroupLayout->addWidget( _myFunc, row, 0 );
170   row++;
171
172   // 1)  display histogram button (pushbutton)
173   _myHisto = new QPushButton( "", _GroupC1 );
174   _myHisto->setText(tr("FILTER_DISPLAY_HISTO"));
175   _myHisto->setAutoDefault(TRUE);
176   _myHisto->setDefault(TRUE);
177   _myGroupLayout->addWidget( _myHisto, row, 0 );
178   row++;
179
180   // 2)  scale of histogram (radiogroup)
181   _myFScale = new QButtonGroup( tr("FILTER_TYPE_DISPLAY"), _GroupC1 );
182   _myFScale->setExclusive( true );
183   _myFScale->setColumnLayout( 0, Qt::Horizontal );
184
185   _myLinear = new QRadioButton( tr("FILTER_LINEAR"), _myFScale );
186   _myLinear->setChecked(true);
187   _myLog = new QRadioButton( tr("FILTER_LOG"), _myFScale );
188   _myFScale->setDisabled(true);
189
190   QGridLayout* scaleLay = new QGridLayout( _myFScale->layout() );
191   scaleLay->addWidget( _myLinear, 0, 0 );
192   scaleLay->addWidget( _myLog, 0, 1 );
193   _myGroupLayout->addWidget( _myFScale, row, 0 );
194   row++;
195
196   // 3)  number of thresholds (radiogroup)
197   _myNbThresh = new QButtonGroup( tr("FILTER_SEL_THRESH"), _GroupC1 );
198   _myNbThresh->setExclusive( true );
199   _myNbThresh->setColumnLayout( 0, Qt::Horizontal );
200   QGridLayout* nbtLay = new QGridLayout( _myNbThresh->layout() );
201   nbtLay->addWidget( _myOneThresh = new QRadioButton( tr("FILTER_ONE_THRESH"), _myNbThresh ), 0, 0 );
202   nbtLay->addWidget( _myTwoThresh = new QRadioButton( tr("FILTER_TWO_THRESH"), _myNbThresh ), 0, 1 );
203   _myGroupLayout->addWidget( _myNbThresh, row, 0 );
204
205   _myOneThresh->setChecked(true);
206   _myNbThresh->setDisabled(true);
207   row++;
208
209   // 4)  reference area on thresholds (radiogroup)
210   _myArea = new QButtonGroup( tr("FILTER_REF_AREA"), _GroupC1 );
211   _myArea->setExclusive( true );
212   _myArea->setColumnLayout( 0, Qt::Horizontal );
213   QGridLayout* areaLay = new QGridLayout( _myArea->layout() );
214   areaLay->addWidget( _myInt = new QRadioButton( tr("FILTER_BOTTOM"), _myArea ), 0, 0 );
215   areaLay->addWidget( _myExt = new QRadioButton( tr("FILTER_UP"), _myArea ), 0, 1 );
216   _myGroupLayout->addWidget( _myArea, row, 0 );
217
218   _myExt->setChecked(true);
219   _myArea->setDisabled(true);
220   row++;
221
222   // 5)  threshold values (line edit)
223   _myVThresh = new QButtonGroup( tr("FILTER_TRESH_VAL"), _GroupC1 );
224   _myVThresh->setExclusive( true );
225   _myVThresh->setColumnLayout( 0, Qt::Horizontal );
226   QGridLayout* ftLay = new QGridLayout( _myVThresh->layout() );
227   ftLay->addWidget( _myLFT = new QLabel( tr("FILTER_VAL_TRESH") , _myVThresh ), 0, 0 );
228   ftLay->addWidget( _myLEFT = new QLineEdit( "", _myVThresh ), 0, 1 );
229   ftLay->addWidget( _myLST = new QLabel( tr("FILTER_VAL_2_TRESH") , _myVThresh ), 1, 0 );
230   ftLay->addWidget( _myLEST = new QLineEdit( "", _myVThresh ), 1, 1 );
231   _myGroupLayout->addWidget( _myVThresh, row, 0 );
232
233   _myVThresh->setDisabled(true);
234   _myLST->hide();
235   _myLEST->hide();
236   row++;
237
238   // 6)  output file name (line edit)
239   _myOutFile = new QButtonGroup( tr("FILTER_OUT_FILE"), _GroupC1 );
240   _myOutFile->setExclusive( true );
241   _myOutFile->setColumnLayout( 0, Qt::Horizontal );
242
243   _myOFB = new QPushButton( "", _myOutFile );
244   _myOFB->setText(tr("FILTER_BROWSE"));
245   _myOFB->setAutoDefault(TRUE);
246
247   QGridLayout* outLay = new QGridLayout( _myOutFile->layout() );
248   outLay->addWidget( _myOFB, 0, 0 );
249   outLay->addWidget( _myOFN = new QLineEdit( "", _myOutFile ), 0, 1 );
250   _myGroupLayout->addWidget( _myOutFile, row, 0 );
251
252   _myOutFile->setDisabled(true);
253   row++;
254
255   // 8)  process button (pushbutton)
256   _myProc = new QPushButton( "", _GroupC1 );
257   _myProc->setText(tr("FILTER_PROCESS"));
258   _myProc->setAutoDefault(TRUE);
259   _myGroupLayout->addWidget( _myProc, row, 0 );
260   _myProc->setDisabled(true);
261   row++;
262
263   _GroupC2 = new QGroupBox( _fr, "GroupC2" );
264   _lay->addWidget( _GroupC2,0,1 );
265
266   _GroupC2->setTitle( tr( "FILTER_HISTO" ) );
267   _GroupC2->setColumnLayout(0, Qt::Vertical );
268   _GroupC2->layout()->setSpacing( 0 );
269   _GroupC2->layout()->setMargin( 0 );
270   _myGroupLayout2 = new QGridLayout( _GroupC2->layout() );
271
272   // 9)  histogram curve
273   _myPlot = new QwtPlot(_GroupC2);
274   _myHistoCurve = _myPlot->insertCurve( QString() );
275   _myPlot->setCurvePen( _myHistoCurve, QPen( Qt::red, 1 ) );
276   _myPlot->setCurveTitle( _myHistoCurve, "Histogram" );
277
278   _myGroupLayout2->addWidget( _myPlot, 0, 0 );
279
280   // 10)  reduction rate (label)
281   QString qs5(tr("FILTER_RED_RATE"));
282   qs5.append(" = 0.5");
283   _myLRR = new QLabel( qs5, _GroupC2 );
284   _myGroupLayout2->addWidget( _myLRR, 1, 0 );
285
286   _GroupC2->hide();
287
288   _myHistoFThresh = _myPlot->insertCurve( QString() );
289   _myPlot->setCurvePen( _myHistoFThresh, QPen( Qt::black, 1 ) );
290   _myHistoSThresh = _myPlot->insertCurve( QString() );
291   _myPlot->setCurvePen( _myHistoSThresh, QPen( Qt::black, 1 ) );
292
293   connect( _myHisto, SIGNAL(clicked()), this, SLOT(updateHisto()));
294   connect( _myLinear, SIGNAL(clicked()), this, SLOT(scaleSelected()));
295   connect( _myLog, SIGNAL(clicked()), this, SLOT(scaleSelected()));
296   connect( _myOneThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
297   connect( _myTwoThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
298   connect( _myInt, SIGNAL(clicked()), this, SLOT(areaSelected()));
299   connect( _myExt, SIGNAL(clicked()), this, SLOT(areaSelected()));
300   connect( _myLEFT, SIGNAL(returnPressed()), this, SLOT(enterFThresh()));
301   connect( _myLEST, SIGNAL(returnPressed()), this, SLOT(enterSThresh()));
302   connect( _myPlot, SIGNAL(plotMouseMoved(const QMouseEvent &)), this, SLOT(moveThresh(const QMouseEvent &)));
303   connect( _myOFB, SIGNAL(clicked()), this, SLOT(getOutFileName()));
304   connect( _myProc, SIGNAL(clicked()), this, SLOT(process()));
305
306   return _fr;
307 }
308
309 void SelectParams::scaleSelected()
310 {
311   if( _myLinear->isChecked() )
312     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
313   else
314     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
315   _myPlot->replot();
316 }
317
318 void SelectParams::nbThreshSelected()
319 {
320   if( _myOneThresh->isChecked() ){
321     _myInt->setText(tr("FILTER_BOTTOM"));
322     _myExt->setText(tr("FILTER_UP"));
323     _myLFT->setText(tr("FILTER_VAL_TRESH"));
324     _myLST->hide();
325     _myLEST->hide();
326     displayFThresh();
327     clearSThresh();
328   }
329   else{
330     _myInt->setText(tr("FILTER_INT"));
331     _myExt->setText(tr("FILTER_EXT"));
332     _myLFT->setText(tr("FILTER_VAL_1_TRESH"));
333     if(_myLST->isHidden())
334       _myLST->show();
335     if(_myLEST->isHidden())
336       _myLEST->show();
337     displayFThresh();
338     displaySThresh();
339   }
340   calcRateRed();
341 }
342
343 void SelectParams::areaSelected()
344 {
345   calcRateRed();
346 }
347
348 void SelectParams::enterFThresh()
349 {
350   displayFThresh();
351   calcRateRed();
352 }
353
354 void SelectParams::enterSThresh()
355 {
356   displaySThresh();
357   calcRateRed();
358 }
359
360 void SelectParams::calcHisto()
361 {
362   char strth[128];
363   _xmin = -50.0;
364   _xmax = 50.0;
365   _ymin = 1.0;
366   _ymax = 0.0;
367
368   for(int i=0;i<_size;i++){
369     _x[i]=_xmin+(i*(_xmax-_xmin))/_size;
370     _y[i]=80.*exp(-_x[i]*_x[i]/400.);
371     if( _y[i] > _ymax )
372       _ymax = _y[i];
373   }
374   _fthresh = (_xmin + _xmax)/2.0;
375   _sthresh = (_xmin + 3.*_xmax)/4.0;
376   sprintf(strth,"%g",_fthresh);
377   _myLEFT->setText(QString(strth));
378   sprintf(strth,"%g",_sthresh);
379   _myLEST->setText(QString(strth));
380 }
381
382 void SelectParams::displayHisto()
383 {
384   if(!_myFScale->isEnabled())
385     _myFScale->setEnabled(true);
386   if(!_myNbThresh->isEnabled())
387     _myNbThresh->setEnabled(true);
388   if(!_myArea->isEnabled())
389     _myArea->setEnabled(true);
390   if(!_myVThresh->isEnabled())
391     _myVThresh->setEnabled(true);
392   if(!_myOutFile->isEnabled())
393     _myOutFile->setEnabled(true);
394   if(!_myProc->isEnabled())
395     _myProc->setEnabled(true);
396   _myPlot->setAxisScale( _myPlot->curveXAxis( _myHistoCurve ), _xmin, _xmax );
397   _myPlot->setAxisScale( _myPlot->curveYAxis( _myHistoCurve ), _ymin, _ymax );
398   _myPlot->setCurveData( _myHistoCurve, _x, _y, _size );
399   if( _myLinear->isChecked() )
400     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
401   else
402     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
403   _qmap = _myPlot->canvasMap(_myPlot->curveXAxis( _myHistoCurve ));
404   _qmap.setDblRange(_xmin,_xmax);
405   _myPlot->replot();
406   if(_GroupC2->isHidden())
407     _GroupC2->show();
408 }
409
410 void SelectParams::updateHisto()
411 {
412   calcHisto();
413   displayHisto();
414   displayFThresh();
415   if( _myTwoThresh->isChecked() )
416     displaySThresh();
417   calcRateRed();
418 }
419
420 void SelectParams::displayFThresh()
421 {
422   _fthresh = atof(_myLEFT->text());
423
424   for(int i=0;i<100;i++){
425     _xft[i]=_fthresh;
426     _yft[i]=((i-1)*_ymax)/100.0;
427   }
428   _myPlot->setCurveData( _myHistoFThresh, _xft, _yft, 100 );
429   _myPlot->replot();
430 }
431
432 void SelectParams::displaySThresh()
433 {
434   _sthresh = atof(_myLEST->text());
435
436   for(int i=0;i<100;i++){
437     _xst[i]=_sthresh;
438     _yst[i]=((i-1)*_ymax)/100.0;
439   }
440   _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
441   _myPlot->replot();
442 }
443
444 void SelectParams::clearSThresh()
445 {
446   _sthresh = atof(_myLEST->text());
447
448   for(int i=0;i<100;i++){
449     _xst[i]=_sthresh;
450     _yst[i]=0.0;
451   }
452   _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
453   _myPlot->replot();
454 }
455
456 void SelectParams::moveThresh(const QMouseEvent &e)
457 {
458   char strth[128];
459   int delta = abs(e.x()-_qmap.transform(_fthresh));
460   if( delta < 5 ){
461     sprintf(strth,"%g",_qmap.invTransform(e.x()));
462     _myLEFT->setText(QString(strth));
463     displayFThresh();
464   }
465   else if( _myTwoThresh->isChecked() ){
466     delta = abs(e.x()-_qmap.transform(_sthresh));
467     if( delta < 5 ){
468       sprintf(strth,"%g",_qmap.invTransform(e.x()));
469       _myLEST->setText(QString(strth));
470       displaySThresh();
471     }
472   }
473   calcRateRed();
474 }
475
476 void SelectParams::getOutFileName()
477 {
478   QString file = QFileDialog::getSaveFileName("",
479                                               "*.med",
480                                               _myOFB,
481                                               "save file dialog",
482                                               "Choose a file to save" );
483   if(!file.isEmpty())
484     _myOFN->setText(file);
485
486   // Selection du Fichier
487 //   file = SUIT_FileDlg::getFileName(application()->desktop(),
488 //                                 "",
489 //                                 filtersList,
490 //                                 "Output MED file name",
491 //                                 true);
492 }
493
494 void SelectParams::process()
495 {
496   string command;
497
498   MESSAGE("Input MED File : "<<_inputFile);
499   MESSAGE("Input Mesh : "<<_inputMesh);
500   MESSAGE("Input Field : "<<_inputField);
501   MESSAGE("Input Time Step : "<<_inputTS);
502   MESSAGE("Output file name: " << _myOFN->text() );
503   command = "cd /export/home/secher/filtoo/example;filtoo -f s -o sf > /tmp/filter.log";
504   MESSAGE(command);
505   system(command.c_str());
506 //   delete this;
507 }
508
509 void SelectParams::calcRateRed()
510 {
511   int i1, i2, i;
512   int atot=0, asel=0, atot1;
513   double rateRed=0.0;
514   char strth[128];
515
516   i1 = (int)((double)_size * ( _fthresh - _xmin ) / ( _xmax - _xmin ));
517   if( _myOneThresh->isChecked() ){
518     for(i=0;i<i1;i++)
519       atot += (int)_y[i];
520     asel = atot;
521     for(i=i1;i<_size;i++)
522       atot += (int)_y[i];
523     if( _myExt->isChecked() )
524       asel = atot - asel;
525   }
526   else{
527     i2 = (int)((double)_size * ( _sthresh - _xmin ) / ( _xmax - _xmin ));
528     if( i2 < i1 ){
529       i=i1;
530       i1=i2;
531       i2=i;
532     }
533     for(i=0;i<i1;i++)
534       atot += (int)_y[i];
535     atot1=atot;
536     for(i=i1;i<i2;i++)
537       atot += (int)_y[i];
538     asel = atot - atot1;
539     for(i=i2;i<_size;i++)
540       atot += (int)_y[i];
541     if( _myExt->isChecked() )
542       asel = atot - asel;
543   }
544   rateRed = (double)asel / (double) atot;
545
546   QString qs(tr("FILTER_RED_RATE"));
547   char str[128];
548   sprintf(str," = %4.2g",rateRed);
549   qs.append(str);
550   _myLRR->setText( qs );
551
552 }
553