1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SelectParams.h"
24 #include "utilities.h"
26 #include <SUIT_FileDlg.h>
28 #include "Utils_SALOME_Exception.hxx"
29 #include <SalomeApp_Tools.h>
32 #include <qgroupbox.h>
35 #include <qlineedit.h>
36 #include <qbuttongroup.h>
37 #include <qradiobutton.h>
38 #include <qpushbutton.h>
39 #include <qfiledialog.h>
41 SelectParams::SelectParams(FilterGUI* theModule,SelectField *sel,
43 bool modal, WFlags fl)
44 : QDialog(FILTER::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose), _size(128)
46 // Allocate histogram arrays
47 _x = new double[_size];
48 _y = new double[_size];
51 // read reference field values
52 ::MEDMEM::MED *med = sel->getMED();
54 // Get reference field and time step
55 _inputFile = sel->getFile();
56 _inputMesh = sel->getMesh();
57 _inputField = sel->getField();
58 _inputTS = sel->getTimeStep();
59 // if no reference field selection: throw exception
60 if( _inputMesh.isNull() || _inputField.isNull() ){
61 MESSAGE("Select an input Field in MED file before filtering!!");
62 throw SALOME_Exception("Salome Exception");
65 deque<DT_IT_> myIteration = med->getFieldIteration (_inputField);
66 MEDMEM::FIELD_* field = med->getField(_inputField,myIteration[_inputTS].dt,myIteration[_inputTS].it);
67 if (dynamic_cast<MEDMEM::FIELD<double>*>(field)){
68 _myDField = new MEDMEM::FIELD<double>(MEDMEM::MED_DRIVER,_inputFile,_inputField,myIteration[_inputTS].dt,myIteration[_inputTS].it);
72 _myIField = new MEDMEM::FIELD<int>(MEDMEM::MED_DRIVER,_inputFile,_inputField,myIteration[_inputTS].dt,myIteration[_inputTS].it);
77 // if no reference field selection: throw exception
79 MESSAGE("Select an input Field in MED file before filtering!!");
80 throw SALOME_Exception("Salome Exception");
85 SelectParams::~SelectParams()
87 cout << "SelectParams: destructor called" << endl;
91 delete _myIField->getSupport();
95 delete _myDField->getSupport();
100 void SelectParams::buildFrame()
102 // build widgets for select filtering parameters
103 QGridLayout* _lay = new QGridLayout( this, 1, 2 );
105 QGroupBox* _GroupC1 = new QGroupBox( this, "GroupC1" );
106 _lay->addWidget( _GroupC1,0,0 );
108 _GroupC1->setTitle( tr( "FILTER_PARAMS" ) );
109 _GroupC1->setColumnLayout(0, Qt::Vertical );
110 _GroupC1->layout()->setSpacing( 0 );
111 _GroupC1->layout()->setMargin( 0 );
112 _myGroupLayout = new QGridLayout( _GroupC1->layout() );
113 _myGroupLayout->setAlignment( Qt::AlignTop );
114 _myGroupLayout->setSpacing( 6 );
115 _myGroupLayout->setMargin( 11 );
116 _myGroupLayout->setColStretch( 0, 0 );
117 _myGroupLayout->setColStretch( 1, 1 );
121 QString qs1(tr("FILTER_INPUT_FILE"));
122 qs1.append(basename(_inputFile));
123 _myGroupLayout->addWidget( new QLabel( qs1, _GroupC1 ), row, 0 );
126 QString qs2(tr("FILTER_INPUT_MESH"));
127 qs2.append(_inputMesh);
128 _myGroupLayout->addWidget( new QLabel( qs2, _GroupC1 ), row, 0 );
131 QString qs3(tr("FILTER_INPUT_FIELD"));
132 qs3.append(_inputField);
133 _myGroupLayout->addWidget( new QLabel( qs3, _GroupC1 ), row, 0 );
136 QString qs4(tr("FILTER_INPUT_TS"));
138 sprintf(strTS,"%d\0",_inputTS);
140 _myGroupLayout->addWidget( new QLabel( qs4, _GroupC1 ), row, 0 );
143 // 0) field function to calculate histogram (radiogroup)
144 _myFunc = new QButtonGroup( tr("FILTER_SELECT_FUNC"), _GroupC1 );
145 _myFunc->setExclusive( true );
146 _myFunc->setColumnLayout( 0, Qt::Horizontal );
148 _myFieldB = new QRadioButton( tr("FILTER_FIELD"), _myFunc );
149 _myFieldB->setChecked(true);
151 QGridLayout* convLay = new QGridLayout( _myFunc->layout() );
152 convLay->addWidget( _myFieldB, 0, 0 );
153 convLay->addWidget( _myCutNeg = new QRadioButton( tr("FILTER_GRADIENT"), _myFunc ), 0, 1 );
154 _myGroupLayout->addWidget( _myFunc, row, 0 );
157 // 1) display histogram button (pushbutton)
158 _myHisto = new QPushButton( "", _GroupC1 );
159 _myHisto->setText(tr("FILTER_DISPLAY_HISTO"));
160 _myHisto->setAutoDefault(TRUE);
161 _myHisto->setDefault(TRUE);
162 _myGroupLayout->addWidget( _myHisto, row, 0 );
165 // 2) scale of histogram (radiogroup)
166 _myFScale = new QButtonGroup( tr("FILTER_TYPE_DISPLAY"), _GroupC1 );
167 _myFScale->setExclusive( true );
168 _myFScale->setColumnLayout( 0, Qt::Horizontal );
170 _myLinear = new QRadioButton( tr("FILTER_LINEAR"), _myFScale );
171 _myLinear->setChecked(true);
172 _myLog = new QRadioButton( tr("FILTER_LOG"), _myFScale );
173 _myFScale->setDisabled(true);
175 QGridLayout* scaleLay = new QGridLayout( _myFScale->layout() );
176 scaleLay->addWidget( _myLinear, 0, 0 );
177 scaleLay->addWidget( _myLog, 0, 1 );
178 _myGroupLayout->addWidget( _myFScale, row, 0 );
181 // 3) number of thresholds (radiogroup)
182 _myNbThresh = new QButtonGroup( tr("FILTER_SEL_THRESH"), _GroupC1 );
183 _myNbThresh->setExclusive( true );
184 _myNbThresh->setColumnLayout( 0, Qt::Horizontal );
185 QGridLayout* nbtLay = new QGridLayout( _myNbThresh->layout() );
186 nbtLay->addWidget( _myOneThresh = new QRadioButton( tr("FILTER_ONE_THRESH"), _myNbThresh ), 0, 0 );
187 nbtLay->addWidget( _myTwoThresh = new QRadioButton( tr("FILTER_TWO_THRESH"), _myNbThresh ), 0, 1 );
188 _myGroupLayout->addWidget( _myNbThresh, row, 0 );
190 _myOneThresh->setChecked(true);
191 _myNbThresh->setDisabled(true);
194 // 4) reference area on thresholds (radiogroup)
195 _myArea = new QButtonGroup( tr("FILTER_REF_AREA"), _GroupC1 );
196 _myArea->setExclusive( true );
197 _myArea->setColumnLayout( 0, Qt::Horizontal );
198 QGridLayout* areaLay = new QGridLayout( _myArea->layout() );
199 areaLay->addWidget( _myInt = new QRadioButton( tr("FILTER_BOTTOM"), _myArea ), 0, 0 );
200 areaLay->addWidget( _myExt = new QRadioButton( tr("FILTER_UP"), _myArea ), 0, 1 );
201 _myGroupLayout->addWidget( _myArea, row, 0 );
203 _myExt->setChecked(true);
204 _myArea->setDisabled(true);
207 // 5) threshold values (line edit)
208 _myVThresh = new QButtonGroup( tr("FILTER_TRESH_VAL"), _GroupC1 );
209 _myVThresh->setExclusive( true );
210 _myVThresh->setColumnLayout( 0, Qt::Horizontal );
211 QGridLayout* ftLay = new QGridLayout( _myVThresh->layout() );
212 ftLay->addWidget( _myLFT = new QLabel( tr("FILTER_VAL_TRESH") , _myVThresh ), 0, 0 );
213 ftLay->addWidget( _myLEFT = new QLineEdit( "", _myVThresh ), 0, 1 );
214 ftLay->addWidget( _myLST = new QLabel( tr("FILTER_VAL_2_TRESH") , _myVThresh ), 1, 0 );
215 ftLay->addWidget( _myLEST = new QLineEdit( "", _myVThresh ), 1, 1 );
216 _myGroupLayout->addWidget( _myVThresh, row, 0 );
218 _myVThresh->setDisabled(true);
223 // 6) output file name (line edit)
224 _myOutFile = new QButtonGroup( tr("FILTER_OUT_FILE"), _GroupC1 );
225 _myOutFile->setExclusive( true );
226 _myOutFile->setColumnLayout( 0, Qt::Horizontal );
228 _myOFB = new QPushButton( "", _myOutFile );
229 _myOFB->setText(tr("FILTER_BROWSE"));
230 _myOFB->setAutoDefault(TRUE);
232 QGridLayout* outLay = new QGridLayout( _myOutFile->layout() );
233 outLay->addWidget( _myOFB, 0, 0 );
234 outLay->addWidget( _myOFN = new QLineEdit( "", _myOutFile ), 0, 1 );
235 _myGroupLayout->addWidget( _myOutFile, row, 0 );
237 _myOutFile->setDisabled(true);
240 // 8) buttons Process, Close and Help
241 _GroupButtons = new QGroupBox(_GroupC1, "GroupButtons");
242 _GroupButtons->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 0, _GroupButtons->sizePolicy().hasHeightForWidth()));
243 _GroupButtons->setTitle(tr("" ));
244 _GroupButtons->setColumnLayout(0, Qt::Vertical);
245 _GroupButtons->layout()->setSpacing(0);
246 _GroupButtons->layout()->setMargin(0);
247 _GroupButtonsLayout = new QGridLayout(_GroupButtons->layout());
248 _GroupButtonsLayout->setAlignment(Qt::AlignTop);
249 _GroupButtonsLayout->setSpacing(6);
250 _GroupButtonsLayout->setMargin(11);
251 _myProc = new QPushButton(_GroupButtons, "buttonProcess");
252 _myProc->setText(tr("FILTER_PROCESS"));
253 _myProc->setAutoDefault(TRUE);
254 _GroupButtonsLayout->addWidget(_myProc, 0, 0);
255 _buttonHelp = new QPushButton(_GroupButtons, "buttonHelp");
256 _buttonHelp->setText(tr("FILTER_BUT_HELP" ));
257 _buttonHelp->setAutoDefault(TRUE);
258 _GroupButtonsLayout->addWidget(_buttonHelp, 0, 2);
259 _buttonClose = new QPushButton(_GroupButtons, "buttonClose");
260 _buttonClose->setText(tr("FILTER_BUT_CLOSE" ));
261 _buttonClose->setAutoDefault(TRUE);
262 _GroupButtonsLayout->addWidget(_buttonClose, 0, 1);
263 _myGroupLayout->addWidget( _GroupButtons, row, 0 );
264 _GroupButtons->setDisabled(true);
267 _GroupC2 = new QGroupBox( this, "GroupC2" );
268 _lay->addWidget( _GroupC2,0,1 );
270 _GroupC2->setTitle( tr( "FILTER_HISTO" ) );
271 _GroupC2->setColumnLayout(0, Qt::Vertical );
272 _GroupC2->layout()->setSpacing( 0 );
273 _GroupC2->layout()->setMargin( 0 );
274 _myGroupLayout2 = new QGridLayout( _GroupC2->layout() );
276 // 9) histogram curve
277 _myPlot = new QwtPlot(_GroupC2);
278 _myHistoCurve = _myPlot->insertCurve( QString() );
279 _myPlot->setCurvePen( _myHistoCurve, QPen( Qt::red, 1 ) );
280 _myPlot->setCurveTitle( _myHistoCurve, "Histogram" );
282 _myGroupLayout2->addWidget( _myPlot, 0, 0 );
284 // 10) reduction rate (label)
285 QString qs5(tr("FILTER_RED_RATE"));
286 qs5.append(" = 0.5");
287 _myLRR = new QLabel( qs5, _GroupC2 );
288 _myGroupLayout2->addWidget( _myLRR, 1, 0 );
292 _myHistoFThresh = _myPlot->insertCurve( QString() );
293 _myPlot->setCurvePen( _myHistoFThresh, QPen( Qt::black, 1 ) );
294 _myHistoSThresh = _myPlot->insertCurve( QString() );
295 _myPlot->setCurvePen( _myHistoSThresh, QPen( Qt::black, 1 ) );
297 connect( _myHisto, SIGNAL(clicked()), this, SLOT(updateHisto()));
298 connect( _myLinear, SIGNAL(clicked()), this, SLOT(scaleSelected()));
299 connect( _myLog, SIGNAL(clicked()), this, SLOT(scaleSelected()));
300 connect( _myOneThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
301 connect( _myTwoThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
302 connect( _myInt, SIGNAL(clicked()), this, SLOT(areaSelected()));
303 connect( _myExt, SIGNAL(clicked()), this, SLOT(areaSelected()));
304 connect( _myLEFT, SIGNAL(returnPressed()), this, SLOT(enterFThresh()));
305 connect( _myLEST, SIGNAL(returnPressed()), this, SLOT(enterSThresh()));
306 connect( _myPlot, SIGNAL(plotMouseMoved(const QMouseEvent &)), this, SLOT(moveThresh(const QMouseEvent &)));
307 connect( _myOFB, SIGNAL(clicked()), this, SLOT(getOutFileName()));
308 connect( _myProc, SIGNAL(clicked()), this, SLOT(process()));
309 connect(_buttonClose, SIGNAL(clicked()), this, SLOT(ClickOnClose()));
310 connect(_buttonHelp, SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
312 _GroupC2->setMinimumSize( 500, 500 );
313 _GroupC2->setMaximumSize( 500, 500 );
314 setMinimumSize( 750, 500 );
315 setMaximumSize( 750, 500 );
316 this->show(); /* displays Dialog */
319 void SelectParams::scaleSelected()
321 // draw linear or log Y scale depend on user
322 if( _myLinear->isChecked() ){
324 _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
327 // set min to 0.1 for log scale
329 _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
331 _myPlot->setAxisScale( _myPlot->curveYAxis( _myHistoCurve ), _ymin, _ymax );
335 void SelectParams::nbThreshSelected()
337 if( _myOneThresh->isChecked() ){
338 // if one threshold choice between bottom and up for reference area
339 _myInt->setText(tr("FILTER_BOTTOM"));
340 _myExt->setText(tr("FILTER_UP"));
341 _myLFT->setText(tr("FILTER_VAL_TRESH"));
344 // draw first threshold
346 // erase second threshold
350 // if two thresholds choice between interior and exterior fir reference area
351 _myInt->setText(tr("FILTER_INT"));
352 _myExt->setText(tr("FILTER_EXT"));
353 _myLFT->setText(tr("FILTER_VAL_1_TRESH"));
354 if(_myLST->isHidden())
356 if(_myLEST->isHidden())
358 // draw two thresholds
365 void SelectParams::areaSelected()
367 // calculate reduction rate after thresholds selection
371 void SelectParams::enterFThresh()
377 void SelectParams::enterSThresh()
383 void SelectParams::calcHisto()
388 // calculate histogram values
390 _myDField->getMinMax(_xmin,_xmax);
391 myh = _myDField->getHistogram(_size);
395 _myIField->getMinMax(xmin,xmax);
396 _xmin = (double)xmin;
397 _xmax = (double)xmax;
398 myh = _myIField->getHistogram(_size);
400 if( _myLinear->isChecked() )
406 for(int i=0;i<_size;i++){
407 // calculate absisses for histogram values
408 _x[i]=_xmin+(i*(_xmax-_xmin))/_size;
409 // set zero to 0.01 because pb of zeros in log display with qwt
411 _y[i]=(double)myh[i];
418 // init thresholds values
419 _fthresh = (_xmin + _xmax)/2.0;
420 _sthresh = (_xmin + 3.*_xmax)/4.0;
421 sprintf(strth,"%g",_fthresh);
422 _myLEFT->setText(QString(strth));
423 sprintf(strth,"%g",_sthresh);
424 _myLEST->setText(QString(strth));
427 void SelectParams::displayHisto()
429 // give extrema values for each axis
430 _myPlot->setAxisScale( _myPlot->curveXAxis( _myHistoCurve ), _xmin, _xmax );
431 _myPlot->setAxisScale( _myPlot->curveYAxis( _myHistoCurve ), _ymin, _ymax );
432 // associate values to curve
433 _myPlot->setCurveData( _myHistoCurve, _x, _y, _size );
434 if( _myLinear->isChecked() )
435 _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
437 _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
438 // associate mapping to plot to move thresholds on display
439 _qmap = _myPlot->canvasMap(_myPlot->curveXAxis( _myHistoCurve ));
440 _qmap.setDblRange(_xmin,_xmax);
444 void SelectParams::enableWidgets()
446 if(_GroupC2->isHidden()){
447 _myFScale->setEnabled(true);
448 _myNbThresh->setEnabled(true);
449 _myArea->setEnabled(true);
450 _myVThresh->setEnabled(true);
451 _myOutFile->setEnabled(true);
452 _GroupButtons->setEnabled(true);
457 void SelectParams::updateHisto()
462 if( _myTwoThresh->isChecked() )
468 void SelectParams::displayFThresh()
470 _fthresh = atof(_myLEFT->text());
472 // draw first threshold curve
473 for(int i=0;i<100;i++){
475 _yft[i]=((i-1)*_ymax)/100.0;
477 _myPlot->setCurveData( _myHistoFThresh, _xft, _yft, 100 );
481 void SelectParams::displaySThresh()
483 _sthresh = atof(_myLEST->text());
485 // draw second threshold curve
486 for(int i=0;i<100;i++){
488 _yst[i]=((i-1)*_ymax)/100.0;
490 _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
494 void SelectParams::clearSThresh()
496 _sthresh = atof(_myLEST->text());
498 // erase second threshold curve
499 for(int i=0;i<100;i++){
503 _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
507 void SelectParams::moveThresh(const QMouseEvent &e)
511 // move threshold curve with mouse
512 int delta = abs(e.x()-_qmap.transform(_fthresh));
514 // moving first threshold curve
515 sprintf(strth,"%g",_qmap.invTransform(e.x()));
516 _myLEFT->setText(QString(strth));
519 else if( _myTwoThresh->isChecked() ){
520 delta = abs(e.x()-_qmap.transform(_sthresh));
522 // moving second threshold curve
523 sprintf(strth,"%g",_qmap.invTransform(e.x()));
524 _myLEST->setText(QString(strth));
531 void SelectParams::getOutFileName()
533 // get output MED file name
534 QString file = QFileDialog::getSaveFileName("",
538 "Choose a file to save" );
540 _myOFN->setText(file);
542 // Selection du Fichier
543 // file = SUIT_FileDlg::getFileName(application()->desktop(),
546 // "Output MED file name",
550 void SelectParams::process()
554 // have to call ensight driver MED to generate input for filtoo
556 MESSAGE("Input MED File : "<<_inputFile);
557 MESSAGE("Input Mesh : "<<_inputMesh);
558 MESSAGE("Input Field : "<<_inputField);
559 MESSAGE("Input Time Step : "<<_inputTS);
560 MESSAGE("Output file name: " << _myOFN->text() );
562 // send filtoo command
563 command = "cd /export/home/secher/filtoo/example;filtoo -f s -o sf > /tmp/filter.log";
565 system(command.c_str());
567 // have to call ensight driver MED to generate output MED file from filtoo output
571 void SelectParams::calcRateRed()
574 int atot=0, asel=0, atot1;
578 // calculate reduction rate depend on reference area defined by threshold values
579 i1 = (int)((double)_size * ( _fthresh - _xmin ) / ( _xmax - _xmin ));
580 if( _myOneThresh->isChecked() ){
584 for(i=i1;i<_size;i++)
586 if( _myExt->isChecked() )
590 i2 = (int)((double)_size * ( _sthresh - _xmin ) / ( _xmax - _xmin ));
602 for(i=i2;i<_size;i++)
604 if( _myExt->isChecked() )
607 rateRed = (double)asel / (double) atot;
609 // display reduction rate value
610 QString qs(tr("FILTER_RED_RATE"));
612 sprintf(str," = %4.2g",rateRed);
614 _myLRR->setText( qs );
618 void SelectParams::ClickOnClose()
620 MESSAGE("click on Cancel");
624 void SelectParams::ClickOnHelp()
626 MESSAGE("click on Help");